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

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

usbmid: Use initialized parent to get address and hc handle.

Remove forwarding helpers from libusb.
Remove unused headers.

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jan Vesely
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 */
29/** @addtogroup libusbdev
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35
36#include <usb_iface.h>
37#include <usb/dev/device.h>
38#include <usb/dev/request.h>
39#include <usb/debug.h>
40#include <usb/dev.h>
41#include <errno.h>
42#include <str_error.h>
43#include <assert.h>
44
45/** USB device structure. */
46typedef struct usb_device {
47 /** Connection to USB hc, used by wire and arbitrary requests. */
48 usb_hc_connection_t hc_conn;
49 /** Connection backing the pipes.
50 * Typically, you will not need to use this attribute at all.
51 */
52 usb_device_connection_t wire;
53 /** The default control pipe. */
54 usb_pipe_t ctrl_pipe;
55
56 /** Other endpoint pipes.
57 * This is an array of other endpoint pipes in the same order as
58 * in usb_driver_t.
59 */
60 usb_endpoint_mapping_t *pipes;
61 /** Number of other endpoint pipes. */
62 size_t pipes_count;
63 /** Current interface.
64 * Usually, drivers operate on single interface only.
65 * This item contains the value of the interface or -1 for any.
66 */
67 int interface_no;
68 /** Alternative interfaces. */
69 usb_alternate_interfaces_t alternate_interfaces;
70 /** Some useful descriptors for USB device. */
71 usb_device_descriptors_t descriptors;
72 /** Generic DDF device backing this one. DO NOT TOUCH! */
73 ddf_dev_t *ddf_dev;
74 /** Custom driver data.
75 * Do not use the entry in generic device, that is already used
76 * by the framework.
77 */
78 void *driver_data;
79
80 usb_dev_session_t *bus_session;
81} usb_device_t;
82
83/** Count number of pipes the driver expects.
84 *
85 * @param drv USB driver.
86 * @return Number of pipes (excluding default control pipe).
87 */
88static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
89{
90 size_t count;
91 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
92 return count;
93}
94
95/** Change interface setting of a device.
96 * This function selects new alternate setting of an interface by issuing
97 * proper USB command to the device and also creates new USB pipes
98 * under @c dev->pipes.
99 *
100 * @warning This function is intended for drivers working at interface level.
101 * For drivers controlling the whole device, you need to change interface
102 * manually using usb_request_set_interface() and creating new pipes
103 * with usb_pipe_initialize_from_configuration().
104 *
105 * @warning This is a wrapper function that does several operations that
106 * can fail and that cannot be rollbacked easily. That means that a failure
107 * during the SET_INTERFACE request would result in having a device with
108 * no pipes at all (except the default control one). That is because the old
109 * pipes needs to be unregistered at HC first and the new ones could not
110 * be created.
111 *
112 * @param dev USB device.
113 * @param alternate_setting Alternate setting to choose.
114 * @param endpoints New endpoint descriptions.
115 * @return Error code.
116 */
117int usb_device_select_interface(usb_device_t *usb_dev,
118 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
119{
120 assert(usb_dev);
121
122 if (usb_dev->interface_no < 0) {
123 return EINVAL;
124 }
125
126 /* Change the interface itself. */
127 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
128 usb_dev->interface_no, alternate_setting);
129 if (rc != EOK) {
130 return rc;
131 }
132
133 /* Change current alternative */
134 usb_dev->alternate_interfaces.current = alternate_setting;
135
136 /* Destroy existing pipes. */
137 usb_device_destroy_pipes(usb_dev);
138
139 /* Create new pipes. */
140 rc = usb_device_create_pipes(usb_dev, endpoints);
141
142 return rc;
143}
144
145/** Retrieve basic descriptors from the device.
146 *
147 * @param[in] ctrl_pipe Control endpoint pipe.
148 * @param[out] descriptors Where to store the descriptors.
149 * @return Error code.
150 */
151static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
152{
153 assert(usb_dev);
154 assert(usb_dev->descriptors.full_config == NULL);
155
156 /* It is worth to start a long transfer. */
157 usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
158
159 /* Get the device descriptor. */
160 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
161 &usb_dev->descriptors.device);
162 if (rc != EOK) {
163 goto leave;
164 }
165
166 /* Get the full configuration descriptor. */
167 rc = usb_request_get_full_configuration_descriptor_alloc(
168 &usb_dev->ctrl_pipe, 0,
169 &usb_dev->descriptors.full_config,
170 &usb_dev->descriptors.full_config_size);
171
172leave:
173 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
174
175 return rc;
176}
177
178/** Cleanup structure initialized via usb_device_retrieve_descriptors.
179 *
180 * @param[in] descriptors Where to store the descriptors.
181 */
182static void usb_device_release_descriptors(usb_device_t *usb_dev)
183{
184 assert(usb_dev);
185 free(usb_dev->descriptors.full_config);
186 usb_dev->descriptors.full_config = NULL;
187 usb_dev->descriptors.full_config_size = 0;
188}
189
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] wire Initialized backing connection to the host controller.
198 * @param[in] endpoints Endpoints description, NULL terminated.
199 * @param[in] config_descr Configuration descriptor of active configuration.
200 * @param[in] config_descr_size Size of @p config_descr in bytes.
201 * @param[in] interface_no Interface to map from.
202 * @param[in] interface_setting Interface setting (default is usually 0).
203 * @param[out] pipes_ptr Where to store array of created pipes
204 * (not NULL terminated).
205 * @param[out] pipes_count_ptr Where to store number of pipes
206 * (set to NULL if you wish to ignore the count).
207 * @return Error code.
208 */
209int usb_device_create_pipes(usb_device_t *usb_dev,
210 const usb_endpoint_description_t **endpoints)
211{
212 assert(usb_dev);
213 assert(usb_dev->descriptors.full_config);
214 assert(usb_dev->pipes == NULL);
215 assert(usb_dev->pipes_count == 0);
216
217 size_t pipe_count = count_pipes(endpoints);
218 if (pipe_count == 0) {
219 return EOK;
220 }
221
222 usb_endpoint_mapping_t *pipes =
223 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
224 if (pipes == NULL) {
225 return ENOMEM;
226 }
227
228 /* Now initialize. */
229 for (size_t i = 0; i < pipe_count; i++) {
230 pipes[i].description = endpoints[i];
231 pipes[i].interface_no = usb_dev->interface_no;
232 pipes[i].interface_setting =
233 usb_dev->alternate_interfaces.current;
234 }
235
236 /* Find the mapping from configuration descriptor. */
237 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
238 usb_dev->descriptors.full_config,
239 usb_dev->descriptors.full_config_size, &usb_dev->wire);
240 if (rc != EOK) {
241 free(pipes);
242 return rc;
243 }
244
245 /* Register created pipes. */
246 for (size_t i = 0; i < pipe_count; i++) {
247 if (pipes[i].present) {
248 rc = usb_pipe_register(&pipes[i].pipe,
249 pipes[i].descriptor->poll_interval);
250 if (rc != EOK) {
251 goto rollback_unregister_endpoints;
252 }
253 }
254 }
255
256 usb_dev->pipes = pipes;
257 usb_dev->pipes_count = pipe_count;
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:
268 for (size_t i = 0; i < pipe_count; i++) {
269 if (pipes[i].present) {
270 usb_pipe_unregister(&pipes[i].pipe);
271 }
272 }
273
274 free(pipes);
275 return rc;
276}
277
278/** Destroy pipes previously created by usb_device_create_pipes.
279 *
280 * @param[in] usb_dev USB device.
281 */
282void usb_device_destroy_pipes(usb_device_t *usb_dev)
283{
284 assert(usb_dev);
285 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
286 /* Destroy the pipes. */
287 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
288 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
289 i, usb_dev->pipes[i].present ? "" : "not ");
290 if (usb_dev->pipes[i].present)
291 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
292 }
293 free(usb_dev->pipes);
294 usb_dev->pipes = NULL;
295 usb_dev->pipes_count = 0;
296}
297
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
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
326int usb_device_get_iface_number(usb_device_t *usb_dev)
327{
328 assert(usb_dev);
329 return usb_dev->interface_no;
330}
331
332const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
333{
334 assert(usb_dev);
335 return &usb_dev->descriptors;
336}
337
338const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
339 usb_device_t *usb_dev)
340{
341 assert(usb_dev);
342 return &usb_dev->alternate_interfaces;
343}
344
345static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
346 usb_address_t *address, int *iface_no)
347{
348 assert(usb_dev);
349
350 int ret = EOK;
351 async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
352 if (!exch)
353 ret = ENOMEM;
354
355 if (ret == EOK && address)
356 ret = usb_get_my_address(exch, address);
357
358 if (ret == EOK && handle)
359 ret = usb_get_hc_handle(exch, handle);
360
361 if (ret == EOK && iface_no) {
362 ret = usb_get_my_interface(exch, iface_no);
363 if (ret == ENOTSUP) {
364 ret = EOK;
365 *iface_no = -1;
366 }
367 }
368
369 async_exchange_end(exch);
370 return ret;
371}
372
373/** Clean instance of a USB device.
374 *
375 * @param dev Device to be de-initialized.
376 *
377 * Does not free/destroy supplied pointer.
378 */
379static void usb_device_fini(usb_device_t *usb_dev)
380{
381 if (usb_dev) {
382 usb_dev_disconnect(usb_dev->bus_session);
383 /* Destroy existing pipes. */
384 usb_device_destroy_pipes(usb_dev);
385 /* Ignore errors and hope for the best. */
386 usb_hc_connection_deinitialize(&usb_dev->hc_conn);
387 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
388 usb_device_release_descriptors(usb_dev);
389 free(usb_dev->driver_data);
390 usb_dev->driver_data = NULL;
391 }
392}
393
394/** Initialize new instance of USB device.
395 *
396 * @param[in] usb_dev Pointer to the new device.
397 * @param[in] ddf_dev Generic DDF device backing the USB one.
398 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
399 * @param[out] errstr_ptr Where to store description of context
400 * (in case error occurs).
401 * @return Error code.
402 */
403static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
404 const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
405 devman_handle_t handle)
406{
407 assert(usb_dev != NULL);
408 assert(errstr_ptr);
409
410 *errstr_ptr = NULL;
411
412 usb_dev->ddf_dev = ddf_dev;
413 usb_dev->driver_data = NULL;
414 usb_dev->descriptors.full_config = NULL;
415 usb_dev->descriptors.full_config_size = 0;
416 usb_dev->pipes_count = 0;
417 usb_dev->pipes = NULL;
418
419 if (ddf_dev)
420 usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
421 else
422 usb_dev->bus_session = usb_dev_connect(handle);
423
424 if (!usb_dev->bus_session) {
425 *errstr_ptr = "device bus session create";
426 return ENOMEM;
427 }
428
429 /* Get assigned params */
430 devman_handle_t hc_handle;
431 usb_address_t address;
432
433 int rc = usb_dev_get_info(usb_dev,
434 &hc_handle, &address, &usb_dev->interface_no);
435 if (rc != EOK) {
436 usb_dev_disconnect(usb_dev->bus_session);
437 *errstr_ptr = "device parameters retrieval";
438 return rc;
439 }
440
441 /* Initialize hc connection. */
442 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
443
444 /* Initialize backing wire and control pipe. */
445 rc = usb_device_connection_initialize(
446 &usb_dev->wire, &usb_dev->hc_conn, address);
447 if (rc != EOK) {
448 usb_dev_disconnect(usb_dev->bus_session);
449 *errstr_ptr = "device connection initialization";
450 return rc;
451 }
452
453 /* This pipe was registered by the hub driver,
454 * during device initialization. */
455 rc = usb_pipe_initialize_default_control(
456 &usb_dev->ctrl_pipe, &usb_dev->wire);
457 if (rc != EOK) {
458 usb_dev_disconnect(usb_dev->bus_session);
459 *errstr_ptr = "default control pipe initialization";
460 return rc;
461 }
462
463 /* Open hc connection for pipe registration. */
464 rc = usb_hc_connection_open(&usb_dev->hc_conn);
465 if (rc != EOK) {
466 usb_dev_disconnect(usb_dev->bus_session);
467 *errstr_ptr = "hc connection open";
468 return rc;
469 }
470
471 /* Retrieve standard descriptors. */
472 rc = usb_device_retrieve_descriptors(usb_dev);
473 if (rc != EOK) {
474 *errstr_ptr = "descriptor retrieval";
475 usb_hc_connection_close(&usb_dev->hc_conn);
476 usb_dev_disconnect(usb_dev->bus_session);
477 return rc;
478 }
479
480 /* Create alternate interfaces. We will silently ignore failure.
481 * We might either control one interface or an entire device,
482 * it makes no sense to speak about alternate interfaces when
483 * controlling a device. */
484 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
485 usb_dev->descriptors.full_config,
486 usb_dev->descriptors.full_config_size, usb_dev->interface_no);
487
488 if (endpoints) {
489 /* Create and register other pipes than default control (EP 0)*/
490 rc = usb_device_create_pipes(usb_dev, endpoints);
491 if (rc != EOK) {
492 usb_hc_connection_close(&usb_dev->hc_conn);
493 usb_device_fini(usb_dev);
494 *errstr_ptr = "pipes initialization";
495 return rc;
496 }
497 }
498
499 usb_hc_connection_close(&usb_dev->hc_conn);
500 return EOK;
501}
502
503int usb_device_create_ddf(ddf_dev_t *ddf_dev,
504 const usb_endpoint_description_t **desc, const char **err)
505{
506 assert(ddf_dev);
507 assert(err);
508 usb_device_t *usb_dev =
509 ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
510 if (usb_dev == NULL) {
511 *err = "DDF data alloc";
512 return ENOMEM;
513 }
514 return usb_device_init(usb_dev, ddf_dev, desc, err, 0);
515}
516
517void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
518{
519 assert(ddf_dev);
520 usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
521 assert(usb_dev);
522 usb_device_fini(usb_dev);
523 return;
524}
525
526usb_device_t * usb_device_create(devman_handle_t handle)
527{
528 usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
529 if (!usb_dev)
530 return NULL;
531 const char* dummy = NULL;
532 const int ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle);
533 if (ret != EOK) {
534 free(usb_dev);
535 usb_dev = NULL;
536 }
537 return usb_dev;
538}
539
540void usb_device_destroy(usb_device_t *usb_dev)
541{
542 if (usb_dev) {
543 usb_device_fini(usb_dev);
544 free(usb_dev);
545 }
546}
547
548const char *usb_device_get_name(usb_device_t *usb_dev)
549{
550 assert(usb_dev);
551 if (usb_dev->ddf_dev)
552 return ddf_dev_get_name(usb_dev->ddf_dev);
553 return NULL;
554}
555
556ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
557 const char* name)
558{
559 assert(usb_dev);
560 if (usb_dev->ddf_dev)
561 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
562 return NULL;
563}
564
565async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
566{
567 assert(usb_dev);
568 return async_exchange_begin(usb_dev->bus_session);
569}
570
571void usb_device_bus_exchange_end(async_exch_t *exch)
572{
573 async_exchange_end(exch);
574}
575
576/** Allocate driver specific data.
577 * @param usb_dev usb_device structure.
578 * @param size requested data size.
579 * @return Pointer to the newly allocated space, NULL on failure.
580 */
581void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
582{
583 assert(usb_dev);
584 assert(usb_dev->driver_data == NULL);
585 return usb_dev->driver_data = calloc(1, size);
586
587}
588
589void * usb_device_data_get(usb_device_t *usb_dev)
590{
591 assert(usb_dev);
592 return usb_dev->driver_data;
593}
594
595usb_address_t usb_device_address(usb_device_t *usb_dev)
596{
597 assert(usb_dev);
598 return usb_dev->wire.address;
599}
600
601devman_handle_t usb_device_hc_handle(usb_device_t *usb_dev)
602{
603 assert(usb_dev);
604 return usb_dev->hc_conn.hc_handle;
605}
606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.