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 errno_t device_add(usb_device_t *dev)
|
---|
50 | {
|
---|
51 | errno_t 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 errno_t 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 errno_t device_remove(usb_device_t *dev)
|
---|
91 | {
|
---|
92 | errno_t 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 errno_t device_gone(usb_device_t *dev)
|
---|
112 | {
|
---|
113 | errno_t 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 errno_t function_online(ddf_fun_t *fun)
|
---|
132 | {
|
---|
133 | return ddf_fun_online(fun);
|
---|
134 | }
|
---|
135 |
|
---|
136 | static errno_t function_offline(ddf_fun_t *fun)
|
---|
137 | {
|
---|
138 | return ddf_fun_offline(fun);
|
---|
139 | }
|
---|
140 |
|
---|
141 | static const usb_endpoint_description_t burst_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 burst_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 burst_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 burst_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 burst_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 burst_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 | static const usb_endpoint_description_t data_intr_in_ep = {
|
---|
190 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
191 | .direction = USB_DIRECTION_IN,
|
---|
192 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
193 | .interface_subclass = 0x00,
|
---|
194 | .interface_protocol = 0x01,
|
---|
195 | .flags = 0
|
---|
196 | };
|
---|
197 | static const usb_endpoint_description_t data_intr_out_ep = {
|
---|
198 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
199 | .direction = USB_DIRECTION_OUT,
|
---|
200 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
201 | .interface_subclass = 0x00,
|
---|
202 | .interface_protocol = 0x01,
|
---|
203 | .flags = 0
|
---|
204 | };
|
---|
205 | static const usb_endpoint_description_t data_bulk_in_ep = {
|
---|
206 | .transfer_type = USB_TRANSFER_BULK,
|
---|
207 | .direction = USB_DIRECTION_IN,
|
---|
208 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
209 | .interface_subclass = 0x00,
|
---|
210 | .interface_protocol = 0x01,
|
---|
211 | .flags = 0
|
---|
212 | };
|
---|
213 | static const usb_endpoint_description_t data_bulk_out_ep = {
|
---|
214 | .transfer_type = USB_TRANSFER_BULK,
|
---|
215 | .direction = USB_DIRECTION_OUT,
|
---|
216 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
217 | .interface_subclass = 0x00,
|
---|
218 | .interface_protocol = 0x01,
|
---|
219 | .flags = 0
|
---|
220 | };
|
---|
221 | static const usb_endpoint_description_t data_isoch_in_ep = {
|
---|
222 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
---|
223 | .direction = USB_DIRECTION_IN,
|
---|
224 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
225 | .interface_subclass = 0x00,
|
---|
226 | .interface_protocol = 0x01,
|
---|
227 | .flags = 0
|
---|
228 | };
|
---|
229 | static const usb_endpoint_description_t data_isoch_out_ep = {
|
---|
230 | .transfer_type = USB_TRANSFER_ISOCHRONOUS,
|
---|
231 | .direction = USB_DIRECTION_OUT,
|
---|
232 | .interface_class = USB_CLASS_DIAGNOSTIC,
|
---|
233 | .interface_subclass = 0x00,
|
---|
234 | .interface_protocol = 0x01,
|
---|
235 | .flags = 0
|
---|
236 | };
|
---|
237 |
|
---|
238 | static const usb_endpoint_description_t *diag_endpoints[] = {
|
---|
239 | [USBDIAG_EP_BURST_INTR_IN] = &burst_intr_in_ep,
|
---|
240 | [USBDIAG_EP_BURST_INTR_OUT] = &burst_intr_out_ep,
|
---|
241 | [USBDIAG_EP_BURST_BULK_IN] = &burst_bulk_in_ep,
|
---|
242 | [USBDIAG_EP_BURST_BULK_OUT] = &burst_bulk_out_ep,
|
---|
243 | [USBDIAG_EP_BURST_ISOCH_IN] = &burst_isoch_in_ep,
|
---|
244 | [USBDIAG_EP_BURST_ISOCH_OUT] = &burst_isoch_out_ep,
|
---|
245 | [USBDIAG_EP_DATA_INTR_IN] = &data_intr_in_ep,
|
---|
246 | [USBDIAG_EP_DATA_INTR_OUT] = &data_intr_out_ep,
|
---|
247 | [USBDIAG_EP_DATA_BULK_IN] = &data_bulk_in_ep,
|
---|
248 | [USBDIAG_EP_DATA_BULK_OUT] = &data_bulk_out_ep,
|
---|
249 | [USBDIAG_EP_DATA_ISOCH_IN] = &data_isoch_in_ep,
|
---|
250 | [USBDIAG_EP_DATA_ISOCH_OUT] = &data_isoch_out_ep,
|
---|
251 | NULL
|
---|
252 | };
|
---|
253 |
|
---|
254 | /** USB diagnostic driver ops. */
|
---|
255 | static const usb_driver_ops_t diag_driver_ops = {
|
---|
256 | .device_add = device_add,
|
---|
257 | .device_remove = device_remove,
|
---|
258 | .device_gone = device_gone,
|
---|
259 | .function_online = function_online,
|
---|
260 | .function_offline = function_offline
|
---|
261 | };
|
---|
262 |
|
---|
263 | /** USB diagnostic driver. */
|
---|
264 | static const usb_driver_t diag_driver = {
|
---|
265 | .name = NAME,
|
---|
266 | .ops = &diag_driver_ops,
|
---|
267 | .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
|
---|
268 | };
|
---|
269 |
|
---|
270 | int main(int argc, char *argv[])
|
---|
271 | {
|
---|
272 | printf(NAME ": USB diagnostic device driver.\n");
|
---|
273 |
|
---|
274 | log_init(NAME);
|
---|
275 |
|
---|
276 | return usb_driver_main(&diag_driver);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * @}
|
---|
281 | */
|
---|