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

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

usb: speed moved from default address reservation to enumeration callback

  • Property mode set to 100644
File size: 14.8 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/**
57 * DDF usbhc_iface callback. Passes the endpoint descriptors, fills the pipe
58 * descriptor according to the contents of the endpoint.
59 *
60 * @param[in] fun DDF function of the device in question.
61 * @param[out] pipe_desc The pipe descriptor to be filled.
62 * @param[in] endpoint_desc Endpoint descriptors from the device.
63 * @return Error code.
64 */
65static int register_endpoint(ddf_fun_t *fun, usb_pipe_desc_t *pipe_desc,
66 const usb_endpoint_descriptors_t *ep_desc)
67{
68 assert(fun);
69 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
70 device_t *dev = ddf_fun_data_get(fun);
71 assert(hcd);
72 assert(hcd->bus);
73 assert(dev);
74
75 endpoint_t *ep;
76 const int err = bus_endpoint_add(dev, ep_desc, &ep);
77 if (err)
78 return err;
79
80 if (pipe_desc) {
81 pipe_desc->endpoint_no = ep->endpoint;
82 pipe_desc->direction = ep->direction;
83 pipe_desc->transfer_type = ep->transfer_type;
84 pipe_desc->max_transfer_size = ep->max_transfer_size;
85 }
86 endpoint_del_ref(ep);
87
88 return EOK;
89}
90
91 /**
92 * DDF usbhc_iface callback. Unregister endpoint that makes the other end of
93 * the pipe described.
94 *
95 * @param fun DDF function of the device in question.
96 * @param pipe_desc Pipe description.
97 * @return Error code.
98 */
99static int unregister_endpoint(ddf_fun_t *fun, const usb_pipe_desc_t *pipe_desc)
100{
101 assert(fun);
102 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
103 device_t *dev = ddf_fun_data_get(fun);
104 assert(hcd);
105 assert(hcd->bus);
106 assert(dev);
107
108 endpoint_t *ep = bus_find_endpoint(dev, pipe_desc->endpoint_no, pipe_desc->direction);
109 if (!ep)
110 return ENOENT;
111
112 return bus_endpoint_remove(ep);
113}
114
115/**
116 * DDF usbhc_iface callback. Calls the bus operation directly.
117 *
118 * @param fun DDF function of the device (hub) requesting the address.
119 */
120static int reserve_default_address(ddf_fun_t *fun)
121{
122 assert(fun);
123 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
124 device_t *dev = ddf_fun_data_get(fun);
125 assert(hcd);
126 assert(hcd->bus);
127 assert(dev);
128
129 usb_log_debug("Device %d requested default address", dev->address);
130 return bus_reserve_default_address(hcd->bus, dev);
131}
132
133/**
134 * DDF usbhc_iface callback. Calls the bus operation directly.
135 *
136 * @param fun DDF function of the device (hub) releasing the address.
137 */
138static int release_default_address(ddf_fun_t *fun)
139{
140 assert(fun);
141 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
142 device_t *dev = ddf_fun_data_get(fun);
143 assert(hcd);
144 assert(hcd->bus);
145 assert(dev);
146
147 usb_log_debug("Device %d released default address", dev->address);
148 bus_release_default_address(hcd->bus, dev);
149
150 return EOK;
151}
152
153/**
154 * DDF usbhc_iface callback. Calls the bus operation directly.
155 *
156 * @param fun DDF function of the device (hub) requesting the address.
157 * @param speed USB speed of the new device
158 */
159static int device_enumerate(ddf_fun_t *fun, unsigned port, usb_speed_t speed)
160{
161 assert(fun);
162 ddf_dev_t *hc = ddf_fun_get_dev(fun);
163 assert(hc);
164 hc_device_t *hcd = dev_to_hcd(hc);
165 assert(hcd);
166 device_t *hub = ddf_fun_data_get(fun);
167 assert(hub);
168
169 int err;
170
171 usb_log_debug("Hub %d reported a new %s device on port: %u",
172 hub->address, usb_str_speed(speed), port);
173
174 device_t *dev = hcd_ddf_fun_create(hcd, speed);
175 if (!dev) {
176 usb_log_error("Failed to create USB device function.");
177 return ENOMEM;
178 }
179
180 dev->hub = hub;
181 dev->port = port;
182 dev->speed = speed;
183
184 if ((err = bus_device_enumerate(dev))) {
185 usb_log_error("Failed to initialize USB dev memory structures.");
186 goto err_usb_dev;
187 }
188
189 /* If the driver didn't name the dev when enumerating,
190 * do it in some generic way.
191 */
192 if (!ddf_fun_get_name(dev->fun)) {
193 bus_device_set_default_name(dev);
194 }
195
196 if ((err = ddf_fun_bind(dev->fun))) {
197 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
198 goto err_usb_dev;
199 }
200
201 return EOK;
202
203err_usb_dev:
204 hcd_ddf_fun_destroy(dev);
205 return err;
206}
207
208static int device_remove(ddf_fun_t *fun, unsigned port)
209{
210 assert(fun);
211 device_t *hub = ddf_fun_data_get(fun);
212 assert(hub);
213 usb_log_debug("Hub `%s' reported removal of device on port %u",
214 ddf_fun_get_name(fun), port);
215
216 device_t *victim = NULL;
217
218 fibril_mutex_lock(&hub->guard);
219 list_foreach(hub->devices, link, device_t, it) {
220 if (it->port == port) {
221 victim = it;
222 break;
223 }
224 }
225 fibril_mutex_unlock(&hub->guard);
226
227 if (!victim) {
228 usb_log_warning("Hub '%s' tried to remove non-existant"
229 " device.", ddf_fun_get_name(fun));
230 return ENOENT;
231 }
232
233 assert(victim->fun);
234 assert(victim->port == port);
235 assert(victim->hub == hub);
236
237 bus_device_gone(victim);
238 return EOK;
239}
240
241/** Gets handle of the respective device.
242 *
243 * @param[in] fun Device function.
244 * @param[out] handle Place to write the handle.
245 * @return Error code.
246 */
247static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
248{
249 assert(fun);
250 if (handle)
251 *handle = ddf_fun_get_handle(fun);
252 return EOK;
253}
254
255/** Inbound communication interface function.
256 * @param fun DDF function.
257 * @param target Communication target.
258 * @param setup_data Data to use in setup stage (control transfers).
259 * @param data Pointer to data buffer.
260 * @param size Size of the data buffer.
261 * @param callback Function to call on communication end.
262 * @param arg Argument passed to the callback function.
263 * @return Error code.
264 */
265static int dev_read(ddf_fun_t *fun, usb_target_t target,
266 uint64_t setup_data, char *data, size_t size,
267 usbhc_iface_transfer_callback_t callback, void *arg)
268{
269 assert(fun);
270 device_t *dev = ddf_fun_data_get(fun);
271 assert(dev);
272
273 target.address = dev->address;
274
275 return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
276 data, size, setup_data,
277 callback, arg, "READ");
278}
279
280/** Outbound communication interface function.
281 * @param fun DDF function.
282 * @param target Communication target.
283 * @param setup_data Data to use in setup stage (control transfers).
284 * @param data Pointer to data buffer.
285 * @param size Size of the data buffer.
286 * @param callback Function to call on communication end.
287 * @param arg Argument passed to the callback function.
288 * @return Error code.
289 */
290static int dev_write(ddf_fun_t *fun, usb_target_t target,
291 uint64_t setup_data, const char *data, size_t size,
292 usbhc_iface_transfer_callback_t callback, void *arg)
293{
294 assert(fun);
295 device_t *dev = ddf_fun_data_get(fun);
296 assert(dev);
297
298 target.address = dev->address;
299
300 return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
301 (char *) data, size, setup_data,
302 callback, arg, "WRITE");
303}
304
305/** USB device interface */
306static usb_iface_t usb_iface = {
307 .get_my_device_handle = get_my_device_handle,
308};
309
310/** USB host controller interface */
311static usbhc_iface_t usbhc_iface = {
312 .reserve_default_address = reserve_default_address,
313 .release_default_address = release_default_address,
314
315 .device_enumerate = device_enumerate,
316 .device_remove = device_remove,
317
318 .register_endpoint = register_endpoint,
319 .unregister_endpoint = unregister_endpoint,
320
321 .read = dev_read,
322 .write = dev_write,
323};
324
325/** Standard USB device interface) */
326static ddf_dev_ops_t usb_ops = {
327 .interfaces[USB_DEV_IFACE] = &usb_iface,
328 .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
329};
330
331
332/* DDF HELPERS */
333
334#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
335do { \
336 match_id_t *mid = malloc(sizeof(match_id_t)); \
337 if (!mid) { \
338 clean_match_ids(list); \
339 return ENOMEM; \
340 } \
341 char *id = NULL; \
342 int ret = asprintf(&id, str, ##__VA_ARGS__); \
343 if (ret < 0) { \
344 clean_match_ids(list); \
345 free(mid); \
346 return ENOMEM; \
347 } \
348 mid->score = sc; \
349 mid->id = id; \
350 add_match_id(list, mid); \
351} while (0)
352
353/* This is a copy of lib/usbdev/src/recognise.c */
354static int create_match_ids(match_id_list_t *l,
355 usb_standard_device_descriptor_t *d)
356{
357 assert(l);
358 assert(d);
359
360 if (d->vendor_id != 0) {
361 /* First, with release number. */
362 ADD_MATCHID_OR_RETURN(l, 100,
363 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
364 d->vendor_id, d->product_id, (d->device_version >> 8),
365 (d->device_version & 0xff));
366
367 /* Next, without release number. */
368 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
369 d->vendor_id, d->product_id);
370 }
371
372 /* Class match id */
373 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
374 usb_str_class(d->device_class));
375
376 /* As a last resort, try fallback driver. */
377 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
378
379 return EOK;
380}
381
382device_t *hcd_ddf_fun_create(hc_device_t *hc, usb_speed_t speed)
383{
384 /* Create DDF function for the new device */
385 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
386 if (!fun)
387 return NULL;
388
389 ddf_fun_set_ops(fun, &usb_ops);
390
391 /* Create USB device node for the new device */
392 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
393 if (!dev) {
394 ddf_fun_destroy(fun);
395 return NULL;
396 }
397
398 bus_device_init(dev, hc->bus);
399 dev->fun = fun;
400 dev->speed = speed;
401 return dev;
402}
403
404void hcd_ddf_fun_destroy(device_t *dev)
405{
406 assert(dev);
407 assert(dev->fun);
408 ddf_fun_destroy(dev->fun);
409}
410
411int hcd_device_explore(device_t *device)
412{
413 int err;
414 match_id_list_t mids;
415 usb_standard_device_descriptor_t desc = { 0 };
416
417 init_match_ids(&mids);
418
419 const usb_target_t control_ep = {{
420 .address = device->address,
421 .endpoint = 0,
422 }};
423
424 /* Get std device descriptor */
425 const usb_device_request_setup_packet_t get_device_desc =
426 GET_DEVICE_DESC(sizeof(desc));
427
428 usb_log_debug("Device(%d): Requesting full device descriptor.",
429 device->address);
430 ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
431 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
432 "read device descriptor");
433 if (got < 0) {
434 err = got < 0 ? got : EOVERFLOW;
435 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
436 device->address, str_error(err));
437 goto out;
438 }
439
440 /* Create match ids from the device descriptor */
441 usb_log_debug("Device(%d): Creating match IDs.", device->address);
442 if ((err = create_match_ids(&mids, &desc))) {
443 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
444 goto out;
445 }
446
447 list_foreach(mids.ids, link, const match_id_t, mid) {
448 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
449 }
450
451out:
452 clean_match_ids(&mids);
453 return err;
454}
455
456/** Announce root hub to the DDF
457 *
458 * @param[in] device Host controller ddf device
459 * @return Error code
460 */
461int hcd_setup_virtual_root_hub(hc_device_t *hcd)
462{
463 int err;
464
465 assert(hcd);
466
467 device_t *dev = hcd_ddf_fun_create(hcd, USB_SPEED_MAX);
468 if (!dev) {
469 usb_log_error("Failed to create function for the root hub.");
470 return ENOMEM;
471 }
472
473 ddf_fun_set_name(dev->fun, "roothub");
474
475 /* Assign an address to the device */
476 if ((err = bus_device_enumerate(dev))) {
477 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
478 goto err_usb_dev;
479 }
480
481 if ((err = ddf_fun_bind(dev->fun))) {
482 usb_log_error("Failed to register roothub: %s.", str_error(err));
483 goto err_enumerated;
484 }
485
486 return EOK;
487
488err_enumerated:
489 bus_device_gone(dev);
490err_usb_dev:
491 hcd_ddf_fun_destroy(dev);
492 return err;
493}
494
495/** Initialize hc structures.
496 *
497 * @param[in] device DDF instance of the device to use.
498 * @param[in] max_speed Maximum supported USB speed.
499 * @param[in] bw available bandwidth.
500 * @param[in] bw_count Function to compute required ep bandwidth.
501 *
502 * @return Error code.
503 * This function does all the ddf work for hc driver.
504 */
505int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
506{
507 assert(device);
508
509 hc_device_t *instance = ddf_dev_data_alloc(device, size);
510 if (instance == NULL) {
511 usb_log_error("Failed to allocate HCD ddf structure.");
512 return ENOMEM;
513 }
514 instance->ddf_dev = device;
515
516 int ret = ENOMEM;
517 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
518 if (!instance->ctl_fun) {
519 usb_log_error("Failed to create HCD ddf fun.");
520 goto err_destroy_fun;
521 }
522
523 ret = ddf_fun_bind(instance->ctl_fun);
524 if (ret != EOK) {
525 usb_log_error("Failed to bind ctl_fun: %s.", str_error(ret));
526 goto err_destroy_fun;
527 }
528
529 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
530 if (ret != EOK) {
531 usb_log_error("Failed to add fun to category: %s.",
532 str_error(ret));
533 ddf_fun_unbind(instance->ctl_fun);
534 goto err_destroy_fun;
535 }
536
537 /* HC should be ok at this point (except it can't do anything) */
538 return EOK;
539
540err_destroy_fun:
541 ddf_fun_destroy(instance->ctl_fun);
542 instance->ctl_fun = NULL;
543 return ret;
544}
545
546void hcd_ddf_clean_hc(hc_device_t *hcd)
547{
548 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
549 ddf_fun_destroy(hcd->ctl_fun);
550}
551
552/** Call the parent driver with a request to enable interrupt
553 *
554 * @param[in] device Device asking for interrupts
555 * @param[in] inum Interrupt number
556 * @return Error code.
557 */
558int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
559{
560 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
561 if (parent_sess == NULL)
562 return EIO;
563
564 return hw_res_enable_interrupt(parent_sess, inum);
565}
566
567int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
568{
569 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
570 if (parent_sess == NULL)
571 return EIO;
572
573 hw_res_list_parsed_init(hw_res);
574 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
575 if (ret != EOK)
576 hw_res_list_parsed_clean(hw_res);
577 return ret;
578}
579
580/**
581 * @}
582 */
Note: See TracBrowser for help on using the repository browser.