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

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

libusbdev: Remove unused parameters and variables.

  • Property mode set to 100644
File size: 16.0 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.h>
39#include <usb/dev/dp.h>
40#include <errno.h>
41#include <str_error.h>
42#include <assert.h>
43
44static int generic_device_add(ddf_dev_t *);
45static int generic_device_remove(ddf_dev_t *);
46static int generic_device_gone(ddf_dev_t *);
47
48static driver_ops_t generic_driver_ops = {
49 .dev_add = generic_device_add,
50 .dev_remove = generic_device_remove,
51 .dev_gone = generic_device_gone,
52};
53static driver_t generic_driver = {
54 .driver_ops = &generic_driver_ops
55};
56
57static const usb_driver_t *driver = NULL;
58
59
60/** Main routine of USB device driver.
61 *
62 * Under normal conditions, this function never returns.
63 *
64 * @param drv USB device driver structure.
65 * @return Task exit status.
66 */
67int usb_driver_main(const usb_driver_t *drv)
68{
69 assert(drv != NULL);
70
71 /* Prepare the generic driver. */
72 generic_driver.name = drv->name;
73
74 driver = drv;
75
76 return ddf_driver_main(&generic_driver);
77}
78
79/** Count number of pipes the driver expects.
80 *
81 * @param drv USB driver.
82 * @return Number of pipes (excluding default control pipe).
83 */
84static inline size_t count_other_pipes(
85 const usb_endpoint_description_t **endpoints)
86{
87 size_t count;
88 for (count = 0; endpoints && endpoints[count] != NULL; ++count);
89 return count;
90}
91
92/** Initialize endpoint pipes, excluding default control one.
93 *
94 * @param drv The device driver.
95 * @param dev Device to be initialized.
96 * @return Error code.
97 */
98static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
99 usb_device_t *dev, int alternate_setting)
100{
101 assert(dev);
102
103 if (endpoints == NULL) {
104 dev->pipes = NULL;
105 dev->pipes_count = 0;
106 return EOK;
107 }
108
109 usb_endpoint_mapping_t *pipes;
110 size_t pipes_count;
111
112 int rc = usb_device_create_pipes(&dev->wire, endpoints,
113 dev->descriptors.configuration, dev->descriptors.configuration_size,
114 dev->interface_no, alternate_setting, &pipes, &pipes_count);
115
116 if (rc != EOK) {
117 return rc;
118 }
119
120 dev->pipes = pipes;
121 dev->pipes_count = pipes_count;
122
123 return EOK;
124}
125/*----------------------------------------------------------------------------*/
126/** Callback when a new device is supposed to be controlled by this driver.
127 *
128 * This callback is a wrapper for USB specific version of @c device_add.
129 *
130 * @param gen_dev Device structure as prepared by DDF.
131 * @return Error code.
132 */
133int generic_device_add(ddf_dev_t *gen_dev)
134{
135 assert(driver);
136 assert(driver->ops);
137 assert(driver->ops->device_add);
138
139 usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
140 if (dev == NULL) {
141 usb_log_error("USB device `%s' structure allocation failed.\n",
142 gen_dev->name);
143 return ENOMEM;
144 }
145 const char *err_msg = NULL;
146 int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
147 if (rc != EOK) {
148 usb_log_error("USB device `%s' init failed (%s): %s.\n",
149 gen_dev->name, err_msg, str_error(rc));
150 return rc;
151 }
152
153 rc = driver->ops->device_add(dev);
154 if (rc != EOK)
155 usb_device_deinit(dev);
156 return rc;
157}
158/*----------------------------------------------------------------------------*/
159/** Callback when a device is supposed to be removed from the system.
160 *
161 * This callback is a wrapper for USB specific version of @c device_remove.
162 *
163 * @param gen_dev Device structure as prepared by DDF.
164 * @return Error code.
165 */
166int generic_device_remove(ddf_dev_t *gen_dev)
167{
168 assert(driver);
169 assert(driver->ops);
170 if (driver->ops->device_rem == NULL)
171 return ENOTSUP;
172 /* Just tell the driver to stop whatever it is doing, keep structures */
173 const int ret = driver->ops->device_rem(gen_dev->driver_data);
174 if (ret != EOK)
175 return ret;
176 return ENOTSUP;
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 const int rc = usb_device_destroy_pipes(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(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(wire != NULL);
341 assert(endpoints != NULL);
342 assert(config_descr != NULL);
343 assert(config_descr_size > 0);
344 assert(pipes_ptr != NULL);
345
346 size_t i;
347 int rc;
348
349 const size_t pipe_count = count_other_pipes(endpoints);
350 if (pipe_count == 0) {
351 if (pipes_count_ptr)
352 *pipes_count_ptr = pipe_count;
353 *pipes_ptr = NULL;
354 return EOK;
355 }
356
357 usb_endpoint_mapping_t *pipes
358 = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
359 if (pipes == NULL) {
360 return ENOMEM;
361 }
362
363 /* Now allocate and fully initialize. */
364 for (i = 0; i < pipe_count; i++) {
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 for (i = 0; i < pipe_count; i++) {
378 if (pipes[i].present) {
379 rc = usb_pipe_register(&pipes[i].pipe,
380 pipes[i].descriptor->poll_interval);
381 if (rc != EOK) {
382 goto rollback_unregister_endpoints;
383 }
384 }
385 }
386
387 *pipes_ptr = pipes;
388 if (pipes_count_ptr != NULL) {
389 *pipes_count_ptr = pipe_count;
390 }
391
392 return EOK;
393
394 /*
395 * Jump here if something went wrong after endpoints have
396 * been registered.
397 * This is also the target when the registration of
398 * endpoints fails.
399 */
400rollback_unregister_endpoints:
401 for (i = 0; i < pipe_count; i++) {
402 if (pipes[i].present) {
403 usb_pipe_unregister(&pipes[i].pipe);
404 }
405 }
406
407 /*
408 * Jump here if something went wrong before some actual communication
409 * with HC. Then the only thing that needs to be done is to free
410 * allocated memory.
411 */
412rollback_free_only:
413 free(pipes);
414
415 return rc;
416}
417
418/** Destroy pipes previously created by usb_device_create_pipes.
419 *
420 * @param[in] dev Generic DDF device backing the USB one.
421 * @param[in] pipes Endpoint mapping to be destroyed.
422 * @param[in] pipes_count Number of endpoints.
423 */
424int usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
425{
426 if (pipes_count == 0) {
427 assert(pipes == NULL);
428 return EOK;
429 }
430 assert(pipes != NULL);
431
432 /* Destroy the pipes. */
433 for (size_t i = 0; i < pipes_count; ++i) {
434 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
435 i, pipes[i].present ? "" : "not ");
436 if (pipes[i].present)
437 usb_pipe_unregister(&pipes[i].pipe);
438 }
439
440 free(pipes);
441
442 return EOK;
443}
444
445/** Initialize new instance of USB device.
446 *
447 * @param[in] usb_dev Pointer to the new device.
448 * @param[in] ddf_dev Generic DDF device backing the USB one.
449 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
450 * @param[out] errstr_ptr Where to store description of context
451 * (in case error occurs).
452 * @return Error code.
453 */
454int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
455 const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
456{
457 assert(usb_dev != NULL);
458 assert(ddf_dev != NULL);
459
460 *errstr_ptr = NULL;
461
462 usb_dev->ddf_dev = ddf_dev;
463 usb_dev->driver_data = NULL;
464 usb_dev->descriptors.configuration = NULL;
465 usb_dev->pipes_count = 0;
466 usb_dev->pipes = NULL;
467
468 /* Get assigned params */
469 devman_handle_t hc_handle;
470 usb_address_t address;
471
472 int rc = usb_get_info_by_handle(ddf_dev->handle,
473 &hc_handle, &address, &usb_dev->interface_no);
474 if (rc != EOK) {
475 *errstr_ptr = "device parameters retrieval";
476 return rc;
477 }
478
479 /* Initialize hc connection. */
480 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
481
482 /* Initialize backing wire and control pipe. */
483 rc = usb_device_connection_initialize(
484 &usb_dev->wire, &usb_dev->hc_conn, address);
485 if (rc != EOK) {
486 *errstr_ptr = "device connection initialization";
487 return rc;
488 }
489
490 /* This pipe was registered by the hub driver,
491 * during device initialization. */
492 rc = usb_pipe_initialize_default_control(
493 &usb_dev->ctrl_pipe, &usb_dev->wire);
494 if (rc != EOK) {
495 *errstr_ptr = "default control pipe initialization";
496 return rc;
497 }
498
499 /* Open hc connection for pipe registration. */
500 rc = usb_hc_connection_open(&usb_dev->hc_conn);
501 if (rc != EOK) {
502 *errstr_ptr = "hc connection open";
503 return rc;
504 }
505
506 /* Retrieve standard descriptors. */
507 rc = usb_device_retrieve_descriptors(
508 &usb_dev->ctrl_pipe, &usb_dev->descriptors);
509 if (rc != EOK) {
510 *errstr_ptr = "descriptor retrieval";
511 usb_hc_connection_close(&usb_dev->hc_conn);
512 return rc;
513 }
514
515 /* Create alternate interfaces. We will silently ignore failure.
516 * We might either control one interface or an entire device,
517 * it makes no sense to speak about alternate interfaces when
518 * controlling a device. */
519 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
520 usb_dev->descriptors.configuration,
521 usb_dev->descriptors.configuration_size, usb_dev->interface_no);
522 const int alternate_iface =
523 (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
524
525 /* TODO Add comment here. */
526 rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface);
527 if (rc != EOK) {
528 usb_hc_connection_close(&usb_dev->hc_conn);
529 /* Full configuration descriptor is allocated. */
530 usb_device_release_descriptors(&usb_dev->descriptors);
531 /* Alternate interfaces may be allocated */
532 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
533 *errstr_ptr = "pipes initialization";
534 return rc;
535 }
536
537 usb_hc_connection_close(&usb_dev->hc_conn);
538 return EOK;
539}
540
541/** Clean instance of a USB device.
542 *
543 * @param dev Device to be de-initialized.
544 *
545 * Does not free/destroy supplied pointer.
546 */
547void usb_device_deinit(usb_device_t *dev)
548{
549 if (dev) {
550 /* Ignore errors and hope for the best. */
551 destroy_current_pipes(dev);
552
553 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
554 usb_device_release_descriptors(&dev->descriptors);
555 free(dev->driver_data);
556 }
557}
558
559/** Allocate driver specific data.
560 * @param usb_dev usb_device structure.
561 * @param size requested data size.
562 * @return Pointer to the newly allocated space, NULL on failure.
563 */
564void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
565{
566 assert(usb_dev);
567 assert(usb_dev->driver_data == NULL);
568 return usb_dev->driver_data = calloc(1, size);
569
570}
571
572/**
573 * @}
574 */
Note: See TracBrowser for help on using the repository browser.