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

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

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

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