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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was d1582b50, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Fix spacing in single-line comments using latest ccheck

This found incorrectly formatted section comments (with blocks of
asterisks or dashes). I strongly believe against using section comments
but I am not simply removing them since that would probably be
controversial.

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