source: mainline/uspace/lib/usb/src/devdrv.c@ 26d46d2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 26d46d2 was 3f2af64, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

libusb: refactoring

  • Property mode set to 100644
File size: 13.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 libusb
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35#include <usb/devdrv.h>
36#include <usb/request.h>
37#include <usb/debug.h>
38#include <usb/dp.h>
39#include <errno.h>
40#include <str_error.h>
41#include <assert.h>
42
43static int generic_add_device(ddf_dev_t *);
44
45static driver_ops_t generic_driver_ops = {
46 .add_device = generic_add_device
47};
48static driver_t generic_driver = {
49 .driver_ops = &generic_driver_ops
50};
51
52static usb_driver_t *driver = NULL;
53
54
55/** Main routine of USB device driver.
56 *
57 * Under normal conditions, this function never returns.
58 *
59 * @param drv USB device driver structure.
60 * @return Task exit status.
61 */
62int usb_driver_main(usb_driver_t *drv)
63{
64 assert(drv != NULL);
65
66 /* Prepare the generic driver. */
67 generic_driver.name = drv->name;
68
69 driver = drv;
70
71 return ddf_driver_main(&generic_driver);
72}
73
74/** Count number of pipes the driver expects.
75 *
76 * @param drv USB driver.
77 * @return Number of pipes (excluding default control pipe).
78 */
79static size_t count_other_pipes(usb_endpoint_description_t **endpoints)
80{
81 size_t count = 0;
82 if (endpoints == NULL) {
83 return 0;
84 }
85
86 while (endpoints[count] != NULL) {
87 count++;
88 }
89
90 return count;
91}
92
93/** Initialize endpoint pipes, excluding default control one.
94 *
95 * @param drv The device driver.
96 * @param dev Device to be initialized.
97 * @return Error code.
98 */
99static int initialize_other_pipes(usb_endpoint_description_t **endpoints,
100 usb_device_t *dev, int alternate_setting)
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,
114 &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 new device is supposed to be controlled by this driver.
127 *
128 * This callback is a wrapper for USB specific version of @c add_device.
129 *
130 * @param gen_dev Device structure as prepared by DDF.
131 * @return Error code.
132 */
133int generic_add_device(ddf_dev_t *gen_dev)
134{
135 assert(driver);
136 assert(driver->ops);
137 assert(driver->ops->add_device);
138
139 int rc;
140
141 usb_device_t *dev = NULL;
142 const char *err_msg = NULL;
143 rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
144 if (rc != EOK) {
145 usb_log_error("USB device `%s' creation failed (%s): %s.\n",
146 gen_dev->name, err_msg, str_error(rc));
147 return rc;
148 }
149
150 return driver->ops->add_device(dev);
151}
152
153/** Destroy existing pipes of a USB device.
154 *
155 * @param dev Device where to destroy the pipes.
156 * @return Error code.
157 */
158static int destroy_current_pipes(usb_device_t *dev)
159{
160 int rc = usb_device_destroy_pipes(dev->ddf_dev,
161 dev->pipes, dev->pipes_count);
162 if (rc != EOK) {
163 return rc;
164 }
165
166 dev->pipes = NULL;
167 dev->pipes_count = 0;
168
169 return EOK;
170}
171
172/** Change interface setting of a device.
173 * This function selects new alternate setting of an interface by issuing
174 * proper USB command to the device and also creates new USB pipes
175 * under @c dev->pipes.
176 *
177 * @warning This function is intended for drivers working at interface level.
178 * For drivers controlling the whole device, you need to change interface
179 * manually using usb_request_set_interface() and creating new pipes
180 * with usb_pipe_initialize_from_configuration().
181 *
182 * @warning This is a wrapper function that does several operations that
183 * can fail and that cannot be rollbacked easily. That means that a failure
184 * during the SET_INTERFACE request would result in having a device with
185 * no pipes at all (except the default control one). That is because the old
186 * pipes needs to be unregistered at HC first and the new ones could not
187 * be created.
188 *
189 * @param dev USB device.
190 * @param alternate_setting Alternate setting to choose.
191 * @param endpoints New endpoint descriptions.
192 * @return Error code.
193 */
194int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
195 usb_endpoint_description_t **endpoints)
196{
197 if (dev->interface_no < 0) {
198 return EINVAL;
199 }
200
201 int rc;
202
203 /* Destroy existing pipes. */
204 rc = destroy_current_pipes(dev);
205 if (rc != EOK) {
206 return rc;
207 }
208
209 /* Change the interface itself. */
210 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
211 alternate_setting);
212 if (rc != EOK) {
213 return rc;
214 }
215
216 /* Create new pipes. */
217 rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
218
219 return rc;
220}
221
222/** Retrieve basic descriptors from the device.
223 *
224 * @param[in] ctrl_pipe Control endpoint pipe.
225 * @param[out] descriptors Where to store the descriptors.
226 * @return Error code.
227 */
228int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
229 usb_device_descriptors_t *descriptors)
230{
231 assert(descriptors != NULL);
232
233 descriptors->configuration = NULL;
234
235 int rc;
236
237 /* It is worth to start a long transfer. */
238 rc = usb_pipe_start_long_transfer(ctrl_pipe);
239 if (rc != EOK) {
240 return rc;
241 }
242
243 /* Get the device descriptor. */
244 rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
245 if (rc != EOK) {
246 goto leave;
247 }
248
249 /* Get the full configuration descriptor. */
250 rc = usb_request_get_full_configuration_descriptor_alloc(
251 ctrl_pipe, 0, (void **) &descriptors->configuration,
252 &descriptors->configuration_size);
253
254leave:
255 usb_pipe_end_long_transfer(ctrl_pipe);
256
257 return rc;
258}
259
260/** Create pipes for a device.
261 *
262 * This is more or less a wrapper that does following actions:
263 * - allocate and initialize pipes
264 * - map endpoints to the pipes based on the descriptions
265 * - registers endpoints with the host controller
266 *
267 * @param[in] dev Generic DDF device backing the USB one.
268 * @param[in] wire Initialized backing connection to the host controller.
269 * @param[in] endpoints Endpoints description, NULL terminated.
270 * @param[in] config_descr Configuration descriptor of active configuration.
271 * @param[in] config_descr_size Size of @p config_descr in bytes.
272 * @param[in] interface_no Interface to map from.
273 * @param[in] interface_setting Interface setting (default is usually 0).
274 * @param[out] pipes_ptr Where to store array of created pipes
275 * (not NULL terminated).
276 * @param[out] pipes_count_ptr Where to store number of pipes
277 * (set to if you wish to ignore the count).
278 * @return Error code.
279 */
280int usb_device_create_pipes(ddf_dev_t *dev, usb_device_connection_t *wire,
281 usb_endpoint_description_t **endpoints,
282 uint8_t *config_descr, size_t config_descr_size,
283 int interface_no, int interface_setting,
284 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
285{
286 assert(dev != NULL);
287 assert(wire != NULL);
288 assert(endpoints != NULL);
289 assert(config_descr != NULL);
290 assert(config_descr_size > 0);
291 assert(pipes_ptr != NULL);
292
293 size_t i;
294 int rc;
295
296 size_t pipe_count = count_other_pipes(endpoints);
297 if (pipe_count == 0) {
298 *pipes_ptr = NULL;
299 return EOK;
300 }
301
302 usb_endpoint_mapping_t *pipes
303 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
304 if (pipes == NULL) {
305 return ENOMEM;
306 }
307
308 /* Initialize to NULL to allow smooth rollback. */
309 for (i = 0; i < pipe_count; i++) {
310 pipes[i].pipe = NULL;
311 }
312
313 /* Now allocate and fully initialize. */
314 for (i = 0; i < pipe_count; i++) {
315 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
316 if (pipes[i].pipe == NULL) {
317 rc = ENOMEM;
318 goto rollback_free_only;
319 }
320 pipes[i].description = endpoints[i];
321 pipes[i].interface_no = interface_no;
322 pipes[i].interface_setting = interface_setting;
323 }
324
325 /* Find the mapping from configuration descriptor. */
326 rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
327 config_descr, config_descr_size, wire);
328 if (rc != EOK) {
329 goto rollback_free_only;
330 }
331
332 /* Register the endpoints with HC. */
333 usb_hc_connection_t hc_conn;
334 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
335 if (rc != EOK) {
336 goto rollback_free_only;
337 }
338
339 rc = usb_hc_connection_open(&hc_conn);
340 if (rc != EOK) {
341 goto rollback_free_only;
342 }
343
344 for (i = 0; i < pipe_count; i++) {
345 if (pipes[i].present) {
346 rc = usb_pipe_register(pipes[i].pipe,
347 pipes[i].descriptor->poll_interval, &hc_conn);
348 if (rc != EOK) {
349 goto rollback_unregister_endpoints;
350 }
351 }
352 }
353
354 usb_hc_connection_close(&hc_conn);
355
356 *pipes_ptr = pipes;
357 if (pipes_count_ptr != NULL) {
358 *pipes_count_ptr = pipe_count;
359 }
360
361 return EOK;
362
363 /*
364 * Jump here if something went wrong after endpoints have
365 * been registered.
366 * This is also the target when the registration of
367 * endpoints fails.
368 */
369rollback_unregister_endpoints:
370 for (i = 0; i < pipe_count; i++) {
371 if (pipes[i].present) {
372 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
373 }
374 }
375
376 usb_hc_connection_close(&hc_conn);
377
378 /*
379 * Jump here if something went wrong before some actual communication
380 * with HC. Then the only thing that needs to be done is to free
381 * allocated memory.
382 */
383rollback_free_only:
384 for (i = 0; i < pipe_count; i++) {
385 if (pipes[i].pipe != NULL) {
386 free(pipes[i].pipe);
387 }
388 }
389 free(pipes);
390
391 return rc;
392}
393
394/** Destroy pipes previously created by usb_device_create_pipes.
395 *
396 * @param[in] dev Generic DDF device backing the USB one.
397 * @param[in] pipes Endpoint mapping to be destroyed.
398 * @param[in] pipes_count Number of endpoints.
399 */
400int usb_device_destroy_pipes(ddf_dev_t *dev,
401 usb_endpoint_mapping_t *pipes, size_t pipes_count)
402{
403 assert(dev != NULL);
404 assert(((pipes != NULL) && (pipes_count > 0))
405 || ((pipes == NULL) && (pipes_count == 0)));
406
407 if (pipes_count == 0) {
408 return EOK;
409 }
410
411 int rc;
412
413 /* Prepare connection to HC to allow endpoint unregistering. */
414 usb_hc_connection_t hc_conn;
415 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
416 if (rc != EOK) {
417 return rc;
418 }
419 rc = usb_hc_connection_open(&hc_conn);
420 if (rc != EOK) {
421 return rc;
422 }
423
424 /* Destroy the pipes. */
425 size_t i;
426 for (i = 0; i < pipes_count; i++) {
427 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
428 free(pipes[i].pipe);
429 }
430
431 usb_hc_connection_close(&hc_conn);
432
433 free(pipes);
434
435 return EOK;
436}
437
438/** Initialize control pipe in a device.
439 *
440 * @param dev USB device in question.
441 * @param errmsg Where to store error context.
442 * @return
443 */
444static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
445{
446 int rc;
447
448 rc = usb_device_connection_initialize_from_device(&dev->wire,
449 dev->ddf_dev);
450 if (rc != EOK) {
451 *errmsg = "device connection initialization";
452 return rc;
453 }
454
455 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
456 &dev->wire);
457 if (rc != EOK) {
458 *errmsg = "default control pipe initialization";
459 return rc;
460 }
461
462 return EOK;
463}
464
465
466/** Create new instance of USB device.
467 *
468 * @param[in] ddf_dev Generic DDF device backing the USB one.
469 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
470 * @param[out] dev_ptr Where to store pointer to the new device.
471 * @param[out] errstr_ptr Where to store description of context
472 * (in case error occurs).
473 * @return Error code.
474 */
475int usb_device_create(ddf_dev_t *ddf_dev,
476 usb_endpoint_description_t **endpoints,
477 usb_device_t **dev_ptr, const char **errstr_ptr)
478{
479 assert(dev_ptr != NULL);
480 assert(ddf_dev != NULL);
481
482 int rc;
483
484 usb_device_t *dev = malloc(sizeof(usb_device_t));
485 if (dev == NULL) {
486 *errstr_ptr = "structure allocation";
487 return ENOMEM;
488 }
489
490 // FIXME: proper deallocation in case of errors
491
492 dev->ddf_dev = ddf_dev;
493 dev->driver_data = NULL;
494 dev->descriptors.configuration = NULL;
495 dev->alternate_interfaces = NULL;
496
497 dev->pipes_count = 0;
498 dev->pipes = NULL;
499
500 /* Initialize backing wire and control pipe. */
501 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
502 if (rc != EOK) {
503 return rc;
504 }
505
506 /* Get our interface. */
507 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
508
509 /* Retrieve standard descriptors. */
510 rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
511 &dev->descriptors);
512 if (rc != EOK) {
513 *errstr_ptr = "descriptor retrieval";
514 return rc;
515 }
516
517 /* Create alternate interfaces. */
518 rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
519 dev->descriptors.configuration_size, dev->interface_no,
520 &dev->alternate_interfaces);
521 if (rc != EOK) {
522 /* We will try to silently ignore this. */
523 dev->alternate_interfaces = NULL;
524 }
525
526 rc = initialize_other_pipes(endpoints, dev, 0);
527 if (rc != EOK) {
528 *errstr_ptr = "pipes initialization";
529 return rc;
530 }
531
532 *errstr_ptr = NULL;
533 *dev_ptr = dev;
534
535 return EOK;
536}
537
538/**
539 * @}
540 */
Note: See TracBrowser for help on using the repository browser.