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