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

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

libusbdev: Cleanup.

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