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

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

OHCI: Start hw during driver initialization. Do not expose start function.

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