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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e657635 was 086f8e3, checked in by Jakub Jermar <jakub@…>, 9 years ago

Test result of devman_parent_device_connect()

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