| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Petr Manek
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbdiag
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Main routines of USB diagnostic device driver.
|
|---|
| 35 | */
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usb/classes/classes.h>
|
|---|
| 39 | #include <usb/dev/driver.h>
|
|---|
| 40 | #include <usbdiag_iface.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "device.h"
|
|---|
| 44 |
|
|---|
| 45 | #define NAME "usbdiag"
|
|---|
| 46 |
|
|---|
| 47 | static int device_add(usb_device_t *dev)
|
|---|
| 48 | {
|
|---|
| 49 | int rc;
|
|---|
| 50 | usb_log_info("Adding device '%s'", usb_device_get_name(dev));
|
|---|
| 51 |
|
|---|
| 52 | usbdiag_dev_t *diag_dev;
|
|---|
| 53 | if ((rc = usbdiag_dev_create(dev, &diag_dev))) {
|
|---|
| 54 | usb_log_error("Failed create device: %s.\n", str_error(rc));
|
|---|
| 55 | goto err;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if ((rc = ddf_fun_bind(diag_dev->fun))) {
|
|---|
| 59 | usb_log_error("Failed to bind DDF function: %s.\n", str_error(rc));
|
|---|
| 60 | goto err_create;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | if ((rc = ddf_fun_add_to_category(diag_dev->fun, USBDIAG_CATEGORY))) {
|
|---|
| 64 | usb_log_error("Failed add DDF to category '"
|
|---|
| 65 | USBDIAG_CATEGORY "': %s.\n", str_error(rc));
|
|---|
| 66 | goto err_bind;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | return EOK;
|
|---|
| 70 |
|
|---|
| 71 | err_bind:
|
|---|
| 72 | ddf_fun_unbind(diag_dev->fun);
|
|---|
| 73 | err_create:
|
|---|
| 74 | usbdiag_dev_destroy(diag_dev);
|
|---|
| 75 | err:
|
|---|
| 76 | return rc;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | static int device_remove(usb_device_t *dev)
|
|---|
| 80 | {
|
|---|
| 81 | int rc;
|
|---|
| 82 | usb_log_info("Removing device '%s'", usb_device_get_name(dev));
|
|---|
| 83 |
|
|---|
| 84 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| 85 |
|
|---|
| 86 | /* TODO: Make sure nothing is going on with the device. */
|
|---|
| 87 |
|
|---|
| 88 | if ((rc = ddf_fun_unbind(diag_dev->fun))) {
|
|---|
| 89 | usb_log_error("Failed to unbind DDF function: %s\n", str_error(rc));
|
|---|
| 90 | goto err;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | usbdiag_dev_destroy(diag_dev);
|
|---|
| 94 |
|
|---|
| 95 | return EOK;
|
|---|
| 96 |
|
|---|
| 97 | err:
|
|---|
| 98 | return rc;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static int device_gone(usb_device_t *dev)
|
|---|
| 102 | {
|
|---|
| 103 | usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
|
|---|
| 104 |
|
|---|
| 105 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| 106 |
|
|---|
| 107 | /* TODO: Make sure nothing is going on with the device. */
|
|---|
| 108 | /* TODO: Unregister device DDF function. */
|
|---|
| 109 | /* TODO: Remove device from list */
|
|---|
| 110 |
|
|---|
| 111 | usbdiag_dev_destroy(diag_dev);
|
|---|
| 112 |
|
|---|
| 113 | return EOK;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int function_online(ddf_fun_t *fun)
|
|---|
| 117 | {
|
|---|
| 118 | return ddf_fun_online(fun);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static int function_offline(ddf_fun_t *fun)
|
|---|
| 122 | {
|
|---|
| 123 | return ddf_fun_offline(fun);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static const usb_endpoint_description_t intr_in_ep = {
|
|---|
| 127 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 128 | .direction = USB_DIRECTION_IN,
|
|---|
| 129 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 130 | .interface_subclass = 0x00,
|
|---|
| 131 | .interface_protocol = 0x01,
|
|---|
| 132 | .flags = 0
|
|---|
| 133 | };
|
|---|
| 134 | static const usb_endpoint_description_t intr_out_ep = {
|
|---|
| 135 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 136 | .direction = USB_DIRECTION_OUT,
|
|---|
| 137 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 138 | .interface_subclass = 0x00,
|
|---|
| 139 | .interface_protocol = 0x01,
|
|---|
| 140 | .flags = 0
|
|---|
| 141 | };
|
|---|
| 142 | static const usb_endpoint_description_t bulk_in_ep = {
|
|---|
| 143 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 144 | .direction = USB_DIRECTION_IN,
|
|---|
| 145 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 146 | .interface_subclass = 0x00,
|
|---|
| 147 | .interface_protocol = 0x01,
|
|---|
| 148 | .flags = 0
|
|---|
| 149 | };
|
|---|
| 150 | static const usb_endpoint_description_t bulk_out_ep = {
|
|---|
| 151 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 152 | .direction = USB_DIRECTION_OUT,
|
|---|
| 153 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 154 | .interface_subclass = 0x00,
|
|---|
| 155 | .interface_protocol = 0x01,
|
|---|
| 156 | .flags = 0
|
|---|
| 157 | };
|
|---|
| 158 | static const usb_endpoint_description_t isoch_in_ep = {
|
|---|
| 159 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
|---|
| 160 | .direction = USB_DIRECTION_IN,
|
|---|
| 161 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 162 | .interface_subclass = 0x00,
|
|---|
| 163 | .interface_protocol = 0x01,
|
|---|
| 164 | .flags = 0
|
|---|
| 165 | };
|
|---|
| 166 | static const usb_endpoint_description_t isoch_out_ep = {
|
|---|
| 167 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
|---|
| 168 | .direction = USB_DIRECTION_OUT,
|
|---|
| 169 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 170 | .interface_subclass = 0x00,
|
|---|
| 171 | .interface_protocol = 0x01,
|
|---|
| 172 | .flags = 0
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | static const usb_endpoint_description_t *diag_endpoints[] = {
|
|---|
| 176 | [USBDIAG_EP_INTR_IN] = &intr_in_ep,
|
|---|
| 177 | [USBDIAG_EP_INTR_OUT] = &intr_out_ep,
|
|---|
| 178 | [USBDIAG_EP_BULK_IN] = &bulk_in_ep,
|
|---|
| 179 | [USBDIAG_EP_BULK_OUT] = &bulk_out_ep,
|
|---|
| 180 | [USBDIAG_EP_ISOCH_IN] = &isoch_in_ep,
|
|---|
| 181 | [USBDIAG_EP_ISOCH_OUT] = &isoch_out_ep,
|
|---|
| 182 | NULL
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 185 | /** USB diagnostic driver ops. */
|
|---|
| 186 | static const usb_driver_ops_t diag_driver_ops = {
|
|---|
| 187 | .device_add = device_add,
|
|---|
| 188 | .device_remove = device_remove,
|
|---|
| 189 | .device_gone = device_gone,
|
|---|
| 190 | .function_online = function_online,
|
|---|
| 191 | .function_offline = function_offline
|
|---|
| 192 | };
|
|---|
| 193 |
|
|---|
| 194 | /** USB diagnostic driver. */
|
|---|
| 195 | static const usb_driver_t diag_driver = {
|
|---|
| 196 | .name = NAME,
|
|---|
| 197 | .ops = &diag_driver_ops,
|
|---|
| 198 | .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
|
|---|
| 199 | };
|
|---|
| 200 |
|
|---|
| 201 | int main(int argc, char *argv[])
|
|---|
| 202 | {
|
|---|
| 203 | printf(NAME ": USB diagnostic device driver.\n");
|
|---|
| 204 |
|
|---|
| 205 | log_init(NAME);
|
|---|
| 206 |
|
|---|
| 207 | return usb_driver_main(&diag_driver);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * @}
|
|---|
| 212 | */
|
|---|