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

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

usb pipes: allocate with policy

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