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 "usbdiag.h"
|
---|
44 | #include "device.h"
|
---|
45 |
|
---|
46 | #define NAME "usbdiag"
|
---|
47 |
|
---|
48 | static int device_add(usb_device_t *dev)
|
---|
49 | {
|
---|
50 | int rc;
|
---|
51 | usb_log_info("Adding device '%s'", usb_device_get_name(dev));
|
---|
52 |
|
---|
53 | usb_diag_dev_t *diag_dev;
|
---|
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 | }
|
---|
63 |
|
---|
64 | if ((rc = ddf_fun_add_to_category(diag_dev->fun, USBDIAG_CATEGORY))) {
|
---|
65 | usb_log_error("Failed add DDF to category '"
|
---|
66 | USBDIAG_CATEGORY "': %s.\n", str_error(rc));
|
---|
67 | goto err_bind;
|
---|
68 | }
|
---|
69 |
|
---|
70 | return EOK;
|
---|
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;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int device_remove(usb_device_t *dev)
|
---|
81 | {
|
---|
82 | int rc;
|
---|
83 | usb_log_info("Removing device '%s'", usb_device_get_name(dev));
|
---|
84 |
|
---|
85 | usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_dev(dev);
|
---|
86 |
|
---|
87 | /* TODO: Make sure nothing is going on with the device. */
|
---|
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 | }
|
---|
93 |
|
---|
94 | usb_diag_dev_destroy(diag_dev);
|
---|
95 |
|
---|
96 | return EOK;
|
---|
97 |
|
---|
98 | err:
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static int device_gone(usb_device_t *dev)
|
---|
103 | {
|
---|
104 | usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
|
---|
105 |
|
---|
106 | usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_dev(dev);
|
---|
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 |
|
---|
112 | usb_diag_dev_destroy(diag_dev);
|
---|
113 |
|
---|
114 | return EOK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static int function_online(ddf_fun_t *fun)
|
---|
118 | {
|
---|
119 | return ddf_fun_online(fun);
|
---|
120 | }
|
---|
121 |
|
---|
122 | static int function_offline(ddf_fun_t *fun)
|
---|
123 | {
|
---|
124 | return ddf_fun_offline(fun);
|
---|
125 | }
|
---|
126 |
|
---|
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,
|
---|
131 | .interface_subclass = 0x00,
|
---|
132 | .interface_protocol = 0x01,
|
---|
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,
|
---|
139 | .interface_subclass = 0x00,
|
---|
140 | .interface_protocol = 0x01,
|
---|
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,
|
---|
147 | .interface_subclass = 0x00,
|
---|
148 | .interface_protocol = 0x01,
|
---|
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,
|
---|
155 | .interface_subclass = 0x00,
|
---|
156 | .interface_protocol = 0x01,
|
---|
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,
|
---|
163 | .interface_subclass = 0x00,
|
---|
164 | .interface_protocol = 0x01,
|
---|
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,
|
---|
171 | .interface_subclass = 0x00,
|
---|
172 | .interface_protocol = 0x01,
|
---|
173 | .flags = 0
|
---|
174 | };
|
---|
175 |
|
---|
176 | static const usb_endpoint_description_t *diag_endpoints[] = {
|
---|
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,
|
---|
183 | NULL
|
---|
184 | };
|
---|
185 |
|
---|
186 | /** USB diagnostic driver ops. */
|
---|
187 | static const usb_driver_ops_t diag_driver_ops = {
|
---|
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
|
---|
193 | };
|
---|
194 |
|
---|
195 | /** USB diagnostic driver. */
|
---|
196 | static const usb_driver_t diag_driver = {
|
---|
197 | .name = NAME,
|
---|
198 | .ops = &diag_driver_ops,
|
---|
199 | .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
|
---|
200 | };
|
---|
201 |
|
---|
202 | int main(int argc, char *argv[])
|
---|
203 | {
|
---|
204 | printf(NAME ": USB diagnostic device driver.\n");
|
---|
205 |
|
---|
206 | log_init(NAME);
|
---|
207 |
|
---|
208 | return usb_driver_main(&diag_driver);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * @}
|
---|
213 | */
|
---|