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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 03362fbd was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

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