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

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

usbhost: endpoint is identified also by its direction

  • Property mode set to 100644
File size: 15.6 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, pipe_desc->direction);
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 device_t *victim = NULL;
339
340 fibril_mutex_lock(&hub->guard);
341 list_foreach(hub->devices, link, device_t, it) {
342 if (it->port == port) {
343 victim = it;
344 break;
345 }
346 }
347 fibril_mutex_unlock(&hub->guard);
348
349 if (!victim)
350 return ENOENT;
351
352 assert(victim->fun);
353 assert(victim->port == port);
354 assert(victim->hub == hub);
355
356 bus_device_gone(victim);
357 return EOK;
358}
359
360device_t *hcd_ddf_fun_create(hc_device_t *hc)
361{
362 /* Create DDF function for the new device */
363 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
364 if (!fun)
365 return NULL;
366
367 ddf_fun_set_ops(fun, &usb_ops);
368
369 /* Create USB device node for the new device */
370 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
371 if (!dev) {
372 ddf_fun_destroy(fun);
373 return NULL;
374 }
375
376 bus_device_init(dev, hc->bus);
377 dev->fun = fun;
378 return dev;
379}
380
381void hcd_ddf_fun_destroy(device_t *dev)
382{
383 assert(dev);
384 assert(dev->fun);
385 ddf_fun_destroy(dev->fun);
386}
387
388int hcd_device_explore(device_t *device)
389{
390 int err;
391 match_id_list_t mids;
392 usb_standard_device_descriptor_t desc = { 0 };
393
394 init_match_ids(&mids);
395
396 const usb_target_t control_ep = {{
397 .address = device->address,
398 .endpoint = 0,
399 }};
400
401 /* Get std device descriptor */
402 const usb_device_request_setup_packet_t get_device_desc =
403 GET_DEVICE_DESC(sizeof(desc));
404
405 usb_log_debug("Device(%d): Requesting full device descriptor.",
406 device->address);
407 ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
408 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
409 "read device descriptor");
410 if (got < 0) {
411 err = got < 0 ? got : EOVERFLOW;
412 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
413 device->address, str_error(err));
414 goto out;
415 }
416
417 /* Create match ids from the device descriptor */
418 usb_log_debug("Device(%d): Creating match IDs.", device->address);
419 if ((err = create_match_ids(&mids, &desc))) {
420 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
421 goto out;
422 }
423
424 list_foreach(mids.ids, link, const match_id_t, mid) {
425 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
426 }
427
428out:
429 clean_match_ids(&mids);
430 return err;
431}
432
433static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
434{
435 int err;
436 assert(hcd);
437 assert(hcd->bus);
438 assert(hub);
439 assert(hc);
440
441 device_t *dev = hcd_ddf_fun_create(hcd);
442 if (!dev) {
443 usb_log_error("Failed to create USB device function.");
444 return ENOMEM;
445 }
446
447 dev->hub = hub;
448 dev->port = port;
449
450 if ((err = bus_device_enumerate(dev))) {
451 usb_log_error("Failed to initialize USB dev memory structures.");
452 return err;
453 }
454
455 /* If the driver didn't name the dev when enumerating,
456 * do it in some generic way.
457 */
458 if (!ddf_fun_get_name(dev->fun)) {
459 bus_device_set_default_name(dev);
460 }
461
462 if ((err = ddf_fun_bind(dev->fun))) {
463 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
464 goto err_usb_dev;
465 }
466
467 return EOK;
468
469err_usb_dev:
470 hcd_ddf_fun_destroy(dev);
471 return err;
472}
473
474/** Announce root hub to the DDF
475 *
476 * @param[in] device Host controller ddf device
477 * @return Error code
478 */
479int hcd_setup_virtual_root_hub(hc_device_t *hcd)
480{
481 int err;
482
483 assert(hcd);
484
485 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
486 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
487 return err;
488 }
489
490 device_t *dev = hcd_ddf_fun_create(hcd);
491 if (!dev) {
492 usb_log_error("Failed to create function for the root hub.");
493 goto err_default_address;
494 }
495
496 ddf_fun_set_name(dev->fun, "roothub");
497
498 /* Assign an address to the device */
499 if ((err = bus_device_enumerate(dev))) {
500 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
501 goto err_usb_dev;
502 }
503
504 if ((err = ddf_fun_bind(dev->fun))) {
505 usb_log_error("Failed to register roothub: %s.", str_error(err));
506 goto err_enumerated;
507 }
508
509 bus_release_default_address(hcd->bus);
510 return EOK;
511
512err_enumerated:
513 bus_device_gone(dev);
514err_usb_dev:
515 hcd_ddf_fun_destroy(dev);
516err_default_address:
517 bus_release_default_address(hcd->bus);
518 return err;
519}
520
521/** Initialize hc structures.
522 *
523 * @param[in] device DDF instance of the device to use.
524 * @param[in] max_speed Maximum supported USB speed.
525 * @param[in] bw available bandwidth.
526 * @param[in] bw_count Function to compute required ep bandwidth.
527 *
528 * @return Error code.
529 * This function does all the ddf work for hc driver.
530 */
531int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
532{
533 assert(device);
534
535 hc_device_t *instance = ddf_dev_data_alloc(device, size);
536 if (instance == NULL) {
537 usb_log_error("Failed to allocate HCD ddf structure.\n");
538 return ENOMEM;
539 }
540 instance->ddf_dev = device;
541
542 int ret = ENOMEM;
543 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
544 if (!instance->ctl_fun) {
545 usb_log_error("Failed to create HCD ddf fun.\n");
546 goto err_destroy_fun;
547 }
548
549 ret = ddf_fun_bind(instance->ctl_fun);
550 if (ret != EOK) {
551 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
552 goto err_destroy_fun;
553 }
554
555 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
556 if (ret != EOK) {
557 usb_log_error("Failed to add fun to category: %s.\n",
558 str_error(ret));
559 ddf_fun_unbind(instance->ctl_fun);
560 goto err_destroy_fun;
561 }
562
563 /* HC should be ok at this point (except it can't do anything) */
564 return EOK;
565
566err_destroy_fun:
567 ddf_fun_destroy(instance->ctl_fun);
568 instance->ctl_fun = NULL;
569 return ret;
570}
571
572void hcd_ddf_clean_hc(hc_device_t *hcd)
573{
574 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
575 ddf_fun_destroy(hcd->ctl_fun);
576}
577
578/** Call the parent driver with a request to enable interrupt
579 *
580 * @param[in] device Device asking for interrupts
581 * @param[in] inum Interrupt number
582 * @return Error code.
583 */
584int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
585{
586 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
587 if (parent_sess == NULL)
588 return EIO;
589
590 return hw_res_enable_interrupt(parent_sess, inum);
591}
592
593int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
594{
595 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
596 if (parent_sess == NULL)
597 return EIO;
598
599 hw_res_list_parsed_init(hw_res);
600 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
601 if (ret != EOK)
602 hw_res_list_parsed_clean(hw_res);
603 return ret;
604}
605
606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.