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

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

Implement and use usb_get_device_handle.

Enables object instantiation on usb interface functions.
avoids forwarding in usbmid driver.

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