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

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

libusb, libusbdev: Provide generic usb_get_info_by_handle function.

Add separate wrappers for host controller handle, address and interface number.

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