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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d37514e was d37514e, checked in by Petr Manek <petr.manek@…>, 8 years ago

Routing fun_online and _offline through the USB bus. Added appropriate stubs and ops.

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