1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Jan Vesely
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 | /** @addtogroup libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * USB device driver framework.
|
---|
34 | */
|
---|
35 | #include <usb/dev/driver.h>
|
---|
36 | #include <usb/dev/request.h>
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usb/dev.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <assert.h>
|
---|
42 |
|
---|
43 | /** USB device structure. */
|
---|
44 | typedef struct usb_device {
|
---|
45 | /** Connection to USB hc, used by wire and arbitrary requests. */
|
---|
46 | usb_hc_connection_t hc_conn;
|
---|
47 | /** Connection backing the pipes.
|
---|
48 | * Typically, you will not need to use this attribute at all.
|
---|
49 | */
|
---|
50 | usb_device_connection_t wire;
|
---|
51 | /** The default control pipe. */
|
---|
52 | usb_pipe_t ctrl_pipe;
|
---|
53 |
|
---|
54 | /** Other endpoint pipes.
|
---|
55 | * This is an array of other endpoint pipes in the same order as
|
---|
56 | * in usb_driver_t.
|
---|
57 | */
|
---|
58 | usb_endpoint_mapping_t *pipes;
|
---|
59 | /** Number of other endpoint pipes. */
|
---|
60 | size_t pipes_count;
|
---|
61 | /** Current interface.
|
---|
62 | * Usually, drivers operate on single interface only.
|
---|
63 | * This item contains the value of the interface or -1 for any.
|
---|
64 | */
|
---|
65 | int interface_no;
|
---|
66 | /** Alternative interfaces. */
|
---|
67 | usb_alternate_interfaces_t alternate_interfaces;
|
---|
68 |
|
---|
69 | /** Some useful descriptors for USB device. */
|
---|
70 | struct {
|
---|
71 | /** Standard device descriptor. */
|
---|
72 | usb_standard_device_descriptor_t device;
|
---|
73 | /** Full configuration descriptor of current configuration. */
|
---|
74 | const uint8_t *configuration;
|
---|
75 | size_t configuration_size;
|
---|
76 | } descriptors;
|
---|
77 |
|
---|
78 | /** Generic DDF device backing this one. DO NOT TOUCH! */
|
---|
79 | ddf_dev_t *ddf_dev;
|
---|
80 | /** Custom driver data.
|
---|
81 | * Do not use the entry in generic device, that is already used
|
---|
82 | * by the framework.
|
---|
83 | */
|
---|
84 | void *driver_data;
|
---|
85 |
|
---|
86 | usb_dev_session_t *bus_session;
|
---|
87 | } usb_device_t;
|
---|
88 |
|
---|
89 | /** Count number of pipes the driver expects.
|
---|
90 | *
|
---|
91 | * @param drv USB driver.
|
---|
92 | * @return Number of pipes (excluding default control pipe).
|
---|
93 | */
|
---|
94 | static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
|
---|
95 | {
|
---|
96 | size_t count;
|
---|
97 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
---|
98 | return count;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** Change interface setting of a device.
|
---|
102 | * This function selects new alternate setting of an interface by issuing
|
---|
103 | * proper USB command to the device and also creates new USB pipes
|
---|
104 | * under @c dev->pipes.
|
---|
105 | *
|
---|
106 | * @warning This function is intended for drivers working at interface level.
|
---|
107 | * For drivers controlling the whole device, you need to change interface
|
---|
108 | * manually using usb_request_set_interface() and creating new pipes
|
---|
109 | * with usb_pipe_initialize_from_configuration().
|
---|
110 | *
|
---|
111 | * @warning This is a wrapper function that does several operations that
|
---|
112 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
113 | * during the SET_INTERFACE request would result in having a device with
|
---|
114 | * no pipes at all (except the default control one). That is because the old
|
---|
115 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
116 | * be created.
|
---|
117 | *
|
---|
118 | * @param dev USB device.
|
---|
119 | * @param alternate_setting Alternate setting to choose.
|
---|
120 | * @param endpoints New endpoint descriptions.
|
---|
121 | * @return Error code.
|
---|
122 | */
|
---|
123 | int usb_device_select_interface(usb_device_t *usb_dev,
|
---|
124 | uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
|
---|
125 | {
|
---|
126 | assert(usb_dev);
|
---|
127 |
|
---|
128 | if (usb_dev->interface_no < 0) {
|
---|
129 | return EINVAL;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Change the interface itself. */
|
---|
133 | int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
|
---|
134 | usb_dev->interface_no, alternate_setting);
|
---|
135 | if (rc != EOK) {
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Change current alternative */
|
---|
140 | usb_dev->alternate_interfaces.current = alternate_setting;
|
---|
141 |
|
---|
142 | /* Destroy existing pipes. */
|
---|
143 | usb_device_destroy_pipes(usb_dev);
|
---|
144 |
|
---|
145 | /* Create new pipes. */
|
---|
146 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
147 |
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Retrieve basic descriptors from the device.
|
---|
152 | *
|
---|
153 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
154 | * @param[out] descriptors Where to store the descriptors.
|
---|
155 | * @return Error code.
|
---|
156 | */
|
---|
157 | static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
158 | {
|
---|
159 | assert(usb_dev);
|
---|
160 | assert(usb_dev->descriptors.configuration == NULL);
|
---|
161 |
|
---|
162 | /* It is worth to start a long transfer. */
|
---|
163 | usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
|
---|
164 |
|
---|
165 | /* Get the device descriptor. */
|
---|
166 | int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
167 | &usb_dev->descriptors.device);
|
---|
168 | if (rc != EOK) {
|
---|
169 | goto leave;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /* Get the full configuration descriptor. */
|
---|
173 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
174 | &usb_dev->ctrl_pipe, 0,
|
---|
175 | (void **) &usb_dev->descriptors.configuration,
|
---|
176 | &usb_dev->descriptors.configuration_size);
|
---|
177 |
|
---|
178 | leave:
|
---|
179 | usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
|
---|
180 |
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
185 | *
|
---|
186 | * @param[in] descriptors Where to store the descriptors.
|
---|
187 | */
|
---|
188 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
189 | {
|
---|
190 | assert(usb_dev);
|
---|
191 | free(usb_dev->descriptors.configuration);
|
---|
192 | usb_dev->descriptors.configuration = NULL;
|
---|
193 | usb_dev->descriptors.configuration_size = 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Create pipes for a device.
|
---|
197 | *
|
---|
198 | * This is more or less a wrapper that does following actions:
|
---|
199 | * - allocate and initialize pipes
|
---|
200 | * - map endpoints to the pipes based on the descriptions
|
---|
201 | * - registers endpoints with the host controller
|
---|
202 | *
|
---|
203 | * @param[in] wire Initialized backing connection to the host controller.
|
---|
204 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
205 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
206 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
207 | * @param[in] interface_no Interface to map from.
|
---|
208 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
209 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
210 | * (not NULL terminated).
|
---|
211 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
212 | * (set to NULL if you wish to ignore the count).
|
---|
213 | * @return Error code.
|
---|
214 | */
|
---|
215 | int usb_device_create_pipes(usb_device_t *usb_dev,
|
---|
216 | const usb_endpoint_description_t **endpoints)
|
---|
217 | {
|
---|
218 | assert(usb_dev);
|
---|
219 | assert(usb_dev->descriptors.configuration);
|
---|
220 | assert(usb_dev->pipes == NULL);
|
---|
221 | assert(usb_dev->pipes_count == 0);
|
---|
222 |
|
---|
223 | size_t pipe_count = count_pipes(endpoints);
|
---|
224 | if (pipe_count == 0) {
|
---|
225 | return EOK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | usb_endpoint_mapping_t *pipes =
|
---|
229 | calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
230 | if (pipes == NULL) {
|
---|
231 | return ENOMEM;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /* Now initialize. */
|
---|
235 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
236 | pipes[i].description = endpoints[i];
|
---|
237 | pipes[i].interface_no = usb_dev->interface_no;
|
---|
238 | pipes[i].interface_setting =
|
---|
239 | usb_dev->alternate_interfaces.current;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /* Find the mapping from configuration descriptor. */
|
---|
243 | int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
244 | usb_dev->descriptors.configuration,
|
---|
245 | usb_dev->descriptors.configuration_size, &usb_dev->wire);
|
---|
246 | if (rc != EOK) {
|
---|
247 | free(pipes);
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /* Register created pipes. */
|
---|
252 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
253 | if (pipes[i].present) {
|
---|
254 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
255 | pipes[i].descriptor->poll_interval);
|
---|
256 | if (rc != EOK) {
|
---|
257 | goto rollback_unregister_endpoints;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | usb_dev->pipes = pipes;
|
---|
263 | usb_dev->pipes_count = pipe_count;
|
---|
264 |
|
---|
265 | return EOK;
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * Jump here if something went wrong after endpoints have
|
---|
269 | * been registered.
|
---|
270 | * This is also the target when the registration of
|
---|
271 | * endpoints fails.
|
---|
272 | */
|
---|
273 | rollback_unregister_endpoints:
|
---|
274 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
275 | if (pipes[i].present) {
|
---|
276 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | free(pipes);
|
---|
281 | return rc;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
285 | *
|
---|
286 | * @param[in] usb_dev USB device.
|
---|
287 | */
|
---|
288 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
289 | {
|
---|
290 | assert(usb_dev);
|
---|
291 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
292 | /* Destroy the pipes. */
|
---|
293 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
294 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
---|
295 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
296 | if (usb_dev->pipes[i].present)
|
---|
297 | usb_pipe_unregister(&usb_dev->pipes[i].pipe);
|
---|
298 | }
|
---|
299 | free(usb_dev->pipes);
|
---|
300 | usb_dev->pipes = NULL;
|
---|
301 | usb_dev->pipes_count = 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
305 | {
|
---|
306 | assert(usb_dev);
|
---|
307 | return &usb_dev->ctrl_pipe;
|
---|
308 | }
|
---|
309 |
|
---|
310 | usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
|
---|
311 | const usb_endpoint_description_t *desc)
|
---|
312 | {
|
---|
313 | assert(usb_dev);
|
---|
314 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
315 | if (usb_dev->pipes[i].description == desc)
|
---|
316 | return &usb_dev->pipes[i];
|
---|
317 | }
|
---|
318 | return NULL;
|
---|
319 | }
|
---|
320 |
|
---|
321 | usb_endpoint_mapping_t * usb_device_get_mapped_ep(
|
---|
322 | usb_device_t *usb_dev, usb_endpoint_t ep)
|
---|
323 | {
|
---|
324 | assert(usb_dev);
|
---|
325 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
326 | if (usb_dev->pipes[i].pipe.endpoint_no == ep)
|
---|
327 | return &usb_dev->pipes[i];
|
---|
328 | }
|
---|
329 | return NULL;
|
---|
330 | }
|
---|
331 |
|
---|
332 | int usb_device_get_iface_number(usb_device_t *usb_dev)
|
---|
333 | {
|
---|
334 | assert(usb_dev);
|
---|
335 | return usb_dev->interface_no;
|
---|
336 | }
|
---|
337 |
|
---|
338 | const usb_standard_device_descriptor_t *
|
---|
339 | usb_device_get_device_descriptor(usb_device_t *usb_dev)
|
---|
340 | {
|
---|
341 | assert(usb_dev);
|
---|
342 | return &usb_dev->descriptors.device;
|
---|
343 | }
|
---|
344 |
|
---|
345 | const void * usb_device_get_configuration_descriptor(
|
---|
346 | usb_device_t *usb_dev, size_t *size)
|
---|
347 | {
|
---|
348 | assert(usb_dev);
|
---|
349 | if (size)
|
---|
350 | *size = usb_dev->descriptors.configuration_size;
|
---|
351 | return usb_dev->descriptors.configuration;
|
---|
352 | }
|
---|
353 |
|
---|
354 | const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
|
---|
355 | usb_device_t *usb_dev)
|
---|
356 | {
|
---|
357 | assert(usb_dev);
|
---|
358 | return &usb_dev->alternate_interfaces;
|
---|
359 | }
|
---|
360 |
|
---|
361 | static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
|
---|
362 | usb_address_t *address, int *iface_no)
|
---|
363 | {
|
---|
364 | assert(usb_dev);
|
---|
365 |
|
---|
366 | int ret = EOK;
|
---|
367 | async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
|
---|
368 | if (!exch)
|
---|
369 | ret = ENOMEM;
|
---|
370 |
|
---|
371 | if (ret == EOK && address)
|
---|
372 | ret = usb_get_my_address(exch, address);
|
---|
373 |
|
---|
374 | if (ret == EOK && handle)
|
---|
375 | ret = usb_get_hc_handle(exch, handle);
|
---|
376 |
|
---|
377 | if (ret == EOK && iface_no) {
|
---|
378 | ret = usb_get_my_interface(exch, iface_no);
|
---|
379 | if (ret == ENOTSUP) {
|
---|
380 | ret = EOK;
|
---|
381 | *iface_no = -1;
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | async_exchange_end(exch);
|
---|
386 | return ret;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /** Initialize new instance of USB device.
|
---|
390 | *
|
---|
391 | * @param[in] usb_dev Pointer to the new device.
|
---|
392 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
393 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
394 | * @param[out] errstr_ptr Where to store description of context
|
---|
395 | * (in case error occurs).
|
---|
396 | * @return Error code.
|
---|
397 | */
|
---|
398 | static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
399 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
400 | {
|
---|
401 | assert(usb_dev != NULL);
|
---|
402 | assert(ddf_dev != NULL);
|
---|
403 |
|
---|
404 | *errstr_ptr = NULL;
|
---|
405 |
|
---|
406 | usb_dev->ddf_dev = ddf_dev;
|
---|
407 | usb_dev->driver_data = NULL;
|
---|
408 | usb_dev->descriptors.configuration = NULL;
|
---|
409 | usb_dev->pipes_count = 0;
|
---|
410 | usb_dev->pipes = NULL;
|
---|
411 |
|
---|
412 | usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
|
---|
413 | if (!usb_dev->bus_session) {
|
---|
414 | *errstr_ptr = "device bus session create";
|
---|
415 | return ENOMEM;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /* Get assigned params */
|
---|
419 | devman_handle_t hc_handle;
|
---|
420 | usb_address_t address;
|
---|
421 |
|
---|
422 | int rc = usb_dev_get_info(usb_dev,
|
---|
423 | &hc_handle, &address, &usb_dev->interface_no);
|
---|
424 | if (rc != EOK) {
|
---|
425 | *errstr_ptr = "device parameters retrieval";
|
---|
426 | return rc;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /* Initialize hc connection. */
|
---|
430 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
---|
431 |
|
---|
432 | /* Initialize backing wire and control pipe. */
|
---|
433 | rc = usb_device_connection_initialize(
|
---|
434 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
---|
435 | if (rc != EOK) {
|
---|
436 | *errstr_ptr = "device connection initialization";
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /* This pipe was registered by the hub driver,
|
---|
441 | * during device initialization. */
|
---|
442 | rc = usb_pipe_initialize_default_control(
|
---|
443 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
---|
444 | if (rc != EOK) {
|
---|
445 | *errstr_ptr = "default control pipe initialization";
|
---|
446 | return rc;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /* Open hc connection for pipe registration. */
|
---|
450 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
---|
451 | if (rc != EOK) {
|
---|
452 | *errstr_ptr = "hc connection open";
|
---|
453 | return rc;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /* Retrieve standard descriptors. */
|
---|
457 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
458 | if (rc != EOK) {
|
---|
459 | *errstr_ptr = "descriptor retrieval";
|
---|
460 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
461 | return rc;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
465 | * We might either control one interface or an entire device,
|
---|
466 | * it makes no sense to speak about alternate interfaces when
|
---|
467 | * controlling a device. */
|
---|
468 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
469 | usb_dev->descriptors.configuration,
|
---|
470 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
---|
471 |
|
---|
472 | /* Create and register other pipes than default control (EP 0) */
|
---|
473 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
474 | if (rc != EOK) {
|
---|
475 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
476 | /* Full configuration descriptor is allocated. */
|
---|
477 | usb_device_release_descriptors(usb_dev);
|
---|
478 | /* Alternate interfaces may be allocated */
|
---|
479 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
480 | *errstr_ptr = "pipes initialization";
|
---|
481 | return rc;
|
---|
482 | }
|
---|
483 |
|
---|
484 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
485 | return EOK;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /** Clean instance of a USB device.
|
---|
489 | *
|
---|
490 | * @param dev Device to be de-initialized.
|
---|
491 | *
|
---|
492 | * Does not free/destroy supplied pointer.
|
---|
493 | */
|
---|
494 | static void usb_device_fini(usb_device_t *dev)
|
---|
495 | {
|
---|
496 | if (dev) {
|
---|
497 | usb_dev_disconnect(dev->bus_session);
|
---|
498 | /* Destroy existing pipes. */
|
---|
499 | usb_device_destroy_pipes(dev);
|
---|
500 | /* Ignore errors and hope for the best. */
|
---|
501 | usb_hc_connection_deinitialize(&dev->hc_conn);
|
---|
502 | usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
|
---|
503 | usb_device_release_descriptors(dev);
|
---|
504 | free(dev->driver_data);
|
---|
505 | dev->driver_data = NULL;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | int usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
510 | const usb_endpoint_description_t **desc, const char **err)
|
---|
511 | {
|
---|
512 | assert(ddf_dev);
|
---|
513 | assert(err);
|
---|
514 | usb_device_t *dev = ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
515 | if (dev == NULL) {
|
---|
516 | *err = "DDF data alloc";
|
---|
517 | return ENOMEM;
|
---|
518 | }
|
---|
519 | return usb_device_init(dev, ddf_dev, desc, err);
|
---|
520 | }
|
---|
521 |
|
---|
522 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
523 | {
|
---|
524 | assert(ddf_dev);
|
---|
525 | usb_device_t *dev = ddf_dev_data_get(ddf_dev);
|
---|
526 | assert(dev);
|
---|
527 | usb_device_fini(dev);
|
---|
528 | return;
|
---|
529 | }
|
---|
530 |
|
---|
531 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
532 | {
|
---|
533 | assert(usb_dev);
|
---|
534 | if (usb_dev->ddf_dev)
|
---|
535 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
536 | return NULL;
|
---|
537 | }
|
---|
538 |
|
---|
539 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
540 | const char* name)
|
---|
541 | {
|
---|
542 | assert(usb_dev);
|
---|
543 | if (usb_dev->ddf_dev)
|
---|
544 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
545 | return NULL;
|
---|
546 | }
|
---|
547 |
|
---|
548 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
549 | {
|
---|
550 | assert(usb_dev);
|
---|
551 | return async_exchange_begin(usb_dev->bus_session);
|
---|
552 | }
|
---|
553 |
|
---|
554 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
555 | {
|
---|
556 | async_exchange_end(exch);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /** Allocate driver specific data.
|
---|
560 | * @param usb_dev usb_device structure.
|
---|
561 | * @param size requested data size.
|
---|
562 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
563 | */
|
---|
564 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
565 | {
|
---|
566 | assert(usb_dev);
|
---|
567 | assert(usb_dev->driver_data == NULL);
|
---|
568 | return usb_dev->driver_data = calloc(1, size);
|
---|
569 |
|
---|
570 | }
|
---|
571 |
|
---|
572 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
573 | {
|
---|
574 | assert(usb_dev);
|
---|
575 | return usb_dev->driver_data;
|
---|
576 | }
|
---|
577 | /**
|
---|
578 | * @}
|
---|
579 | */
|
---|