source: mainline/uspace/drv/uhci-hcd/uhci.c@ 92b41f33

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

Refactoring final touches

  • Property mode set to 100644
File size: 7.1 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 drvusbuhci
30 * @{
31 */
32/** @file
33 * @brief UHCI driver
34 */
35#include <errno.h>
36#include <str_error.h>
37#include <ddf/interrupt.h>
38#include <usb_iface.h>
39#include <usb/ddfiface.h>
40#include <usb/debug.h>
41
42#include "uhci.h"
43#include "iface.h"
44#include "pci.h"
45
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 uhci_hc_t *hc = &((uhci_t*)dev->driver_data)->hc;
57 uint16_t status = IPC_GET_ARG1(*call);
58 assert(hc);
59 uhci_hc_interrupt(hc, status);
60}
61/*----------------------------------------------------------------------------*/
62static int usb_iface_get_address(
63 ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
64{
65 assert(fun);
66 device_keeper_t *manager = &((uhci_t*)fun->dev->driver_data)->hc.device_manager;
67
68 usb_address_t addr = device_keeper_find(manager, handle);
69 if (addr < 0) {
70 return addr;
71 }
72
73 if (address != NULL) {
74 *address = addr;
75 }
76
77 return EOK;
78}
79/*----------------------------------------------------------------------------*/
80/** Gets handle of the respective hc (this or parent device).
81 *
82 * @param[in] root_hub_fun Root hub function seeking hc handle.
83 * @param[out] handle Place to write the handle.
84 * @return Error code.
85 */
86static int usb_iface_get_hc_handle(
87 ddf_fun_t *fun, devman_handle_t *handle)
88{
89 assert(handle);
90 ddf_fun_t *hc_fun = ((uhci_t*)fun->dev->driver_data)->hc_fun;
91 assert(hc_fun != NULL);
92
93 *handle = hc_fun->handle;
94 return EOK;
95}
96/*----------------------------------------------------------------------------*/
97/** This iface is generic for both RH and HC. */
98static usb_iface_t usb_iface = {
99 .get_hc_handle = usb_iface_get_hc_handle,
100 .get_address = usb_iface_get_address
101};
102/*----------------------------------------------------------------------------*/
103static ddf_dev_ops_t uhci_hc_ops = {
104 .interfaces[USB_DEV_IFACE] = &usb_iface,
105 .interfaces[USBHC_DEV_IFACE] = &uhci_hc_iface, /* see iface.h/c */
106};
107/*----------------------------------------------------------------------------*/
108/** Gets root hub hw resources.
109 *
110 * @param[in] fun Root hub function.
111 * @return Pointer to the resource list used by the root hub.
112 */
113static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
114{
115 assert(fun);
116 return &((uhci_rh_t*)fun->driver_data)->resource_list;
117}
118/*----------------------------------------------------------------------------*/
119static hw_res_ops_t hw_res_iface = {
120 .get_resource_list = get_resource_list,
121 .enable_interrupt = NULL
122};
123/*----------------------------------------------------------------------------*/
124static ddf_dev_ops_t uhci_rh_ops = {
125 .interfaces[USB_DEV_IFACE] = &usb_iface,
126 .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
127};
128/*----------------------------------------------------------------------------*/
129int uhci_init(uhci_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 io_reg_base = 0;
145 size_t io_reg_size = 0;
146 int irq = 0;
147
148 int ret =
149 pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
150 CHECK_RET_DEST_FUN_RETURN(ret,
151 "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
152 usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
153 io_reg_base, io_reg_size, irq);
154
155 ret = pci_disable_legacy(device);
156 CHECK_RET_DEST_FUN_RETURN(ret,
157 "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
158
159#if 0
160 ret = pci_enable_interrupts(device);
161 if (ret != EOK) {
162 usb_log_warning(
163 "Failed(%d) to enable interrupts, fall back to polling.\n",
164 ret);
165 }
166#endif
167
168 instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci-hc");
169 ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
170 CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to create HC function.\n", ret);
171
172 ret = uhci_hc_init(
173 &instance->hc, instance->hc_fun, (void*)io_reg_base, io_reg_size);
174 CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to init uhci-hcd.\n", ret);
175 instance->hc_fun->ops = &uhci_hc_ops;
176 instance->hc_fun->driver_data = &instance->hc;
177 ret = ddf_fun_bind(instance->hc_fun);
178 CHECK_RET_DEST_FUN_RETURN(ret,
179 "Failed(%d) to bind UHCI device function: %s.\n",
180 ret, str_error(ret));
181#undef CHECK_RET_HC_RETURN
182
183#define CHECK_RET_FINI_RETURN(ret, message...) \
184if (ret != EOK) { \
185 usb_log_error(message); \
186 if (instance->hc_fun) \
187 ddf_fun_destroy(instance->hc_fun); \
188 if (instance->rh_fun) \
189 ddf_fun_destroy(instance->rh_fun); \
190 uhci_hc_fini(&instance->hc); \
191 return ret; \
192}
193
194 /* It does no harm if we register this on polling */
195 ret = register_interrupt_handler(device, irq, irq_handler,
196 &instance->hc.interrupt_code);
197 CHECK_RET_FINI_RETURN(ret,
198 "Failed(%d) to register interrupt handler.\n", ret);
199
200 instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci-rh");
201 ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
202 CHECK_RET_FINI_RETURN(ret,
203 "Failed(%d) to create root hub function.\n", ret);
204
205 ret = uhci_rh_init(&instance->rh, instance->rh_fun,
206 (uintptr_t)instance->hc.registers + 0x10, 4);
207 CHECK_RET_FINI_RETURN(ret,
208 "Failed(%d) to setup UHCI root hub.\n", ret);
209
210 instance->rh_fun->ops = &uhci_rh_ops;
211 instance->rh_fun->driver_data = &instance->rh;
212 ret = ddf_fun_bind(instance->rh_fun);
213 CHECK_RET_FINI_RETURN(ret,
214 "Failed(%d) to register UHCI root hub.\n", ret);
215
216 return EOK;
217#undef CHECK_RET_FINI_RETURN
218}
219
220/**
221 * @}
222 */
Note: See TracBrowser for help on using the repository browser.