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

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

usbhost: provide usb_endpoint_desc_t to bus when registering endpoint

This finishes the path of arbitrary information fetched from device all the way down to registering the endpoint in the bus.

  • Property mode set to 100644
File size: 22.0 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
33 *
34 */
35
[237df2f]36#include <usb/classes/classes.h>
[20eaa82]37#include <usb/host/bus.h>
[53332b5b]38#include <usb/debug.h>
[237df2f]39#include <usb/descriptor.h>
40#include <usb/request.h>
[8d2e251]41#include <usb/usb.h>
42
43#include <adt/list.h>
44#include <assert.h>
45#include <async.h>
46#include <ddf/driver.h>
47#include <ddf/interrupt.h>
48#include <device/hw_res_parsed.h>
[53332b5b]49#include <errno.h>
[8d2e251]50#include <fibril_synch.h>
[8d7552c]51#include <macros.h>
[8d2e251]52#include <stdlib.h>
[53332b5b]53#include <str_error.h>
[8d2e251]54#include <usb_iface.h>
[53332b5b]55
56#include "ddf_helpers.h"
57
[14dd4c9]58typedef struct hc_dev {
59 ddf_fun_t *ctl_fun;
[e991937]60 hcd_t hcd;
[53332b5b]61} hc_dev_t;
62
63static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
64{
65 return ddf_dev_data_get(dev);
66}
67
68hcd_t *dev_to_hcd(ddf_dev_t *dev)
69{
70 hc_dev_t *hc_dev = dev_to_hc_dev(dev);
[e991937]71 if (!hc_dev) {
[2b0929e]72 usb_log_error("Invalid HCD device.\n");
[53332b5b]73 return NULL;
74 }
[e991937]75 return &hc_dev->hcd;
[53332b5b]76}
77
78
[20eaa82]79static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
80static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
[2757247]81
82
83/* DDF INTERFACE */
84
[d2cfe72]85/** Register endpoint interface function.
86 * @param fun DDF function.
[816f5f4]87 * @param endpoint_desc Endpoint description.
[d2cfe72]88 * @return Error code.
89 */
90static int register_endpoint(
[816f5f4]91 ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
[d2cfe72]92{
93 assert(fun);
94 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]95 device_t *dev = ddf_fun_data_get(fun);
[d2cfe72]96 assert(hcd);
[20eaa82]97 assert(hcd->bus);
[d2cfe72]98 assert(dev);
99
[816f5f4]100 usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
101 dev->address, endpoint_desc->endpoint_no,
102 usb_str_transfer_type(endpoint_desc->transfer_type),
103 usb_str_direction(endpoint_desc->direction),
104 endpoint_desc->max_packet_size, endpoint_desc->usb2.polling_interval);
105
[56db65d]106 return bus_add_ep(hcd->bus, dev, endpoint_desc);
[d2cfe72]107}
108
[816f5f4]109 /** Unregister endpoint interface function.
110 * @param fun DDF function.
111 * @param endpoint_desc Endpoint description.
112 * @return Error code.
113 */
[d2cfe72]114static int unregister_endpoint(
[816f5f4]115 ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
[d2cfe72]116{
117 assert(fun);
118 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]119 device_t *dev = ddf_fun_data_get(fun);
[d2cfe72]120 assert(hcd);
[20eaa82]121 assert(hcd->bus);
[d2cfe72]122 assert(dev);
[816f5f4]123
[20eaa82]124 const usb_target_t target = {{
125 .address = dev->address,
[816f5f4]126 .endpoint = endpoint_desc->endpoint_no
[20eaa82]127 }};
[816f5f4]128
[d2cfe72]129 usb_log_debug("Unregister endpoint %d:%d %s.\n",
[816f5f4]130 dev->address, endpoint_desc->endpoint_no,
131 usb_str_direction(endpoint_desc->direction));
[327f147]132 return bus_remove_ep(hcd->bus, dev, target, endpoint_desc->direction);
[d2cfe72]133}
134
[56bd6f11]135static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
136{
137 assert(fun);
138 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]139 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]140 assert(hcd);
[20eaa82]141 assert(hcd->bus);
[56bd6f11]142 assert(dev);
143
144 usb_log_debug("Device %d requested default address at %s speed\n",
145 dev->address, usb_str_speed(speed));
[20eaa82]146 return bus_reserve_default_address(hcd->bus, speed);
[56bd6f11]147}
148
149static int release_default_address(ddf_fun_t *fun)
150{
151 assert(fun);
152 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]153 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]154 assert(hcd);
[20eaa82]155 assert(hcd->bus);
[56bd6f11]156 assert(dev);
157
158 usb_log_debug("Device %d released default address\n", dev->address);
[20eaa82]159 return bus_release_default_address(hcd->bus);
[56bd6f11]160}
161
[0918382f]162static int device_enumerate(ddf_fun_t *fun, unsigned port)
[56bd6f11]163{
164 assert(fun);
[867b375]165 ddf_dev_t *hc = ddf_fun_get_dev(fun);
166 assert(hc);
167 hcd_t *hcd = dev_to_hcd(hc);
168 assert(hcd);
[20eaa82]169 device_t *hub = ddf_fun_data_get(fun);
[867b375]170 assert(hub);
171
[0918382f]172 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
[867b375]173 hub->address, port);
174 return hcd_ddf_new_device(hcd, hc, hub, port);
[56bd6f11]175}
176
[0918382f]177static int device_remove(ddf_fun_t *fun, unsigned port)
[56bd6f11]178{
179 assert(fun);
180 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
[20eaa82]181 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]182 assert(ddf_dev);
183 assert(dev);
[0918382f]184 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
185 ddf_fun_get_name(fun), port);
186 return hcd_ddf_remove_device(ddf_dev, dev, port);
[56bd6f11]187}
188
[3121b5f]189/** Gets handle of the respective device.
[8e4219ab]190 *
[3121b5f]191 * @param[in] fun Device function.
[8e4219ab]192 * @param[out] handle Place to write the handle.
193 * @return Error code.
194 */
[3121b5f]195static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
[8e4219ab]196{
197 assert(fun);
198 if (handle)
199 *handle = ddf_fun_get_handle(fun);
200 return EOK;
201}
202
[1845003]203/** Inbound communication interface function.
204 * @param fun DDF function.
205 * @param target Communication target.
206 * @param setup_data Data to use in setup stage (control transfers).
207 * @param data Pointer to data buffer.
208 * @param size Size of the data buffer.
209 * @param callback Function to call on communication end.
210 * @param arg Argument passed to the callback function.
211 * @return Error code.
212 */
[327f147]213static int dev_read(ddf_fun_t *fun, usb_target_t target,
214 uint64_t setup_data, char *data, size_t size,
215 usb_iface_transfer_callback_t callback, void *arg)
[1845003]216{
217 assert(fun);
[327f147]218 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]219 device_t *dev = ddf_fun_data_get(fun);
220 assert(dev);
[327f147]221
222 target.address = dev->address;
223
224 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
225 data, size, setup_data,
226 callback, arg, "READ");
[1845003]227}
228
229/** Outbound communication interface function.
230 * @param fun DDF function.
231 * @param target Communication target.
232 * @param setup_data Data to use in setup stage (control transfers).
233 * @param data Pointer to data buffer.
234 * @param size Size of the data buffer.
235 * @param callback Function to call on communication end.
236 * @param arg Argument passed to the callback function.
237 * @return Error code.
238 */
[327f147]239static int dev_write(ddf_fun_t *fun, usb_target_t target,
240 uint64_t setup_data, const char *data, size_t size,
241 usb_iface_transfer_callback_t callback, void *arg)
[1845003]242{
243 assert(fun);
[327f147]244 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]245 device_t *dev = ddf_fun_data_get(fun);
246 assert(dev);
[327f147]247
248 target.address = dev->address;
249
250 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
251 (char *) data, size, setup_data,
[1845003]252 callback, arg, "WRITE");
253}
254
[2757247]255/** USB device interface */
[53332b5b]256static usb_iface_t usb_iface = {
[3121b5f]257 .get_my_device_handle = get_my_device_handle,
[8e4219ab]258
[56bd6f11]259 .reserve_default_address = reserve_default_address,
260 .release_default_address = release_default_address,
[4b8ecff]261
[56bd6f11]262 .device_enumerate = device_enumerate,
263 .device_remove = device_remove,
[4b8ecff]264
[d2cfe72]265 .register_endpoint = register_endpoint,
266 .unregister_endpoint = unregister_endpoint,
[4b8ecff]267
[1845003]268 .read = dev_read,
269 .write = dev_write,
[53332b5b]270};
[8e4219ab]271
[2757247]272/** Standard USB device interface) */
[53332b5b]273static ddf_dev_ops_t usb_ops = {
274 .interfaces[USB_DEV_IFACE] = &usb_iface,
275};
276
[2757247]277
278/* DDF HELPERS */
279
[237df2f]280#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
281do { \
282 match_id_t *mid = malloc(sizeof(match_id_t)); \
283 if (!mid) { \
284 clean_match_ids(list); \
285 return ENOMEM; \
286 } \
287 char *id = NULL; \
288 int ret = asprintf(&id, str, ##__VA_ARGS__); \
289 if (ret < 0) { \
290 clean_match_ids(list); \
291 free(mid); \
292 return ENOMEM; \
293 } \
294 mid->score = sc; \
295 mid->id = id; \
296 add_match_id(list, mid); \
297} while (0)
[b995183]298
[237df2f]299/* This is a copy of lib/usbdev/src/recognise.c */
300static int create_match_ids(match_id_list_t *l,
301 usb_standard_device_descriptor_t *d)
302{
303 assert(l);
304 assert(d);
[42bc933]305
[237df2f]306 if (d->vendor_id != 0) {
307 /* First, with release number. */
308 ADD_MATCHID_OR_RETURN(l, 100,
309 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
310 d->vendor_id, d->product_id, (d->device_version >> 8),
311 (d->device_version & 0xff));
[42bc933]312
[0ee999d]313 /* Next, without release number. */
[237df2f]314 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
315 d->vendor_id, d->product_id);
316 }
317
318 /* Class match id */
319 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
320 usb_str_class(d->device_class));
321
322 /* As a last resort, try fallback driver. */
323 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
324
325 return EOK;
326}
327
[20eaa82]328static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
[0918382f]329 unsigned port)
[237df2f]330{
331 assert(device);
332
333 hcd_t *hcd = dev_to_hcd(device);
334 assert(hcd);
[20eaa82]335 assert(hcd->bus);
[b995183]336
337 hc_dev_t *hc_dev = dev_to_hc_dev(device);
338 assert(hc_dev);
339
[14dd4c9]340 fibril_mutex_lock(&hub->guard);
[b995183]341
[20eaa82]342 device_t *victim = NULL;
[b995183]343
[20eaa82]344 list_foreach(hub->devices, link, device_t, it) {
345 if (it->port == port) {
[3f03199]346 victim = it;
[b995183]347 break;
[3f03199]348 }
[b995183]349 }
[9ff59981]350 if (victim) {
[867b375]351 assert(victim->fun);
[20eaa82]352 assert(victim->port == port);
353 assert(victim->hub == hub);
[b995183]354 list_remove(&victim->link);
[14dd4c9]355 fibril_mutex_unlock(&hub->guard);
[e6becb9]356 const int ret = ddf_fun_unbind(victim->fun);
357 if (ret == EOK) {
[e657635]358 usb_address_t address = victim->address;
[20eaa82]359 bus_remove_device(hcd->bus, hcd, victim);
[e6becb9]360 ddf_fun_destroy(victim->fun);
[20eaa82]361 bus_release_address(hcd->bus, address);
[e6becb9]362 } else {
[0918382f]363 usb_log_warning("Failed to unbind device `%s': %s\n",
364 ddf_fun_get_name(victim->fun), str_error(ret));
[e6becb9]365 }
[b995183]366 return EOK;
367 }
[9ff59981]368 fibril_mutex_unlock(&hub->guard);
[b995183]369 return ENOENT;
370}
371
[20eaa82]372device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
[867b375]373{
374 /* Create DDF function for the new device */
375 ddf_fun_t *fun = ddf_fun_create(hc, fun_inner, NULL);
376 if (!fun)
377 return NULL;
378
379 ddf_fun_set_ops(fun, &usb_ops);
380
381 /* Create USB device node for the new device */
[20eaa82]382 device_t *dev = ddf_fun_data_alloc(fun, device_size);
383 if (!dev) {
[867b375]384 ddf_fun_destroy(fun);
385 return NULL;
[237df2f]386 }
[2003739]387
[20eaa82]388 device_init(dev);
389 dev->fun = fun;
390 return dev;
[867b375]391}
392
[20eaa82]393void hcd_ddf_device_destroy(device_t *dev)
[867b375]394{
395 assert(dev);
396 assert(dev->fun);
397 ddf_fun_destroy(dev->fun);
398}
399
[20eaa82]400int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
[867b375]401{
402 int err;
403 match_id_list_t mids;
404 usb_standard_device_descriptor_t desc = { 0 };
405
406 init_match_ids(&mids);
407
[327f147]408 const usb_target_t control_ep = {{
[20eaa82]409 .address = device->address,
[327f147]410 .endpoint = 0,
[20eaa82]411 }};
[867b375]412
[237df2f]413 /* Get std device descriptor */
[2003739]414 const usb_device_request_setup_packet_t get_device_desc =
[237df2f]415 GET_DEVICE_DESC(sizeof(desc));
416
[f29643d5]417 usb_log_debug("Device(%d): Requesting full device descriptor.",
[20eaa82]418 device->address);
[327f147]419 ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
420 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
[237df2f]421 "read device descriptor");
[867b375]422 if (got < 0) {
423 err = got < 0 ? got : EOVERFLOW;
[f29643d5]424 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
[20eaa82]425 device->address, str_error(err));
[867b375]426 goto out;
[237df2f]427 }
[b995183]428
[237df2f]429 /* Create match ids from the device descriptor */
[20eaa82]430 usb_log_debug("Device(%d): Creating match IDs.", device->address);
[867b375]431 if ((err = create_match_ids(&mids, &desc))) {
[20eaa82]432 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
[867b375]433 goto out;
434 }
[237df2f]435
[867b375]436 list_foreach(mids.ids, link, const match_id_t, mid) {
[20eaa82]437 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
[237df2f]438 }
439
[867b375]440out:
[237df2f]441 clean_match_ids(&mids);
[867b375]442 return err;
443}
444
[20eaa82]445static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
[867b375]446{
447 int err;
448 assert(hcd);
[20eaa82]449 assert(hcd->bus);
450 assert(hub);
[867b375]451 assert(hc);
452
[20eaa82]453 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
454 if (!dev) {
[867b375]455 usb_log_error("Failed to create USB device function.");
[20eaa82]456 return ENOMEM;
[867b375]457 }
458
[20eaa82]459 dev->hub = hub;
460 dev->port = port;
[867b375]461
[20eaa82]462 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
463 usb_log_error("Failed to initialize USB dev memory structures.");
464 return err;
[867b375]465 }
466
[20eaa82]467 /* If the driver didn't name the dev when enumerating,
468 * do it in some generic way.
[867b375]469 */
[20eaa82]470 if (!ddf_fun_get_name(dev->fun)) {
471 device_set_default_name(dev);
[867b375]472 }
473
[20eaa82]474 if ((err = ddf_fun_bind(dev->fun))) {
475 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
[867b375]476 goto err_usb_dev;
477 }
478
[20eaa82]479 fibril_mutex_lock(&hub->guard);
480 list_append(&dev->link, &hub->devices);
481 fibril_mutex_unlock(&hub->guard);
[867b375]482
483 return EOK;
484
485err_usb_dev:
[20eaa82]486 hcd_ddf_device_destroy(dev);
[867b375]487 return err;
[237df2f]488}
489
490/** Announce root hub to the DDF
491 *
492 * @param[in] device Host controller ddf device
493 * @return Error code
494 */
[867b375]495int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
[237df2f]496{
[867b375]497 int err;
498
499 assert(hc);
[237df2f]500 assert(hcd);
[20eaa82]501 assert(hcd->bus);
[237df2f]502
[20eaa82]503 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
[867b375]504 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
505 return err;
506 }
507
[20eaa82]508 device_t *dev = hcd_ddf_device_create(hc, USB_SPEED_MAX);
509 if (!dev) {
[867b375]510 usb_log_error("Failed to create function for the root hub.");
511 goto err_default_address;
512 }
513
[20eaa82]514 ddf_fun_set_name(dev->fun, "roothub");
[867b375]515
[20eaa82]516 dev->tt = (usb_tt_address_t) {
517 .address = -1,
518 .port = 0,
519 };
[867b375]520
[20eaa82]521 /* Assign an address to the device */
522 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
523 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
[867b375]524 goto err_usb_dev;
525 }
526
[20eaa82]527 if ((err = ddf_fun_bind(dev->fun))) {
[867b375]528 usb_log_error("Failed to register roothub: %s.", str_error(err));
529 goto err_usb_dev;
530 }
531
[20eaa82]532 bus_release_default_address(hcd->bus);
[867b375]533 return EOK;
534
535err_usb_dev:
[20eaa82]536 hcd_ddf_device_destroy(dev);
[867b375]537err_default_address:
[20eaa82]538 bus_release_default_address(hcd->bus);
[867b375]539 return err;
[237df2f]540}
541
[53332b5b]542/** Initialize hc structures.
543 *
544 * @param[in] device DDF instance of the device to use.
[ce33c10]545 * @param[in] max_speed Maximum supported USB speed.
546 * @param[in] bw available bandwidth.
547 * @param[in] bw_count Function to compute required ep bandwidth.
[53332b5b]548 *
[ce33c10]549 * @return Error code.
[2b0929e]550 * This function does all the ddf work for hc driver.
[53332b5b]551 */
[41924f30]552int hcd_ddf_setup_hc(ddf_dev_t *device)
[53332b5b]553{
[e991937]554 assert(device);
[53332b5b]555
556 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
557 if (instance == NULL) {
[2b0929e]558 usb_log_error("Failed to allocate HCD ddf structure.\n");
[53332b5b]559 return ENOMEM;
560 }
[41924f30]561 hcd_init(&instance->hcd);
[53332b5b]562
[e991937]563 int ret = ENOMEM;
564 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
565 if (!instance->ctl_fun) {
[cce3228]566 usb_log_error("Failed to create HCD ddf fun.\n");
567 goto err_destroy_fun;
568 }
569
[e991937]570 ret = ddf_fun_bind(instance->ctl_fun);
[cce3228]571 if (ret != EOK) {
[e991937]572 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
[cce3228]573 goto err_destroy_fun;
574 }
575
[e991937]576 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
[cce3228]577 if (ret != EOK) {
578 usb_log_error("Failed to add fun to category: %s.\n",
579 str_error(ret));
[e991937]580 ddf_fun_unbind(instance->ctl_fun);
[cce3228]581 goto err_destroy_fun;
582 }
[53332b5b]583
584 /* HC should be ok at this point (except it can't do anything) */
585 return EOK;
[cce3228]586
587err_destroy_fun:
[e991937]588 ddf_fun_destroy(instance->ctl_fun);
589 instance->ctl_fun = NULL;
[cce3228]590 return ret;
[53332b5b]591}
592
[e991937]593void hcd_ddf_clean_hc(ddf_dev_t *device)
594{
595 assert(device);
596 hc_dev_t *hc = dev_to_hc_dev(device);
597 assert(hc);
598 const int ret = ddf_fun_unbind(hc->ctl_fun);
599 if (ret == EOK)
600 ddf_fun_destroy(hc->ctl_fun);
601}
[57c8fc9]602
[2bdf92a5]603//TODO: Cache parent session in HCD
[cccd60c3]604/** Call the parent driver with a request to enable interrupt
[57c8fc9]605 *
606 * @param[in] device Device asking for interrupts
[cccd60c3]607 * @param[in] inum Interrupt number
[57c8fc9]608 * @return Error code.
609 */
[cccd60c3]610int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
[57c8fc9]611{
[2bdf92a5]612 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
613 if (parent_sess == NULL)
614 return EIO;
[57c8fc9]615
[cccd60c3]616 return hw_res_enable_interrupt(parent_sess, inum);
[57c8fc9]617}
618
[2bdf92a5]619//TODO: Cache parent session in HCD
[57c8fc9]620int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
621{
[2bdf92a5]622 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
623 if (parent_sess == NULL)
624 return EIO;
[57c8fc9]625
626 hw_res_list_parsed_init(hw_res);
627 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
[c898236]628 if (ret != EOK)
629 hw_res_list_parsed_clean(hw_res);
[57c8fc9]630 return ret;
631}
[19d21728]632
633// TODO: move this someplace else
634static inline void irq_code_clean(irq_code_t *code)
635{
636 if (code) {
637 free(code->ranges);
638 free(code->cmds);
639 code->ranges = NULL;
640 code->cmds = NULL;
641 code->rangecount = 0;
642 code->cmdcount = 0;
643 }
644}
645
646/** Register interrupt handler
647 *
648 * @param[in] device Host controller DDF device
649 * @param[in] regs Register range
650 * @param[in] irq Interrupt number
651 * @paran[in] handler Interrupt handler
652 * @param[in] gen_irq_code IRQ code generator.
653 *
[3f74275]654 * @return IRQ capability handle on success.
[e9d15d9]655 * @return Negative error code.
[19d21728]656 */
[ba4a03a5]657int hcd_ddf_setup_interrupts(ddf_dev_t *device,
658 const hw_res_list_parsed_t *hw_res,
[19d21728]659 interrupt_handler_t handler,
[e4d7363]660 irq_code_gen_t gen_irq_code)
[19d21728]661{
662 assert(device);
[e4d7363]663
664 hcd_t *hcd = dev_to_hcd(device);
665
[3e200736]666 if (!handler || !gen_irq_code)
667 return ENOTSUP;
[19d21728]668
669 irq_code_t irq_code = {0};
670
[e4d7363]671 const int irq = gen_irq_code(&irq_code, hcd, hw_res);
[ba4a03a5]672 if (irq < 0) {
[19d21728]673 usb_log_error("Failed to generate IRQ code: %s.\n",
[ba4a03a5]674 str_error(irq));
675 return irq;
[19d21728]676 }
677
678 /* Register handler to avoid interrupt lockup */
[e9d15d9]679 const int irq_cap = register_interrupt_handler(device, irq, handler,
680 &irq_code);
[19d21728]681 irq_code_clean(&irq_code);
[e9d15d9]682 if (irq_cap < 0) {
[19d21728]683 usb_log_error("Failed to register interrupt handler: %s.\n",
[e9d15d9]684 str_error(irq_cap));
685 return irq_cap;
[19d21728]686 }
687
688 /* Enable interrupts */
[cccd60c3]689 int ret = hcd_ddf_enable_interrupt(device, irq);
[19d21728]690 if (ret != EOK) {
[cb89430]691 usb_log_error("Failed to enable interrupts: %s.\n",
[19d21728]692 str_error(ret));
[e9d15d9]693 unregister_interrupt_handler(device, irq_cap);
[19d21728]694 return ret;
695 }
[e9d15d9]696 return irq_cap;
[19d21728]697}
[7191992]698
699/** IRQ handling callback, forward status from call to diver structure.
700 *
701 * @param[in] dev DDF instance of the device to use.
702 * @param[in] iid (Unused).
703 * @param[in] call Pointer to the call from kernel.
704 */
[8e7c9fe]705void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
[7191992]706{
707 assert(dev);
708 hcd_t *hcd = dev_to_hcd(dev);
[b5f813c]709 if (!hcd || !hcd->ops.irq_hook) {
[7191992]710 usb_log_error("Interrupt on not yet initialized device.\n");
711 return;
712 }
713 const uint32_t status = IPC_GET_ARG1(*call);
[b5f813c]714 hcd->ops.irq_hook(hcd, status);
[7191992]715}
[3e200736]716
717static int interrupt_polling(void *arg)
718{
719 hcd_t *hcd = arg;
720 assert(hcd);
[b5f813c]721 if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
[3e200736]722 return ENOTSUP;
723 uint32_t status = 0;
[b5f813c]724 while (hcd->ops.status_hook(hcd, &status) == EOK) {
725 hcd->ops.irq_hook(hcd, status);
[3e200736]726 status = 0;
727 /* We should wait 1 frame - 1ms here, but this polling is a
728 * lame crutch anyway so don't hog the system. 10ms is still
729 * good enough for emergency mode */
730 async_usleep(10000);
731 }
732 return EOK;
733}
734
[7191992]735/** Initialize hc and rh DDF structures and their respective drivers.
736 *
737 * @param device DDF instance of the device to use
738 * @param speed Maximum supported speed
739 * @param bw Available bandwidth (arbitrary units)
740 * @param bw_count Bandwidth computing function
741 * @param irq_handler IRQ handling function
742 * @param gen_irq_code Function to generate IRQ pseudocode
743 * (it needs to return used irq number)
744 * @param driver_init Function to initialize HC driver
745 * @param driver_fini Function to cleanup HC driver
746 * @return Error code
747 *
748 * This function does all the preparatory work for hc and rh drivers:
749 * - gets device's hw resources
750 * - attempts to enable interrupts
751 * - registers interrupt handler
752 * - calls driver specific initialization
753 * - registers root hub
754 */
[d51ba359]755int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
[7191992]756{
[d51ba359]757 assert(driver);
758
759 int ret = EOK;
760
[7191992]761 hw_res_list_parsed_t hw_res;
[d51ba359]762 ret = hcd_ddf_get_registers(device, &hw_res);
[7191992]763 if (ret != EOK) {
764 usb_log_error("Failed to get register memory addresses "
[d51ba359]765 "for `%s': %s.\n", ddf_dev_get_name(device),
[7191992]766 str_error(ret));
767 return ret;
768 }
769
[41924f30]770 ret = hcd_ddf_setup_hc(device);
[7191992]771 if (ret != EOK) {
772 usb_log_error("Failed to setup generic HCD.\n");
[e4d7363]773 goto err_hw_res;
774 }
775
776 hcd_t *hcd = dev_to_hcd(device);
777
778 if (driver->init)
[0f6b50f]779 ret = driver->init(hcd, &hw_res, device);
[e4d7363]780 if (ret != EOK) {
781 usb_log_error("Failed to init HCD.\n");
782 goto err_hcd;
[7191992]783 }
784
[e4d7363]785 /* Setup interrupts */
[d51ba359]786 interrupt_handler_t *irq_handler =
787 driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
[e9d15d9]788 const int irq_cap = hcd_ddf_setup_interrupts(device, &hw_res,
789 irq_handler, driver->irq_code_gen);
790 bool irqs_enabled = !(irq_cap < 0);
791 if (irqs_enabled) {
[7191992]792 usb_log_debug("Hw interrupts enabled.\n");
793 }
794
[e4d7363]795 /* Claim the device from BIOS */
[f29643d5]796 if (driver->claim)
[e4d7363]797 ret = driver->claim(hcd, device);
[f29643d5]798 if (ret != EOK) {
[e4d7363]799 usb_log_error("Failed to claim `%s' for driver `%s': %s",
800 ddf_dev_get_name(device), driver->name, str_error(ret));
801 goto err_irq;
[f29643d5]802 }
803
[e4d7363]804 /* Start hw driver */
805 if (driver->start)
[95c675b]806 ret = driver->start(hcd, irqs_enabled);
[7191992]807 if (ret != EOK) {
[e4d7363]808 usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
809 goto err_irq;
[7191992]810 }
811
[3e200736]812 /* Need working irq replacement to setup root hub */
[e9d15d9]813 if (!irqs_enabled && hcd->ops.status_hook) {
[3e200736]814 hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
815 if (hcd->polling_fibril == 0) {
816 usb_log_error("Failed to create polling fibril\n");
817 ret = ENOMEM;
[e4d7363]818 goto err_started;
[3e200736]819 }
820 fibril_add_ready(hcd->polling_fibril);
821 usb_log_warning("Failed to enable interrupts: %s."
[e9d15d9]822 " Falling back to polling.\n", str_error(irq_cap));
[3e200736]823 }
824
[7191992]825 /*
826 * Creating root hub registers a new USB device so HC
827 * needs to be ready at this time.
828 */
[366e9b6]829 if (driver->setup_root_hub)
[867b375]830 ret = driver->setup_root_hub(hcd, device);
[7191992]831 if (ret != EOK) {
[c86ca9a]832 usb_log_error("Failed to setup HC root hub: %s.\n",
[7191992]833 str_error(ret));
[e4d7363]834 goto err_polling;
[7191992]835 }
[d51ba359]836
837 usb_log_info("Controlling new `%s' device `%s'.\n",
838 driver->name, ddf_dev_get_name(device));
839 return EOK;
[42bc933]840
[e4d7363]841err_polling:
842 // TODO: Stop the polling fibril (refactor the interrupt_polling func)
843 //
844err_started:
845 if (driver->stop)
846 driver->stop(hcd);
847err_irq:
[95c675b]848 unregister_interrupt_handler(device, irq_cap);
[e4d7363]849 if (driver->fini)
850 driver->fini(hcd);
851err_hcd:
852 hcd_ddf_clean_hc(device);
853err_hw_res:
854 hw_res_list_parsed_clean(&hw_res);
855 return ret;
[7191992]856}
[867b375]857
[53332b5b]858/**
859 * @}
860 */
Note: See TracBrowser for help on using the repository browser.