source: mainline/uspace/drv/bus/usb/ohci/ohci.c@ d25e0a4

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

ohci: OHCI root hub knows only its own address.

  • Property mode set to 100644
File size: 7.7 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 "pci.h"
45#include "hc.h"
46
47typedef struct ohci {
48 ddf_fun_t *hc_fun;
49 ddf_fun_t *rh_fun;
50
51 hc_t hc;
52} ohci_t;
53
54static inline ohci_t * dev_to_ohci(ddf_dev_t *dev)
55{
56 assert(dev);
57 return dev->driver_data;
58}
59/** IRQ handling callback, identifies device
60 *
61 * @param[in] dev DDF instance of the device to use.
62 * @param[in] iid (Unused).
63 * @param[in] call Pointer to the call that represents interrupt.
64 */
65static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
66{
67 assert(dev);
68
69 ohci_t *ohci = dev_to_ohci(dev);
70 if (!ohci) {
71 usb_log_warning("Interrupt on device that is not ready.\n");
72 return;
73 }
74 const uint16_t status = IPC_GET_ARG1(*call);
75 hc_interrupt(&ohci->hc, status);
76}
77/*----------------------------------------------------------------------------*/
78/** Get address of the device identified by handle.
79 *
80 * @param[in] dev DDF instance of the device to use.
81 * @param[in] iid (Unused).
82 * @param[in] call Pointer to the call that represents interrupt.
83 */
84static int usb_iface_get_address(
85 ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
86{
87 assert(fun);
88 assert(handle == 0);
89
90 if (address != NULL) {
91 *address = dev_to_ohci(fun->dev)->hc.rh.address;
92 }
93
94 return EOK;
95}
96/*----------------------------------------------------------------------------*/
97/** Gets handle of the respective hc (this device, hc function).
98 *
99 * @param[in] root_hub_fun Root hub function seeking hc handle.
100 * @param[out] handle Place to write the handle.
101 * @return Error code.
102 */
103static int usb_iface_get_hc_handle(
104 ddf_fun_t *fun, devman_handle_t *handle)
105{
106 assert(fun);
107 ddf_fun_t *hc_fun = dev_to_ohci(fun->dev)->hc_fun;
108 assert(hc_fun);
109
110 if (handle != NULL)
111 *handle = hc_fun->handle;
112 return EOK;
113}
114/*----------------------------------------------------------------------------*/
115/** Root hub USB interface */
116static usb_iface_t usb_iface = {
117 .get_hc_handle = usb_iface_get_hc_handle,
118 .get_address = usb_iface_get_address
119};
120/*----------------------------------------------------------------------------*/
121/** Standard USB HC options (HC interface) */
122static ddf_dev_ops_t hc_ops = {
123 .interfaces[USBHC_DEV_IFACE] = &hcd_iface,
124};
125/*----------------------------------------------------------------------------*/
126/** Standard USB RH options (RH interface) */
127static ddf_dev_ops_t rh_ops = {
128 .interfaces[USB_DEV_IFACE] = &usb_iface,
129};
130/*----------------------------------------------------------------------------*/
131/** Initialize hc and rh ddf structures and their respective drivers.
132 *
133 * @param[in] device DDF instance of the device to use.
134 * @param[in] instance OHCI structure to use.
135 *
136 * This function does all the preparatory work for hc and rh drivers:
137 * - gets device hw resources
138 * - disables OHCI legacy support
139 * - asks for interrupt
140 * - registers interrupt handler
141 */
142int device_setup_ohci(ddf_dev_t *device)
143{
144 assert(device);
145
146 ohci_t *instance = malloc(sizeof(ohci_t));
147 if (instance == NULL) {
148 usb_log_error("Failed to allocate OHCI driver.\n");
149 return ENOMEM;
150 }
151 instance->rh_fun = NULL;
152 instance->hc_fun = NULL;
153
154#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
155if (ret != EOK) { \
156 if (instance->hc_fun) { \
157 ddf_fun_destroy(instance->hc_fun); \
158 } \
159 if (instance->rh_fun) { \
160 ddf_fun_destroy(instance->rh_fun); \
161 } \
162 free(instance); \
163 usb_log_error(message); \
164 return ret; \
165} else (void)0
166
167 instance->hc_fun = ddf_fun_create(device, fun_exposed, "ohci_hc");
168 int ret = instance->hc_fun ? EOK : ENOMEM;
169 CHECK_RET_DEST_FREE_RETURN(ret,
170 "Failed to create OHCI HC function: %s.\n", str_error(ret));
171 instance->hc_fun->ops = &hc_ops;
172 instance->hc_fun->driver_data = &instance->hc;
173
174 instance->rh_fun = ddf_fun_create(device, fun_inner, "ohci_rh");
175 ret = instance->rh_fun ? EOK : ENOMEM;
176 CHECK_RET_DEST_FREE_RETURN(ret,
177 "Failed to create OHCI RH function: %s.\n", str_error(ret));
178 instance->rh_fun->ops = &rh_ops;
179
180 uintptr_t reg_base = 0;
181 size_t reg_size = 0;
182 int irq = 0;
183
184 ret = pci_get_my_registers(device, &reg_base, &reg_size, &irq);
185 CHECK_RET_DEST_FREE_RETURN(ret,
186 "Failed to get register memory addresses for %" PRIun ": %s.\n",
187 device->handle, str_error(ret));
188 usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
189 (void *) reg_base, reg_size, irq);
190
191 const size_t cmd_count = hc_irq_cmd_count();
192 irq_cmd_t irq_cmds[cmd_count];
193 irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = irq_cmds };
194
195 ret =
196 hc_get_irq_commands(irq_cmds, sizeof(irq_cmds), reg_base, reg_size);
197 CHECK_RET_DEST_FREE_RETURN(ret,
198 "Failed to generate IRQ commands: %s.\n", str_error(ret));
199
200
201 /* Register handler to avoid interrupt lockup */
202 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
203 CHECK_RET_DEST_FREE_RETURN(ret,
204 "Failed to register interrupt handler: %s.\n", str_error(ret));
205
206 /* Try to enable interrupts */
207 bool interrupts = false;
208 ret = pci_enable_interrupts(device);
209 if (ret != EOK) {
210 usb_log_warning("Failed to enable interrupts: %s."
211 " Falling back to polling\n", str_error(ret));
212 /* We don't need that handler */
213 unregister_interrupt_handler(device, irq);
214 } else {
215 usb_log_debug("Hw interrupts enabled.\n");
216 interrupts = true;
217 }
218
219 ret = hc_init(&instance->hc, reg_base, reg_size, interrupts);
220 CHECK_RET_DEST_FREE_RETURN(ret,
221 "Failed to init ohci_hcd: %s.\n", str_error(ret));
222
223 device->driver_data = instance;
224
225#define CHECK_RET_FINI_RETURN(ret, message...) \
226if (ret != EOK) { \
227 hc_fini(&instance->hc); \
228 unregister_interrupt_handler(device, irq); \
229 CHECK_RET_DEST_FREE_RETURN(ret, message); \
230} else (void)0
231
232
233 ret = ddf_fun_bind(instance->hc_fun);
234 CHECK_RET_FINI_RETURN(ret,
235 "Failed to bind OHCI device function: %s.\n", str_error(ret));
236
237 ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
238 CHECK_RET_FINI_RETURN(ret,
239 "Failed to add OHCI to HC class: %s.\n", str_error(ret));
240
241 ret = hc_register_hub(&instance->hc, instance->rh_fun);
242 CHECK_RET_FINI_RETURN(ret,
243 "Failed to register OHCI root hub: %s.\n", str_error(ret));
244 return ret;
245
246#undef CHECK_RET_FINI_RETURN
247}
248/**
249 * @}
250 */
Note: See TracBrowser for help on using the repository browser.