source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ bad4a05

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

usbhost: documentation & cleanup

  • Property mode set to 100644
File size: 16.0 KB
Line 
1/*
2 * Copyright (c) 2013 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * Helpers to work with the DDF interface.
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <async.h>
39#include <ddf/driver.h>
40#include <ddf/interrupt.h>
41#include <device/hw_res_parsed.h>
42#include <errno.h>
43#include <str_error.h>
44#include <usb/classes/classes.h>
45#include <usb/debug.h>
46#include <usb/descriptor.h>
47#include <usb/usb.h>
48#include <usb_iface.h>
49#include <usbhc_iface.h>
50
51#include "bus.h"
52#include "endpoint.h"
53
54#include "ddf_helpers.h"
55
56
57static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
58static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
59
60
61/**
62 * DDF usbhc_iface callback. Passes the endpoint descriptors, fills the pipe
63 * descriptor according to the contents of the endpoint.
64 *
65 * @param[in] fun DDF function of the device in question.
66 * @param[out] pipe_desc The pipe descriptor to be filled.
67 * @param[in] endpoint_desc Endpoint descriptors from the device.
68 * @return Error code.
69 */
70static int register_endpoint(ddf_fun_t *fun, usb_pipe_desc_t *pipe_desc,
71 const usb_endpoint_descriptors_t *ep_desc)
72{
73 assert(fun);
74 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
75 device_t *dev = ddf_fun_data_get(fun);
76 assert(hcd);
77 assert(hcd->bus);
78 assert(dev);
79
80 endpoint_t *ep;
81 const int err = bus_endpoint_add(dev, ep_desc, &ep);
82 if (err)
83 return err;
84
85 if (pipe_desc) {
86 pipe_desc->endpoint_no = ep->endpoint;
87 pipe_desc->direction = ep->direction;
88 pipe_desc->transfer_type = ep->transfer_type;
89 pipe_desc->max_transfer_size = ep->max_transfer_size;
90 }
91 endpoint_del_ref(ep);
92
93 return EOK;
94}
95
96 /**
97 * DDF usbhc_iface callback. Unregister endpoint that makes the other end of
98 * the pipe described.
99 *
100 * @param fun DDF function of the device in question.
101 * @param pipe_desc Pipe description.
102 * @return Error code.
103 */
104static int unregister_endpoint(ddf_fun_t *fun, const usb_pipe_desc_t *pipe_desc)
105{
106 assert(fun);
107 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
108 device_t *dev = ddf_fun_data_get(fun);
109 assert(hcd);
110 assert(hcd->bus);
111 assert(dev);
112
113 endpoint_t *ep = bus_find_endpoint(dev, pipe_desc->endpoint_no);
114 if (!ep)
115 return ENOENT;
116
117 return bus_endpoint_remove(ep);
118}
119
120/**
121 * DDF usbhc_iface callback. Calls the bus operation directly.
122 *
123 * @param fun DDF function of the device (hub) requesting the address.
124 * @param speed An USB speed of the device for which the address is reserved.
125 */
126static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
127{
128 assert(fun);
129 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
130 device_t *dev = ddf_fun_data_get(fun);
131 assert(hcd);
132 assert(hcd->bus);
133 assert(dev);
134
135 usb_log_debug("Device %d requested default address at %s speed\n",
136 dev->address, usb_str_speed(speed));
137 return bus_reserve_default_address(hcd->bus, speed);
138}
139
140/**
141 * DDF usbhc_iface callback. Calls the bus operation directly.
142 *
143 * @param fun DDF function of the device (hub) releasing the address.
144 */
145static int release_default_address(ddf_fun_t *fun)
146{
147 assert(fun);
148 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
149 device_t *dev = ddf_fun_data_get(fun);
150 assert(hcd);
151 assert(hcd->bus);
152 assert(dev);
153
154 usb_log_debug("Device %d released default address\n", dev->address);
155 bus_release_default_address(hcd->bus);
156
157 return EOK;
158}
159
160/**
161 * DDF usbhc_iface callback. Calls the bus operation directly.
162 *
163 * @param fun DDF function of the device (hub) requesting the address.
164 */
165static int device_enumerate(ddf_fun_t *fun, unsigned port)
166{
167 assert(fun);
168 ddf_dev_t *hc = ddf_fun_get_dev(fun);
169 assert(hc);
170 hc_device_t *hcd = dev_to_hcd(hc);
171 assert(hcd);
172 device_t *hub = ddf_fun_data_get(fun);
173 assert(hub);
174
175 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
176 hub->address, port);
177 return hcd_ddf_new_device(hcd, hc, hub, port);
178}
179
180static int device_remove(ddf_fun_t *fun, unsigned port)
181{
182 assert(fun);
183 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
184 device_t *dev = ddf_fun_data_get(fun);
185 assert(ddf_dev);
186 assert(dev);
187 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
188 ddf_fun_get_name(fun), port);
189 return hcd_ddf_remove_device(ddf_dev, dev, port);
190}
191
192/** Gets handle of the respective device.
193 *
194 * @param[in] fun Device function.
195 * @param[out] handle Place to write the handle.
196 * @return Error code.
197 */
198static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
199{
200 assert(fun);
201 if (handle)
202 *handle = ddf_fun_get_handle(fun);
203 return EOK;
204}
205
206/** Inbound communication interface function.
207 * @param fun DDF function.
208 * @param target Communication target.
209 * @param setup_data Data to use in setup stage (control transfers).
210 * @param data Pointer to data buffer.
211 * @param size Size of the data buffer.
212 * @param callback Function to call on communication end.
213 * @param arg Argument passed to the callback function.
214 * @return Error code.
215 */
216static int dev_read(ddf_fun_t *fun, usb_target_t target,
217 uint64_t setup_data, char *data, size_t size,
218 usbhc_iface_transfer_callback_t callback, void *arg)
219{
220 assert(fun);
221 device_t *dev = ddf_fun_data_get(fun);
222 assert(dev);
223
224 target.address = dev->address;
225
226 return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
227 data, size, setup_data,
228 callback, arg, "READ");
229}
230
231/** Outbound communication interface function.
232 * @param fun DDF function.
233 * @param target Communication target.
234 * @param setup_data Data to use in setup stage (control transfers).
235 * @param data Pointer to data buffer.
236 * @param size Size of the data buffer.
237 * @param callback Function to call on communication end.
238 * @param arg Argument passed to the callback function.
239 * @return Error code.
240 */
241static int dev_write(ddf_fun_t *fun, usb_target_t target,
242 uint64_t setup_data, const char *data, size_t size,
243 usbhc_iface_transfer_callback_t callback, void *arg)
244{
245 assert(fun);
246 device_t *dev = ddf_fun_data_get(fun);
247 assert(dev);
248
249 target.address = dev->address;
250
251 return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
252 (char *) data, size, setup_data,
253 callback, arg, "WRITE");
254}
255
256/** USB device interface */
257static usb_iface_t usb_iface = {
258 .get_my_device_handle = get_my_device_handle,
259};
260
261/** USB host controller interface */
262static usbhc_iface_t usbhc_iface = {
263 .reserve_default_address = reserve_default_address,
264 .release_default_address = release_default_address,
265
266 .device_enumerate = device_enumerate,
267 .device_remove = device_remove,
268
269 .register_endpoint = register_endpoint,
270 .unregister_endpoint = unregister_endpoint,
271
272 .read = dev_read,
273 .write = dev_write,
274};
275
276/** Standard USB device interface) */
277static ddf_dev_ops_t usb_ops = {
278 .interfaces[USB_DEV_IFACE] = &usb_iface,
279 .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
280};
281
282
283/* DDF HELPERS */
284
285#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
286do { \
287 match_id_t *mid = malloc(sizeof(match_id_t)); \
288 if (!mid) { \
289 clean_match_ids(list); \
290 return ENOMEM; \
291 } \
292 char *id = NULL; \
293 int ret = asprintf(&id, str, ##__VA_ARGS__); \
294 if (ret < 0) { \
295 clean_match_ids(list); \
296 free(mid); \
297 return ENOMEM; \
298 } \
299 mid->score = sc; \
300 mid->id = id; \
301 add_match_id(list, mid); \
302} while (0)
303
304/* This is a copy of lib/usbdev/src/recognise.c */
305static int create_match_ids(match_id_list_t *l,
306 usb_standard_device_descriptor_t *d)
307{
308 assert(l);
309 assert(d);
310
311 if (d->vendor_id != 0) {
312 /* First, with release number. */
313 ADD_MATCHID_OR_RETURN(l, 100,
314 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
315 d->vendor_id, d->product_id, (d->device_version >> 8),
316 (d->device_version & 0xff));
317
318 /* Next, without release number. */
319 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
320 d->vendor_id, d->product_id);
321 }
322
323 /* Class match id */
324 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
325 usb_str_class(d->device_class));
326
327 /* As a last resort, try fallback driver. */
328 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
329
330 return EOK;
331}
332
333static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
334 unsigned port)
335{
336 assert(device);
337
338 hc_device_t *hcd = dev_to_hcd(device);
339 assert(hcd);
340 assert(hcd->bus);
341
342 fibril_mutex_lock(&hub->guard);
343
344 device_t *victim = NULL;
345
346 list_foreach(hub->devices, link, device_t, it) {
347 if (it->port == port) {
348 victim = it;
349 break;
350 }
351 }
352 if (victim) {
353 assert(victim->fun);
354 assert(victim->port == port);
355 assert(victim->hub == hub);
356 list_remove(&victim->link);
357 fibril_mutex_unlock(&hub->guard);
358 const int ret = ddf_fun_unbind(victim->fun);
359 if (ret == EOK) {
360 bus_device_remove(victim);
361 ddf_fun_destroy(victim->fun);
362 } else {
363 usb_log_warning("Failed to unbind device `%s': %s\n",
364 ddf_fun_get_name(victim->fun), str_error(ret));
365 }
366 return EOK;
367 }
368 fibril_mutex_unlock(&hub->guard);
369 return ENOENT;
370}
371
372device_t *hcd_ddf_fun_create(hc_device_t *hc)
373{
374 /* Create DDF function for the new device */
375 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
376 if (!fun)
377 return NULL;
378
379 ddf_fun_set_ops(fun, &usb_ops);
380
381 /* Create USB device node for the new device */
382 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
383 if (!dev) {
384 ddf_fun_destroy(fun);
385 return NULL;
386 }
387
388 bus_device_init(dev, hc->bus);
389 dev->fun = fun;
390 return dev;
391}
392
393void hcd_ddf_fun_destroy(device_t *dev)
394{
395 assert(dev);
396 assert(dev->fun);
397 ddf_fun_destroy(dev->fun);
398}
399
400int hcd_device_explore(device_t *device)
401{
402 int err;
403 match_id_list_t mids;
404 usb_standard_device_descriptor_t desc = { 0 };
405
406 init_match_ids(&mids);
407
408 const usb_target_t control_ep = {{
409 .address = device->address,
410 .endpoint = 0,
411 }};
412
413 /* Get std device descriptor */
414 const usb_device_request_setup_packet_t get_device_desc =
415 GET_DEVICE_DESC(sizeof(desc));
416
417 usb_log_debug("Device(%d): Requesting full device descriptor.",
418 device->address);
419 ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
420 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
421 "read device descriptor");
422 if (got < 0) {
423 err = got < 0 ? got : EOVERFLOW;
424 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
425 device->address, str_error(err));
426 goto out;
427 }
428
429 /* Create match ids from the device descriptor */
430 usb_log_debug("Device(%d): Creating match IDs.", device->address);
431 if ((err = create_match_ids(&mids, &desc))) {
432 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
433 goto out;
434 }
435
436 list_foreach(mids.ids, link, const match_id_t, mid) {
437 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
438 }
439
440out:
441 clean_match_ids(&mids);
442 return err;
443}
444
445static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
446{
447 int err;
448 assert(hcd);
449 assert(hcd->bus);
450 assert(hub);
451 assert(hc);
452
453 device_t *dev = hcd_ddf_fun_create(hcd);
454 if (!dev) {
455 usb_log_error("Failed to create USB device function.");
456 return ENOMEM;
457 }
458
459 dev->hub = hub;
460 dev->port = port;
461
462 if ((err = bus_device_enumerate(dev))) {
463 usb_log_error("Failed to initialize USB dev memory structures.");
464 return err;
465 }
466
467 /* If the driver didn't name the dev when enumerating,
468 * do it in some generic way.
469 */
470 if (!ddf_fun_get_name(dev->fun)) {
471 bus_device_set_default_name(dev);
472 }
473
474 if ((err = ddf_fun_bind(dev->fun))) {
475 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
476 goto err_usb_dev;
477 }
478
479 fibril_mutex_lock(&hub->guard);
480 list_append(&dev->link, &hub->devices);
481 fibril_mutex_unlock(&hub->guard);
482
483 return EOK;
484
485err_usb_dev:
486 hcd_ddf_fun_destroy(dev);
487 return err;
488}
489
490/** Announce root hub to the DDF
491 *
492 * @param[in] device Host controller ddf device
493 * @return Error code
494 */
495int hcd_setup_virtual_root_hub(hc_device_t *hcd)
496{
497 int err;
498
499 assert(hcd);
500
501 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
502 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
503 return err;
504 }
505
506 device_t *dev = hcd_ddf_fun_create(hcd);
507 if (!dev) {
508 usb_log_error("Failed to create function for the root hub.");
509 goto err_default_address;
510 }
511
512 ddf_fun_set_name(dev->fun, "roothub");
513
514 /* Assign an address to the device */
515 if ((err = bus_device_enumerate(dev))) {
516 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
517 goto err_usb_dev;
518 }
519
520 if ((err = ddf_fun_bind(dev->fun))) {
521 usb_log_error("Failed to register roothub: %s.", str_error(err));
522 goto err_usb_dev;
523 }
524
525 bus_release_default_address(hcd->bus);
526 return EOK;
527
528err_usb_dev:
529 hcd_ddf_fun_destroy(dev);
530err_default_address:
531 bus_release_default_address(hcd->bus);
532 return err;
533}
534
535/** Initialize hc structures.
536 *
537 * @param[in] device DDF instance of the device to use.
538 * @param[in] max_speed Maximum supported USB speed.
539 * @param[in] bw available bandwidth.
540 * @param[in] bw_count Function to compute required ep bandwidth.
541 *
542 * @return Error code.
543 * This function does all the ddf work for hc driver.
544 */
545int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
546{
547 assert(device);
548
549 hc_device_t *instance = ddf_dev_data_alloc(device, size);
550 if (instance == NULL) {
551 usb_log_error("Failed to allocate HCD ddf structure.\n");
552 return ENOMEM;
553 }
554 instance->ddf_dev = device;
555
556 int ret = ENOMEM;
557 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
558 if (!instance->ctl_fun) {
559 usb_log_error("Failed to create HCD ddf fun.\n");
560 goto err_destroy_fun;
561 }
562
563 ret = ddf_fun_bind(instance->ctl_fun);
564 if (ret != EOK) {
565 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
566 goto err_destroy_fun;
567 }
568
569 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
570 if (ret != EOK) {
571 usb_log_error("Failed to add fun to category: %s.\n",
572 str_error(ret));
573 ddf_fun_unbind(instance->ctl_fun);
574 goto err_destroy_fun;
575 }
576
577 /* HC should be ok at this point (except it can't do anything) */
578 return EOK;
579
580err_destroy_fun:
581 ddf_fun_destroy(instance->ctl_fun);
582 instance->ctl_fun = NULL;
583 return ret;
584}
585
586void hcd_ddf_clean_hc(hc_device_t *hcd)
587{
588 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
589 ddf_fun_destroy(hcd->ctl_fun);
590}
591
592/** Call the parent driver with a request to enable interrupt
593 *
594 * @param[in] device Device asking for interrupts
595 * @param[in] inum Interrupt number
596 * @return Error code.
597 */
598int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
599{
600 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
601 if (parent_sess == NULL)
602 return EIO;
603
604 return hw_res_enable_interrupt(parent_sess, inum);
605}
606
607int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
608{
609 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
610 if (parent_sess == NULL)
611 return EIO;
612
613 hw_res_list_parsed_init(hw_res);
614 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
615 if (ret != EOK)
616 hw_res_list_parsed_clean(hw_res);
617 return ret;
618}
619
620/**
621 * @}
622 */
Note: See TracBrowser for help on using the repository browser.