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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 129b821f was 129b821f, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usbhub: be aware of its own speed

This resulted in a bunch of changes just because the roothubs in older
HC's are virtual, and need to be aware of their own speed too.

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