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

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

usbhost ddf: inlined hcd_ddf_new_device and hcd_ddf_remove_device

These are now short and unused anywhere else.

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