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

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

libusbdev: Fix uninitialized pipes_count if there are no interfaces.

Make asserts more meaningful.

  • Property mode set to 100644
File size: 15.8 KB
RevLine 
[6105fc0]1/*
2 * Copyright (c) 2011 Vojtech Horky
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
[160b75e]29/** @addtogroup libusbdev
[6105fc0]30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
[7d521e24]35#include <usb/dev/driver.h>
36#include <usb/dev/request.h>
[6105fc0]37#include <usb/debug.h>
[7d521e24]38#include <usb/dev/dp.h>
[6105fc0]39#include <errno.h>
[69334af]40#include <str_error.h>
[6105fc0]41#include <assert.h>
42
[1a4ea01d]43static int generic_device_add(ddf_dev_t *);
[96646a6]44static int generic_device_remove(ddf_dev_t *);
45static int generic_device_gone(ddf_dev_t *);
[6105fc0]46
47static driver_ops_t generic_driver_ops = {
[96646a6]48 .add_device = generic_device_add,
49 .dev_remove = generic_device_remove,
50 .dev_gone = generic_device_gone,
[6105fc0]51};
52static driver_t generic_driver = {
53 .driver_ops = &generic_driver_ops
54};
55
56static usb_driver_t *driver = NULL;
57
[69334af]58
59/** Main routine of USB device driver.
60 *
61 * Under normal conditions, this function never returns.
62 *
63 * @param drv USB device driver structure.
64 * @return Task exit status.
65 */
[6105fc0]66int usb_driver_main(usb_driver_t *drv)
67{
[69334af]68 assert(drv != NULL);
69
[6105fc0]70 /* Prepare the generic driver. */
71 generic_driver.name = drv->name;
72
73 driver = drv;
74
75 return ddf_driver_main(&generic_driver);
76}
77
[69334af]78/** Count number of pipes the driver expects.
79 *
80 * @param drv USB driver.
81 * @return Number of pipes (excluding default control pipe).
82 */
[0b4e7ca]83static size_t count_other_pipes(usb_endpoint_description_t **endpoints)
[6105fc0]84{
85 size_t count = 0;
[0b4e7ca]86 if (endpoints == NULL) {
[6105fc0]87 return 0;
88 }
89
[0b4e7ca]90 while (endpoints[count] != NULL) {
[6105fc0]91 count++;
92 }
93
94 return count;
95}
96
[69334af]97/** Initialize endpoint pipes, excluding default control one.
98 *
99 * @param drv The device driver.
100 * @param dev Device to be initialized.
101 * @return Error code.
102 */
[0b4e7ca]103static int initialize_other_pipes(usb_endpoint_description_t **endpoints,
[c1b1944]104 usb_device_t *dev, int alternate_setting)
[6105fc0]105{
[6ee6e6f]106 if (endpoints == NULL) {
107 dev->pipes = NULL;
108 dev->pipes_count = 0;
109 return EOK;
110 }
111
[c1b1944]112 usb_endpoint_mapping_t *pipes;
113 size_t pipes_count;
[6105fc0]114
[c1b1944]115 int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
[e484f3b]116 dev->descriptors.configuration, dev->descriptors.configuration_size,
[c1b1944]117 dev->interface_no, alternate_setting,
118 &pipes, &pipes_count);
[6105fc0]119
[5fd22d8]120 if (rc != EOK) {
[c1b1944]121 return rc;
[5fd22d8]122 }
123
[c1b1944]124 dev->pipes = pipes;
125 dev->pipes_count = pipes_count;
[0b4e7ca]126
[6105fc0]127 return EOK;
128}
[51f033ce]129/*----------------------------------------------------------------------------*/
130/** Callback when a new device is supposed to be controlled by this driver.
[69334af]131 *
[51f033ce]132 * This callback is a wrapper for USB specific version of @c device_add.
[69334af]133 *
134 * @param gen_dev Device structure as prepared by DDF.
135 * @return Error code.
136 */
[1a4ea01d]137int generic_device_add(ddf_dev_t *gen_dev)
[6105fc0]138{
139 assert(driver);
140 assert(driver->ops);
[1a4ea01d]141 assert(driver->ops->device_add);
[6105fc0]142
143 int rc;
144
[6ee6e6f]145 usb_device_t *dev = NULL;
146 const char *err_msg = NULL;
147 rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
[69334af]148 if (rc != EOK) {
[6ee6e6f]149 usb_log_error("USB device `%s' creation failed (%s): %s.\n",
150 gen_dev->name, err_msg, str_error(rc));
[69334af]151 return rc;
[6105fc0]152 }
[844f4ef]153 gen_dev->driver_data = dev;
[6105fc0]154
[1a4ea01d]155 return driver->ops->device_add(dev);
[6105fc0]156}
[96646a6]157/*----------------------------------------------------------------------------*/
[51f033ce]158/** Callback when a device is supposed to be removed from the system.
159 *
160 * This callback is a wrapper for USB specific version of @c device_remove.
161 *
162 * @param gen_dev Device structure as prepared by DDF.
163 * @return Error code.
164 */
[96646a6]165int generic_device_remove(ddf_dev_t *gen_dev)
166{
167 assert(driver);
168 assert(driver->ops);
169 if (driver->ops->device_rem == NULL)
170 return ENOTSUP;
[844f4ef]171 /* Just tell the driver to stop whatever it is doing, keep structures */
172 return driver->ops->device_rem(gen_dev->driver_data);
[96646a6]173}
174/*----------------------------------------------------------------------------*/
[51f033ce]175/** Callback when a device was removed from the system.
176 *
177 * This callback is a wrapper for USB specific version of @c device_gone.
178 *
179 * @param gen_dev Device structure as prepared by DDF.
180 * @return Error code.
181 */
[96646a6]182int generic_device_gone(ddf_dev_t *gen_dev)
183{
184 assert(driver);
185 assert(driver->ops);
[844f4ef]186 if (driver->ops->device_gone == NULL)
187 return ENOTSUP;
188 const int ret = driver->ops->device_gone(gen_dev->driver_data);
189 if (ret == EOK)
190 usb_device_destroy(gen_dev->driver_data);
[96646a6]191
[844f4ef]192 return ret;
[96646a6]193}
194/*----------------------------------------------------------------------------*/
[0b4e7ca]195/** Destroy existing pipes of a USB device.
196 *
197 * @param dev Device where to destroy the pipes.
198 * @return Error code.
199 */
200static int destroy_current_pipes(usb_device_t *dev)
201{
[c1b1944]202 int rc = usb_device_destroy_pipes(dev->ddf_dev,
203 dev->pipes, dev->pipes_count);
[0b4e7ca]204 if (rc != EOK) {
205 return rc;
206 }
207
208 dev->pipes = NULL;
209 dev->pipes_count = 0;
210
211 return EOK;
212}
213
214/** Change interface setting of a device.
215 * This function selects new alternate setting of an interface by issuing
216 * proper USB command to the device and also creates new USB pipes
217 * under @c dev->pipes.
218 *
219 * @warning This function is intended for drivers working at interface level.
220 * For drivers controlling the whole device, you need to change interface
221 * manually using usb_request_set_interface() and creating new pipes
222 * with usb_pipe_initialize_from_configuration().
223 *
[3f2af64]224 * @warning This is a wrapper function that does several operations that
225 * can fail and that cannot be rollbacked easily. That means that a failure
226 * during the SET_INTERFACE request would result in having a device with
227 * no pipes at all (except the default control one). That is because the old
228 * pipes needs to be unregistered at HC first and the new ones could not
229 * be created.
230 *
[0b4e7ca]231 * @param dev USB device.
232 * @param alternate_setting Alternate setting to choose.
233 * @param endpoints New endpoint descriptions.
234 * @return Error code.
235 */
236int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
237 usb_endpoint_description_t **endpoints)
238{
239 if (dev->interface_no < 0) {
240 return EINVAL;
241 }
242
243 int rc;
244
245 /* Destroy existing pipes. */
246 rc = destroy_current_pipes(dev);
247 if (rc != EOK) {
248 return rc;
249 }
250
251 /* Change the interface itself. */
252 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
253 alternate_setting);
254 if (rc != EOK) {
255 return rc;
256 }
257
258 /* Create new pipes. */
[c1b1944]259 rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
260
261 return rc;
262}
263
264/** Retrieve basic descriptors from the device.
265 *
[4fa0a384]266 * @param[in] ctrl_pipe Control endpoint pipe.
[c1b1944]267 * @param[out] descriptors Where to store the descriptors.
268 * @return Error code.
269 */
270int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
271 usb_device_descriptors_t *descriptors)
272{
273 assert(descriptors != NULL);
274
275 descriptors->configuration = NULL;
276
277 int rc;
278
[4fa0a384]279 /* It is worth to start a long transfer. */
[2c2cbcf]280 usb_pipe_start_long_transfer(ctrl_pipe);
[4fa0a384]281
[c1b1944]282 /* Get the device descriptor. */
283 rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
284 if (rc != EOK) {
[4fa0a384]285 goto leave;
[c1b1944]286 }
287
288 /* Get the full configuration descriptor. */
289 rc = usb_request_get_full_configuration_descriptor_alloc(
290 ctrl_pipe, 0, (void **) &descriptors->configuration,
291 &descriptors->configuration_size);
292
[4fa0a384]293leave:
294 usb_pipe_end_long_transfer(ctrl_pipe);
295
296 return rc;
[c1b1944]297}
298
299/** Create pipes for a device.
300 *
301 * This is more or less a wrapper that does following actions:
302 * - allocate and initialize pipes
303 * - map endpoints to the pipes based on the descriptions
304 * - registers endpoints with the host controller
305 *
306 * @param[in] dev Generic DDF device backing the USB one.
307 * @param[in] wire Initialized backing connection to the host controller.
308 * @param[in] endpoints Endpoints description, NULL terminated.
309 * @param[in] config_descr Configuration descriptor of active configuration.
310 * @param[in] config_descr_size Size of @p config_descr in bytes.
311 * @param[in] interface_no Interface to map from.
312 * @param[in] interface_setting Interface setting (default is usually 0).
313 * @param[out] pipes_ptr Where to store array of created pipes
314 * (not NULL terminated).
315 * @param[out] pipes_count_ptr Where to store number of pipes
316 * (set to if you wish to ignore the count).
317 * @return Error code.
318 */
[8e5ce07]319int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
[c1b1944]320 usb_endpoint_description_t **endpoints,
321 uint8_t *config_descr, size_t config_descr_size,
322 int interface_no, int interface_setting,
323 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
324{
325 assert(dev != NULL);
326 assert(wire != NULL);
327 assert(endpoints != NULL);
328 assert(config_descr != NULL);
329 assert(config_descr_size > 0);
330 assert(pipes_ptr != NULL);
331
332 size_t i;
333 int rc;
334
[5917859c]335 const size_t pipe_count = count_other_pipes(endpoints);
[c1b1944]336 if (pipe_count == 0) {
[5917859c]337 *pipes_count_ptr = pipe_count;
[c1b1944]338 *pipes_ptr = NULL;
339 return EOK;
340 }
341
342 usb_endpoint_mapping_t *pipes
343 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
344 if (pipes == NULL) {
345 return ENOMEM;
346 }
347
348 /* Initialize to NULL to allow smooth rollback. */
349 for (i = 0; i < pipe_count; i++) {
350 pipes[i].pipe = NULL;
351 }
352
353 /* Now allocate and fully initialize. */
354 for (i = 0; i < pipe_count; i++) {
355 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
356 if (pipes[i].pipe == NULL) {
357 rc = ENOMEM;
358 goto rollback_free_only;
359 }
360 pipes[i].description = endpoints[i];
361 pipes[i].interface_no = interface_no;
362 pipes[i].interface_setting = interface_setting;
363 }
364
365 /* Find the mapping from configuration descriptor. */
366 rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
367 config_descr, config_descr_size, wire);
368 if (rc != EOK) {
369 goto rollback_free_only;
370 }
371
372 /* Register the endpoints with HC. */
373 usb_hc_connection_t hc_conn;
374 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
375 if (rc != EOK) {
376 goto rollback_free_only;
377 }
378
379 rc = usb_hc_connection_open(&hc_conn);
380 if (rc != EOK) {
381 goto rollback_free_only;
382 }
383
384 for (i = 0; i < pipe_count; i++) {
385 if (pipes[i].present) {
386 rc = usb_pipe_register(pipes[i].pipe,
387 pipes[i].descriptor->poll_interval, &hc_conn);
388 if (rc != EOK) {
389 goto rollback_unregister_endpoints;
390 }
391 }
392 }
393
[fb422312]394 if (usb_hc_connection_close(&hc_conn) != EOK)
395 usb_log_warning("usb_device_create_pipes(): "
396 "Failed to close connection.\n");
[c1b1944]397
398 *pipes_ptr = pipes;
399 if (pipes_count_ptr != NULL) {
400 *pipes_count_ptr = pipe_count;
401 }
402
403 return EOK;
404
405 /*
406 * Jump here if something went wrong after endpoints have
407 * been registered.
408 * This is also the target when the registration of
409 * endpoints fails.
410 */
411rollback_unregister_endpoints:
412 for (i = 0; i < pipe_count; i++) {
413 if (pipes[i].present) {
414 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
415 }
416 }
417
[fb422312]418 if (usb_hc_connection_close(&hc_conn) != EOK)
419 usb_log_warning("usb_device_create_pipes(): "
420 "Failed to close connection.\n");
[c1b1944]421
422 /*
423 * Jump here if something went wrong before some actual communication
424 * with HC. Then the only thing that needs to be done is to free
425 * allocated memory.
426 */
427rollback_free_only:
428 for (i = 0; i < pipe_count; i++) {
429 if (pipes[i].pipe != NULL) {
430 free(pipes[i].pipe);
431 }
432 }
433 free(pipes);
[0b4e7ca]434
435 return rc;
436}
[159b91f4]437
[c1b1944]438/** Destroy pipes previously created by usb_device_create_pipes.
439 *
440 * @param[in] dev Generic DDF device backing the USB one.
441 * @param[in] pipes Endpoint mapping to be destroyed.
442 * @param[in] pipes_count Number of endpoints.
443 */
[8e5ce07]444int usb_device_destroy_pipes(const ddf_dev_t *dev,
[c1b1944]445 usb_endpoint_mapping_t *pipes, size_t pipes_count)
446{
447 assert(dev != NULL);
448
449 if (pipes_count == 0) {
[5917859c]450 assert(pipes == NULL);
[c1b1944]451 return EOK;
452 }
[5917859c]453 assert(pipes != NULL);
[c1b1944]454
455 int rc;
456
457 /* Prepare connection to HC to allow endpoint unregistering. */
458 usb_hc_connection_t hc_conn;
459 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
460 if (rc != EOK) {
461 return rc;
462 }
463 rc = usb_hc_connection_open(&hc_conn);
464 if (rc != EOK) {
465 return rc;
466 }
467
468 /* Destroy the pipes. */
469 size_t i;
470 for (i = 0; i < pipes_count; i++) {
471 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
472 free(pipes[i].pipe);
473 }
474
[fb422312]475 if (usb_hc_connection_close(&hc_conn) != EOK)
476 usb_log_warning("usb_device_destroy_pipes(): "
477 "Failed to close connection.\n");
[c1b1944]478
479 free(pipes);
480
481 return EOK;
482}
483
[3f2af64]484/** Initialize control pipe in a device.
485 *
486 * @param dev USB device in question.
487 * @param errmsg Where to store error context.
488 * @return
489 */
490static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
[6ee6e6f]491{
492 int rc;
493
494 rc = usb_device_connection_initialize_from_device(&dev->wire,
495 dev->ddf_dev);
496 if (rc != EOK) {
497 *errmsg = "device connection initialization";
498 return rc;
499 }
500
501 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
502 &dev->wire);
503 if (rc != EOK) {
504 *errmsg = "default control pipe initialization";
505 return rc;
506 }
507
[3f2af64]508 return EOK;
[6ee6e6f]509}
510
511
512/** Create new instance of USB device.
513 *
514 * @param[in] ddf_dev Generic DDF device backing the USB one.
515 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
516 * @param[out] dev_ptr Where to store pointer to the new device.
517 * @param[out] errstr_ptr Where to store description of context
518 * (in case error occurs).
519 * @return Error code.
520 */
521int usb_device_create(ddf_dev_t *ddf_dev,
522 usb_endpoint_description_t **endpoints,
523 usb_device_t **dev_ptr, const char **errstr_ptr)
524{
525 assert(dev_ptr != NULL);
526 assert(ddf_dev != NULL);
527
528 int rc;
529
530 usb_device_t *dev = malloc(sizeof(usb_device_t));
531 if (dev == NULL) {
532 *errstr_ptr = "structure allocation";
533 return ENOMEM;
534 }
535
[3f2af64]536 // FIXME: proper deallocation in case of errors
537
[6ee6e6f]538 dev->ddf_dev = ddf_dev;
539 dev->driver_data = NULL;
540 dev->descriptors.configuration = NULL;
541 dev->alternate_interfaces = NULL;
542
543 dev->pipes_count = 0;
544 dev->pipes = NULL;
545
[3f2af64]546 /* Initialize backing wire and control pipe. */
547 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
548 if (rc != EOK) {
549 return rc;
550 }
551
552 /* Get our interface. */
553 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
554
555 /* Retrieve standard descriptors. */
556 rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
557 &dev->descriptors);
[6ee6e6f]558 if (rc != EOK) {
[3f2af64]559 *errstr_ptr = "descriptor retrieval";
[6ee6e6f]560 return rc;
561 }
562
[3f2af64]563 /* Create alternate interfaces. */
[231748a]564 rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
565 dev->descriptors.configuration_size, dev->interface_no,
566 &dev->alternate_interfaces);
[6ee6e6f]567 if (rc != EOK) {
568 /* We will try to silently ignore this. */
569 dev->alternate_interfaces = NULL;
570 }
571
572 rc = initialize_other_pipes(endpoints, dev, 0);
573 if (rc != EOK) {
574 *errstr_ptr = "pipes initialization";
575 return rc;
576 }
577
578 *errstr_ptr = NULL;
579 *dev_ptr = dev;
580
581 return EOK;
582}
583
[70452dd4]584/** Destroy instance of a USB device.
585 *
586 * @param dev Device to be destroyed.
587 */
588void usb_device_destroy(usb_device_t *dev)
589{
590 if (dev == NULL) {
591 return;
592 }
593
594 /* Ignore errors and hope for the best. */
595 usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
[9636bad6]596 free(dev->descriptors.configuration);
[70452dd4]597
598 if (dev->alternate_interfaces != NULL) {
[9636bad6]599 free(dev->alternate_interfaces->alternatives);
[70452dd4]600 }
[9636bad6]601 free(dev->alternate_interfaces);
[70452dd4]602
603 free(dev);
604}
605
[6105fc0]606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.