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

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

libusbdev: Cleanup unused code.

  • Property mode set to 100644
File size: 15.5 KB
RevLine 
[6105fc0]1/*
2 * Copyright (c) 2011 Vojtech Horky
[4291215]3 * Copyright (c) 2011 Jan Vesely
[6105fc0]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
[160b75e]29/** @addtogroup libusbdev
[6105fc0]30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
[87619045]35
36#include <usb_iface.h>
37#include <usb/dev/device.h>
[7d521e24]38#include <usb/dev/request.h>
[6105fc0]39#include <usb/debug.h>
[1a38701]40#include <usb/dev.h>
[6105fc0]41#include <errno.h>
[69334af]42#include <str_error.h>
[6105fc0]43#include <assert.h>
44
[fd9b3a67]45/** USB device structure. */
46typedef struct usb_device {
[d93f5afb]47 /** Connection to device on USB bus */
48 usb_dev_session_t *bus_session;
49 /** devman handle */
50 devman_handle_t handle;
[fd9b3a67]51 /** The default control pipe. */
52 usb_pipe_t ctrl_pipe;
53
54 /** Other endpoint pipes.
55 * This is an array of other endpoint pipes in the same order as
56 * in usb_driver_t.
57 */
58 usb_endpoint_mapping_t *pipes;
59 /** Number of other endpoint pipes. */
60 size_t pipes_count;
61 /** Current interface.
62 * Usually, drivers operate on single interface only.
63 * This item contains the value of the interface or -1 for any.
64 */
65 int interface_no;
66 /** Alternative interfaces. */
67 usb_alternate_interfaces_t alternate_interfaces;
68 /** Some useful descriptors for USB device. */
[e2dfa86]69 usb_device_descriptors_t descriptors;
[fd9b3a67]70 /** Generic DDF device backing this one. DO NOT TOUCH! */
71 ddf_dev_t *ddf_dev;
72 /** Custom driver data.
73 * Do not use the entry in generic device, that is already used
74 * by the framework.
75 */
76 void *driver_data;
77
78} usb_device_t;
79
[69334af]80/** Count number of pipes the driver expects.
81 *
82 * @param drv USB driver.
83 * @return Number of pipes (excluding default control pipe).
84 */
[b06d35a]85static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
[6105fc0]86{
[ab27e01]87 size_t count;
[64e3dad]88 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
[6105fc0]89 return count;
90}
[a76b01b4]91
[0b4e7ca]92/** Change interface setting of a device.
93 * This function selects new alternate setting of an interface by issuing
94 * proper USB command to the device and also creates new USB pipes
95 * under @c dev->pipes.
96 *
97 * @warning This function is intended for drivers working at interface level.
98 * For drivers controlling the whole device, you need to change interface
99 * manually using usb_request_set_interface() and creating new pipes
100 * with usb_pipe_initialize_from_configuration().
101 *
[3f2af64]102 * @warning This is a wrapper function that does several operations that
103 * can fail and that cannot be rollbacked easily. That means that a failure
104 * during the SET_INTERFACE request would result in having a device with
105 * no pipes at all (except the default control one). That is because the old
106 * pipes needs to be unregistered at HC first and the new ones could not
107 * be created.
108 *
[0b4e7ca]109 * @param dev USB device.
110 * @param alternate_setting Alternate setting to choose.
111 * @param endpoints New endpoint descriptions.
112 * @return Error code.
113 */
[f4138ac]114int usb_device_select_interface(usb_device_t *usb_dev,
115 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
[0b4e7ca]116{
[f4138ac]117 assert(usb_dev);
118
119 if (usb_dev->interface_no < 0) {
[0b4e7ca]120 return EINVAL;
121 }
122
123 /* Change the interface itself. */
[f4138ac]124 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
125 usb_dev->interface_no, alternate_setting);
[0b4e7ca]126 if (rc != EOK) {
127 return rc;
128 }
129
[f4138ac]130 /* Change current alternative */
131 usb_dev->alternate_interfaces.current = alternate_setting;
132
133 /* Destroy existing pipes. */
134 usb_device_destroy_pipes(usb_dev);
135
[0b4e7ca]136 /* Create new pipes. */
[b06d35a]137 rc = usb_device_create_pipes(usb_dev, endpoints);
[c1b1944]138
139 return rc;
140}
141
142/** Retrieve basic descriptors from the device.
143 *
[4fa0a384]144 * @param[in] ctrl_pipe Control endpoint pipe.
[c1b1944]145 * @param[out] descriptors Where to store the descriptors.
146 * @return Error code.
147 */
[945d66c]148static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
[c1b1944]149{
[945d66c]150 assert(usb_dev);
[e2dfa86]151 assert(usb_dev->descriptors.full_config == NULL);
[c1b1944]152
153 /* Get the device descriptor. */
[945d66c]154 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
155 &usb_dev->descriptors.device);
[c1b1944]156 if (rc != EOK) {
[d93f5afb]157 return rc;
[c1b1944]158 }
159
160 /* Get the full configuration descriptor. */
161 rc = usb_request_get_full_configuration_descriptor_alloc(
[945d66c]162 &usb_dev->ctrl_pipe, 0,
[0eb2a0f]163 &usb_dev->descriptors.full_config,
[e2dfa86]164 &usb_dev->descriptors.full_config_size);
[c1b1944]165
[4fa0a384]166
167 return rc;
[c1b1944]168}
169
[7fc260ff]170/** Cleanup structure initialized via usb_device_retrieve_descriptors.
171 *
172 * @param[in] descriptors Where to store the descriptors.
173 */
[945d66c]174static void usb_device_release_descriptors(usb_device_t *usb_dev)
[7fc260ff]175{
[945d66c]176 assert(usb_dev);
[e2dfa86]177 free(usb_dev->descriptors.full_config);
178 usb_dev->descriptors.full_config = NULL;
179 usb_dev->descriptors.full_config_size = 0;
[7fc260ff]180}
181
[c1b1944]182/** Create pipes for a device.
183 *
184 * This is more or less a wrapper that does following actions:
185 * - allocate and initialize pipes
186 * - map endpoints to the pipes based on the descriptions
187 * - registers endpoints with the host controller
188 *
189 * @param[in] endpoints Endpoints description, NULL terminated.
190 * @param[in] config_descr Configuration descriptor of active configuration.
191 * @param[in] config_descr_size Size of @p config_descr in bytes.
192 * @param[in] interface_no Interface to map from.
193 * @param[in] interface_setting Interface setting (default is usually 0).
194 * @param[out] pipes_ptr Where to store array of created pipes
195 * (not NULL terminated).
196 * @param[out] pipes_count_ptr Where to store number of pipes
[ab27e01]197 * (set to NULL if you wish to ignore the count).
[c1b1944]198 * @return Error code.
199 */
[b06d35a]200int usb_device_create_pipes(usb_device_t *usb_dev,
201 const usb_endpoint_description_t **endpoints)
[c1b1944]202{
[b06d35a]203 assert(usb_dev);
[e2dfa86]204 assert(usb_dev->descriptors.full_config);
[b06d35a]205 assert(usb_dev->pipes == NULL);
206 assert(usb_dev->pipes_count == 0);
[c1b1944]207
[b06d35a]208 size_t pipe_count = count_pipes(endpoints);
[c1b1944]209 if (pipe_count == 0) {
210 return EOK;
211 }
212
[b06d35a]213 usb_endpoint_mapping_t *pipes =
214 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
[c1b1944]215 if (pipes == NULL) {
216 return ENOMEM;
217 }
218
[441633f]219 /* Now initialize. */
[b06d35a]220 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]221 pipes[i].description = endpoints[i];
[b06d35a]222 pipes[i].interface_no = usb_dev->interface_no;
223 pipes[i].interface_setting =
224 usb_dev->alternate_interfaces.current;
[c1b1944]225 }
226
227 /* Find the mapping from configuration descriptor. */
[b06d35a]228 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
[e2dfa86]229 usb_dev->descriptors.full_config,
[d93f5afb]230 usb_dev->descriptors.full_config_size,
[8582076]231 usb_dev->bus_session);
[c1b1944]232 if (rc != EOK) {
[441633f]233 free(pipes);
234 return rc;
[c1b1944]235 }
236
[441633f]237 /* Register created pipes. */
[b06d35a]238 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]239 if (pipes[i].present) {
[b77931d]240 rc = usb_pipe_register(&pipes[i].pipe,
[bd575647]241 pipes[i].descriptor->poll_interval);
[c1b1944]242 if (rc != EOK) {
243 goto rollback_unregister_endpoints;
244 }
245 }
246 }
247
[b06d35a]248 usb_dev->pipes = pipes;
249 usb_dev->pipes_count = pipe_count;
[c1b1944]250
251 return EOK;
252
253 /*
254 * Jump here if something went wrong after endpoints have
255 * been registered.
256 * This is also the target when the registration of
257 * endpoints fails.
258 */
259rollback_unregister_endpoints:
[b06d35a]260 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]261 if (pipes[i].present) {
[bd575647]262 usb_pipe_unregister(&pipes[i].pipe);
[c1b1944]263 }
264 }
265
266 free(pipes);
[0b4e7ca]267 return rc;
268}
[159b91f4]269
[c1b1944]270/** Destroy pipes previously created by usb_device_create_pipes.
271 *
[2dc5a9f]272 * @param[in] usb_dev USB device.
[c1b1944]273 */
[2dc5a9f]274void usb_device_destroy_pipes(usb_device_t *usb_dev)
[c1b1944]275{
[2dc5a9f]276 assert(usb_dev);
277 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
[c1b1944]278 /* Destroy the pipes. */
[2dc5a9f]279 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
[8a01a0b]280 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
[2dc5a9f]281 i, usb_dev->pipes[i].present ? "" : "not ");
282 if (usb_dev->pipes[i].present)
283 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
[c1b1944]284 }
[2dc5a9f]285 free(usb_dev->pipes);
286 usb_dev->pipes = NULL;
287 usb_dev->pipes_count = 0;
[c1b1944]288}
289
[0f4bff8]290usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
291{
292 assert(usb_dev);
293 return &usb_dev->ctrl_pipe;
294}
295
[a6a5b25]296usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
297 const usb_endpoint_description_t *desc)
298{
299 assert(usb_dev);
300 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
301 if (usb_dev->pipes[i].description == desc)
302 return &usb_dev->pipes[i];
303 }
304 return NULL;
305}
306
307usb_endpoint_mapping_t * usb_device_get_mapped_ep(
308 usb_device_t *usb_dev, usb_endpoint_t ep)
309{
310 assert(usb_dev);
311 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
312 if (usb_dev->pipes[i].pipe.endpoint_no == ep)
313 return &usb_dev->pipes[i];
314 }
315 return NULL;
316}
317
[8e10ef4]318int usb_device_get_iface_number(usb_device_t *usb_dev)
319{
320 assert(usb_dev);
321 return usb_dev->interface_no;
322}
323
[8e4219ab]324devman_handle_t usb_device_get_devman_handle(usb_device_t *usb_dev)
325{
326 assert(usb_dev);
327 return usb_dev->handle;
328}
329
[e2dfa86]330const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
[945d66c]331{
332 assert(usb_dev);
[e2dfa86]333 return &usb_dev->descriptors;
[945d66c]334}
335
[8e10ef4]336const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
337 usb_device_t *usb_dev)
338{
339 assert(usb_dev);
340 return &usb_dev->alternate_interfaces;
341}
342
[7363fc1]343/** Clean instance of a USB device.
344 *
345 * @param dev Device to be de-initialized.
346 *
347 * Does not free/destroy supplied pointer.
348 */
349static void usb_device_fini(usb_device_t *usb_dev)
350{
351 if (usb_dev) {
352 /* Destroy existing pipes. */
353 usb_device_destroy_pipes(usb_dev);
354 /* Ignore errors and hope for the best. */
355 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
356 usb_device_release_descriptors(usb_dev);
357 free(usb_dev->driver_data);
358 usb_dev->driver_data = NULL;
[4c86c7c]359 usb_dev_disconnect(usb_dev->bus_session);
360 usb_dev->bus_session = NULL;
[7363fc1]361 }
362}
363
[7d9cd62]364/** Initialize new instance of USB device.
[6ee6e6f]365 *
[7d9cd62]366 * @param[in] usb_dev Pointer to the new device.
[6ee6e6f]367 * @param[in] ddf_dev Generic DDF device backing the USB one.
368 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
369 * @param[out] errstr_ptr Where to store description of context
370 * (in case error occurs).
371 * @return Error code.
372 */
[fd9b3a67]373static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
[7363fc1]374 const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
[8e4219ab]375 devman_handle_t handle, int interface_no)
[6ee6e6f]376{
[7d9cd62]377 assert(usb_dev != NULL);
[7363fc1]378 assert(errstr_ptr);
[6ee6e6f]379
[7fc260ff]380 *errstr_ptr = NULL;
381
[7d9cd62]382 usb_dev->ddf_dev = ddf_dev;
[8e4219ab]383 usb_dev->handle = handle;
384 usb_dev->interface_no = interface_no;
[7d9cd62]385 usb_dev->driver_data = NULL;
[e2dfa86]386 usb_dev->descriptors.full_config = NULL;
387 usb_dev->descriptors.full_config_size = 0;
[7d9cd62]388 usb_dev->pipes_count = 0;
389 usb_dev->pipes = NULL;
[6ee6e6f]390
[8e4219ab]391 usb_dev->bus_session = usb_dev_connect(handle);
[7363fc1]392
[c8c758d]393 if (!usb_dev->bus_session) {
394 *errstr_ptr = "device bus session create";
395 return ENOMEM;
396 }
397
[7fc260ff]398 /* This pipe was registered by the hub driver,
399 * during device initialization. */
[d93f5afb]400 int rc = usb_pipe_initialize_default_control(
401 &usb_dev->ctrl_pipe, usb_dev->bus_session);
[7fc260ff]402 if (rc != EOK) {
[7363fc1]403 usb_dev_disconnect(usb_dev->bus_session);
[7fc260ff]404 *errstr_ptr = "default control pipe initialization";
[3f2af64]405 return rc;
406 }
407
408 /* Retrieve standard descriptors. */
[945d66c]409 rc = usb_device_retrieve_descriptors(usb_dev);
[6ee6e6f]410 if (rc != EOK) {
[3f2af64]411 *errstr_ptr = "descriptor retrieval";
[7363fc1]412 usb_dev_disconnect(usb_dev->bus_session);
[6ee6e6f]413 return rc;
414 }
415
[7fc260ff]416 /* Create alternate interfaces. We will silently ignore failure.
417 * We might either control one interface or an entire device,
418 * it makes no sense to speak about alternate interfaces when
419 * controlling a device. */
[ab27e01]420 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
[e2dfa86]421 usb_dev->descriptors.full_config,
422 usb_dev->descriptors.full_config_size, usb_dev->interface_no);
[6ee6e6f]423
[7363fc1]424 if (endpoints) {
425 /* Create and register other pipes than default control (EP 0)*/
426 rc = usb_device_create_pipes(usb_dev, endpoints);
427 if (rc != EOK) {
428 usb_device_fini(usb_dev);
429 *errstr_ptr = "pipes initialization";
430 return rc;
431 }
[6ee6e6f]432 }
433
434 return EOK;
435}
436
[8e4219ab]437static int usb_device_get_info(async_sess_t *sess, devman_handle_t *handle,
438 int *iface_no)
439{
440 assert(handle);
441 assert(iface_no);
442 async_exch_t *exch = async_exchange_begin(sess);
443 if (!exch)
444 return EPARTY;
445 int ret = usb_get_device_handle(exch, handle);
446 if (ret == EOK) {
447 ret = usb_get_my_interface(exch, iface_no);
448 if (ret == ENOTSUP) {
449 *iface_no = -1;
450 ret = EOK;
451 }
452 }
453 async_exchange_end(exch);
454 return ret;
455}
456
[fd9b3a67]457int usb_device_create_ddf(ddf_dev_t *ddf_dev,
458 const usb_endpoint_description_t **desc, const char **err)
459{
460 assert(ddf_dev);
461 assert(err);
[8e4219ab]462
463 devman_handle_t h = 0;
464 int iface_no = -1;
465
466 async_sess_t *sess = devman_parent_device_connect(EXCHANGE_ATOMIC,
467 ddf_dev_get_handle(ddf_dev), IPC_FLAG_BLOCKING);
468 const int ret = usb_device_get_info(sess, &h, &iface_no);
469 async_hangup(sess);
470 if (ret != EOK)
471 return ret;
472
[7363fc1]473 usb_device_t *usb_dev =
474 ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
475 if (usb_dev == NULL) {
[fd9b3a67]476 *err = "DDF data alloc";
477 return ENOMEM;
478 }
[8e4219ab]479
480 return usb_device_init(usb_dev, ddf_dev, desc, err, h, iface_no);
[fd9b3a67]481}
482
483void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
484{
485 assert(ddf_dev);
[7363fc1]486 usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
487 assert(usb_dev);
488 usb_device_fini(usb_dev);
[fd9b3a67]489 return;
490}
491
[7363fc1]492usb_device_t * usb_device_create(devman_handle_t handle)
493{
[8e4219ab]494 devman_handle_t h = 0;
495 int iface_no = -1;
496
497 async_sess_t *sess = devman_device_connect(
498 EXCHANGE_ATOMIC, handle, IPC_FLAG_BLOCKING);
499 int ret = usb_device_get_info(sess, &h, &iface_no);
500 async_hangup(sess);
501 if (ret != EOK)
502 return NULL;
503
[7363fc1]504 usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
505 if (!usb_dev)
506 return NULL;
[8e4219ab]507
[7363fc1]508 const char* dummy = NULL;
[8e4219ab]509 ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle, iface_no);
[7363fc1]510 if (ret != EOK) {
511 free(usb_dev);
512 usb_dev = NULL;
513 }
514 return usb_dev;
515}
516
517void usb_device_destroy(usb_device_t *usb_dev)
518{
519 if (usb_dev) {
520 usb_device_fini(usb_dev);
521 free(usb_dev);
522 }
523}
524
[f6b2a76b]525const char *usb_device_get_name(usb_device_t *usb_dev)
526{
527 assert(usb_dev);
[35bc430]528 if (usb_dev->ddf_dev)
529 return ddf_dev_get_name(usb_dev->ddf_dev);
530 return NULL;
[f6b2a76b]531}
532
[bb512b2d]533ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
534 const char* name)
535{
536 assert(usb_dev);
537 if (usb_dev->ddf_dev)
538 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
539 return NULL;
540}
541
[0f4bff8]542async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
543{
544 assert(usb_dev);
545 return async_exchange_begin(usb_dev->bus_session);
546}
547
548void usb_device_bus_exchange_end(async_exch_t *exch)
549{
550 async_exchange_end(exch);
551}
552
[6e3c005]553/** Allocate driver specific data.
554 * @param usb_dev usb_device structure.
555 * @param size requested data size.
556 * @return Pointer to the newly allocated space, NULL on failure.
557 */
[065064e6]558void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
559{
560 assert(usb_dev);
561 assert(usb_dev->driver_data == NULL);
562 return usb_dev->driver_data = calloc(1, size);
[70452dd4]563
564}
565
[0f4bff8]566void * usb_device_data_get(usb_device_t *usb_dev)
567{
568 assert(usb_dev);
569 return usb_dev->driver_data;
570}
[4ca778b]571
[6105fc0]572/**
573 * @}
574 */
Note: See TracBrowser for help on using the repository browser.