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

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

usb: Further const madness.

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