source: mainline/uspace/lib/usbdev/src/devdrv.c@ af4e464e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since af4e464e was 4c86c7c, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Close bus_session after every other thing is down.

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