source: mainline/uspace/drv/bus/usb/uhci/uhci.c@ 8442d10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8442d10 was c53007f, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Eliminate CHECK_RETxxx macros from uhci and uhcirh.

  • Property mode set to 100644
File size: 7.5 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
134/** RH function support for uhci_rhd */
135static ddf_dev_ops_t rh_ops = {
136 .interfaces[USB_DEV_IFACE] = &usb_iface,
137 .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
138};
139
140/** Initialize hc and rh DDF structures and their respective drivers.
141 *
142 * @param[in] device DDF instance of the device to use.
143 *
144 * This function does all the preparatory work for hc and rh drivers:
145 * - gets device's hw resources
146 * - disables UHCI legacy support (PCI config space)
147 * - attempts to enable interrupts
148 * - registers interrupt handler
149 */
150int device_setup_uhci(ddf_dev_t *device)
151{
152 bool ih_registered = false;
153 bool hc_inited = false;
154 bool fun_bound = false;
155 int rc;
156
157 if (!device)
158 return EBADMEM;
159
160 uhci_t *instance = ddf_dev_data_alloc(device, sizeof(uhci_t));
161 if (instance == NULL) {
162 usb_log_error("Failed to allocate OHCI driver.\n");
163 return ENOMEM;
164 }
165
166 instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
167 if (instance->hc_fun == NULL) {
168 usb_log_error("Failed to create UHCI HC function.\n");
169 rc = ENOMEM;
170 goto error;
171 }
172
173 ddf_fun_set_ops(instance->hc_fun, &hc_ops);
174 ddf_fun_data_implant(instance->hc_fun, &instance->hc.generic);
175
176 instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
177 if (instance->rh_fun == NULL) {
178 usb_log_error("Failed to create UHCI RH function.\n");
179 rc = ENOMEM;
180 goto error;
181 }
182
183 ddf_fun_set_ops(instance->rh_fun, &rh_ops);
184 ddf_fun_data_implant(instance->rh_fun, &instance->rh);
185
186 uintptr_t reg_base = 0;
187 size_t reg_size = 0;
188 int irq = 0;
189
190 rc = get_my_registers(device, &reg_base, &reg_size, &irq);
191 if (rc != EOK) {
192 usb_log_error("Failed to get I/O addresses for %" PRIun ": %s.\n",
193 ddf_dev_get_handle(device), str_error(rc));
194 goto error;
195 }
196 usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
197 (void *) reg_base, reg_size, irq);
198
199 rc = disable_legacy(device);
200 if (rc != EOK) {
201 usb_log_error("Failed to disable legacy USB: %s.\n",
202 str_error(rc));
203 goto error;
204 }
205
206 rc = hc_register_irq_handler(device, reg_base, reg_size, irq, irq_handler);
207 if (rc != EOK) {
208 usb_log_error("Failed to register interrupt handler: %s.\n",
209 str_error(rc));
210 goto error;
211 }
212
213 ih_registered = true;
214
215 bool interrupts = false;
216 rc = enable_interrupts(device);
217 if (rc != EOK) {
218 usb_log_warning("Failed to enable interrupts: %s."
219 " Falling back to polling.\n", str_error(rc));
220 } else {
221 usb_log_debug("Hw interrupts enabled.\n");
222 interrupts = true;
223 }
224
225 rc = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
226 if (rc != EOK) {
227 usb_log_error("Failed to init uhci_hcd: %s.\n", str_error(rc));
228 goto error;
229 }
230
231 hc_inited = true;
232
233 rc = ddf_fun_bind(instance->hc_fun);
234 if (rc != EOK) {
235 usb_log_error("Failed to bind UHCI device function: %s.\n",
236 str_error(rc));
237 goto error;
238 }
239
240 fun_bound = true;
241
242 rc = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
243 if (rc != EOK) {
244 usb_log_error("Failed to add UHCI to HC class: %s.\n",
245 str_error(rc));
246 goto error;
247 }
248
249 rc = rh_init(&instance->rh, instance->rh_fun,
250 (uintptr_t)instance->hc.registers + 0x10, 4);
251 if (rc != EOK) {
252 usb_log_error("Failed to setup UHCI root hub: %s.\n",
253 str_error(rc));
254 goto error;
255 }
256
257 rc = ddf_fun_bind(instance->rh_fun);
258 if (rc != EOK) {
259 usb_log_error("Failed to register UHCI root hub: %s.\n",
260 str_error(rc));
261 goto error;
262 }
263
264 return EOK;
265
266error:
267 if (fun_bound)
268 ddf_fun_unbind(instance->hc_fun);
269 if (hc_inited)
270 hc_fini(&instance->hc);
271 if (ih_registered)
272 unregister_interrupt_handler(device, irq);
273 if (instance->hc_fun != NULL)
274 ddf_fun_destroy(instance->hc_fun);
275 if (instance->rh_fun != NULL) {
276 ddf_fun_destroy(instance->rh_fun);
277 }
278 return rc;
279}
280/**
281 * @}
282 */
Note: See TracBrowser for help on using the repository browser.