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