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

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

libusbdev: Unregister only endpoints that are present.

Not present endpoints don't need it and do not have to be fully initialized.
Fixes crash on usbhid unplug.

  • Property mode set to 100644
File size: 16.0 KB
Line 
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
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/dp.h>
39#include <errno.h>
40#include <str_error.h>
41#include <assert.h>
42
43static int generic_device_add(ddf_dev_t *);
44static int generic_device_remove(ddf_dev_t *);
45static int generic_device_gone(ddf_dev_t *);
46
47static driver_ops_t generic_driver_ops = {
48 .add_device = generic_device_add,
49 .dev_remove = generic_device_remove,
50 .dev_gone = generic_device_gone,
51};
52static driver_t generic_driver = {
53 .driver_ops = &generic_driver_ops
54};
55
56static const usb_driver_t *driver = NULL;
57
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 */
66int usb_driver_main(usb_driver_t *drv)
67{
68 assert(drv != NULL);
69
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
78/** Count number of pipes the driver expects.
79 *
80 * @param drv USB driver.
81 * @return Number of pipes (excluding default control pipe).
82 */
83static size_t count_other_pipes(usb_endpoint_description_t **endpoints)
84{
85 size_t count = 0;
86 if (endpoints == NULL) {
87 return 0;
88 }
89
90 while (endpoints[count] != NULL) {
91 count++;
92 }
93
94 return count;
95}
96
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 */
103static int initialize_other_pipes(usb_endpoint_description_t **endpoints,
104 usb_device_t *dev, int alternate_setting)
105{
106 if (endpoints == NULL) {
107 dev->pipes = NULL;
108 dev->pipes_count = 0;
109 return EOK;
110 }
111
112 usb_endpoint_mapping_t *pipes;
113 size_t pipes_count;
114
115 int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
116 dev->descriptors.configuration, dev->descriptors.configuration_size,
117 dev->interface_no, alternate_setting,
118 &pipes, &pipes_count);
119
120 if (rc != EOK) {
121 return rc;
122 }
123
124 dev->pipes = pipes;
125 dev->pipes_count = pipes_count;
126
127 return EOK;
128}
129/*----------------------------------------------------------------------------*/
130/** Callback when a new device is supposed to be controlled by this driver.
131 *
132 * This callback is a wrapper for USB specific version of @c device_add.
133 *
134 * @param gen_dev Device structure as prepared by DDF.
135 * @return Error code.
136 */
137int generic_device_add(ddf_dev_t *gen_dev)
138{
139 assert(driver);
140 assert(driver->ops);
141 assert(driver->ops->device_add);
142
143 int rc;
144
145 usb_device_t *dev = NULL;
146 const char *err_msg = NULL;
147 rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
148 if (rc != EOK) {
149 usb_log_error("USB device `%s' creation failed (%s): %s.\n",
150 gen_dev->name, err_msg, str_error(rc));
151 return rc;
152 }
153 gen_dev->driver_data = dev;
154
155 return driver->ops->device_add(dev);
156}
157/*----------------------------------------------------------------------------*/
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 */
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;
171 /* Just tell the driver to stop whatever it is doing, keep structures */
172 return driver->ops->device_rem(gen_dev->driver_data);
173}
174/*----------------------------------------------------------------------------*/
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 */
182int generic_device_gone(ddf_dev_t *gen_dev)
183{
184 assert(driver);
185 assert(driver->ops);
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);
191
192 return ret;
193}
194/*----------------------------------------------------------------------------*/
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{
202 int rc = usb_device_destroy_pipes(dev->ddf_dev,
203 dev->pipes, dev->pipes_count);
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 *
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 *
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. */
259 rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
260
261 return rc;
262}
263
264/** Retrieve basic descriptors from the device.
265 *
266 * @param[in] ctrl_pipe Control endpoint pipe.
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
279 /* It is worth to start a long transfer. */
280 usb_pipe_start_long_transfer(ctrl_pipe);
281
282 /* Get the device descriptor. */
283 rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
284 if (rc != EOK) {
285 goto leave;
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
293leave:
294 usb_pipe_end_long_transfer(ctrl_pipe);
295
296 return rc;
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 */
319int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
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
335 const size_t pipe_count = count_other_pipes(endpoints);
336 if (pipe_count == 0) {
337 *pipes_count_ptr = pipe_count;
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
394 if (usb_hc_connection_close(&hc_conn) != EOK)
395 usb_log_warning("usb_device_create_pipes(): "
396 "Failed to close connection.\n");
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
418 if (usb_hc_connection_close(&hc_conn) != EOK)
419 usb_log_warning("usb_device_create_pipes(): "
420 "Failed to close connection.\n");
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);
434
435 return rc;
436}
437
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 */
444int usb_device_destroy_pipes(const ddf_dev_t *dev,
445 usb_endpoint_mapping_t *pipes, size_t pipes_count)
446{
447 assert(dev != NULL);
448
449 if (pipes_count == 0) {
450 assert(pipes == NULL);
451 return EOK;
452 }
453 assert(pipes != NULL);
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_log_debug2("Unregistering pipe %zu (%spresent).\n",
472 i, pipes[i].present ? "" : "not ");
473 if (pipes[i].present)
474 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
475 free(pipes[i].pipe);
476 }
477
478 if (usb_hc_connection_close(&hc_conn) != EOK)
479 usb_log_warning("usb_device_destroy_pipes(): "
480 "Failed to close connection.\n");
481
482 free(pipes);
483
484 return EOK;
485}
486
487/** Initialize control pipe in a device.
488 *
489 * @param dev USB device in question.
490 * @param errmsg Where to store error context.
491 * @return
492 */
493static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
494{
495 int rc;
496
497 rc = usb_device_connection_initialize_from_device(&dev->wire,
498 dev->ddf_dev);
499 if (rc != EOK) {
500 *errmsg = "device connection initialization";
501 return rc;
502 }
503
504 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
505 &dev->wire);
506 if (rc != EOK) {
507 *errmsg = "default control pipe initialization";
508 return rc;
509 }
510
511 return EOK;
512}
513
514
515/** Create new instance of USB device.
516 *
517 * @param[in] ddf_dev Generic DDF device backing the USB one.
518 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
519 * @param[out] dev_ptr Where to store pointer to the new device.
520 * @param[out] errstr_ptr Where to store description of context
521 * (in case error occurs).
522 * @return Error code.
523 */
524int usb_device_create(ddf_dev_t *ddf_dev,
525 usb_endpoint_description_t **endpoints,
526 usb_device_t **dev_ptr, const char **errstr_ptr)
527{
528 assert(dev_ptr != NULL);
529 assert(ddf_dev != NULL);
530
531 int rc;
532
533 usb_device_t *dev = malloc(sizeof(usb_device_t));
534 if (dev == NULL) {
535 *errstr_ptr = "structure allocation";
536 return ENOMEM;
537 }
538
539 // FIXME: proper deallocation in case of errors
540
541 dev->ddf_dev = ddf_dev;
542 dev->driver_data = NULL;
543 dev->descriptors.configuration = NULL;
544 dev->alternate_interfaces = NULL;
545
546 dev->pipes_count = 0;
547 dev->pipes = NULL;
548
549 /* Initialize backing wire and control pipe. */
550 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
551 if (rc != EOK) {
552 return rc;
553 }
554
555 /* Get our interface. */
556 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
557
558 /* Retrieve standard descriptors. */
559 rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
560 &dev->descriptors);
561 if (rc != EOK) {
562 *errstr_ptr = "descriptor retrieval";
563 return rc;
564 }
565
566 /* Create alternate interfaces. */
567 rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
568 dev->descriptors.configuration_size, dev->interface_no,
569 &dev->alternate_interfaces);
570 if (rc != EOK) {
571 /* We will try to silently ignore this. */
572 dev->alternate_interfaces = NULL;
573 }
574
575 rc = initialize_other_pipes(endpoints, dev, 0);
576 if (rc != EOK) {
577 *errstr_ptr = "pipes initialization";
578 return rc;
579 }
580
581 *errstr_ptr = NULL;
582 *dev_ptr = dev;
583
584 return EOK;
585}
586
587/** Destroy instance of a USB device.
588 *
589 * @param dev Device to be destroyed.
590 */
591void usb_device_destroy(usb_device_t *dev)
592{
593 if (dev == NULL) {
594 return;
595 }
596
597 /* Ignore errors and hope for the best. */
598 usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
599 free(dev->descriptors.configuration);
600
601 if (dev->alternate_interfaces != NULL) {
602 free(dev->alternate_interfaces->alternatives);
603 }
604 free(dev->alternate_interfaces);
605
606 free(dev);
607}
608
609/**
610 * @}
611 */
Note: See TracBrowser for help on using the repository browser.