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

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

libusbhost: manage (and report) depth of the device

  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[53332b5b]1/*
[d2cfe72]2 * Copyright (c) 2013 Jan Vesely
[53332b5b]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
[1102eca]33 * Helpers to work with the DDF interface.
[53332b5b]34 */
35
[8d2e251]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>
[53332b5b]42#include <errno.h>
43#include <str_error.h>
[64fea02]44#include <usb/classes/classes.h>
45#include <usb/debug.h>
46#include <usb/descriptor.h>
47#include <usb/usb.h>
[8d2e251]48#include <usb_iface.h>
[41df71f9]49#include <usbhc_iface.h>
[53332b5b]50
[64fea02]51#include "bus.h"
[9efad54]52#include "endpoint.h"
[64fea02]53
[53332b5b]54#include "ddf_helpers.h"
55
[1102eca]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.
[d2cfe72]63 * @return Error code.
64 */
[9efad54]65static int register_endpoint(ddf_fun_t *fun, usb_pipe_desc_t *pipe_desc,
66 const usb_endpoint_descriptors_t *ep_desc)
[d2cfe72]67{
68 assert(fun);
[32fb6bce]69 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]70 device_t *dev = ddf_fun_data_get(fun);
[d2cfe72]71 assert(hcd);
[20eaa82]72 assert(hcd->bus);
[d2cfe72]73 assert(dev);
74
[9efad54]75 endpoint_t *ep;
76 const int err = bus_endpoint_add(dev, ep_desc, &ep);
77 if (err)
78 return err;
[816f5f4]79
[9efad54]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;
[d2cfe72]89}
90
[1102eca]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.
[816f5f4]97 * @return Error code.
98 */
[1102eca]99static int unregister_endpoint(ddf_fun_t *fun, const usb_pipe_desc_t *pipe_desc)
[d2cfe72]100{
101 assert(fun);
[32fb6bce]102 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]103 device_t *dev = ddf_fun_data_get(fun);
[d2cfe72]104 assert(hcd);
[20eaa82]105 assert(hcd->bus);
[d2cfe72]106 assert(dev);
[816f5f4]107
[1ed3eb4]108 endpoint_t *ep = bus_find_endpoint(dev, pipe_desc->endpoint_no, pipe_desc->direction);
[0206d35]109 if (!ep)
110 return ENOENT;
111
[6832245]112 return bus_endpoint_remove(ep);
[d2cfe72]113}
114
[1102eca]115/**
[4603b35]116 * DDF usbhc_iface callback. Calls the respective bus operation directly.
[1102eca]117 *
118 * @param fun DDF function of the device (hub) requesting the address.
119 */
[4603b35]120static int default_address_reservation(ddf_fun_t *fun, bool reserve)
[56bd6f11]121{
122 assert(fun);
[32fb6bce]123 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]124 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]125 assert(hcd);
[20eaa82]126 assert(hcd->bus);
[56bd6f11]127 assert(dev);
128
[4603b35]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 }
[56bd6f11]136}
137
[1102eca]138/**
139 * DDF usbhc_iface callback. Calls the bus operation directly.
140 *
141 * @param fun DDF function of the device (hub) requesting the address.
[eeca8a6]142 * @param speed USB speed of the new device
[1102eca]143 */
[eeca8a6]144static int device_enumerate(ddf_fun_t *fun, unsigned port, usb_speed_t speed)
[56bd6f11]145{
146 assert(fun);
[867b375]147 ddf_dev_t *hc = ddf_fun_get_dev(fun);
148 assert(hc);
[32fb6bce]149 hc_device_t *hcd = dev_to_hcd(hc);
[867b375]150 assert(hcd);
[20eaa82]151 device_t *hub = ddf_fun_data_get(fun);
[867b375]152 assert(hub);
153
[c952abc4]154 int err;
155
[4603b35]156 usb_log_debug("Hub %d reported a new %s speed device on port: %u",
[eeca8a6]157 hub->address, usb_str_speed(speed), port);
[c952abc4]158
[eeca8a6]159 device_t *dev = hcd_ddf_fun_create(hcd, speed);
[c952abc4]160 if (!dev) {
161 usb_log_error("Failed to create USB device function.");
162 return ENOMEM;
163 }
164
165 dev->hub = hub;
[2aaba7e]166 dev->tier = hub->tier + 1;
[c952abc4]167 dev->port = port;
[eeca8a6]168 dev->speed = speed;
[c952abc4]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;
[56bd6f11]192}
193
[0918382f]194static int device_remove(ddf_fun_t *fun, unsigned port)
[56bd6f11]195{
196 assert(fun);
[c952abc4]197 device_t *hub = ddf_fun_data_get(fun);
198 assert(hub);
[a1732929]199 usb_log_debug("Hub `%s' reported removal of device on port %u",
[0918382f]200 ddf_fun_get_name(fun), port);
[c952abc4]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;
[56bd6f11]225}
226
[c280d7e]227/**
228 * Gets description of the device that is calling.
[8e4219ab]229 *
[3121b5f]230 * @param[in] fun Device function.
[c280d7e]231 * @param[out] desc Device descriptor to be filled.
[8e4219ab]232 * @return Error code.
233 */
[c280d7e]234static int get_device_description(ddf_fun_t *fun, usb_device_desc_t *desc)
[8e4219ab]235{
236 assert(fun);
[c280d7e]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,
[2aaba7e]245 .depth = dev->tier,
[c280d7e]246 .speed = dev->speed,
247 .handle = ddf_fun_get_handle(fun),
248 .iface = -1,
249 };
[8e4219ab]250 return EOK;
251}
252
[1845003]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 */
[327f147]263static int dev_read(ddf_fun_t *fun, usb_target_t target,
264 uint64_t setup_data, char *data, size_t size,
[41df71f9]265 usbhc_iface_transfer_callback_t callback, void *arg)
[1845003]266{
267 assert(fun);
[20eaa82]268 device_t *dev = ddf_fun_data_get(fun);
269 assert(dev);
[327f147]270
271 target.address = dev->address;
272
[32fb6bce]273 return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
[327f147]274 data, size, setup_data,
275 callback, arg, "READ");
[1845003]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 */
[327f147]288static int dev_write(ddf_fun_t *fun, usb_target_t target,
289 uint64_t setup_data, const char *data, size_t size,
[41df71f9]290 usbhc_iface_transfer_callback_t callback, void *arg)
[1845003]291{
292 assert(fun);
[20eaa82]293 device_t *dev = ddf_fun_data_get(fun);
294 assert(dev);
[327f147]295
296 target.address = dev->address;
297
[32fb6bce]298 return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
[327f147]299 (char *) data, size, setup_data,
[1845003]300 callback, arg, "WRITE");
301}
302
[2757247]303/** USB device interface */
[53332b5b]304static usb_iface_t usb_iface = {
[c280d7e]305 .get_my_description = get_device_description,
[41df71f9]306};
[8e4219ab]307
[41df71f9]308/** USB host controller interface */
309static usbhc_iface_t usbhc_iface = {
[4603b35]310 .default_address_reservation = default_address_reservation,
[4b8ecff]311
[56bd6f11]312 .device_enumerate = device_enumerate,
313 .device_remove = device_remove,
[4b8ecff]314
[d2cfe72]315 .register_endpoint = register_endpoint,
316 .unregister_endpoint = unregister_endpoint,
[4b8ecff]317
[1845003]318 .read = dev_read,
319 .write = dev_write,
[53332b5b]320};
[8e4219ab]321
[2757247]322/** Standard USB device interface) */
[53332b5b]323static ddf_dev_ops_t usb_ops = {
324 .interfaces[USB_DEV_IFACE] = &usb_iface,
[41df71f9]325 .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
[53332b5b]326};
327
[2757247]328
329/* DDF HELPERS */
330
[237df2f]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)
[b995183]349
[237df2f]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);
[42bc933]356
[237df2f]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));
[42bc933]363
[0ee999d]364 /* Next, without release number. */
[237df2f]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
[eeca8a6]379device_t *hcd_ddf_fun_create(hc_device_t *hc, usb_speed_t speed)
[867b375]380{
381 /* Create DDF function for the new device */
[32fb6bce]382 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
[867b375]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 */
[32fb6bce]389 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
[20eaa82]390 if (!dev) {
[867b375]391 ddf_fun_destroy(fun);
392 return NULL;
[237df2f]393 }
[2003739]394
[32fb6bce]395 bus_device_init(dev, hc->bus);
[20eaa82]396 dev->fun = fun;
[eeca8a6]397 dev->speed = speed;
[20eaa82]398 return dev;
[867b375]399}
400
[32fb6bce]401void hcd_ddf_fun_destroy(device_t *dev)
[867b375]402{
403 assert(dev);
404 assert(dev->fun);
405 ddf_fun_destroy(dev->fun);
406}
407
[944f8fdd]408int hcd_ddf_setup_match_ids(device_t *device, usb_standard_device_descriptor_t *desc)
[c7d5189]409{
410 int err;
411 match_id_list_t mids;
412
413 init_match_ids(&mids);
[b995183]414
[237df2f]415 /* Create match ids from the device descriptor */
[20eaa82]416 usb_log_debug("Device(%d): Creating match IDs.", device->address);
[c7d5189]417 if ((err = create_match_ids(&mids, desc))) {
418 return err;
[867b375]419 }
[237df2f]420
[867b375]421 list_foreach(mids.ids, link, const match_id_t, mid) {
[20eaa82]422 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
[237df2f]423 }
424
[c7d5189]425 return EOK;
426}
427
[53332b5b]428/** Initialize hc structures.
429 *
430 * @param[in] device DDF instance of the device to use.
[ce33c10]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.
[53332b5b]434 *
[ce33c10]435 * @return Error code.
[2b0929e]436 * This function does all the ddf work for hc driver.
[53332b5b]437 */
[32fb6bce]438int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
[53332b5b]439{
[e991937]440 assert(device);
[53332b5b]441
[32fb6bce]442 hc_device_t *instance = ddf_dev_data_alloc(device, size);
[53332b5b]443 if (instance == NULL) {
[a1732929]444 usb_log_error("Failed to allocate HCD ddf structure.");
[53332b5b]445 return ENOMEM;
446 }
[32fb6bce]447 instance->ddf_dev = device;
[53332b5b]448
[e991937]449 int ret = ENOMEM;
450 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
451 if (!instance->ctl_fun) {
[a1732929]452 usb_log_error("Failed to create HCD ddf fun.");
[cce3228]453 goto err_destroy_fun;
454 }
455
[e991937]456 ret = ddf_fun_bind(instance->ctl_fun);
[cce3228]457 if (ret != EOK) {
[a1732929]458 usb_log_error("Failed to bind ctl_fun: %s.", str_error(ret));
[cce3228]459 goto err_destroy_fun;
460 }
461
[e991937]462 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
[cce3228]463 if (ret != EOK) {
[a1732929]464 usb_log_error("Failed to add fun to category: %s.",
[cce3228]465 str_error(ret));
[e991937]466 ddf_fun_unbind(instance->ctl_fun);
[cce3228]467 goto err_destroy_fun;
468 }
[53332b5b]469
470 /* HC should be ok at this point (except it can't do anything) */
471 return EOK;
[cce3228]472
473err_destroy_fun:
[e991937]474 ddf_fun_destroy(instance->ctl_fun);
475 instance->ctl_fun = NULL;
[cce3228]476 return ret;
[53332b5b]477}
478
[32fb6bce]479void hcd_ddf_clean_hc(hc_device_t *hcd)
[e991937]480{
[32fb6bce]481 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
482 ddf_fun_destroy(hcd->ctl_fun);
[e991937]483}
[57c8fc9]484
[cccd60c3]485/** Call the parent driver with a request to enable interrupt
[57c8fc9]486 *
487 * @param[in] device Device asking for interrupts
[cccd60c3]488 * @param[in] inum Interrupt number
[57c8fc9]489 * @return Error code.
490 */
[32fb6bce]491int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
[57c8fc9]492{
[32fb6bce]493 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
[2bdf92a5]494 if (parent_sess == NULL)
495 return EIO;
[57c8fc9]496
[cccd60c3]497 return hw_res_enable_interrupt(parent_sess, inum);
[57c8fc9]498}
499
[32fb6bce]500int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
[57c8fc9]501{
[32fb6bce]502 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
[2bdf92a5]503 if (parent_sess == NULL)
504 return EIO;
[57c8fc9]505
506 hw_res_list_parsed_init(hw_res);
507 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
[c898236]508 if (ret != EOK)
509 hw_res_list_parsed_clean(hw_res);
[57c8fc9]510 return ret;
511}
[19d21728]512
[53332b5b]513/**
514 * @}
515 */
Note: See TracBrowser for help on using the repository browser.