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

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

libusbhost bus: endpoint→device is now managed by bus implementation

That allows xhci to better isolate responsibilities.

  • 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
[0206d35]106 return bus_add_endpoint(hcd->bus, dev, endpoint_desc, NULL);
[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));
[0206d35]132
133 endpoint_t *ep = bus_find_endpoint(hcd->bus, dev, target, endpoint_desc->direction);
134 if (!ep)
135 return ENOENT;
136
137 return bus_remove_endpoint(hcd->bus, ep);
[d2cfe72]138}
139
[56bd6f11]140static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
141{
142 assert(fun);
143 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]144 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]145 assert(hcd);
[20eaa82]146 assert(hcd->bus);
[56bd6f11]147 assert(dev);
148
149 usb_log_debug("Device %d requested default address at %s speed\n",
150 dev->address, usb_str_speed(speed));
[20eaa82]151 return bus_reserve_default_address(hcd->bus, speed);
[56bd6f11]152}
153
154static int release_default_address(ddf_fun_t *fun)
155{
156 assert(fun);
157 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]158 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]159 assert(hcd);
[20eaa82]160 assert(hcd->bus);
[56bd6f11]161 assert(dev);
162
163 usb_log_debug("Device %d released default address\n", dev->address);
[20eaa82]164 return bus_release_default_address(hcd->bus);
[56bd6f11]165}
166
[0918382f]167static int device_enumerate(ddf_fun_t *fun, unsigned port)
[56bd6f11]168{
169 assert(fun);
[867b375]170 ddf_dev_t *hc = ddf_fun_get_dev(fun);
171 assert(hc);
172 hcd_t *hcd = dev_to_hcd(hc);
173 assert(hcd);
[20eaa82]174 device_t *hub = ddf_fun_data_get(fun);
[867b375]175 assert(hub);
176
[0918382f]177 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
[867b375]178 hub->address, port);
179 return hcd_ddf_new_device(hcd, hc, hub, port);
[56bd6f11]180}
181
[0918382f]182static int device_remove(ddf_fun_t *fun, unsigned port)
[56bd6f11]183{
184 assert(fun);
185 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
[20eaa82]186 device_t *dev = ddf_fun_data_get(fun);
[56bd6f11]187 assert(ddf_dev);
188 assert(dev);
[0918382f]189 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
190 ddf_fun_get_name(fun), port);
191 return hcd_ddf_remove_device(ddf_dev, dev, port);
[56bd6f11]192}
193
[3121b5f]194/** Gets handle of the respective device.
[8e4219ab]195 *
[3121b5f]196 * @param[in] fun Device function.
[8e4219ab]197 * @param[out] handle Place to write the handle.
198 * @return Error code.
199 */
[3121b5f]200static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
[8e4219ab]201{
202 assert(fun);
203 if (handle)
204 *handle = ddf_fun_get_handle(fun);
205 return EOK;
206}
207
[1845003]208/** Inbound communication interface function.
209 * @param fun DDF function.
210 * @param target Communication target.
211 * @param setup_data Data to use in setup stage (control transfers).
212 * @param data Pointer to data buffer.
213 * @param size Size of the data buffer.
214 * @param callback Function to call on communication end.
215 * @param arg Argument passed to the callback function.
216 * @return Error code.
217 */
[327f147]218static int dev_read(ddf_fun_t *fun, usb_target_t target,
219 uint64_t setup_data, char *data, size_t size,
220 usb_iface_transfer_callback_t callback, void *arg)
[1845003]221{
222 assert(fun);
[327f147]223 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]224 device_t *dev = ddf_fun_data_get(fun);
225 assert(dev);
[327f147]226
227 target.address = dev->address;
228
229 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
230 data, size, setup_data,
231 callback, arg, "READ");
[1845003]232}
233
234/** Outbound communication interface function.
235 * @param fun DDF function.
236 * @param target Communication target.
237 * @param setup_data Data to use in setup stage (control transfers).
238 * @param data Pointer to data buffer.
239 * @param size Size of the data buffer.
240 * @param callback Function to call on communication end.
241 * @param arg Argument passed to the callback function.
242 * @return Error code.
243 */
[327f147]244static int dev_write(ddf_fun_t *fun, usb_target_t target,
245 uint64_t setup_data, const char *data, size_t size,
246 usb_iface_transfer_callback_t callback, void *arg)
[1845003]247{
248 assert(fun);
[327f147]249 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
[20eaa82]250 device_t *dev = ddf_fun_data_get(fun);
251 assert(dev);
[327f147]252
253 target.address = dev->address;
254
255 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
256 (char *) data, size, setup_data,
[1845003]257 callback, arg, "WRITE");
258}
259
[2757247]260/** USB device interface */
[53332b5b]261static usb_iface_t usb_iface = {
[3121b5f]262 .get_my_device_handle = get_my_device_handle,
[8e4219ab]263
[56bd6f11]264 .reserve_default_address = reserve_default_address,
265 .release_default_address = release_default_address,
[4b8ecff]266
[56bd6f11]267 .device_enumerate = device_enumerate,
268 .device_remove = device_remove,
[4b8ecff]269
[d2cfe72]270 .register_endpoint = register_endpoint,
271 .unregister_endpoint = unregister_endpoint,
[4b8ecff]272
[1845003]273 .read = dev_read,
274 .write = dev_write,
[53332b5b]275};
[8e4219ab]276
[2757247]277/** Standard USB device interface) */
[53332b5b]278static ddf_dev_ops_t usb_ops = {
279 .interfaces[USB_DEV_IFACE] = &usb_iface,
280};
281
[2757247]282
283/* DDF HELPERS */
284
[237df2f]285#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
286do { \
287 match_id_t *mid = malloc(sizeof(match_id_t)); \
288 if (!mid) { \
289 clean_match_ids(list); \
290 return ENOMEM; \
291 } \
292 char *id = NULL; \
293 int ret = asprintf(&id, str, ##__VA_ARGS__); \
294 if (ret < 0) { \
295 clean_match_ids(list); \
296 free(mid); \
297 return ENOMEM; \
298 } \
299 mid->score = sc; \
300 mid->id = id; \
301 add_match_id(list, mid); \
302} while (0)
[b995183]303
[237df2f]304/* This is a copy of lib/usbdev/src/recognise.c */
305static int create_match_ids(match_id_list_t *l,
306 usb_standard_device_descriptor_t *d)
307{
308 assert(l);
309 assert(d);
[42bc933]310
[237df2f]311 if (d->vendor_id != 0) {
312 /* First, with release number. */
313 ADD_MATCHID_OR_RETURN(l, 100,
314 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
315 d->vendor_id, d->product_id, (d->device_version >> 8),
316 (d->device_version & 0xff));
[42bc933]317
[0ee999d]318 /* Next, without release number. */
[237df2f]319 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
320 d->vendor_id, d->product_id);
321 }
322
323 /* Class match id */
324 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
325 usb_str_class(d->device_class));
326
327 /* As a last resort, try fallback driver. */
328 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
329
330 return EOK;
331}
332
[20eaa82]333static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
[0918382f]334 unsigned port)
[237df2f]335{
336 assert(device);
337
338 hcd_t *hcd = dev_to_hcd(device);
339 assert(hcd);
[20eaa82]340 assert(hcd->bus);
[b995183]341
342 hc_dev_t *hc_dev = dev_to_hc_dev(device);
343 assert(hc_dev);
344
[14dd4c9]345 fibril_mutex_lock(&hub->guard);
[b995183]346
[20eaa82]347 device_t *victim = NULL;
[b995183]348
[20eaa82]349 list_foreach(hub->devices, link, device_t, it) {
350 if (it->port == port) {
[3f03199]351 victim = it;
[b995183]352 break;
[3f03199]353 }
[b995183]354 }
[9ff59981]355 if (victim) {
[867b375]356 assert(victim->fun);
[20eaa82]357 assert(victim->port == port);
358 assert(victim->hub == hub);
[b995183]359 list_remove(&victim->link);
[14dd4c9]360 fibril_mutex_unlock(&hub->guard);
[e6becb9]361 const int ret = ddf_fun_unbind(victim->fun);
362 if (ret == EOK) {
[e657635]363 usb_address_t address = victim->address;
[20eaa82]364 bus_remove_device(hcd->bus, hcd, victim);
[e6becb9]365 ddf_fun_destroy(victim->fun);
[20eaa82]366 bus_release_address(hcd->bus, address);
[e6becb9]367 } else {
[0918382f]368 usb_log_warning("Failed to unbind device `%s': %s\n",
369 ddf_fun_get_name(victim->fun), str_error(ret));
[e6becb9]370 }
[b995183]371 return EOK;
372 }
[9ff59981]373 fibril_mutex_unlock(&hub->guard);
[b995183]374 return ENOENT;
375}
376
[20eaa82]377device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
[867b375]378{
379 /* Create DDF function for the new device */
380 ddf_fun_t *fun = ddf_fun_create(hc, fun_inner, NULL);
381 if (!fun)
382 return NULL;
383
384 ddf_fun_set_ops(fun, &usb_ops);
385
386 /* Create USB device node for the new device */
[20eaa82]387 device_t *dev = ddf_fun_data_alloc(fun, device_size);
388 if (!dev) {
[867b375]389 ddf_fun_destroy(fun);
390 return NULL;
[237df2f]391 }
[2003739]392
[20eaa82]393 device_init(dev);
394 dev->fun = fun;
395 return dev;
[867b375]396}
397
[20eaa82]398void hcd_ddf_device_destroy(device_t *dev)
[867b375]399{
400 assert(dev);
401 assert(dev->fun);
402 ddf_fun_destroy(dev->fun);
403}
404
[20eaa82]405int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
[867b375]406{
407 int err;
408 match_id_list_t mids;
409 usb_standard_device_descriptor_t desc = { 0 };
410
411 init_match_ids(&mids);
412
[327f147]413 const usb_target_t control_ep = {{
[20eaa82]414 .address = device->address,
[327f147]415 .endpoint = 0,
[20eaa82]416 }};
[867b375]417
[237df2f]418 /* Get std device descriptor */
[2003739]419 const usb_device_request_setup_packet_t get_device_desc =
[237df2f]420 GET_DEVICE_DESC(sizeof(desc));
421
[f29643d5]422 usb_log_debug("Device(%d): Requesting full device descriptor.",
[20eaa82]423 device->address);
[327f147]424 ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
425 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
[237df2f]426 "read device descriptor");
[867b375]427 if (got < 0) {
428 err = got < 0 ? got : EOVERFLOW;
[f29643d5]429 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
[20eaa82]430 device->address, str_error(err));
[867b375]431 goto out;
[237df2f]432 }
[b995183]433
[237df2f]434 /* Create match ids from the device descriptor */
[20eaa82]435 usb_log_debug("Device(%d): Creating match IDs.", device->address);
[867b375]436 if ((err = create_match_ids(&mids, &desc))) {
[20eaa82]437 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
[867b375]438 goto out;
439 }
[237df2f]440
[867b375]441 list_foreach(mids.ids, link, const match_id_t, mid) {
[20eaa82]442 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
[237df2f]443 }
444
[867b375]445out:
[237df2f]446 clean_match_ids(&mids);
[867b375]447 return err;
448}
449
[20eaa82]450static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
[867b375]451{
452 int err;
453 assert(hcd);
[20eaa82]454 assert(hcd->bus);
455 assert(hub);
[867b375]456 assert(hc);
457
[20eaa82]458 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
459 if (!dev) {
[867b375]460 usb_log_error("Failed to create USB device function.");
[20eaa82]461 return ENOMEM;
[867b375]462 }
463
[20eaa82]464 dev->hub = hub;
465 dev->port = port;
[867b375]466
[20eaa82]467 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
468 usb_log_error("Failed to initialize USB dev memory structures.");
469 return err;
[867b375]470 }
471
[20eaa82]472 /* If the driver didn't name the dev when enumerating,
473 * do it in some generic way.
[867b375]474 */
[20eaa82]475 if (!ddf_fun_get_name(dev->fun)) {
476 device_set_default_name(dev);
[867b375]477 }
478
[20eaa82]479 if ((err = ddf_fun_bind(dev->fun))) {
480 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
[867b375]481 goto err_usb_dev;
482 }
483
[20eaa82]484 fibril_mutex_lock(&hub->guard);
485 list_append(&dev->link, &hub->devices);
486 fibril_mutex_unlock(&hub->guard);
[867b375]487
488 return EOK;
489
490err_usb_dev:
[20eaa82]491 hcd_ddf_device_destroy(dev);
[867b375]492 return err;
[237df2f]493}
494
495/** Announce root hub to the DDF
496 *
497 * @param[in] device Host controller ddf device
498 * @return Error code
499 */
[867b375]500int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
[237df2f]501{
[867b375]502 int err;
503
504 assert(hc);
[237df2f]505 assert(hcd);
[20eaa82]506 assert(hcd->bus);
[237df2f]507
[20eaa82]508 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
[867b375]509 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
510 return err;
511 }
512
[2b35478]513 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
[20eaa82]514 if (!dev) {
[867b375]515 usb_log_error("Failed to create function for the root hub.");
516 goto err_default_address;
517 }
518
[20eaa82]519 ddf_fun_set_name(dev->fun, "roothub");
[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.