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

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

libusbhost: move utility functions to new header utility.h

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