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

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

ddf: split usb interface to usb and usbhc

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