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

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

libusbhost: Merge find_by_address and get_speed.

These functions were almost identical.
Clear handle on address release.

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