source: mainline/uspace/drv/ohci/ohci.c@ 7ab7c7f6

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

Reset OHCI root hub even if taking control from SMM driver

postpone root hub registration

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