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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 81ee1009 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
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_add_endpoint(hcd->bus, 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(hcd->bus, dev, target, endpoint_desc->direction);
131 if (!ep)
132 return ENOENT;
133
134 return bus_remove_endpoint(hcd->bus, 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 usb_address_t address = victim->address;
365 bus_remove_device(hcd->bus, hcd, victim);
366 ddf_fun_destroy(victim->fun);
367 bus_release_address(hcd->bus, address);
368 } else {
369 usb_log_warning("Failed to unbind device `%s': %s\n",
370 ddf_fun_get_name(victim->fun), str_error(ret));
371 }
372 return EOK;
373 }
374 fibril_mutex_unlock(&hub->guard);
375 return ENOENT;
376}
377
378device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
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 */
388 device_t *dev = ddf_fun_data_alloc(fun, device_size);
389 if (!dev) {
390 ddf_fun_destroy(fun);
391 return NULL;
392 }
393
394 device_init(dev);
395 dev->fun = fun;
396 return dev;
397}
398
399void hcd_ddf_device_destroy(device_t *dev)
400{
401 assert(dev);
402 assert(dev->fun);
403 ddf_fun_destroy(dev->fun);
404}
405
406int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
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
414 const usb_target_t control_ep = {{
415 .address = device->address,
416 .endpoint = 0,
417 }};
418
419 /* Get std device descriptor */
420 const usb_device_request_setup_packet_t get_device_desc =
421 GET_DEVICE_DESC(sizeof(desc));
422
423 usb_log_debug("Device(%d): Requesting full device descriptor.",
424 device->address);
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,
427 "read device descriptor");
428 if (got < 0) {
429 err = got < 0 ? got : EOVERFLOW;
430 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
431 device->address, str_error(err));
432 goto out;
433 }
434
435 /* Create match ids from the device descriptor */
436 usb_log_debug("Device(%d): Creating match IDs.", device->address);
437 if ((err = create_match_ids(&mids, &desc))) {
438 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
439 goto out;
440 }
441
442 list_foreach(mids.ids, link, const match_id_t, mid) {
443 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
444 }
445
446out:
447 clean_match_ids(&mids);
448 return err;
449}
450
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
479static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
480{
481 int err;
482 assert(hcd);
483 assert(hcd->bus);
484 assert(hub);
485 assert(hc);
486
487 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
488 if (!dev) {
489 usb_log_error("Failed to create USB device function.");
490 return ENOMEM;
491 }
492
493 dev->hub = hub;
494 dev->port = port;
495
496 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
497 usb_log_error("Failed to initialize USB dev memory structures.");
498 return err;
499 }
500
501 /* If the driver didn't name the dev when enumerating,
502 * do it in some generic way.
503 */
504 if (!ddf_fun_get_name(dev->fun)) {
505 device_set_default_name(dev);
506 }
507
508 if ((err = ddf_fun_bind(dev->fun))) {
509 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
510 goto err_usb_dev;
511 }
512
513 fibril_mutex_lock(&hub->guard);
514 list_append(&dev->link, &hub->devices);
515 fibril_mutex_unlock(&hub->guard);
516
517 return EOK;
518
519err_usb_dev:
520 hcd_ddf_device_destroy(dev);
521 return err;
522}
523
524/** Announce root hub to the DDF
525 *
526 * @param[in] device Host controller ddf device
527 * @return Error code
528 */
529int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
530{
531 int err;
532
533 assert(hc);
534 assert(hcd);
535 assert(hcd->bus);
536
537 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
538 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
539 return err;
540 }
541
542 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
543 if (!dev) {
544 usb_log_error("Failed to create function for the root hub.");
545 goto err_default_address;
546 }
547
548 ddf_fun_set_name(dev->fun, "roothub");
549
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));
553 goto err_usb_dev;
554 }
555
556 if ((err = ddf_fun_bind(dev->fun))) {
557 usb_log_error("Failed to register roothub: %s.", str_error(err));
558 goto err_usb_dev;
559 }
560
561 bus_release_default_address(hcd->bus);
562 return EOK;
563
564err_usb_dev:
565 hcd_ddf_device_destroy(dev);
566err_default_address:
567 bus_release_default_address(hcd->bus);
568 return err;
569}
570
571/** Initialize hc structures.
572 *
573 * @param[in] device DDF instance of the device to use.
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.
577 *
578 * @return Error code.
579 * This function does all the ddf work for hc driver.
580 */
581int hcd_ddf_setup_hc(ddf_dev_t *device)
582{
583 assert(device);
584
585 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
586 if (instance == NULL) {
587 usb_log_error("Failed to allocate HCD ddf structure.\n");
588 return ENOMEM;
589 }
590 hcd_init(&instance->hcd);
591
592 int ret = ENOMEM;
593 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
594 if (!instance->ctl_fun) {
595 usb_log_error("Failed to create HCD ddf fun.\n");
596 goto err_destroy_fun;
597 }
598
599 ret = ddf_fun_bind(instance->ctl_fun);
600 if (ret != EOK) {
601 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
602 goto err_destroy_fun;
603 }
604
605 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
606 if (ret != EOK) {
607 usb_log_error("Failed to add fun to category: %s.\n",
608 str_error(ret));
609 ddf_fun_unbind(instance->ctl_fun);
610 goto err_destroy_fun;
611 }
612
613 /* HC should be ok at this point (except it can't do anything) */
614 return EOK;
615
616err_destroy_fun:
617 ddf_fun_destroy(instance->ctl_fun);
618 instance->ctl_fun = NULL;
619 return ret;
620}
621
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}
631
632//TODO: Cache parent session in HCD
633/** Call the parent driver with a request to enable interrupt
634 *
635 * @param[in] device Device asking for interrupts
636 * @param[in] inum Interrupt number
637 * @return Error code.
638 */
639int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
640{
641 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
642 if (parent_sess == NULL)
643 return EIO;
644
645 return hw_res_enable_interrupt(parent_sess, inum);
646}
647
648//TODO: Cache parent session in HCD
649int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
650{
651 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
652 if (parent_sess == NULL)
653 return EIO;
654
655 hw_res_list_parsed_init(hw_res);
656 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
657 if (ret != EOK)
658 hw_res_list_parsed_clean(hw_res);
659 return ret;
660}
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 *
683 * @return IRQ capability handle on success.
684 * @return Negative error code.
685 */
686int hcd_ddf_setup_interrupts(ddf_dev_t *device,
687 const hw_res_list_parsed_t *hw_res,
688 interrupt_handler_t handler,
689 irq_code_gen_t gen_irq_code)
690{
691 assert(device);
692
693 hcd_t *hcd = dev_to_hcd(device);
694
695 if (!handler || !gen_irq_code)
696 return ENOTSUP;
697
698 irq_code_t irq_code = {0};
699
700 const int irq = gen_irq_code(&irq_code, hcd, hw_res);
701 if (irq < 0) {
702 usb_log_error("Failed to generate IRQ code: %s.\n",
703 str_error(irq));
704 return irq;
705 }
706
707 /* Register handler to avoid interrupt lockup */
708 const int irq_cap = register_interrupt_handler(device, irq, handler,
709 &irq_code);
710 irq_code_clean(&irq_code);
711 if (irq_cap < 0) {
712 usb_log_error("Failed to register interrupt handler: %s.\n",
713 str_error(irq_cap));
714 return irq_cap;
715 }
716
717 /* Enable interrupts */
718 int ret = hcd_ddf_enable_interrupt(device, irq);
719 if (ret != EOK) {
720 usb_log_error("Failed to enable interrupts: %s.\n",
721 str_error(ret));
722 unregister_interrupt_handler(device, irq_cap);
723 return ret;
724 }
725 return irq_cap;
726}
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 */
734void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
735{
736 assert(dev);
737 hcd_t *hcd = dev_to_hcd(dev);
738 if (!hcd || !hcd->ops.irq_hook) {
739 usb_log_error("Interrupt on not yet initialized device.\n");
740 return;
741 }
742 const uint32_t status = IPC_GET_ARG1(*call);
743 hcd->ops.irq_hook(hcd, status);
744}
745
746static int interrupt_polling(void *arg)
747{
748 hcd_t *hcd = arg;
749 assert(hcd);
750 if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
751 return ENOTSUP;
752 uint32_t status = 0;
753 while (hcd->ops.status_hook(hcd, &status) == EOK) {
754 hcd->ops.irq_hook(hcd, status);
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
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 */
784int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
785{
786 assert(driver);
787
788 int ret = EOK;
789
790 hw_res_list_parsed_t hw_res;
791 ret = hcd_ddf_get_registers(device, &hw_res);
792 if (ret != EOK) {
793 usb_log_error("Failed to get register memory addresses "
794 "for `%s': %s.\n", ddf_dev_get_name(device),
795 str_error(ret));
796 return ret;
797 }
798
799 ret = hcd_ddf_setup_hc(device);
800 if (ret != EOK) {
801 usb_log_error("Failed to setup generic HCD.\n");
802 goto err_hw_res;
803 }
804
805 hcd_t *hcd = dev_to_hcd(device);
806
807 if (driver->init)
808 ret = driver->init(hcd, &hw_res, device);
809 if (ret != EOK) {
810 usb_log_error("Failed to init HCD.\n");
811 goto err_hcd;
812 }
813
814 /* Setup interrupts */
815 interrupt_handler_t *irq_handler =
816 driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
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) {
821 usb_log_debug("Hw interrupts enabled.\n");
822 }
823
824 /* Claim the device from BIOS */
825 if (driver->claim)
826 ret = driver->claim(hcd, device);
827 if (ret != EOK) {
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;
831 }
832
833 /* Start hw driver */
834 if (driver->start)
835 ret = driver->start(hcd, irqs_enabled);
836 if (ret != EOK) {
837 usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
838 goto err_irq;
839 }
840
841 /* Need working irq replacement to setup root hub */
842 if (!irqs_enabled && hcd->ops.status_hook) {
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;
847 goto err_started;
848 }
849 fibril_add_ready(hcd->polling_fibril);
850 usb_log_warning("Failed to enable interrupts: %s."
851 " Falling back to polling.\n", str_error(irq_cap));
852 }
853
854 /*
855 * Creating root hub registers a new USB device so HC
856 * needs to be ready at this time.
857 */
858 if (driver->setup_root_hub)
859 ret = driver->setup_root_hub(hcd, device);
860 if (ret != EOK) {
861 usb_log_error("Failed to setup HC root hub: %s.\n",
862 str_error(ret));
863 goto err_polling;
864 }
865
866 usb_log_info("Controlling new `%s' device `%s'.\n",
867 driver->name, ddf_dev_get_name(device));
868 return EOK;
869
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:
877 unregister_interrupt_handler(device, irq_cap);
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;
885}
886
887/**
888 * @}
889 */
Note: See TracBrowser for help on using the repository browser.