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

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

usb: Add bunch of const qualifiers.

We shall need them.

  • Property mode set to 100644
File size: 14.9 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 new device is supposed to be controlled by this driver.
131 *
132 * This callback is a wrapper for USB specific version of @c add_device.
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
154 return driver->ops->device_add(dev);
155}
156/*----------------------------------------------------------------------------*/
157int generic_device_remove(ddf_dev_t *gen_dev)
158{
159 assert(driver);
160 assert(driver->ops);
161 if (driver->ops->device_rem == NULL)
162 return ENOTSUP;
163
164 return ENOTSUP;
165}
166/*----------------------------------------------------------------------------*/
167int generic_device_gone(ddf_dev_t *gen_dev)
168{
169 assert(driver);
170 assert(driver->ops);
171 assert(driver->ops->device_gone);
172
173 return ENOTSUP;
174}
175/*----------------------------------------------------------------------------*/
176/** Destroy existing pipes of a USB device.
177 *
178 * @param dev Device where to destroy the pipes.
179 * @return Error code.
180 */
181static int destroy_current_pipes(usb_device_t *dev)
182{
183 int rc = usb_device_destroy_pipes(dev->ddf_dev,
184 dev->pipes, dev->pipes_count);
185 if (rc != EOK) {
186 return rc;
187 }
188
189 dev->pipes = NULL;
190 dev->pipes_count = 0;
191
192 return EOK;
193}
194
195/** Change interface setting of a device.
196 * This function selects new alternate setting of an interface by issuing
197 * proper USB command to the device and also creates new USB pipes
198 * under @c dev->pipes.
199 *
200 * @warning This function is intended for drivers working at interface level.
201 * For drivers controlling the whole device, you need to change interface
202 * manually using usb_request_set_interface() and creating new pipes
203 * with usb_pipe_initialize_from_configuration().
204 *
205 * @warning This is a wrapper function that does several operations that
206 * can fail and that cannot be rollbacked easily. That means that a failure
207 * during the SET_INTERFACE request would result in having a device with
208 * no pipes at all (except the default control one). That is because the old
209 * pipes needs to be unregistered at HC first and the new ones could not
210 * be created.
211 *
212 * @param dev USB device.
213 * @param alternate_setting Alternate setting to choose.
214 * @param endpoints New endpoint descriptions.
215 * @return Error code.
216 */
217int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
218 usb_endpoint_description_t **endpoints)
219{
220 if (dev->interface_no < 0) {
221 return EINVAL;
222 }
223
224 int rc;
225
226 /* Destroy existing pipes. */
227 rc = destroy_current_pipes(dev);
228 if (rc != EOK) {
229 return rc;
230 }
231
232 /* Change the interface itself. */
233 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
234 alternate_setting);
235 if (rc != EOK) {
236 return rc;
237 }
238
239 /* Create new pipes. */
240 rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
241
242 return rc;
243}
244
245/** Retrieve basic descriptors from the device.
246 *
247 * @param[in] ctrl_pipe Control endpoint pipe.
248 * @param[out] descriptors Where to store the descriptors.
249 * @return Error code.
250 */
251int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
252 usb_device_descriptors_t *descriptors)
253{
254 assert(descriptors != NULL);
255
256 descriptors->configuration = NULL;
257
258 int rc;
259
260 /* It is worth to start a long transfer. */
261 usb_pipe_start_long_transfer(ctrl_pipe);
262
263 /* Get the device descriptor. */
264 rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
265 if (rc != EOK) {
266 goto leave;
267 }
268
269 /* Get the full configuration descriptor. */
270 rc = usb_request_get_full_configuration_descriptor_alloc(
271 ctrl_pipe, 0, (void **) &descriptors->configuration,
272 &descriptors->configuration_size);
273
274leave:
275 usb_pipe_end_long_transfer(ctrl_pipe);
276
277 return rc;
278}
279
280/** Create pipes for a device.
281 *
282 * This is more or less a wrapper that does following actions:
283 * - allocate and initialize pipes
284 * - map endpoints to the pipes based on the descriptions
285 * - registers endpoints with the host controller
286 *
287 * @param[in] dev Generic DDF device backing the USB one.
288 * @param[in] wire Initialized backing connection to the host controller.
289 * @param[in] endpoints Endpoints description, NULL terminated.
290 * @param[in] config_descr Configuration descriptor of active configuration.
291 * @param[in] config_descr_size Size of @p config_descr in bytes.
292 * @param[in] interface_no Interface to map from.
293 * @param[in] interface_setting Interface setting (default is usually 0).
294 * @param[out] pipes_ptr Where to store array of created pipes
295 * (not NULL terminated).
296 * @param[out] pipes_count_ptr Where to store number of pipes
297 * (set to if you wish to ignore the count).
298 * @return Error code.
299 */
300int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
301 usb_endpoint_description_t **endpoints,
302 uint8_t *config_descr, size_t config_descr_size,
303 int interface_no, int interface_setting,
304 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
305{
306 assert(dev != NULL);
307 assert(wire != NULL);
308 assert(endpoints != NULL);
309 assert(config_descr != NULL);
310 assert(config_descr_size > 0);
311 assert(pipes_ptr != NULL);
312
313 size_t i;
314 int rc;
315
316 size_t pipe_count = count_other_pipes(endpoints);
317 if (pipe_count == 0) {
318 *pipes_ptr = NULL;
319 return EOK;
320 }
321
322 usb_endpoint_mapping_t *pipes
323 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
324 if (pipes == NULL) {
325 return ENOMEM;
326 }
327
328 /* Initialize to NULL to allow smooth rollback. */
329 for (i = 0; i < pipe_count; i++) {
330 pipes[i].pipe = NULL;
331 }
332
333 /* Now allocate and fully initialize. */
334 for (i = 0; i < pipe_count; i++) {
335 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
336 if (pipes[i].pipe == NULL) {
337 rc = ENOMEM;
338 goto rollback_free_only;
339 }
340 pipes[i].description = endpoints[i];
341 pipes[i].interface_no = interface_no;
342 pipes[i].interface_setting = interface_setting;
343 }
344
345 /* Find the mapping from configuration descriptor. */
346 rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
347 config_descr, config_descr_size, wire);
348 if (rc != EOK) {
349 goto rollback_free_only;
350 }
351
352 /* Register the endpoints with HC. */
353 usb_hc_connection_t hc_conn;
354 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
355 if (rc != EOK) {
356 goto rollback_free_only;
357 }
358
359 rc = usb_hc_connection_open(&hc_conn);
360 if (rc != EOK) {
361 goto rollback_free_only;
362 }
363
364 for (i = 0; i < pipe_count; i++) {
365 if (pipes[i].present) {
366 rc = usb_pipe_register(pipes[i].pipe,
367 pipes[i].descriptor->poll_interval, &hc_conn);
368 if (rc != EOK) {
369 goto rollback_unregister_endpoints;
370 }
371 }
372 }
373
374 usb_hc_connection_close(&hc_conn);
375
376 *pipes_ptr = pipes;
377 if (pipes_count_ptr != NULL) {
378 *pipes_count_ptr = pipe_count;
379 }
380
381 return EOK;
382
383 /*
384 * Jump here if something went wrong after endpoints have
385 * been registered.
386 * This is also the target when the registration of
387 * endpoints fails.
388 */
389rollback_unregister_endpoints:
390 for (i = 0; i < pipe_count; i++) {
391 if (pipes[i].present) {
392 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
393 }
394 }
395
396 usb_hc_connection_close(&hc_conn);
397
398 /*
399 * Jump here if something went wrong before some actual communication
400 * with HC. Then the only thing that needs to be done is to free
401 * allocated memory.
402 */
403rollback_free_only:
404 for (i = 0; i < pipe_count; i++) {
405 if (pipes[i].pipe != NULL) {
406 free(pipes[i].pipe);
407 }
408 }
409 free(pipes);
410
411 return rc;
412}
413
414/** Destroy pipes previously created by usb_device_create_pipes.
415 *
416 * @param[in] dev Generic DDF device backing the USB one.
417 * @param[in] pipes Endpoint mapping to be destroyed.
418 * @param[in] pipes_count Number of endpoints.
419 */
420int usb_device_destroy_pipes(const ddf_dev_t *dev,
421 usb_endpoint_mapping_t *pipes, size_t pipes_count)
422{
423 assert(dev != NULL);
424 assert(((pipes != NULL) && (pipes_count > 0))
425 || ((pipes == NULL) && (pipes_count == 0)));
426
427 if (pipes_count == 0) {
428 return EOK;
429 }
430
431 int rc;
432
433 /* Prepare connection to HC to allow endpoint unregistering. */
434 usb_hc_connection_t hc_conn;
435 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
436 if (rc != EOK) {
437 return rc;
438 }
439 rc = usb_hc_connection_open(&hc_conn);
440 if (rc != EOK) {
441 return rc;
442 }
443
444 /* Destroy the pipes. */
445 size_t i;
446 for (i = 0; i < pipes_count; i++) {
447 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
448 free(pipes[i].pipe);
449 }
450
451 usb_hc_connection_close(&hc_conn);
452
453 free(pipes);
454
455 return EOK;
456}
457
458/** Initialize control pipe in a device.
459 *
460 * @param dev USB device in question.
461 * @param errmsg Where to store error context.
462 * @return
463 */
464static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
465{
466 int rc;
467
468 rc = usb_device_connection_initialize_from_device(&dev->wire,
469 dev->ddf_dev);
470 if (rc != EOK) {
471 *errmsg = "device connection initialization";
472 return rc;
473 }
474
475 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
476 &dev->wire);
477 if (rc != EOK) {
478 *errmsg = "default control pipe initialization";
479 return rc;
480 }
481
482 return EOK;
483}
484
485
486/** Create new instance of USB device.
487 *
488 * @param[in] ddf_dev Generic DDF device backing the USB one.
489 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
490 * @param[out] dev_ptr Where to store pointer to the new device.
491 * @param[out] errstr_ptr Where to store description of context
492 * (in case error occurs).
493 * @return Error code.
494 */
495int usb_device_create(ddf_dev_t *ddf_dev,
496 usb_endpoint_description_t **endpoints,
497 usb_device_t **dev_ptr, const char **errstr_ptr)
498{
499 assert(dev_ptr != NULL);
500 assert(ddf_dev != NULL);
501
502 int rc;
503
504 usb_device_t *dev = malloc(sizeof(usb_device_t));
505 if (dev == NULL) {
506 *errstr_ptr = "structure allocation";
507 return ENOMEM;
508 }
509
510 // FIXME: proper deallocation in case of errors
511
512 dev->ddf_dev = ddf_dev;
513 dev->driver_data = NULL;
514 dev->descriptors.configuration = NULL;
515 dev->alternate_interfaces = NULL;
516
517 dev->pipes_count = 0;
518 dev->pipes = NULL;
519
520 /* Initialize backing wire and control pipe. */
521 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
522 if (rc != EOK) {
523 return rc;
524 }
525
526 /* Get our interface. */
527 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
528
529 /* Retrieve standard descriptors. */
530 rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
531 &dev->descriptors);
532 if (rc != EOK) {
533 *errstr_ptr = "descriptor retrieval";
534 return rc;
535 }
536
537 /* Create alternate interfaces. */
538 rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
539 dev->descriptors.configuration_size, dev->interface_no,
540 &dev->alternate_interfaces);
541 if (rc != EOK) {
542 /* We will try to silently ignore this. */
543 dev->alternate_interfaces = NULL;
544 }
545
546 rc = initialize_other_pipes(endpoints, dev, 0);
547 if (rc != EOK) {
548 *errstr_ptr = "pipes initialization";
549 return rc;
550 }
551
552 *errstr_ptr = NULL;
553 *dev_ptr = dev;
554
555 return EOK;
556}
557
558/** Destroy instance of a USB device.
559 *
560 * @param dev Device to be destroyed.
561 */
562void usb_device_destroy(usb_device_t *dev)
563{
564 if (dev == NULL) {
565 return;
566 }
567
568 /* Ignore errors and hope for the best. */
569 usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
570 if (dev->descriptors.configuration != NULL) {
571 free(dev->descriptors.configuration);
572 }
573
574 if (dev->alternate_interfaces != NULL) {
575 if (dev->alternate_interfaces->alternatives != NULL) {
576 free(dev->alternate_interfaces->alternatives);
577 }
578 free(dev->alternate_interfaces);
579 }
580
581 free(dev);
582}
583
584/**
585 * @}
586 */
Note: See TracBrowser for help on using the repository browser.