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
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 /* Get place for driver data. */
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 }
111
112 /* Initialize generic USB driver data. */
113 const char *err_msg = NULL;
114 int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
115 if (rc != EOK) {
116 usb_log_error("USB device `%s' init failed (%s): %s.\n",
117 gen_dev->name, err_msg, str_error(rc));
118 return rc;
119 }
120
121 /* Start USB driver specific initialization. */
122 rc = driver->ops->device_add(dev);
123 if (rc != EOK)
124 usb_device_deinit(dev);
125 return rc;
126}
127/*----------------------------------------------------------------------------*/
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 */
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;
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);
144 if (ret != EOK)
145 return ret;
146 usb_device_deinit(usb_dev);
147 return EOK;
148}
149/*----------------------------------------------------------------------------*/
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 */
157int generic_device_gone(ddf_dev_t *gen_dev)
158{
159 assert(driver);
160 assert(driver->ops);
161 if (driver->ops->device_gone == NULL)
162 return ENOTSUP;
163 usb_device_t *usb_dev = gen_dev->driver_data;
164 const int ret = driver->ops->device_gone(usb_dev);
165 if (ret == EOK)
166 usb_device_deinit(usb_dev);
167
168 return ret;
169}
170/*----------------------------------------------------------------------------*/
171/** Destroy existing pipes of a USB device.
172 *
173 * @param dev Device where to destroy the pipes.
174 */
175static void destroy_current_pipes(usb_device_t *dev)
176{
177 usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
178 dev->pipes = NULL;
179 dev->pipes_count = 0;
180}
181/*----------------------------------------------------------------------------*/
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 *
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 *
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,
205 const usb_endpoint_description_t **endpoints)
206{
207 if (dev->interface_no < 0) {
208 return EINVAL;
209 }
210
211 /* Destroy existing pipes. */
212 destroy_current_pipes(dev);
213
214 /* Change the interface itself. */
215 int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
216 alternate_setting);
217 if (rc != EOK) {
218 return rc;
219 }
220
221 /* Create new pipes. */
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);
226
227 return rc;
228}
229
230/** Retrieve basic descriptors from the device.
231 *
232 * @param[in] ctrl_pipe Control endpoint pipe.
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
245 /* It is worth to start a long transfer. */
246 usb_pipe_start_long_transfer(ctrl_pipe);
247
248 /* Get the device descriptor. */
249 rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
250 if (rc != EOK) {
251 goto leave;
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
259leave:
260 usb_pipe_end_long_transfer(ctrl_pipe);
261
262 return rc;
263}
264
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
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
292 * (set to NULL if you wish to ignore the count).
293 * @return Error code.
294 */
295int usb_device_create_pipes(usb_device_connection_t *wire,
296 const usb_endpoint_description_t **endpoints,
297 const uint8_t *config_descr, size_t config_descr_size,
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
309 const size_t pipe_count = count_other_pipes(endpoints);
310 if (pipe_count == 0) {
311 if (pipes_count_ptr)
312 *pipes_count_ptr = pipe_count;
313 *pipes_ptr = NULL;
314 return EOK;
315 }
316
317 usb_endpoint_mapping_t *pipes
318 = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
319 if (pipes == NULL) {
320 return ENOMEM;
321 }
322
323 /* Now initialize. */
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) {
334 free(pipes);
335 return rc;
336 }
337
338 /* Register created pipes. */
339 for (i = 0; i < pipe_count; i++) {
340 if (pipes[i].present) {
341 rc = usb_pipe_register(&pipes[i].pipe,
342 pipes[i].descriptor->poll_interval);
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) {
365 usb_pipe_unregister(&pipes[i].pipe);
366 }
367 }
368
369 free(pipes);
370 return rc;
371}
372
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 */
378void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
379{
380 /* Destroy the pipes. */
381 for (size_t i = 0; i < pipes_count; ++i) {
382 assert(pipes);
383 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
384 i, pipes[i].present ? "" : "not ");
385 if (pipes[i].present)
386 usb_pipe_unregister(&pipes[i].pipe);
387 }
388 free(pipes);
389}
390
391/** Initialize new instance of USB device.
392 *
393 * @param[in] usb_dev Pointer to the new device.
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 */
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)
402{
403 assert(usb_dev != NULL);
404 assert(ddf_dev != NULL);
405
406 *errstr_ptr = NULL;
407
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;
413
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
425 /* Initialize hc connection. */
426 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
427
428 /* Initialize backing wire and control pipe. */
429 rc = usb_device_connection_initialize(
430 &usb_dev->wire, &usb_dev->hc_conn, address);
431 if (rc != EOK) {
432 *errstr_ptr = "device connection initialization";
433 return rc;
434 }
435
436 /* This pipe was registered by the hub driver,
437 * during device initialization. */
438 rc = usb_pipe_initialize_default_control(
439 &usb_dev->ctrl_pipe, &usb_dev->wire);
440 if (rc != EOK) {
441 *errstr_ptr = "default control pipe initialization";
442 return rc;
443 }
444
445 /* Open hc connection for pipe registration. */
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
452 /* Retrieve standard descriptors. */
453 rc = usb_device_retrieve_descriptors(
454 &usb_dev->ctrl_pipe, &usb_dev->descriptors);
455 if (rc != EOK) {
456 *errstr_ptr = "descriptor retrieval";
457 usb_hc_connection_close(&usb_dev->hc_conn);
458 return rc;
459 }
460
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. */
465 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
466 usb_dev->descriptors.configuration,
467 usb_dev->descriptors.configuration_size, usb_dev->interface_no);
468 const int alternate_iface =
469 (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
470
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);
477 if (rc != EOK) {
478 usb_hc_connection_close(&usb_dev->hc_conn);
479 /* Full configuration descriptor is allocated. */
480 usb_device_release_descriptors(&usb_dev->descriptors);
481 /* Alternate interfaces may be allocated */
482 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
483 *errstr_ptr = "pipes initialization";
484 return rc;
485 }
486
487 usb_hc_connection_close(&usb_dev->hc_conn);
488 return EOK;
489}
490
491/** Clean instance of a USB device.
492 *
493 * @param dev Device to be de-initialized.
494 *
495 * Does not free/destroy supplied pointer.
496 */
497void usb_device_deinit(usb_device_t *dev)
498{
499 if (dev) {
500 /* Destroy existing pipes. */
501 destroy_current_pipes(dev);
502 /* Ignore errors and hope for the best. */
503 usb_hc_connection_deinitialize(&dev->hc_conn);
504 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
505 usb_device_release_descriptors(&dev->descriptors);
506 free(dev->driver_data);
507 dev->driver_data = NULL;
508 }
509}
510
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 */
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);
521
522}
523
524/**
525 * @}
526 */
Note: See TracBrowser for help on using the repository browser.