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

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

usbhost bus: refactor the bus ops

This way, method names better represent the entity it is working with.
Their semantics was shifted a bit.

Regarding the tree of structures:

bus ← device ← endpoint ← batch

Previously, devices were kept in DDF function nodes, and endpoints had
pointer to the bus and device. Now, devices have pointer to bus,
endpoints don't.

Pointer to hcd_t in bus is WIP, and will be removed.

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