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 drvusbohci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief OHCI driver
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <ddf/interrupt.h>
|
---|
39 | #include <usb_iface.h>
|
---|
40 | #include <usb/usb.h>
|
---|
41 | #include <usb/ddfiface.h>
|
---|
42 | #include <usb/debug.h>
|
---|
43 |
|
---|
44 | #include "ohci.h"
|
---|
45 | #include "res.h"
|
---|
46 | #include "hc.h"
|
---|
47 |
|
---|
48 | typedef struct ohci {
|
---|
49 | ddf_fun_t *hc_fun;
|
---|
50 | ddf_fun_t *rh_fun;
|
---|
51 | } ohci_t;
|
---|
52 |
|
---|
53 | static inline ohci_t *dev_to_ohci(ddf_dev_t *dev)
|
---|
54 | {
|
---|
55 | return ddf_dev_data_get(dev);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static inline hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
---|
59 | {
|
---|
60 | ohci_t *ohci = dev_to_ohci(dev);
|
---|
61 | if (!ohci || !ohci->hc_fun) {
|
---|
62 | usb_log_error("Invalid OHCI device.\n");
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 | return ddf_fun_data_get(ohci->hc_fun);
|
---|
66 | }
|
---|
67 |
|
---|
68 | static inline hc_t * dev_to_hc(ddf_dev_t *dev)
|
---|
69 | {
|
---|
70 | hcd_t *hcd = dev_to_hcd(dev);
|
---|
71 | if (!hcd) {
|
---|
72 | usb_log_error("Invalid OHCI HCD");
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 | return hcd->private_data;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** IRQ handling callback, identifies device
|
---|
79 | *
|
---|
80 | * @param[in] dev DDF instance of the device to use.
|
---|
81 | * @param[in] iid (Unused).
|
---|
82 | * @param[in] call Pointer to the call that represents interrupt.
|
---|
83 | */
|
---|
84 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
85 | {
|
---|
86 | assert(dev);
|
---|
87 | hc_t *hc = dev_to_hc(dev);
|
---|
88 | if (!hc) {
|
---|
89 | usb_log_warning("Interrupt on device that is not ready.\n");
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
94 | hc_interrupt(hc, status);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Get USB address assigned to root hub.
|
---|
98 | *
|
---|
99 | * @param[in] fun Root hub function.
|
---|
100 | * @param[out] address Store the address here.
|
---|
101 | * @return Error code.
|
---|
102 | */
|
---|
103 | static int rh_get_my_address(ddf_fun_t *fun, usb_address_t *address)
|
---|
104 | {
|
---|
105 | assert(fun);
|
---|
106 |
|
---|
107 | if (address != NULL) {
|
---|
108 | hc_t *hc = dev_to_hc(ddf_fun_get_dev(fun));
|
---|
109 | assert(hc);
|
---|
110 | *address = hc->rh.address;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Gets handle of the respective hc (this device, hc function).
|
---|
117 | *
|
---|
118 | * @param[in] root_hub_fun Root hub function seeking hc handle.
|
---|
119 | * @param[out] handle Place to write the handle.
|
---|
120 | * @return Error code.
|
---|
121 | */
|
---|
122 | static int rh_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
123 | {
|
---|
124 | assert(fun);
|
---|
125 |
|
---|
126 | if (handle != NULL) {
|
---|
127 | ddf_fun_t *hc_fun = dev_to_ohci(ddf_fun_get_dev(fun))->hc_fun;
|
---|
128 | assert(hc_fun);
|
---|
129 | *handle = ddf_fun_get_handle(hc_fun);
|
---|
130 | }
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Root hub USB interface */
|
---|
135 | static usb_iface_t usb_iface = {
|
---|
136 | .get_hc_handle = rh_get_hc_handle,
|
---|
137 | .get_my_address = rh_get_my_address,
|
---|
138 | };
|
---|
139 |
|
---|
140 | /** Standard USB HC options (HC interface) */
|
---|
141 | static ddf_dev_ops_t hc_ops = {
|
---|
142 | .interfaces[USBHC_DEV_IFACE] = &hcd_iface,
|
---|
143 | };
|
---|
144 |
|
---|
145 | /** Standard USB RH options (RH interface) */
|
---|
146 | static ddf_dev_ops_t rh_ops = {
|
---|
147 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
148 | };
|
---|
149 |
|
---|
150 | /** Initialize hc and rh ddf structures and their respective drivers.
|
---|
151 | *
|
---|
152 | * @param[in] device DDF instance of the device to use.
|
---|
153 | * @param[in] instance OHCI structure to use.
|
---|
154 | *
|
---|
155 | * This function does all the preparatory work for hc and rh drivers:
|
---|
156 | * - gets device hw resources
|
---|
157 | * - disables OHCI legacy support
|
---|
158 | * - asks for interrupt
|
---|
159 | * - registers interrupt handler
|
---|
160 | */
|
---|
161 | int device_setup_ohci(ddf_dev_t *device)
|
---|
162 | {
|
---|
163 | if (device == NULL)
|
---|
164 | return EBADMEM;
|
---|
165 |
|
---|
166 | ohci_t *instance = ddf_dev_data_alloc(device, sizeof(ohci_t));
|
---|
167 | if (instance == NULL) {
|
---|
168 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
169 | return ENOMEM;
|
---|
170 | }
|
---|
171 |
|
---|
172 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
173 | if (ret != EOK) { \
|
---|
174 | if (instance->hc_fun) { \
|
---|
175 | ddf_fun_destroy(instance->hc_fun); \
|
---|
176 | } \
|
---|
177 | if (instance->rh_fun) { \
|
---|
178 | ddf_fun_destroy(instance->rh_fun); \
|
---|
179 | } \
|
---|
180 | usb_log_error(message); \
|
---|
181 | return ret; \
|
---|
182 | } else (void)0
|
---|
183 |
|
---|
184 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "ohci_hc");
|
---|
185 | int ret = instance->hc_fun ? EOK : ENOMEM;
|
---|
186 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
187 | "Failed to create OHCI HC function: %s.\n", str_error(ret));
|
---|
188 | ddf_fun_set_ops(instance->hc_fun, &hc_ops);
|
---|
189 | hcd_t *hcd = ddf_fun_data_alloc(instance->hc_fun, sizeof(hcd_t));
|
---|
190 | ret = hcd ? EOK : ENOMEM;
|
---|
191 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
192 | "Failed to allocate HCD structure: %s.\n", str_error(ret));
|
---|
193 |
|
---|
194 | hcd_init(hcd, USB_SPEED_FULL, BANDWIDTH_AVAILABLE_USB11,
|
---|
195 | bandwidth_count_usb11);
|
---|
196 |
|
---|
197 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
198 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
199 | "Failed to bind OHCI device function: %s.\n", str_error(ret));
|
---|
200 |
|
---|
201 | ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
|
---|
202 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
203 | "Failed to add OHCI to HC class: %s.\n", str_error(ret));
|
---|
204 |
|
---|
205 | /* HC should be ok at this point (except it can't do anything) */
|
---|
206 |
|
---|
207 | instance->rh_fun = ddf_fun_create(device, fun_inner, "ohci_rh");
|
---|
208 | ret = instance->rh_fun ? EOK : ENOMEM;
|
---|
209 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
210 | "Failed to create OHCI RH function: %s.\n", str_error(ret));
|
---|
211 | ddf_fun_set_ops(instance->rh_fun, &rh_ops);
|
---|
212 |
|
---|
213 | uintptr_t reg_base = 0;
|
---|
214 | size_t reg_size = 0;
|
---|
215 | int irq = 0;
|
---|
216 |
|
---|
217 | ret = get_my_registers(device, ®_base, ®_size, &irq);
|
---|
218 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
219 | "Failed to get register memory addresses for %" PRIun ": %s.\n",
|
---|
220 | ddf_dev_get_handle(device), str_error(ret));
|
---|
221 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
---|
222 | (void *) reg_base, reg_size, irq);
|
---|
223 |
|
---|
224 | const size_t ranges_count = hc_irq_pio_range_count();
|
---|
225 | const size_t cmds_count = hc_irq_cmd_count();
|
---|
226 | irq_pio_range_t irq_ranges[ranges_count];
|
---|
227 | irq_cmd_t irq_cmds[cmds_count];
|
---|
228 | irq_code_t irq_code = {
|
---|
229 | .rangecount = ranges_count,
|
---|
230 | .ranges = irq_ranges,
|
---|
231 | .cmdcount = cmds_count,
|
---|
232 | .cmds = irq_cmds
|
---|
233 | };
|
---|
234 |
|
---|
235 | ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
|
---|
236 | sizeof(irq_cmds), reg_base, reg_size);
|
---|
237 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
238 | "Failed to generate IRQ code: %s.\n", str_error(ret));
|
---|
239 |
|
---|
240 | /* Register handler to avoid interrupt lockup */
|
---|
241 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
242 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
243 | "Failed to register interrupt handler: %s.\n", str_error(ret));
|
---|
244 |
|
---|
245 | /* Try to enable interrupts */
|
---|
246 | bool interrupts = false;
|
---|
247 | ret = enable_interrupts(device);
|
---|
248 | if (ret != EOK) {
|
---|
249 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
250 | " Falling back to polling\n", str_error(ret));
|
---|
251 | /* We don't need that handler */
|
---|
252 | unregister_interrupt_handler(device, irq);
|
---|
253 | } else {
|
---|
254 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
255 | interrupts = true;
|
---|
256 | }
|
---|
257 |
|
---|
258 | hc_t *hc_impl = malloc(sizeof(hc_t));
|
---|
259 | assert(hc_impl);
|
---|
260 |
|
---|
261 | ret = hc_init(hc_impl, reg_base, reg_size, interrupts);
|
---|
262 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
263 | "Failed to init ohci_hcd: %s.\n", str_error(ret));
|
---|
264 |
|
---|
265 | hcd_set_implementation(hcd, hc_impl, hc_schedule,
|
---|
266 | ohci_endpoint_init, ohci_endpoint_fini);
|
---|
267 |
|
---|
268 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
269 | if (ret != EOK) { \
|
---|
270 | hc_fini(hc_impl); \
|
---|
271 | unregister_interrupt_handler(device, irq); \
|
---|
272 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
273 | } else (void)0
|
---|
274 |
|
---|
275 | ret = hcd_register_hub(hcd, &hc_impl->rh.address, instance->rh_fun);
|
---|
276 | CHECK_RET_FINI_RETURN(ret,
|
---|
277 | "Failed to register OHCI root hub: %s.\n", str_error(ret));
|
---|
278 | return ret;
|
---|
279 |
|
---|
280 | #undef CHECK_RET_FINI_RETURN
|
---|
281 | }
|
---|
282 | /**
|
---|
283 | * @}
|
---|
284 | */
|
---|