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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 549ff23 was 4267908, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbhost: Merge find_by_address and get_speed.

These functions were almost identical.
Clear handle on address release.

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