source: mainline/uspace/lib/usbdev/src/devdrv.c@ 54cbda2

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

libusbdev: Add support for dev_remove.

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