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

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

libusbdev: Comments

  • Property mode set to 100644
File size: 15.6 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 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 size_t pipe_count = count_other_pipes(endpoints);
336 if (pipe_count == 0) {
337 *pipes_ptr = NULL;
338 return EOK;
339 }
340
341 usb_endpoint_mapping_t *pipes
342 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
343 if (pipes == NULL) {
344 return ENOMEM;
345 }
346
347 /* Initialize to NULL to allow smooth rollback. */
348 for (i = 0; i < pipe_count; i++) {
349 pipes[i].pipe = NULL;
350 }
351
352 /* Now allocate and fully initialize. */
353 for (i = 0; i < pipe_count; i++) {
354 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
355 if (pipes[i].pipe == NULL) {
356 rc = ENOMEM;
357 goto rollback_free_only;
358 }
359 pipes[i].description = endpoints[i];
360 pipes[i].interface_no = interface_no;
361 pipes[i].interface_setting = interface_setting;
362 }
363
364 /* Find the mapping from configuration descriptor. */
365 rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
366 config_descr, config_descr_size, wire);
367 if (rc != EOK) {
368 goto rollback_free_only;
369 }
370
371 /* Register the endpoints with HC. */
372 usb_hc_connection_t hc_conn;
373 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
374 if (rc != EOK) {
375 goto rollback_free_only;
376 }
377
378 rc = usb_hc_connection_open(&hc_conn);
379 if (rc != EOK) {
380 goto rollback_free_only;
381 }
382
383 for (i = 0; i < pipe_count; i++) {
384 if (pipes[i].present) {
385 rc = usb_pipe_register(pipes[i].pipe,
386 pipes[i].descriptor->poll_interval, &hc_conn);
387 if (rc != EOK) {
388 goto rollback_unregister_endpoints;
389 }
390 }
391 }
392
393 usb_hc_connection_close(&hc_conn);
394
395 *pipes_ptr = pipes;
396 if (pipes_count_ptr != NULL) {
397 *pipes_count_ptr = pipe_count;
398 }
399
400 return EOK;
401
402 /*
403 * Jump here if something went wrong after endpoints have
404 * been registered.
405 * This is also the target when the registration of
406 * endpoints fails.
407 */
408rollback_unregister_endpoints:
409 for (i = 0; i < pipe_count; i++) {
410 if (pipes[i].present) {
411 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
412 }
413 }
414
415 usb_hc_connection_close(&hc_conn);
416
417 /*
418 * Jump here if something went wrong before some actual communication
419 * with HC. Then the only thing that needs to be done is to free
420 * allocated memory.
421 */
422rollback_free_only:
423 for (i = 0; i < pipe_count; i++) {
424 if (pipes[i].pipe != NULL) {
425 free(pipes[i].pipe);
426 }
427 }
428 free(pipes);
429
430 return rc;
431}
432
433/** Destroy pipes previously created by usb_device_create_pipes.
434 *
435 * @param[in] dev Generic DDF device backing the USB one.
436 * @param[in] pipes Endpoint mapping to be destroyed.
437 * @param[in] pipes_count Number of endpoints.
438 */
439int usb_device_destroy_pipes(const ddf_dev_t *dev,
440 usb_endpoint_mapping_t *pipes, size_t pipes_count)
441{
442 assert(dev != NULL);
443 assert(((pipes != NULL) && (pipes_count > 0))
444 || ((pipes == NULL) && (pipes_count == 0)));
445
446 if (pipes_count == 0) {
447 return EOK;
448 }
449
450 int rc;
451
452 /* Prepare connection to HC to allow endpoint unregistering. */
453 usb_hc_connection_t hc_conn;
454 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
455 if (rc != EOK) {
456 return rc;
457 }
458 rc = usb_hc_connection_open(&hc_conn);
459 if (rc != EOK) {
460 return rc;
461 }
462
463 /* Destroy the pipes. */
464 size_t i;
465 for (i = 0; i < pipes_count; i++) {
466 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
467 free(pipes[i].pipe);
468 }
469
470 usb_hc_connection_close(&hc_conn);
471
472 free(pipes);
473
474 return EOK;
475}
476
477/** Initialize control pipe in a device.
478 *
479 * @param dev USB device in question.
480 * @param errmsg Where to store error context.
481 * @return
482 */
483static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
484{
485 int rc;
486
487 rc = usb_device_connection_initialize_from_device(&dev->wire,
488 dev->ddf_dev);
489 if (rc != EOK) {
490 *errmsg = "device connection initialization";
491 return rc;
492 }
493
494 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
495 &dev->wire);
496 if (rc != EOK) {
497 *errmsg = "default control pipe initialization";
498 return rc;
499 }
500
501 return EOK;
502}
503
504
505/** Create new instance of USB device.
506 *
507 * @param[in] ddf_dev Generic DDF device backing the USB one.
508 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
509 * @param[out] dev_ptr Where to store pointer to the new device.
510 * @param[out] errstr_ptr Where to store description of context
511 * (in case error occurs).
512 * @return Error code.
513 */
514int usb_device_create(ddf_dev_t *ddf_dev,
515 usb_endpoint_description_t **endpoints,
516 usb_device_t **dev_ptr, const char **errstr_ptr)
517{
518 assert(dev_ptr != NULL);
519 assert(ddf_dev != NULL);
520
521 int rc;
522
523 usb_device_t *dev = malloc(sizeof(usb_device_t));
524 if (dev == NULL) {
525 *errstr_ptr = "structure allocation";
526 return ENOMEM;
527 }
528
529 // FIXME: proper deallocation in case of errors
530
531 dev->ddf_dev = ddf_dev;
532 dev->driver_data = NULL;
533 dev->descriptors.configuration = NULL;
534 dev->alternate_interfaces = NULL;
535
536 dev->pipes_count = 0;
537 dev->pipes = NULL;
538
539 /* Initialize backing wire and control pipe. */
540 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
541 if (rc != EOK) {
542 return rc;
543 }
544
545 /* Get our interface. */
546 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
547
548 /* Retrieve standard descriptors. */
549 rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
550 &dev->descriptors);
551 if (rc != EOK) {
552 *errstr_ptr = "descriptor retrieval";
553 return rc;
554 }
555
556 /* Create alternate interfaces. */
557 rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
558 dev->descriptors.configuration_size, dev->interface_no,
559 &dev->alternate_interfaces);
560 if (rc != EOK) {
561 /* We will try to silently ignore this. */
562 dev->alternate_interfaces = NULL;
563 }
564
565 rc = initialize_other_pipes(endpoints, dev, 0);
566 if (rc != EOK) {
567 *errstr_ptr = "pipes initialization";
568 return rc;
569 }
570
571 *errstr_ptr = NULL;
572 *dev_ptr = dev;
573
574 return EOK;
575}
576
577/** Destroy instance of a USB device.
578 *
579 * @param dev Device to be destroyed.
580 */
581void usb_device_destroy(usb_device_t *dev)
582{
583 if (dev == NULL) {
584 return;
585 }
586
587 /* Ignore errors and hope for the best. */
588 usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
589 free(dev->descriptors.configuration);
590
591 if (dev->alternate_interfaces != NULL) {
592 free(dev->alternate_interfaces->alternatives);
593 }
594 free(dev->alternate_interfaces);
595
596 free(dev);
597}
598
599/**
600 * @}
601 */
Note: See TracBrowser for help on using the repository browser.