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

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

usb: Move usb_device to a separate header

  • Property mode set to 100644
File size: 16.1 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
71 /** Some useful descriptors for USB device. */
72 struct {
73 /** Standard device descriptor. */
74 usb_standard_device_descriptor_t device;
75 /** Full configuration descriptor of current configuration. */
76 const uint8_t *configuration;
77 size_t configuration_size;
78 } descriptors;
79
80 /** Generic DDF device backing this one. DO NOT TOUCH! */
81 ddf_dev_t *ddf_dev;
82 /** Custom driver data.
83 * Do not use the entry in generic device, that is already used
84 * by the framework.
85 */
86 void *driver_data;
87
88 usb_dev_session_t *bus_session;
89} usb_device_t;
90
91/** Count number of pipes the driver expects.
92 *
93 * @param drv USB driver.
94 * @return Number of pipes (excluding default control pipe).
95 */
96static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
97{
98 size_t count;
99 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
100 return count;
101}
102
103/** Change interface setting of a device.
104 * This function selects new alternate setting of an interface by issuing
105 * proper USB command to the device and also creates new USB pipes
106 * under @c dev->pipes.
107 *
108 * @warning This function is intended for drivers working at interface level.
109 * For drivers controlling the whole device, you need to change interface
110 * manually using usb_request_set_interface() and creating new pipes
111 * with usb_pipe_initialize_from_configuration().
112 *
113 * @warning This is a wrapper function that does several operations that
114 * can fail and that cannot be rollbacked easily. That means that a failure
115 * during the SET_INTERFACE request would result in having a device with
116 * no pipes at all (except the default control one). That is because the old
117 * pipes needs to be unregistered at HC first and the new ones could not
118 * be created.
119 *
120 * @param dev USB device.
121 * @param alternate_setting Alternate setting to choose.
122 * @param endpoints New endpoint descriptions.
123 * @return Error code.
124 */
125int usb_device_select_interface(usb_device_t *usb_dev,
126 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
127{
128 assert(usb_dev);
129
130 if (usb_dev->interface_no < 0) {
131 return EINVAL;
132 }
133
134 /* Change the interface itself. */
135 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
136 usb_dev->interface_no, alternate_setting);
137 if (rc != EOK) {
138 return rc;
139 }
140
141 /* Change current alternative */
142 usb_dev->alternate_interfaces.current = alternate_setting;
143
144 /* Destroy existing pipes. */
145 usb_device_destroy_pipes(usb_dev);
146
147 /* Create new pipes. */
148 rc = usb_device_create_pipes(usb_dev, endpoints);
149
150 return rc;
151}
152
153/** Retrieve basic descriptors from the device.
154 *
155 * @param[in] ctrl_pipe Control endpoint pipe.
156 * @param[out] descriptors Where to store the descriptors.
157 * @return Error code.
158 */
159static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
160{
161 assert(usb_dev);
162 assert(usb_dev->descriptors.configuration == NULL);
163
164 /* It is worth to start a long transfer. */
165 usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
166
167 /* Get the device descriptor. */
168 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
169 &usb_dev->descriptors.device);
170 if (rc != EOK) {
171 goto leave;
172 }
173
174 /* Get the full configuration descriptor. */
175 rc = usb_request_get_full_configuration_descriptor_alloc(
176 &usb_dev->ctrl_pipe, 0,
177 (void **) &usb_dev->descriptors.configuration,
178 &usb_dev->descriptors.configuration_size);
179
180leave:
181 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
182
183 return rc;
184}
185
186/** Cleanup structure initialized via usb_device_retrieve_descriptors.
187 *
188 * @param[in] descriptors Where to store the descriptors.
189 */
190static void usb_device_release_descriptors(usb_device_t *usb_dev)
191{
192 assert(usb_dev);
193 free(usb_dev->descriptors.configuration);
194 usb_dev->descriptors.configuration = NULL;
195 usb_dev->descriptors.configuration_size = 0;
196}
197
198/** Create pipes for a device.
199 *
200 * This is more or less a wrapper that does following actions:
201 * - allocate and initialize pipes
202 * - map endpoints to the pipes based on the descriptions
203 * - registers endpoints with the host controller
204 *
205 * @param[in] wire Initialized backing connection to the host controller.
206 * @param[in] endpoints Endpoints description, NULL terminated.
207 * @param[in] config_descr Configuration descriptor of active configuration.
208 * @param[in] config_descr_size Size of @p config_descr in bytes.
209 * @param[in] interface_no Interface to map from.
210 * @param[in] interface_setting Interface setting (default is usually 0).
211 * @param[out] pipes_ptr Where to store array of created pipes
212 * (not NULL terminated).
213 * @param[out] pipes_count_ptr Where to store number of pipes
214 * (set to NULL if you wish to ignore the count).
215 * @return Error code.
216 */
217int usb_device_create_pipes(usb_device_t *usb_dev,
218 const usb_endpoint_description_t **endpoints)
219{
220 assert(usb_dev);
221 assert(usb_dev->descriptors.configuration);
222 assert(usb_dev->pipes == NULL);
223 assert(usb_dev->pipes_count == 0);
224
225 size_t pipe_count = count_pipes(endpoints);
226 if (pipe_count == 0) {
227 return EOK;
228 }
229
230 usb_endpoint_mapping_t *pipes =
231 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
232 if (pipes == NULL) {
233 return ENOMEM;
234 }
235
236 /* Now initialize. */
237 for (size_t i = 0; i < pipe_count; i++) {
238 pipes[i].description = endpoints[i];
239 pipes[i].interface_no = usb_dev->interface_no;
240 pipes[i].interface_setting =
241 usb_dev->alternate_interfaces.current;
242 }
243
244 /* Find the mapping from configuration descriptor. */
245 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
246 usb_dev->descriptors.configuration,
247 usb_dev->descriptors.configuration_size, &usb_dev->wire);
248 if (rc != EOK) {
249 free(pipes);
250 return rc;
251 }
252
253 /* Register created pipes. */
254 for (size_t i = 0; i < pipe_count; i++) {
255 if (pipes[i].present) {
256 rc = usb_pipe_register(&pipes[i].pipe,
257 pipes[i].descriptor->poll_interval);
258 if (rc != EOK) {
259 goto rollback_unregister_endpoints;
260 }
261 }
262 }
263
264 usb_dev->pipes = pipes;
265 usb_dev->pipes_count = pipe_count;
266
267 return EOK;
268
269 /*
270 * Jump here if something went wrong after endpoints have
271 * been registered.
272 * This is also the target when the registration of
273 * endpoints fails.
274 */
275rollback_unregister_endpoints:
276 for (size_t i = 0; i < pipe_count; i++) {
277 if (pipes[i].present) {
278 usb_pipe_unregister(&pipes[i].pipe);
279 }
280 }
281
282 free(pipes);
283 return rc;
284}
285
286/** Destroy pipes previously created by usb_device_create_pipes.
287 *
288 * @param[in] usb_dev USB device.
289 */
290void usb_device_destroy_pipes(usb_device_t *usb_dev)
291{
292 assert(usb_dev);
293 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
294 /* Destroy the pipes. */
295 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
296 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
297 i, usb_dev->pipes[i].present ? "" : "not ");
298 if (usb_dev->pipes[i].present)
299 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
300 }
301 free(usb_dev->pipes);
302 usb_dev->pipes = NULL;
303 usb_dev->pipes_count = 0;
304}
305
306usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
307{
308 assert(usb_dev);
309 return &usb_dev->ctrl_pipe;
310}
311
312usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
313 const usb_endpoint_description_t *desc)
314{
315 assert(usb_dev);
316 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
317 if (usb_dev->pipes[i].description == desc)
318 return &usb_dev->pipes[i];
319 }
320 return NULL;
321}
322
323usb_endpoint_mapping_t * usb_device_get_mapped_ep(
324 usb_device_t *usb_dev, usb_endpoint_t ep)
325{
326 assert(usb_dev);
327 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
328 if (usb_dev->pipes[i].pipe.endpoint_no == ep)
329 return &usb_dev->pipes[i];
330 }
331 return NULL;
332}
333
334int usb_device_get_iface_number(usb_device_t *usb_dev)
335{
336 assert(usb_dev);
337 return usb_dev->interface_no;
338}
339
340const usb_standard_device_descriptor_t *
341usb_device_get_device_descriptor(usb_device_t *usb_dev)
342{
343 assert(usb_dev);
344 return &usb_dev->descriptors.device;
345}
346
347const void * usb_device_get_configuration_descriptor(
348 usb_device_t *usb_dev, size_t *size)
349{
350 assert(usb_dev);
351 if (size)
352 *size = usb_dev->descriptors.configuration_size;
353 return usb_dev->descriptors.configuration;
354}
355
356const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
357 usb_device_t *usb_dev)
358{
359 assert(usb_dev);
360 return &usb_dev->alternate_interfaces;
361}
362
363static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
364 usb_address_t *address, int *iface_no)
365{
366 assert(usb_dev);
367
368 int ret = EOK;
369 async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
370 if (!exch)
371 ret = ENOMEM;
372
373 if (ret == EOK && address)
374 ret = usb_get_my_address(exch, address);
375
376 if (ret == EOK && handle)
377 ret = usb_get_hc_handle(exch, handle);
378
379 if (ret == EOK && iface_no) {
380 ret = usb_get_my_interface(exch, iface_no);
381 if (ret == ENOTSUP) {
382 ret = EOK;
383 *iface_no = -1;
384 }
385 }
386
387 async_exchange_end(exch);
388 return ret;
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 */
400static int 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 usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
415 if (!usb_dev->bus_session) {
416 *errstr_ptr = "device bus session create";
417 return ENOMEM;
418 }
419
420 /* Get assigned params */
421 devman_handle_t hc_handle;
422 usb_address_t address;
423
424 int rc = usb_dev_get_info(usb_dev,
425 &hc_handle, &address, &usb_dev->interface_no);
426 if (rc != EOK) {
427 *errstr_ptr = "device parameters retrieval";
428 return rc;
429 }
430
431 /* Initialize hc connection. */
432 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
433
434 /* Initialize backing wire and control pipe. */
435 rc = usb_device_connection_initialize(
436 &usb_dev->wire, &usb_dev->hc_conn, address);
437 if (rc != EOK) {
438 *errstr_ptr = "device connection initialization";
439 return rc;
440 }
441
442 /* This pipe was registered by the hub driver,
443 * during device initialization. */
444 rc = usb_pipe_initialize_default_control(
445 &usb_dev->ctrl_pipe, &usb_dev->wire);
446 if (rc != EOK) {
447 *errstr_ptr = "default control pipe initialization";
448 return rc;
449 }
450
451 /* Open hc connection for pipe registration. */
452 rc = usb_hc_connection_open(&usb_dev->hc_conn);
453 if (rc != EOK) {
454 *errstr_ptr = "hc connection open";
455 return rc;
456 }
457
458 /* Retrieve standard descriptors. */
459 rc = usb_device_retrieve_descriptors(usb_dev);
460 if (rc != EOK) {
461 *errstr_ptr = "descriptor retrieval";
462 usb_hc_connection_close(&usb_dev->hc_conn);
463 return rc;
464 }
465
466 /* Create alternate interfaces. We will silently ignore failure.
467 * We might either control one interface or an entire device,
468 * it makes no sense to speak about alternate interfaces when
469 * controlling a device. */
470 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
471 usb_dev->descriptors.configuration,
472 usb_dev->descriptors.configuration_size, usb_dev->interface_no);
473
474 /* Create and register other pipes than default control (EP 0) */
475 rc = usb_device_create_pipes(usb_dev, endpoints);
476 if (rc != EOK) {
477 usb_hc_connection_close(&usb_dev->hc_conn);
478 /* Full configuration descriptor is allocated. */
479 usb_device_release_descriptors(usb_dev);
480 /* Alternate interfaces may be allocated */
481 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
482 *errstr_ptr = "pipes initialization";
483 return rc;
484 }
485
486 usb_hc_connection_close(&usb_dev->hc_conn);
487 return EOK;
488}
489
490/** Clean instance of a USB device.
491 *
492 * @param dev Device to be de-initialized.
493 *
494 * Does not free/destroy supplied pointer.
495 */
496static void usb_device_fini(usb_device_t *dev)
497{
498 if (dev) {
499 usb_dev_disconnect(dev->bus_session);
500 /* Destroy existing pipes. */
501 usb_device_destroy_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);
506 free(dev->driver_data);
507 dev->driver_data = NULL;
508 }
509}
510
511int usb_device_create_ddf(ddf_dev_t *ddf_dev,
512 const usb_endpoint_description_t **desc, const char **err)
513{
514 assert(ddf_dev);
515 assert(err);
516 usb_device_t *dev = ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
517 if (dev == NULL) {
518 *err = "DDF data alloc";
519 return ENOMEM;
520 }
521 return usb_device_init(dev, ddf_dev, desc, err);
522}
523
524void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
525{
526 assert(ddf_dev);
527 usb_device_t *dev = ddf_dev_data_get(ddf_dev);
528 assert(dev);
529 usb_device_fini(dev);
530 return;
531}
532
533const char *usb_device_get_name(usb_device_t *usb_dev)
534{
535 assert(usb_dev);
536 if (usb_dev->ddf_dev)
537 return ddf_dev_get_name(usb_dev->ddf_dev);
538 return NULL;
539}
540
541ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
542 const char* name)
543{
544 assert(usb_dev);
545 if (usb_dev->ddf_dev)
546 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
547 return NULL;
548}
549
550async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
551{
552 assert(usb_dev);
553 return async_exchange_begin(usb_dev->bus_session);
554}
555
556void usb_device_bus_exchange_end(async_exch_t *exch)
557{
558 async_exchange_end(exch);
559}
560
561/** Allocate driver specific data.
562 * @param usb_dev usb_device structure.
563 * @param size requested data size.
564 * @return Pointer to the newly allocated space, NULL on failure.
565 */
566void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
567{
568 assert(usb_dev);
569 assert(usb_dev->driver_data == NULL);
570 return usb_dev->driver_data = calloc(1, size);
571
572}
573
574void * usb_device_data_get(usb_device_t *usb_dev)
575{
576 assert(usb_dev);
577 return usb_dev->driver_data;
578}
579/**
580 * @}
581 */
Note: See TracBrowser for help on using the repository browser.