| [d23fab9] | 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 |
|
|---|
| [b7e1458] | 29 | /** @addtogroup drvusbdiag
|
|---|
| [d23fab9] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| [a8723748] | 34 | * Main routines of USB diagnostic device driver.
|
|---|
| [d23fab9] | 35 | */
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| [47a9633] | 38 | #include <usb/classes/classes.h>
|
|---|
| [d23fab9] | 39 | #include <usb/dev/driver.h>
|
|---|
| [41ebc36] | 40 | #include <usbdiag_iface.h>
|
|---|
| [64d138b] | 41 | #include <str_error.h>
|
|---|
| [d23fab9] | 42 |
|
|---|
| [6c8a221c] | 43 | #include "device.h"
|
|---|
| 44 |
|
|---|
| [b7e1458] | 45 | #define NAME "usbdiag"
|
|---|
| [d23fab9] | 46 |
|
|---|
| [6c8a221c] | 47 | static int device_add(usb_device_t *dev)
|
|---|
| [d23fab9] | 48 | {
|
|---|
| [64d138b] | 49 | int rc;
|
|---|
| [6c8a221c] | 50 | usb_log_info("Adding device '%s'", usb_device_get_name(dev));
|
|---|
| 51 |
|
|---|
| [b7b7898] | 52 | usbdiag_dev_t *diag_dev;
|
|---|
| 53 | if ((rc = usbdiag_dev_create(dev, &diag_dev))) {
|
|---|
| [64d138b] | 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 | }
|
|---|
| [6c8a221c] | 62 |
|
|---|
| [41ebc36] | 63 | if ((rc = ddf_fun_add_to_category(diag_dev->fun, USBDIAG_CATEGORY))) {
|
|---|
| [64d138b] | 64 | usb_log_error("Failed add DDF to category '"
|
|---|
| [41ebc36] | 65 | USBDIAG_CATEGORY "': %s.\n", str_error(rc));
|
|---|
| [64d138b] | 66 | goto err_bind;
|
|---|
| 67 | }
|
|---|
| [6c8a221c] | 68 |
|
|---|
| [d23fab9] | 69 | return EOK;
|
|---|
| [64d138b] | 70 |
|
|---|
| 71 | err_bind:
|
|---|
| 72 | ddf_fun_unbind(diag_dev->fun);
|
|---|
| 73 | err_create:
|
|---|
| [b7b7898] | 74 | usbdiag_dev_destroy(diag_dev);
|
|---|
| [64d138b] | 75 | err:
|
|---|
| 76 | return rc;
|
|---|
| [d23fab9] | 77 | }
|
|---|
| 78 |
|
|---|
| [6c8a221c] | 79 | static int device_remove(usb_device_t *dev)
|
|---|
| [d23fab9] | 80 | {
|
|---|
| [64d138b] | 81 | int rc;
|
|---|
| [6c8a221c] | 82 | usb_log_info("Removing device '%s'", usb_device_get_name(dev));
|
|---|
| 83 |
|
|---|
| [b7b7898] | 84 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| [6c8a221c] | 85 |
|
|---|
| 86 | /* TODO: Make sure nothing is going on with the device. */
|
|---|
| [64d138b] | 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 | }
|
|---|
| [6c8a221c] | 92 |
|
|---|
| [b7b7898] | 93 | usbdiag_dev_destroy(diag_dev);
|
|---|
| [6c8a221c] | 94 |
|
|---|
| [d23fab9] | 95 | return EOK;
|
|---|
| [64d138b] | 96 |
|
|---|
| 97 | err:
|
|---|
| 98 | return rc;
|
|---|
| [d23fab9] | 99 | }
|
|---|
| 100 |
|
|---|
| [6c8a221c] | 101 | static int device_gone(usb_device_t *dev)
|
|---|
| [d23fab9] | 102 | {
|
|---|
| [6c8a221c] | 103 | usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
|
|---|
| 104 |
|
|---|
| [b7b7898] | 105 | usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
|
|---|
| [6c8a221c] | 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 |
|
|---|
| [b7b7898] | 111 | usbdiag_dev_destroy(diag_dev);
|
|---|
| [6c8a221c] | 112 |
|
|---|
| [d23fab9] | 113 | return EOK;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| [6c8a221c] | 116 | static int function_online(ddf_fun_t *fun)
|
|---|
| [d23fab9] | 117 | {
|
|---|
| 118 | return ddf_fun_online(fun);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| [6c8a221c] | 121 | static int function_offline(ddf_fun_t *fun)
|
|---|
| [d23fab9] | 122 | {
|
|---|
| 123 | return ddf_fun_offline(fun);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 130 | .interface_subclass = 0x00,
|
|---|
| 131 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 138 | .interface_subclass = 0x00,
|
|---|
| 139 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 146 | .interface_subclass = 0x00,
|
|---|
| 147 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 154 | .interface_subclass = 0x00,
|
|---|
| 155 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 162 | .interface_subclass = 0x00,
|
|---|
| 163 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 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,
|
|---|
| [02a7575] | 170 | .interface_subclass = 0x00,
|
|---|
| 171 | .interface_protocol = 0x01,
|
|---|
| [47a9633] | 172 | .flags = 0
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | static const usb_endpoint_description_t *diag_endpoints[] = {
|
|---|
| [b7b7898] | 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,
|
|---|
| [47a9633] | 182 | NULL
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| [a8723748] | 185 | /** USB diagnostic driver ops. */
|
|---|
| [b7e1458] | 186 | static const usb_driver_ops_t diag_driver_ops = {
|
|---|
| [6c8a221c] | 187 | .device_add = device_add,
|
|---|
| 188 | .device_rem = device_remove,
|
|---|
| 189 | .device_gone = device_gone,
|
|---|
| 190 | .function_online = function_online,
|
|---|
| 191 | .function_offline = function_offline
|
|---|
| [d23fab9] | 192 | };
|
|---|
| 193 |
|
|---|
| [a8723748] | 194 | /** USB diagnostic driver. */
|
|---|
| [b7e1458] | 195 | static const usb_driver_t diag_driver = {
|
|---|
| [d23fab9] | 196 | .name = NAME,
|
|---|
| [b7e1458] | 197 | .ops = &diag_driver_ops,
|
|---|
| [85bf12ba] | 198 | .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
|
|---|
| [d23fab9] | 199 | };
|
|---|
| 200 |
|
|---|
| 201 | int main(int argc, char *argv[])
|
|---|
| 202 | {
|
|---|
| [a8723748] | 203 | printf(NAME ": USB diagnostic device driver.\n");
|
|---|
| [d23fab9] | 204 |
|
|---|
| 205 | log_init(NAME);
|
|---|
| 206 |
|
|---|
| [b7e1458] | 207 | return usb_driver_main(&diag_driver);
|
|---|
| [d23fab9] | 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * @}
|
|---|
| 212 | */
|
|---|