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

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

libusbdev: Add device connection to pipe structure.

  • 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 usb_dev->bus_session);
241 if (rc != EOK) {
242 free(pipes);
243 return rc;
244 }
245
246 /* Register created pipes. */
247 for (size_t i = 0; i < pipe_count; i++) {
248 if (pipes[i].present) {
249 rc = usb_pipe_register(&pipes[i].pipe,
250 pipes[i].descriptor->poll_interval);
251 if (rc != EOK) {
252 goto rollback_unregister_endpoints;
253 }
254 }
255 }
256
257 usb_dev->pipes = pipes;
258 usb_dev->pipes_count = pipe_count;
259
260 return EOK;
261
262 /*
263 * Jump here if something went wrong after endpoints have
264 * been registered.
265 * This is also the target when the registration of
266 * endpoints fails.
267 */
268rollback_unregister_endpoints:
269 for (size_t i = 0; i < pipe_count; i++) {
270 if (pipes[i].present) {
271 usb_pipe_unregister(&pipes[i].pipe);
272 }
273 }
274
275 free(pipes);
276 return rc;
277}
278
279/** Destroy pipes previously created by usb_device_create_pipes.
280 *
281 * @param[in] usb_dev USB device.
282 */
283void usb_device_destroy_pipes(usb_device_t *usb_dev)
284{
285 assert(usb_dev);
286 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
287 /* Destroy the pipes. */
288 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
289 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
290 i, usb_dev->pipes[i].present ? "" : "not ");
291 if (usb_dev->pipes[i].present)
292 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
293 }
294 free(usb_dev->pipes);
295 usb_dev->pipes = NULL;
296 usb_dev->pipes_count = 0;
297}
298
299usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
300{
301 assert(usb_dev);
302 return &usb_dev->ctrl_pipe;
303}
304
305usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
306 const usb_endpoint_description_t *desc)
307{
308 assert(usb_dev);
309 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
310 if (usb_dev->pipes[i].description == desc)
311 return &usb_dev->pipes[i];
312 }
313 return NULL;
314}
315
316usb_endpoint_mapping_t * usb_device_get_mapped_ep(
317 usb_device_t *usb_dev, usb_endpoint_t ep)
318{
319 assert(usb_dev);
320 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
321 if (usb_dev->pipes[i].pipe.endpoint_no == ep)
322 return &usb_dev->pipes[i];
323 }
324 return NULL;
325}
326
327int usb_device_get_iface_number(usb_device_t *usb_dev)
328{
329 assert(usb_dev);
330 return usb_dev->interface_no;
331}
332
333const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
334{
335 assert(usb_dev);
336 return &usb_dev->descriptors;
337}
338
339const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
340 usb_device_t *usb_dev)
341{
342 assert(usb_dev);
343 return &usb_dev->alternate_interfaces;
344}
345
346static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
347 usb_address_t *address, int *iface_no)
348{
349 assert(usb_dev);
350
351 int ret = EOK;
352 async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
353 if (!exch)
354 ret = ENOMEM;
355
356 if (ret == EOK && address)
357 ret = usb_get_my_address(exch, address);
358
359 if (ret == EOK && handle)
360 ret = usb_get_hc_handle(exch, handle);
361
362 if (ret == EOK && iface_no) {
363 ret = usb_get_my_interface(exch, iface_no);
364 if (ret == ENOTSUP) {
365 ret = EOK;
366 *iface_no = -1;
367 }
368 }
369
370 async_exchange_end(exch);
371 return ret;
372}
373
374/** Clean instance of a USB device.
375 *
376 * @param dev Device to be de-initialized.
377 *
378 * Does not free/destroy supplied pointer.
379 */
380static void usb_device_fini(usb_device_t *usb_dev)
381{
382 if (usb_dev) {
383 usb_dev_disconnect(usb_dev->bus_session);
384 /* Destroy existing pipes. */
385 usb_device_destroy_pipes(usb_dev);
386 /* Ignore errors and hope for the best. */
387 usb_hc_connection_deinitialize(&usb_dev->hc_conn);
388 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
389 usb_device_release_descriptors(usb_dev);
390 free(usb_dev->driver_data);
391 usb_dev->driver_data = NULL;
392 }
393}
394
395/** Initialize new instance of USB device.
396 *
397 * @param[in] usb_dev Pointer to the new device.
398 * @param[in] ddf_dev Generic DDF device backing the USB one.
399 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
400 * @param[out] errstr_ptr Where to store description of context
401 * (in case error occurs).
402 * @return Error code.
403 */
404static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
405 const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
406 devman_handle_t handle)
407{
408 assert(usb_dev != NULL);
409 assert(errstr_ptr);
410
411 *errstr_ptr = NULL;
412
413 usb_dev->ddf_dev = ddf_dev;
414 usb_dev->driver_data = NULL;
415 usb_dev->descriptors.full_config = NULL;
416 usb_dev->descriptors.full_config_size = 0;
417 usb_dev->pipes_count = 0;
418 usb_dev->pipes = NULL;
419
420 if (ddf_dev)
421 usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
422 else
423 usb_dev->bus_session = usb_dev_connect(handle);
424
425 if (!usb_dev->bus_session) {
426 *errstr_ptr = "device bus session create";
427 return ENOMEM;
428 }
429
430 /* Get assigned params */
431 devman_handle_t hc_handle;
432 usb_address_t address;
433
434 int rc = usb_dev_get_info(usb_dev,
435 &hc_handle, &address, &usb_dev->interface_no);
436 if (rc != EOK) {
437 usb_dev_disconnect(usb_dev->bus_session);
438 *errstr_ptr = "device parameters retrieval";
439 return rc;
440 }
441
442 /* Initialize hc connection. */
443 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
444
445 /* Initialize backing wire and control pipe. */
446 rc = usb_device_connection_initialize(
447 &usb_dev->wire, &usb_dev->hc_conn, address);
448 if (rc != EOK) {
449 usb_dev_disconnect(usb_dev->bus_session);
450 *errstr_ptr = "device connection initialization";
451 return rc;
452 }
453
454 /* This pipe was registered by the hub driver,
455 * during device initialization. */
456 rc = usb_pipe_initialize_default_control(
457 &usb_dev->ctrl_pipe, &usb_dev->wire, usb_dev->bus_session);
458 if (rc != EOK) {
459 usb_dev_disconnect(usb_dev->bus_session);
460 *errstr_ptr = "default control pipe initialization";
461 return rc;
462 }
463
464 /* Open hc connection for pipe registration. */
465 rc = usb_hc_connection_open(&usb_dev->hc_conn);
466 if (rc != EOK) {
467 usb_dev_disconnect(usb_dev->bus_session);
468 *errstr_ptr = "hc connection open";
469 return rc;
470 }
471
472 /* Retrieve standard descriptors. */
473 rc = usb_device_retrieve_descriptors(usb_dev);
474 if (rc != EOK) {
475 *errstr_ptr = "descriptor retrieval";
476 usb_hc_connection_close(&usb_dev->hc_conn);
477 usb_dev_disconnect(usb_dev->bus_session);
478 return rc;
479 }
480
481 /* Create alternate interfaces. We will silently ignore failure.
482 * We might either control one interface or an entire device,
483 * it makes no sense to speak about alternate interfaces when
484 * controlling a device. */
485 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
486 usb_dev->descriptors.full_config,
487 usb_dev->descriptors.full_config_size, usb_dev->interface_no);
488
489 if (endpoints) {
490 /* Create and register other pipes than default control (EP 0)*/
491 rc = usb_device_create_pipes(usb_dev, endpoints);
492 if (rc != EOK) {
493 usb_hc_connection_close(&usb_dev->hc_conn);
494 usb_device_fini(usb_dev);
495 *errstr_ptr = "pipes initialization";
496 return rc;
497 }
498 }
499
500 usb_hc_connection_close(&usb_dev->hc_conn);
501 return EOK;
502}
503
504int usb_device_create_ddf(ddf_dev_t *ddf_dev,
505 const usb_endpoint_description_t **desc, const char **err)
506{
507 assert(ddf_dev);
508 assert(err);
509 usb_device_t *usb_dev =
510 ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
511 if (usb_dev == NULL) {
512 *err = "DDF data alloc";
513 return ENOMEM;
514 }
515 return usb_device_init(usb_dev, ddf_dev, desc, err, 0);
516}
517
518void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
519{
520 assert(ddf_dev);
521 usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
522 assert(usb_dev);
523 usb_device_fini(usb_dev);
524 return;
525}
526
527usb_device_t * usb_device_create(devman_handle_t handle)
528{
529 usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
530 if (!usb_dev)
531 return NULL;
532 const char* dummy = NULL;
533 const int ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle);
534 if (ret != EOK) {
535 free(usb_dev);
536 usb_dev = NULL;
537 }
538 return usb_dev;
539}
540
541void usb_device_destroy(usb_device_t *usb_dev)
542{
543 if (usb_dev) {
544 usb_device_fini(usb_dev);
545 free(usb_dev);
546 }
547}
548
549const char *usb_device_get_name(usb_device_t *usb_dev)
550{
551 assert(usb_dev);
552 if (usb_dev->ddf_dev)
553 return ddf_dev_get_name(usb_dev->ddf_dev);
554 return NULL;
555}
556
557ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
558 const char* name)
559{
560 assert(usb_dev);
561 if (usb_dev->ddf_dev)
562 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
563 return NULL;
564}
565
566async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
567{
568 assert(usb_dev);
569 return async_exchange_begin(usb_dev->bus_session);
570}
571
572void usb_device_bus_exchange_end(async_exch_t *exch)
573{
574 async_exchange_end(exch);
575}
576
577/** Allocate driver specific data.
578 * @param usb_dev usb_device structure.
579 * @param size requested data size.
580 * @return Pointer to the newly allocated space, NULL on failure.
581 */
582void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
583{
584 assert(usb_dev);
585 assert(usb_dev->driver_data == NULL);
586 return usb_dev->driver_data = calloc(1, size);
587
588}
589
590void * usb_device_data_get(usb_device_t *usb_dev)
591{
592 assert(usb_dev);
593 return usb_dev->driver_data;
594}
595
596usb_address_t usb_device_address(usb_device_t *usb_dev)
597{
598 assert(usb_dev);
599 return usb_dev->wire.address;
600}
601
602devman_handle_t usb_device_hc_handle(usb_device_t *usb_dev)
603{
604 assert(usb_dev);
605 return usb_dev->hc_conn.hc_handle;
606}
607/**
608 * @}
609 */
Note: See TracBrowser for help on using the repository browser.