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

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

usbhost: refactor include hiearchy

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