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

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

libusbdev: Accept const pointer in driver_main.

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