| 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.", 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.", 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_cleanup(usbdiag_dev_t *diag_dev)
|
|---|
| 80 | {
|
|---|
| 81 | /* TODO: Join some fibrils? */
|
|---|
| 82 |
|
|---|
| 83 | /* Free memory. */
|
|---|
| 84 | usbdiag_dev_destroy(diag_dev);
|
|---|
| 85 | return EOK;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static int device_remove(usb_device_t *dev)
|
|---|
| 89 | {
|
|---|
| 90 | int rc;
|
|---|
| 91 | usb_log_info("Removing device '%s'", usb_device_get_name(dev));
|
|---|
| 92 |
|
|---|
| 93 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| 94 |
|
|---|
| 95 | /* TODO: Make sure nothing is going on with the device. */
|
|---|
| 96 |
|
|---|
| 97 | if ((rc = ddf_fun_unbind(diag_dev->fun))) {
|
|---|
| 98 | usb_log_error("Failed to unbind DDF function: %s", str_error(rc));
|
|---|
| 99 | goto err;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | usb_log_info("Device '%s' removed.", usb_device_get_name(dev));
|
|---|
| 103 | return device_cleanup(diag_dev);
|
|---|
| 104 |
|
|---|
| 105 | err:
|
|---|
| 106 | return rc;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static int device_gone(usb_device_t *dev)
|
|---|
| 110 | {
|
|---|
| 111 | int rc;
|
|---|
| 112 | usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
|
|---|
| 113 |
|
|---|
| 114 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| 115 |
|
|---|
| 116 | /* TODO: Make sure nothing is going on with the device. */
|
|---|
| 117 |
|
|---|
| 118 | if ((rc = ddf_fun_unbind(diag_dev->fun))) {
|
|---|
| 119 | usb_log_error("Failed to unbind DDF function: %s", str_error(rc));
|
|---|
| 120 | goto err;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return device_cleanup(diag_dev);
|
|---|
| 124 |
|
|---|
| 125 | err:
|
|---|
| 126 | return rc;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static int function_online(ddf_fun_t *fun)
|
|---|
| 130 | {
|
|---|
| 131 | return ddf_fun_online(fun);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static int function_offline(ddf_fun_t *fun)
|
|---|
| 135 | {
|
|---|
| 136 | return ddf_fun_offline(fun);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | static const usb_endpoint_description_t intr_in_ep = {
|
|---|
| 140 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 141 | .direction = USB_DIRECTION_IN,
|
|---|
| 142 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 143 | .interface_subclass = 0x00,
|
|---|
| 144 | .interface_protocol = 0x01,
|
|---|
| 145 | .flags = 0
|
|---|
| 146 | };
|
|---|
| 147 | static const usb_endpoint_description_t intr_out_ep = {
|
|---|
| 148 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 149 | .direction = USB_DIRECTION_OUT,
|
|---|
| 150 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 151 | .interface_subclass = 0x00,
|
|---|
| 152 | .interface_protocol = 0x01,
|
|---|
| 153 | .flags = 0
|
|---|
| 154 | };
|
|---|
| 155 | static const usb_endpoint_description_t bulk_in_ep = {
|
|---|
| 156 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 157 | .direction = USB_DIRECTION_IN,
|
|---|
| 158 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 159 | .interface_subclass = 0x00,
|
|---|
| 160 | .interface_protocol = 0x01,
|
|---|
| 161 | .flags = 0
|
|---|
| 162 | };
|
|---|
| 163 | static const usb_endpoint_description_t bulk_out_ep = {
|
|---|
| 164 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 165 | .direction = USB_DIRECTION_OUT,
|
|---|
| 166 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 167 | .interface_subclass = 0x00,
|
|---|
| 168 | .interface_protocol = 0x01,
|
|---|
| 169 | .flags = 0
|
|---|
| 170 | };
|
|---|
| 171 | static const usb_endpoint_description_t isoch_in_ep = {
|
|---|
| 172 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
|---|
| 173 | .direction = USB_DIRECTION_IN,
|
|---|
| 174 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 175 | .interface_subclass = 0x00,
|
|---|
| 176 | .interface_protocol = 0x01,
|
|---|
| 177 | .flags = 0
|
|---|
| 178 | };
|
|---|
| 179 | static const usb_endpoint_description_t isoch_out_ep = {
|
|---|
| 180 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
|---|
| 181 | .direction = USB_DIRECTION_OUT,
|
|---|
| 182 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
|---|
| 183 | .interface_subclass = 0x00,
|
|---|
| 184 | .interface_protocol = 0x01,
|
|---|
| 185 | .flags = 0
|
|---|
| 186 | };
|
|---|
| 187 |
|
|---|
| 188 | static const usb_endpoint_description_t *diag_endpoints[] = {
|
|---|
| 189 | [USBDIAG_EP_INTR_IN] = &intr_in_ep,
|
|---|
| 190 | [USBDIAG_EP_INTR_OUT] = &intr_out_ep,
|
|---|
| 191 | [USBDIAG_EP_BULK_IN] = &bulk_in_ep,
|
|---|
| 192 | [USBDIAG_EP_BULK_OUT] = &bulk_out_ep,
|
|---|
| 193 | [USBDIAG_EP_ISOCH_IN] = &isoch_in_ep,
|
|---|
| 194 | [USBDIAG_EP_ISOCH_OUT] = &isoch_out_ep,
|
|---|
| 195 | NULL
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 | /** USB diagnostic driver ops. */
|
|---|
| 199 | static const usb_driver_ops_t diag_driver_ops = {
|
|---|
| 200 | .device_add = device_add,
|
|---|
| 201 | .device_remove = device_remove,
|
|---|
| 202 | .device_gone = device_gone,
|
|---|
| 203 | .function_online = function_online,
|
|---|
| 204 | .function_offline = function_offline
|
|---|
| 205 | };
|
|---|
| 206 |
|
|---|
| 207 | /** USB diagnostic driver. */
|
|---|
| 208 | static const usb_driver_t diag_driver = {
|
|---|
| 209 | .name = NAME,
|
|---|
| 210 | .ops = &diag_driver_ops,
|
|---|
| 211 | .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
|
|---|
| 212 | };
|
|---|
| 213 |
|
|---|
| 214 | int main(int argc, char *argv[])
|
|---|
| 215 | {
|
|---|
| 216 | printf(NAME ": USB diagnostic device driver.\n");
|
|---|
| 217 |
|
|---|
| 218 | log_init(NAME);
|
|---|
| 219 |
|
|---|
| 220 | return usb_driver_main(&diag_driver);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /**
|
|---|
| 224 | * @}
|
|---|
| 225 | */
|
|---|