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

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

libusbdev: Remove wrapper.

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