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

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

libusbdev: Implement generic gone and remove callbacks.

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