source: mainline/uspace/drv/bus/usb/uhci/uhci.c@ 6e04581

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6e04581 was 5b89d43b, checked in by Jakub Jermar <jakub@…>, 12 years ago

Unbreak USB UHCI support.

  • Support PIO_WINDOW_DEV_IFACE in the uhci driver.
  • Do not base the UHCI RH resources on the address obtained from pio_enable_range().
  • Provide a restricted PIO window instead, based on the UHCI IO range. The UHCI RH resources are then relative to this window.
  • Property mode set to 100644
File size: 7.7 KB
Line 
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/* XXX Fix this */
37#define _DDF_DATA_IMPLANT
38
39#include <errno.h>
40#include <stdbool.h>
41#include <str_error.h>
42#include <ddf/interrupt.h>
43#include <usb_iface.h>
44#include <usb/ddfiface.h>
45#include <usb/debug.h>
46
47#include "uhci.h"
48
49#include "res.h"
50#include "hc.h"
51#include "root_hub.h"
52
53/** Structure representing both functions of UHCI hc, USB host controller
54 * and USB root hub */
55typedef struct uhci {
56 /** Pointer to DDF representation of UHCI host controller */
57 ddf_fun_t *hc_fun;
58 /** Pointer to DDF representation of UHCI root hub */
59 ddf_fun_t *rh_fun;
60
61 /** Internal driver's representation of UHCI host controller */
62 hc_t hc;
63 /** Internal driver's representation of UHCI root hub */
64 rh_t rh;
65} uhci_t;
66
67static inline uhci_t *dev_to_uhci(ddf_dev_t *dev)
68{
69 return ddf_dev_data_get(dev);
70}
71
72/** IRQ handling callback, forward status from call to diver structure.
73 *
74 * @param[in] dev DDF instance of the device to use.
75 * @param[in] iid (Unused).
76 * @param[in] call Pointer to the call from kernel.
77 */
78static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
79{
80 assert(dev);
81 uhci_t *uhci = dev_to_uhci(dev);
82 if (!uhci) {
83 usb_log_error("Interrupt on not yet initialized device.\n");
84 return;
85 }
86 const uint16_t status = IPC_GET_ARG1(*call);
87 hc_interrupt(&uhci->hc, status);
88}
89
90/** Operations supported by the HC driver */
91static ddf_dev_ops_t hc_ops = {
92 .interfaces[USBHC_DEV_IFACE] = &hcd_iface, /* see iface.h/c */
93};
94
95/** Gets handle of the respective hc.
96 *
97 * @param[in] fun DDF function of uhci device.
98 * @param[out] handle Host cotnroller handle.
99 * @return Error code.
100 */
101static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
102{
103 ddf_fun_t *hc_fun = dev_to_uhci(ddf_fun_get_dev(fun))->hc_fun;
104 assert(hc_fun);
105
106 if (handle != NULL)
107 *handle = ddf_fun_get_handle(hc_fun);
108 return EOK;
109}
110
111/** USB interface implementation used by RH */
112static usb_iface_t usb_iface = {
113 .get_hc_handle = usb_iface_get_hc_handle,
114};
115
116/** Get root hub hw resources (I/O registers).
117 *
118 * @param[in] fun Root hub function.
119 * @return Pointer to the resource list used by the root hub.
120 */
121static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
122{
123 rh_t *rh = ddf_fun_data_get(fun);
124 assert(rh);
125 return &rh->resource_list;
126}
127
128/** Interface to provide the root hub driver with hw info */
129static hw_res_ops_t hw_res_iface = {
130 .get_resource_list = get_resource_list,
131 .enable_interrupt = NULL,
132};
133
134static pio_window_t *get_pio_window(ddf_fun_t *fun)
135{
136 rh_t *rh = ddf_fun_data_get(fun);
137
138 if (rh == NULL)
139 return NULL;
140 return &rh->pio_window;
141}
142
143static pio_window_ops_t pio_window_iface = {
144 .get_pio_window = get_pio_window
145};
146
147/** RH function support for uhci_rhd */
148static ddf_dev_ops_t rh_ops = {
149 .interfaces[USB_DEV_IFACE] = &usb_iface,
150 .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface,
151 .interfaces[PIO_WINDOW_DEV_IFACE] = &pio_window_iface
152};
153
154/** Initialize hc and rh DDF structures and their respective drivers.
155 *
156 * @param[in] device DDF instance of the device to use.
157 *
158 * This function does all the preparatory work for hc and rh drivers:
159 * - gets device's hw resources
160 * - disables UHCI legacy support (PCI config space)
161 * - attempts to enable interrupts
162 * - registers interrupt handler
163 */
164int device_setup_uhci(ddf_dev_t *device)
165{
166 bool ih_registered = false;
167 bool hc_inited = false;
168 bool fun_bound = false;
169 int rc;
170
171 if (!device)
172 return EBADMEM;
173
174 uhci_t *instance = ddf_dev_data_alloc(device, sizeof(uhci_t));
175 if (instance == NULL) {
176 usb_log_error("Failed to allocate OHCI driver.\n");
177 return ENOMEM;
178 }
179
180 instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
181 if (instance->hc_fun == NULL) {
182 usb_log_error("Failed to create UHCI HC function.\n");
183 rc = ENOMEM;
184 goto error;
185 }
186
187 ddf_fun_set_ops(instance->hc_fun, &hc_ops);
188 ddf_fun_data_implant(instance->hc_fun, &instance->hc.generic);
189
190 instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
191 if (instance->rh_fun == NULL) {
192 usb_log_error("Failed to create UHCI RH function.\n");
193 rc = ENOMEM;
194 goto error;
195 }
196
197 ddf_fun_set_ops(instance->rh_fun, &rh_ops);
198 ddf_fun_data_implant(instance->rh_fun, &instance->rh);
199
200 addr_range_t regs;
201 int irq = 0;
202
203 rc = get_my_registers(device, &regs, &irq);
204 if (rc != EOK) {
205 usb_log_error("Failed to get I/O addresses for %" PRIun ": %s.\n",
206 ddf_dev_get_handle(device), str_error(rc));
207 goto error;
208 }
209 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
210 RNGABSPTR(regs), RNGSZ(regs), irq);
211
212 rc = disable_legacy(device);
213 if (rc != EOK) {
214 usb_log_error("Failed to disable legacy USB: %s.\n",
215 str_error(rc));
216 goto error;
217 }
218
219 rc = hc_register_irq_handler(device, &regs, irq, irq_handler);
220 if (rc != EOK) {
221 usb_log_error("Failed to register interrupt handler: %s.\n",
222 str_error(rc));
223 goto error;
224 }
225
226 ih_registered = true;
227
228 bool interrupts = false;
229 rc = enable_interrupts(device);
230 if (rc != EOK) {
231 usb_log_warning("Failed to enable interrupts: %s."
232 " Falling back to polling.\n", str_error(rc));
233 } else {
234 usb_log_debug("Hw interrupts enabled.\n");
235 interrupts = true;
236 }
237
238 rc = hc_init(&instance->hc, &regs, interrupts);
239 if (rc != EOK) {
240 usb_log_error("Failed to init uhci_hcd: %s.\n", str_error(rc));
241 goto error;
242 }
243
244 hc_inited = true;
245
246 rc = ddf_fun_bind(instance->hc_fun);
247 if (rc != EOK) {
248 usb_log_error("Failed to bind UHCI device function: %s.\n",
249 str_error(rc));
250 goto error;
251 }
252
253 fun_bound = true;
254
255 rc = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
256 if (rc != EOK) {
257 usb_log_error("Failed to add UHCI to HC class: %s.\n",
258 str_error(rc));
259 goto error;
260 }
261
262 rc = rh_init(&instance->rh, instance->rh_fun, &regs, 0x10, 4);
263 if (rc != EOK) {
264 usb_log_error("Failed to setup UHCI root hub: %s.\n",
265 str_error(rc));
266 goto error;
267 }
268
269 rc = ddf_fun_bind(instance->rh_fun);
270 if (rc != EOK) {
271 usb_log_error("Failed to register UHCI root hub: %s.\n",
272 str_error(rc));
273 goto error;
274 }
275
276 return EOK;
277
278error:
279 if (fun_bound)
280 ddf_fun_unbind(instance->hc_fun);
281 if (hc_inited)
282 hc_fini(&instance->hc);
283 if (ih_registered)
284 unregister_interrupt_handler(device, irq);
285 if (instance->hc_fun != NULL)
286 ddf_fun_destroy(instance->hc_fun);
287 if (instance->rh_fun != NULL) {
288 ddf_fun_destroy(instance->rh_fun);
289 }
290 return rc;
291}
292/**
293 * @}
294 */
Note: See TracBrowser for help on using the repository browser.