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

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

libusbhost: manage (and report) depth of the device

  • Property mode set to 100644
File size: 13.1 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 respective bus operation directly.
117 *
118 * @param fun DDF function of the device (hub) requesting the address.
119 */
120static int default_address_reservation(ddf_fun_t *fun, bool reserve)
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 %s default address", dev->address, reserve ? "requested" : "releasing");
130 if (reserve) {
131 return bus_reserve_default_address(hcd->bus, dev);
132 } else {
133 bus_release_default_address(hcd->bus, dev);
134 return EOK;
135 }
136}
137
138/**
139 * DDF usbhc_iface callback. Calls the bus operation directly.
140 *
141 * @param fun DDF function of the device (hub) requesting the address.
142 * @param speed USB speed of the new device
143 */
144static int device_enumerate(ddf_fun_t *fun, unsigned port, usb_speed_t speed)
145{
146 assert(fun);
147 ddf_dev_t *hc = ddf_fun_get_dev(fun);
148 assert(hc);
149 hc_device_t *hcd = dev_to_hcd(hc);
150 assert(hcd);
151 device_t *hub = ddf_fun_data_get(fun);
152 assert(hub);
153
154 int err;
155
156 usb_log_debug("Hub %d reported a new %s speed device on port: %u",
157 hub->address, usb_str_speed(speed), port);
158
159 device_t *dev = hcd_ddf_fun_create(hcd, speed);
160 if (!dev) {
161 usb_log_error("Failed to create USB device function.");
162 return ENOMEM;
163 }
164
165 dev->hub = hub;
166 dev->tier = hub->tier + 1;
167 dev->port = port;
168 dev->speed = speed;
169
170 if ((err = bus_device_enumerate(dev))) {
171 usb_log_error("Failed to initialize USB dev memory structures.");
172 goto err_usb_dev;
173 }
174
175 /* If the driver didn't name the dev when enumerating,
176 * do it in some generic way.
177 */
178 if (!ddf_fun_get_name(dev->fun)) {
179 bus_device_set_default_name(dev);
180 }
181
182 if ((err = ddf_fun_bind(dev->fun))) {
183 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
184 goto err_usb_dev;
185 }
186
187 return EOK;
188
189err_usb_dev:
190 hcd_ddf_fun_destroy(dev);
191 return err;
192}
193
194static int device_remove(ddf_fun_t *fun, unsigned port)
195{
196 assert(fun);
197 device_t *hub = ddf_fun_data_get(fun);
198 assert(hub);
199 usb_log_debug("Hub `%s' reported removal of device on port %u",
200 ddf_fun_get_name(fun), port);
201
202 device_t *victim = NULL;
203
204 fibril_mutex_lock(&hub->guard);
205 list_foreach(hub->devices, link, device_t, it) {
206 if (it->port == port) {
207 victim = it;
208 break;
209 }
210 }
211 fibril_mutex_unlock(&hub->guard);
212
213 if (!victim) {
214 usb_log_warning("Hub '%s' tried to remove non-existant"
215 " device.", ddf_fun_get_name(fun));
216 return ENOENT;
217 }
218
219 assert(victim->fun);
220 assert(victim->port == port);
221 assert(victim->hub == hub);
222
223 bus_device_gone(victim);
224 return EOK;
225}
226
227/**
228 * Gets description of the device that is calling.
229 *
230 * @param[in] fun Device function.
231 * @param[out] desc Device descriptor to be filled.
232 * @return Error code.
233 */
234static int get_device_description(ddf_fun_t *fun, usb_device_desc_t *desc)
235{
236 assert(fun);
237 device_t *dev = ddf_fun_data_get(fun);
238 assert(dev);
239
240 if (!desc)
241 return EOK;
242
243 *desc = (usb_device_desc_t) {
244 .address = dev->address,
245 .depth = dev->tier,
246 .speed = dev->speed,
247 .handle = ddf_fun_get_handle(fun),
248 .iface = -1,
249 };
250 return EOK;
251}
252
253/** Inbound communication interface function.
254 * @param fun DDF function.
255 * @param target Communication target.
256 * @param setup_data Data to use in setup stage (control transfers).
257 * @param data Pointer to data buffer.
258 * @param size Size of the data buffer.
259 * @param callback Function to call on communication end.
260 * @param arg Argument passed to the callback function.
261 * @return Error code.
262 */
263static int dev_read(ddf_fun_t *fun, usb_target_t target,
264 uint64_t setup_data, char *data, size_t size,
265 usbhc_iface_transfer_callback_t callback, void *arg)
266{
267 assert(fun);
268 device_t *dev = ddf_fun_data_get(fun);
269 assert(dev);
270
271 target.address = dev->address;
272
273 return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
274 data, size, setup_data,
275 callback, arg, "READ");
276}
277
278/** Outbound communication interface function.
279 * @param fun DDF function.
280 * @param target Communication target.
281 * @param setup_data Data to use in setup stage (control transfers).
282 * @param data Pointer to data buffer.
283 * @param size Size of the data buffer.
284 * @param callback Function to call on communication end.
285 * @param arg Argument passed to the callback function.
286 * @return Error code.
287 */
288static int dev_write(ddf_fun_t *fun, usb_target_t target,
289 uint64_t setup_data, const char *data, size_t size,
290 usbhc_iface_transfer_callback_t callback, void *arg)
291{
292 assert(fun);
293 device_t *dev = ddf_fun_data_get(fun);
294 assert(dev);
295
296 target.address = dev->address;
297
298 return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
299 (char *) data, size, setup_data,
300 callback, arg, "WRITE");
301}
302
303/** USB device interface */
304static usb_iface_t usb_iface = {
305 .get_my_description = get_device_description,
306};
307
308/** USB host controller interface */
309static usbhc_iface_t usbhc_iface = {
310 .default_address_reservation = default_address_reservation,
311
312 .device_enumerate = device_enumerate,
313 .device_remove = device_remove,
314
315 .register_endpoint = register_endpoint,
316 .unregister_endpoint = unregister_endpoint,
317
318 .read = dev_read,
319 .write = dev_write,
320};
321
322/** Standard USB device interface) */
323static ddf_dev_ops_t usb_ops = {
324 .interfaces[USB_DEV_IFACE] = &usb_iface,
325 .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
326};
327
328
329/* DDF HELPERS */
330
331#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
332do { \
333 match_id_t *mid = malloc(sizeof(match_id_t)); \
334 if (!mid) { \
335 clean_match_ids(list); \
336 return ENOMEM; \
337 } \
338 char *id = NULL; \
339 int ret = asprintf(&id, str, ##__VA_ARGS__); \
340 if (ret < 0) { \
341 clean_match_ids(list); \
342 free(mid); \
343 return ENOMEM; \
344 } \
345 mid->score = sc; \
346 mid->id = id; \
347 add_match_id(list, mid); \
348} while (0)
349
350/* This is a copy of lib/usbdev/src/recognise.c */
351static int create_match_ids(match_id_list_t *l,
352 usb_standard_device_descriptor_t *d)
353{
354 assert(l);
355 assert(d);
356
357 if (d->vendor_id != 0) {
358 /* First, with release number. */
359 ADD_MATCHID_OR_RETURN(l, 100,
360 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
361 d->vendor_id, d->product_id, (d->device_version >> 8),
362 (d->device_version & 0xff));
363
364 /* Next, without release number. */
365 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
366 d->vendor_id, d->product_id);
367 }
368
369 /* Class match id */
370 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
371 usb_str_class(d->device_class));
372
373 /* As a last resort, try fallback driver. */
374 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
375
376 return EOK;
377}
378
379device_t *hcd_ddf_fun_create(hc_device_t *hc, usb_speed_t speed)
380{
381 /* Create DDF function for the new device */
382 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
383 if (!fun)
384 return NULL;
385
386 ddf_fun_set_ops(fun, &usb_ops);
387
388 /* Create USB device node for the new device */
389 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
390 if (!dev) {
391 ddf_fun_destroy(fun);
392 return NULL;
393 }
394
395 bus_device_init(dev, hc->bus);
396 dev->fun = fun;
397 dev->speed = speed;
398 return dev;
399}
400
401void hcd_ddf_fun_destroy(device_t *dev)
402{
403 assert(dev);
404 assert(dev->fun);
405 ddf_fun_destroy(dev->fun);
406}
407
408int hcd_ddf_setup_match_ids(device_t *device, usb_standard_device_descriptor_t *desc)
409{
410 int err;
411 match_id_list_t mids;
412
413 init_match_ids(&mids);
414
415 /* Create match ids from the device descriptor */
416 usb_log_debug("Device(%d): Creating match IDs.", device->address);
417 if ((err = create_match_ids(&mids, desc))) {
418 return err;
419 }
420
421 list_foreach(mids.ids, link, const match_id_t, mid) {
422 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
423 }
424
425 return EOK;
426}
427
428/** Initialize hc structures.
429 *
430 * @param[in] device DDF instance of the device to use.
431 * @param[in] max_speed Maximum supported USB speed.
432 * @param[in] bw available bandwidth.
433 * @param[in] bw_count Function to compute required ep bandwidth.
434 *
435 * @return Error code.
436 * This function does all the ddf work for hc driver.
437 */
438int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
439{
440 assert(device);
441
442 hc_device_t *instance = ddf_dev_data_alloc(device, size);
443 if (instance == NULL) {
444 usb_log_error("Failed to allocate HCD ddf structure.");
445 return ENOMEM;
446 }
447 instance->ddf_dev = device;
448
449 int ret = ENOMEM;
450 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
451 if (!instance->ctl_fun) {
452 usb_log_error("Failed to create HCD ddf fun.");
453 goto err_destroy_fun;
454 }
455
456 ret = ddf_fun_bind(instance->ctl_fun);
457 if (ret != EOK) {
458 usb_log_error("Failed to bind ctl_fun: %s.", str_error(ret));
459 goto err_destroy_fun;
460 }
461
462 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
463 if (ret != EOK) {
464 usb_log_error("Failed to add fun to category: %s.",
465 str_error(ret));
466 ddf_fun_unbind(instance->ctl_fun);
467 goto err_destroy_fun;
468 }
469
470 /* HC should be ok at this point (except it can't do anything) */
471 return EOK;
472
473err_destroy_fun:
474 ddf_fun_destroy(instance->ctl_fun);
475 instance->ctl_fun = NULL;
476 return ret;
477}
478
479void hcd_ddf_clean_hc(hc_device_t *hcd)
480{
481 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
482 ddf_fun_destroy(hcd->ctl_fun);
483}
484
485/** Call the parent driver with a request to enable interrupt
486 *
487 * @param[in] device Device asking for interrupts
488 * @param[in] inum Interrupt number
489 * @return Error code.
490 */
491int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
492{
493 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
494 if (parent_sess == NULL)
495 return EIO;
496
497 return hw_res_enable_interrupt(parent_sess, inum);
498}
499
500int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
501{
502 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
503 if (parent_sess == NULL)
504 return EIO;
505
506 hw_res_list_parsed_init(hw_res);
507 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
508 if (ret != EOK)
509 hw_res_list_parsed_clean(hw_res);
510 return ret;
511}
512
513/**
514 * @}
515 */
Note: See TracBrowser for help on using the repository browser.