1 | /*
|
---|
2 | * Copyright (c) 2011 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 drvusbuhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief UHCI driver
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <ddf/driver.h>
|
---|
38 | #include <ddf/interrupt.h>
|
---|
39 | #include <device/hw_res_parsed.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <sys/types.h>
|
---|
45 |
|
---|
46 | #include <usb/debug.h>
|
---|
47 | #include <usb/usb.h>
|
---|
48 | #include <usb/host/ddf_helpers.h>
|
---|
49 | #include <usb/host/hcd.h>
|
---|
50 | #include <usb/host/usb_bus.h>
|
---|
51 |
|
---|
52 | #include "uhci.h"
|
---|
53 |
|
---|
54 | #include "res.h"
|
---|
55 | #include "hc.h"
|
---|
56 |
|
---|
57 |
|
---|
58 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
59 | *
|
---|
60 | * @param[in] dev DDF instance of the device to use.
|
---|
61 | * @param[in] iid (Unused).
|
---|
62 | * @param[in] call Pointer to the call from kernel.
|
---|
63 | */
|
---|
64 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
65 | {
|
---|
66 | assert(dev);
|
---|
67 | hcd_t *hcd = dev_to_hcd(dev);
|
---|
68 | if (!hcd || !hcd->driver.data) {
|
---|
69 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
73 | hc_interrupt(hcd->driver.data, status);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
77 | *
|
---|
78 | * @param[in] device DDF instance of the device to use.
|
---|
79 | *
|
---|
80 | * This function does all the preparatory work for hc and rh drivers:
|
---|
81 | * - gets device's hw resources
|
---|
82 | * - disables UHCI legacy support (PCI config space)
|
---|
83 | * - attempts to enable interrupts
|
---|
84 | * - registers interrupt handler
|
---|
85 | */
|
---|
86 | int device_setup_uhci(ddf_dev_t *device)
|
---|
87 | {
|
---|
88 | assert(device);
|
---|
89 |
|
---|
90 | hw_res_list_parsed_t hw_res;
|
---|
91 | int ret = hcd_ddf_get_registers(device, &hw_res);
|
---|
92 | if (ret != EOK ||
|
---|
93 | hw_res.irqs.count != 1 || hw_res.io_ranges.count != 1) {
|
---|
94 | usb_log_error("Failed to get register memory addresses "
|
---|
95 | "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
|
---|
96 | str_error(ret));
|
---|
97 | return ret;
|
---|
98 | }
|
---|
99 | addr_range_t regs = hw_res.io_ranges.ranges[0];
|
---|
100 | const int irq = hw_res.irqs.irqs[0];
|
---|
101 | hw_res_list_parsed_clean(&hw_res);
|
---|
102 |
|
---|
103 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
|
---|
104 | RNGABSPTR(regs), RNGSZ(regs), irq);
|
---|
105 |
|
---|
106 | ret = hcd_ddf_setup_hc(device, USB_SPEED_FULL,
|
---|
107 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
---|
108 | if (ret != EOK) {
|
---|
109 | usb_log_error("Failed to setup generic HCD.\n");
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 |
|
---|
113 | hc_t *hc = malloc(sizeof(hc_t));
|
---|
114 | if (!hc) {
|
---|
115 | usb_log_error("Failed to allocate UHCI HC structure.\n");
|
---|
116 | ret = ENOMEM;
|
---|
117 | goto ddf_hc_clean;
|
---|
118 | }
|
---|
119 |
|
---|
120 | bool interrupts = false;
|
---|
121 | ret = hcd_ddf_setup_interrupts(device, ®s, irq, irq_handler,
|
---|
122 | hc_gen_irq_code);
|
---|
123 | if (ret != EOK) {
|
---|
124 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
125 | " Falling back to polling.\n", str_error(ret));
|
---|
126 | unregister_interrupt_handler(device, irq);
|
---|
127 | } else {
|
---|
128 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
129 | interrupts = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | ret = disable_legacy(device);
|
---|
133 | if (ret != EOK) {
|
---|
134 | usb_log_error("Failed to disable legacy USB: %s.\n",
|
---|
135 | str_error(ret));
|
---|
136 | goto irq_unregister;
|
---|
137 | }
|
---|
138 |
|
---|
139 | ret = hc_init(hc, ®s, interrupts);
|
---|
140 | if (ret != EOK) {
|
---|
141 | usb_log_error("Failed to init uhci_hcd: %s.\n", str_error(ret));
|
---|
142 | goto irq_unregister;
|
---|
143 | // TODO This is unfortunate, we have neither legacy nor real USB
|
---|
144 | }
|
---|
145 |
|
---|
146 | hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Creating root hub registers a new USB device so HC
|
---|
150 | * needs to be ready at this time.
|
---|
151 | */
|
---|
152 | ret = hcd_ddf_setup_root_hub(device);
|
---|
153 | if (ret != EOK) {
|
---|
154 | usb_log_error("Failed to setup UHCI root hub: %s.\n",
|
---|
155 | str_error(ret));
|
---|
156 | hc_fini(hc);
|
---|
157 | irq_unregister:
|
---|
158 | unregister_interrupt_handler(device, irq);
|
---|
159 | free(hc);
|
---|
160 | ddf_hc_clean:
|
---|
161 | hcd_ddf_clean_hc(device);
|
---|
162 | }
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 | /**
|
---|
166 | * @}
|
---|
167 | */
|
---|