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

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

libusbdev: No need for special handling of NULL corner case.

Counting works on NULL endpoints pointer.

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