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

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

usb: rethinking DMA buffers

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