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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ec700c7 was ec700c7, checked in by Michal Staruch <salmelu@…>, 8 years ago

Superspeed companion descriptor is processed

Superspeed endpoint companion is now correctly read and processed while parsing descriptors.
As a side effect, mass storage endpoints now initialize correctly and therefore mass storage driver starts.

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