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 | #include <errno.h>
|
---|
36 | #include <str_error.h>
|
---|
37 | #include <ddf/interrupt.h>
|
---|
38 | #include <usb_iface.h>
|
---|
39 | #include <usb/ddfiface.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 |
|
---|
42 | #include "uhci.h"
|
---|
43 | #include "iface.h"
|
---|
44 | #include "pci.h"
|
---|
45 |
|
---|
46 | #include "hc.h"
|
---|
47 | #include "root_hub.h"
|
---|
48 |
|
---|
49 | /** Structure representing both functions of UHCI hc, USB host controller
|
---|
50 | * and USB root hub */
|
---|
51 | typedef struct uhci {
|
---|
52 | /** Pointer to DDF represenation of UHCI host controller */
|
---|
53 | ddf_fun_t *hc_fun;
|
---|
54 | /** Pointer to DDF represenation of UHCI root hub */
|
---|
55 | ddf_fun_t *rh_fun;
|
---|
56 |
|
---|
57 | /** Internal driver's represenation of UHCI host controller */
|
---|
58 | hc_t hc;
|
---|
59 | /** Internal driver's represenation of UHCI root hub */
|
---|
60 | rh_t rh;
|
---|
61 | } uhci_t;
|
---|
62 |
|
---|
63 | static inline uhci_t * dev_to_uhci(ddf_dev_t *dev)
|
---|
64 | {
|
---|
65 | assert(dev);
|
---|
66 | assert(dev->driver_data);
|
---|
67 | return dev->driver_data;
|
---|
68 | }
|
---|
69 | /*----------------------------------------------------------------------------*/
|
---|
70 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
71 | *
|
---|
72 | * @param[in] dev DDF instance of the device to use.
|
---|
73 | * @param[in] iid (Unused).
|
---|
74 | * @param[in] call Pointer to the call from kernel.
|
---|
75 | */
|
---|
76 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
77 | {
|
---|
78 | assert(dev);
|
---|
79 | hc_t *hc = &((uhci_t*)dev->driver_data)->hc;
|
---|
80 | uint16_t status = IPC_GET_ARG1(*call);
|
---|
81 | assert(hc);
|
---|
82 | hc_interrupt(hc, status);
|
---|
83 | }
|
---|
84 | /*----------------------------------------------------------------------------*/
|
---|
85 | /** Get address of the device identified by handle.
|
---|
86 | *
|
---|
87 | * @param[in] fun DDF instance of the function to use.
|
---|
88 | * @param[in] handle DDF handle of the driver seeking its USB address.
|
---|
89 | * @param[out] address Found address.
|
---|
90 | */
|
---|
91 | static int usb_iface_get_address(
|
---|
92 | ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
|
---|
93 | {
|
---|
94 | assert(fun);
|
---|
95 | usb_device_keeper_t *manager = &dev_to_uhci(fun->dev)->hc.manager;
|
---|
96 | usb_address_t addr = usb_device_keeper_find(manager, handle);
|
---|
97 |
|
---|
98 | if (addr < 0) {
|
---|
99 | return addr;
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (address != NULL) {
|
---|
103 | *address = addr;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 | /*----------------------------------------------------------------------------*/
|
---|
109 | /** Gets handle of the respective hc.
|
---|
110 | *
|
---|
111 | * @param[in] fun DDF function of uhci device.
|
---|
112 | * @param[out] handle Host cotnroller handle.
|
---|
113 | * @return Error code.
|
---|
114 | */
|
---|
115 | static int usb_iface_get_hc_handle(
|
---|
116 | ddf_fun_t *fun, devman_handle_t *handle)
|
---|
117 | {
|
---|
118 | assert(fun);
|
---|
119 | ddf_fun_t *hc_fun = dev_to_uhci(fun->dev)->hc_fun;
|
---|
120 | assert(hc_fun);
|
---|
121 |
|
---|
122 | if (handle != NULL)
|
---|
123 | *handle = hc_fun->handle;
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 | /*----------------------------------------------------------------------------*/
|
---|
127 | /** USB interface implementation used by RH */
|
---|
128 | static usb_iface_t usb_iface = {
|
---|
129 | .get_hc_handle = usb_iface_get_hc_handle,
|
---|
130 | .get_address = usb_iface_get_address
|
---|
131 | };
|
---|
132 | /*----------------------------------------------------------------------------*/
|
---|
133 | /** Operations supported by the HC driver */
|
---|
134 | static ddf_dev_ops_t hc_ops = {
|
---|
135 | .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */
|
---|
136 | };
|
---|
137 | /*----------------------------------------------------------------------------*/
|
---|
138 | /** Get root hub hw resources (I/O registers).
|
---|
139 | *
|
---|
140 | * @param[in] fun Root hub function.
|
---|
141 | * @return Pointer to the resource list used by the root hub.
|
---|
142 | */
|
---|
143 | static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
|
---|
144 | {
|
---|
145 | assert(fun);
|
---|
146 | return &((rh_t*)fun->driver_data)->resource_list;
|
---|
147 | }
|
---|
148 | /*----------------------------------------------------------------------------*/
|
---|
149 | /** Interface to provide the root hub driver with hw info */
|
---|
150 | static hw_res_ops_t hw_res_iface = {
|
---|
151 | .get_resource_list = get_resource_list,
|
---|
152 | .enable_interrupt = NULL,
|
---|
153 | };
|
---|
154 | /*----------------------------------------------------------------------------*/
|
---|
155 | /** RH function support for uhci-rhd */
|
---|
156 | static ddf_dev_ops_t rh_ops = {
|
---|
157 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
158 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
159 | };
|
---|
160 | /*----------------------------------------------------------------------------*/
|
---|
161 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
162 | *
|
---|
163 | * @param[in] device DDF instance of the device to use.
|
---|
164 | *
|
---|
165 | * This function does all the preparatory work for hc and rh drivers:
|
---|
166 | * - gets device's hw resources
|
---|
167 | * - disables UHCI legacy support (PCI config space)
|
---|
168 | * - attempts to enable interrupts
|
---|
169 | * - registers interrupt handler
|
---|
170 | */
|
---|
171 | int device_setup_uhci(ddf_dev_t *device)
|
---|
172 | {
|
---|
173 | assert(device);
|
---|
174 | uhci_t *instance = malloc(sizeof(uhci_t));
|
---|
175 | if (instance == NULL) {
|
---|
176 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
177 | return ENOMEM;
|
---|
178 | }
|
---|
179 |
|
---|
180 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
181 | if (ret != EOK) { \
|
---|
182 | if (instance->hc_fun) \
|
---|
183 | instance->hc_fun->ops = NULL; \
|
---|
184 | instance->hc_fun->driver_data = NULL; \
|
---|
185 | ddf_fun_destroy(instance->hc_fun); \
|
---|
186 | if (instance->rh_fun) {\
|
---|
187 | instance->rh_fun->ops = NULL; \
|
---|
188 | instance->rh_fun->driver_data = NULL; \
|
---|
189 | ddf_fun_destroy(instance->rh_fun); \
|
---|
190 | } \
|
---|
191 | free(instance); \
|
---|
192 | usb_log_error(message); \
|
---|
193 | return ret; \
|
---|
194 | } else (void)0
|
---|
195 |
|
---|
196 | instance->rh_fun = NULL;
|
---|
197 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci-hc");
|
---|
198 | int ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
|
---|
199 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI HC function.\n");
|
---|
200 | instance->hc_fun->ops = &hc_ops;
|
---|
201 | instance->hc_fun->driver_data = &instance->hc;
|
---|
202 |
|
---|
203 | instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci-rh");
|
---|
204 | ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
|
---|
205 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI RH function.\n");
|
---|
206 | instance->rh_fun->ops = &rh_ops;
|
---|
207 | instance->rh_fun->driver_data = &instance->rh;
|
---|
208 |
|
---|
209 | uintptr_t reg_base = 0;
|
---|
210 | size_t reg_size = 0;
|
---|
211 | int irq = 0;
|
---|
212 |
|
---|
213 | ret = pci_get_my_registers(device, ®_base, ®_size, &irq);
|
---|
214 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
215 | "Failed to get I/O addresses for %" PRIun ": %s.\n",
|
---|
216 | device->handle, str_error(ret));
|
---|
217 | usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
|
---|
218 | (void *) reg_base, reg_size, irq);
|
---|
219 |
|
---|
220 | ret = pci_disable_legacy(device);
|
---|
221 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
222 | "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
|
---|
223 |
|
---|
224 | bool interrupts = false;
|
---|
225 | #ifdef CONFIG_USBHC_NO_INTERRUPTS
|
---|
226 | usb_log_warning("Interrupts disabled in OS config, " \
|
---|
227 | "falling back to polling.\n");
|
---|
228 | #else
|
---|
229 | ret = pci_enable_interrupts(device);
|
---|
230 | if (ret != EOK) {
|
---|
231 | usb_log_warning("Failed to enable interrupts: %s.\n",
|
---|
232 | str_error(ret));
|
---|
233 | usb_log_info("HW interrupts not available, " \
|
---|
234 | "falling back to polling.\n");
|
---|
235 | } else {
|
---|
236 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
237 | interrupts = true;
|
---|
238 | }
|
---|
239 | #endif
|
---|
240 |
|
---|
241 |
|
---|
242 | ret = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
|
---|
243 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
244 | "Failed(%d) to init uhci-hcd: %s.\n", ret, str_error(ret));
|
---|
245 |
|
---|
246 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
247 | if (ret != EOK) { \
|
---|
248 | hc_fini(&instance->hc); \
|
---|
249 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
250 | return ret; \
|
---|
251 | } else (void)0
|
---|
252 |
|
---|
253 | /* It does no harm if we register this on polling */
|
---|
254 | ret = register_interrupt_handler(device, irq, irq_handler,
|
---|
255 | &instance->hc.interrupt_code);
|
---|
256 | CHECK_RET_FINI_RETURN(ret,
|
---|
257 | "Failed(%d) to register interrupt handler: %s.\n",
|
---|
258 | ret, str_error(ret));
|
---|
259 |
|
---|
260 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
261 | CHECK_RET_FINI_RETURN(ret,
|
---|
262 | "Failed(%d) to bind UHCI device function: %s.\n",
|
---|
263 | ret, str_error(ret));
|
---|
264 |
|
---|
265 | ret = ddf_fun_add_to_class(instance->hc_fun, USB_HC_DDF_CLASS_NAME);
|
---|
266 | CHECK_RET_FINI_RETURN(ret,
|
---|
267 | "Failed to add UHCI to HC class: %s.\n", str_error(ret));
|
---|
268 |
|
---|
269 | ret = rh_init(&instance->rh, instance->rh_fun,
|
---|
270 | (uintptr_t)instance->hc.registers + 0x10, 4);
|
---|
271 | CHECK_RET_FINI_RETURN(ret,
|
---|
272 | "Failed(%d) to setup UHCI root hub: %s.\n", ret, str_error(ret));
|
---|
273 |
|
---|
274 | ret = ddf_fun_bind(instance->rh_fun);
|
---|
275 | CHECK_RET_FINI_RETURN(ret,
|
---|
276 | "Failed(%d) to register UHCI root hub: %s.\n", ret, str_error(ret));
|
---|
277 |
|
---|
278 | device->driver_data = instance;
|
---|
279 | return EOK;
|
---|
280 | #undef CHECK_RET_FINI_RETURN
|
---|
281 | }
|
---|
282 | /**
|
---|
283 | * @}
|
---|
284 | */
|
---|