source: mainline/uspace/drv/uhci-hcd/uhci.c@ ea696998

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

HC does not need usb iface

  • Property mode set to 100644
File size: 7.9 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 "iface.h"
44#include "pci.h"
45
46/** IRQ handling callback, identifies device
47 *
48 * @param[in] dev DDF instance of the device to use.
49 * @param[in] iid (Unused).
50 * @param[in] call Pointer to the call that represents interrupt.
51 */
52static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
53{
54 assert(dev);
55 hc_t *hc = &((uhci_t*)dev->driver_data)->hc;
56 uint16_t status = IPC_GET_ARG1(*call);
57 assert(hc);
58 hc_interrupt(hc, status);
59}
60/*----------------------------------------------------------------------------*/
61/** Get address of the device identified by handle.
62 *
63 * @param[in] dev DDF instance of the device to use.
64 * @param[in] iid (Unused).
65 * @param[in] call Pointer to the call that represents interrupt.
66 */
67static int usb_iface_get_address(
68 ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
69{
70 assert(fun);
71 usb_device_keeper_t *manager = &((uhci_t*)fun->dev->driver_data)->hc.manager;
72
73 usb_address_t addr = usb_device_keeper_find(manager, handle);
74 if (addr < 0) {
75 return addr;
76 }
77
78 if (address != NULL) {
79 *address = addr;
80 }
81
82 return EOK;
83}
84/*----------------------------------------------------------------------------*/
85/** Gets handle of the respective hc (this or parent device).
86 *
87 * @param[in] root_hub_fun Root hub function seeking hc handle.
88 * @param[out] handle Place to write the handle.
89 * @return Error code.
90 */
91static int usb_iface_get_hc_handle(
92 ddf_fun_t *fun, devman_handle_t *handle)
93{
94 assert(handle);
95 ddf_fun_t *hc_fun = ((uhci_t*)fun->dev->driver_data)->hc_fun;
96 assert(hc_fun != NULL);
97
98 *handle = hc_fun->handle;
99 return EOK;
100}
101/*----------------------------------------------------------------------------*/
102/** This iface is generic for both RH and HC. */
103static usb_iface_t usb_iface = {
104 .get_hc_handle = usb_iface_get_hc_handle,
105 .get_address = usb_iface_get_address
106};
107/*----------------------------------------------------------------------------*/
108static ddf_dev_ops_t hc_ops = {
109// .interfaces[USB_DEV_IFACE] = &usb_iface,
110 .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */
111};
112/*----------------------------------------------------------------------------*/
113/** Get root hub hw resources (I/O registers).
114 *
115 * @param[in] fun Root hub function.
116 * @return Pointer to the resource list used by the root hub.
117 */
118static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
119{
120 assert(fun);
121 return &((rh_t*)fun->driver_data)->resource_list;
122}
123/*----------------------------------------------------------------------------*/
124static hw_res_ops_t hw_res_iface = {
125 .get_resource_list = get_resource_list,
126 .enable_interrupt = NULL
127};
128/*----------------------------------------------------------------------------*/
129static ddf_dev_ops_t rh_ops = {
130 .interfaces[USB_DEV_IFACE] = &usb_iface,
131 .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
132};
133/*----------------------------------------------------------------------------*/
134/** Initialize hc and rh ddf structures and their respective drivers.
135 *
136 * @param[in] instance UHCI structure to use.
137 * @param[in] device DDF instance of the device to use.
138 *
139 * This function does all the preparatory work for hc and rh drivers:
140 * - gets device hw resources
141 * - disables UHCI legacy support
142 * - asks for interrupt
143 * - registers interrupt handler
144 */
145int uhci_init(uhci_t *instance, ddf_dev_t *device)
146{
147 assert(instance);
148 instance->hc_fun = NULL;
149 instance->rh_fun = NULL;
150#define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
151if (ret != EOK) { \
152 usb_log_error(message); \
153 if (instance->hc_fun) \
154 ddf_fun_destroy(instance->hc_fun); \
155 if (instance->rh_fun) \
156 ddf_fun_destroy(instance->rh_fun); \
157 return ret; \
158}
159
160 uintptr_t io_reg_base = 0;
161 size_t io_reg_size = 0;
162 int irq = 0;
163
164 int ret =
165 pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
166 CHECK_RET_DEST_FUN_RETURN(ret,
167 "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
168 usb_log_debug("I/O regs at 0x%X (size %zu), IRQ %d.\n",
169 io_reg_base, io_reg_size, irq);
170
171 ret = pci_disable_legacy(device);
172 CHECK_RET_DEST_FUN_RETURN(ret,
173 "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
174
175 bool interrupts = false;
176#ifdef CONFIG_USBHC_NO_INTERRUPTS
177 usb_log_warning("Interrupts disabled in OS config, " \
178 "falling back to polling.\n");
179#else
180 ret = pci_enable_interrupts(device);
181 if (ret != EOK) {
182 usb_log_warning("Failed to enable interrupts: %s.\n",
183 str_error(ret));
184 usb_log_info("HW interrupts not available, " \
185 "falling back to polling.\n");
186 } else {
187 usb_log_debug("Hw interrupts enabled.\n");
188 interrupts = true;
189 }
190#endif
191
192 instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci-hc");
193 ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
194 CHECK_RET_DEST_FUN_RETURN(ret,
195 "Failed(%d) to create HC function.\n", ret);
196
197 ret = hc_init(&instance->hc, instance->hc_fun,
198 (void*)io_reg_base, io_reg_size, interrupts);
199 CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to init uhci-hcd.\n", ret);
200 instance->hc_fun->ops = &hc_ops;
201 instance->hc_fun->driver_data = &instance->hc;
202 ret = ddf_fun_bind(instance->hc_fun);
203 CHECK_RET_DEST_FUN_RETURN(ret,
204 "Failed(%d) to bind UHCI device function: %s.\n",
205 ret, str_error(ret));
206#undef CHECK_RET_HC_RETURN
207
208#define CHECK_RET_FINI_RETURN(ret, message...) \
209if (ret != EOK) { \
210 usb_log_error(message); \
211 if (instance->hc_fun) \
212 ddf_fun_destroy(instance->hc_fun); \
213 if (instance->rh_fun) \
214 ddf_fun_destroy(instance->rh_fun); \
215 hc_fini(&instance->hc); \
216 return ret; \
217}
218
219 /* It does no harm if we register this on polling */
220 ret = register_interrupt_handler(device, irq, irq_handler,
221 &instance->hc.interrupt_code);
222 CHECK_RET_FINI_RETURN(ret,
223 "Failed(%d) to register interrupt handler.\n", ret);
224
225 instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci-rh");
226 ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
227 CHECK_RET_FINI_RETURN(ret,
228 "Failed(%d) to create root hub function.\n", ret);
229
230 ret = rh_init(&instance->rh, instance->rh_fun,
231 (uintptr_t)instance->hc.registers + 0x10, 4);
232 CHECK_RET_FINI_RETURN(ret,
233 "Failed(%d) to setup UHCI root hub.\n", ret);
234
235 instance->rh_fun->ops = &rh_ops;
236 instance->rh_fun->driver_data = &instance->rh;
237 ret = ddf_fun_bind(instance->rh_fun);
238 CHECK_RET_FINI_RETURN(ret,
239 "Failed(%d) to register UHCI root hub.\n", ret);
240
241 return EOK;
242#undef CHECK_RET_FINI_RETURN
243}
244/**
245 * @}
246 */
Note: See TracBrowser for help on using the repository browser.