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

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

libusbdev: Add usb_device constructor using devman handle.

Fix initialization error paths.

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