source: mainline/uspace/drv/bus/usb/uhci/uhci.c@ 54464f6a

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

uhci: UHCI root hub does not have an address.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[9351353]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 "pci.h"
44
[d2bff2f]45#include "hc.h"
46#include "root_hub.h"
47
48/** Structure representing both functions of UHCI hc, USB host controller
49 * and USB root hub */
50typedef struct uhci {
51 /** Pointer to DDF represenation of UHCI host controller */
52 ddf_fun_t *hc_fun;
53 /** Pointer to DDF represenation of UHCI root hub */
54 ddf_fun_t *rh_fun;
55
56 /** Internal driver's represenation of UHCI host controller */
57 hc_t hc;
58 /** Internal driver's represenation of UHCI root hub */
59 rh_t rh;
60} uhci_t;
61
[2df648c2]62static inline uhci_t * dev_to_uhci(const ddf_dev_t *dev)
[d2bff2f]63{
64 assert(dev);
65 return dev->driver_data;
66}
67/*----------------------------------------------------------------------------*/
[9d2d444]68/** IRQ handling callback, forward status from call to diver structure.
[9351353]69 *
70 * @param[in] dev DDF instance of the device to use.
71 * @param[in] iid (Unused).
[9d2d444]72 * @param[in] call Pointer to the call from kernel.
[9351353]73 */
74static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
75{
76 assert(dev);
[2df648c2]77 uhci_t *uhci = dev_to_uhci(dev);
[dfe4955]78 if (!uhci) {
79 usb_log_error("Interrupt on not yet initialized device.\n");
80 return;
81 }
[2df648c2]82 const uint16_t status = IPC_GET_ARG1(*call);
[fc6e607]83 hc_interrupt(&uhci->hc, status);
[9351353]84}
85/*----------------------------------------------------------------------------*/
[2df648c2]86/** Operations supported by the HC driver */
87static ddf_dev_ops_t hc_ops = {
[5fe0a697]88 .interfaces[USBHC_DEV_IFACE] = &hcd_iface, /* see iface.h/c */
[2df648c2]89};
90/*----------------------------------------------------------------------------*/
[9d2d444]91/** Gets handle of the respective hc.
[9351353]92 *
[9d2d444]93 * @param[in] fun DDF function of uhci device.
[ea993d18]94 * @param[out] handle Host cotnroller handle.
[9351353]95 * @return Error code.
96 */
[2df648c2]97static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
[9351353]98{
[d2bff2f]99 assert(fun);
100 ddf_fun_t *hc_fun = dev_to_uhci(fun->dev)->hc_fun;
101 assert(hc_fun);
[9351353]102
[d2bff2f]103 if (handle != NULL)
104 *handle = hc_fun->handle;
[9351353]105 return EOK;
106}
107/*----------------------------------------------------------------------------*/
[9d2d444]108/** USB interface implementation used by RH */
[9351353]109static usb_iface_t usb_iface = {
110 .get_hc_handle = usb_iface_get_hc_handle,
111};
112/*----------------------------------------------------------------------------*/
[17ceb72]113/** Get root hub hw resources (I/O registers).
[9351353]114 *
115 * @param[in] fun Root hub function.
116 * @return Pointer to the resource list used by the root hub.
117 */
118static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
119{
120 assert(fun);
[e247d83]121 rh_t *rh = fun->driver_data;
122 assert(rh);
123 return &rh->resource_list;
[9351353]124}
125/*----------------------------------------------------------------------------*/
[9d2d444]126/** Interface to provide the root hub driver with hw info */
[9351353]127static hw_res_ops_t hw_res_iface = {
128 .get_resource_list = get_resource_list,
[d2bff2f]129 .enable_interrupt = NULL,
[9351353]130};
131/*----------------------------------------------------------------------------*/
[8d6c1f1]132/** RH function support for uhci_rhd */
[33fbe95]133static ddf_dev_ops_t rh_ops = {
[9351353]134 .interfaces[USB_DEV_IFACE] = &usb_iface,
135 .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
136};
137/*----------------------------------------------------------------------------*/
[9d2d444]138/** Initialize hc and rh DDF structures and their respective drivers.
[17ceb72]139 *
140 * @param[in] device DDF instance of the device to use.
141 *
142 * This function does all the preparatory work for hc and rh drivers:
[9d2d444]143 * - gets device's hw resources
144 * - disables UHCI legacy support (PCI config space)
[23f40280]145 * - attempts to enable interrupts
[17ceb72]146 * - registers interrupt handler
147 */
[d2bff2f]148int device_setup_uhci(ddf_dev_t *device)
[9351353]149{
[d2bff2f]150 assert(device);
151 uhci_t *instance = malloc(sizeof(uhci_t));
152 if (instance == NULL) {
153 usb_log_error("Failed to allocate OHCI driver.\n");
154 return ENOMEM;
155 }
156
157#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
[9351353]158if (ret != EOK) { \
159 if (instance->hc_fun) \
[d2bff2f]160 instance->hc_fun->ops = NULL; \
161 instance->hc_fun->driver_data = NULL; \
[9351353]162 ddf_fun_destroy(instance->hc_fun); \
[d2bff2f]163 if (instance->rh_fun) {\
164 instance->rh_fun->ops = NULL; \
165 instance->rh_fun->driver_data = NULL; \
[9351353]166 ddf_fun_destroy(instance->rh_fun); \
[d2bff2f]167 } \
[dfe4955]168 device->driver_data = NULL; \
[d2bff2f]169 usb_log_error(message); \
[9351353]170 return ret; \
[d2bff2f]171} else (void)0
172
173 instance->rh_fun = NULL;
[8d6c1f1]174 instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
[d2bff2f]175 int ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
176 CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI HC function.\n");
177 instance->hc_fun->ops = &hc_ops;
[5fe0a697]178 instance->hc_fun->driver_data = &instance->hc.generic;
[d2bff2f]179
[8d6c1f1]180 instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
[d2bff2f]181 ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
182 CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI RH function.\n");
183 instance->rh_fun->ops = &rh_ops;
184 instance->rh_fun->driver_data = &instance->rh;
[9351353]185
[d2bff2f]186 uintptr_t reg_base = 0;
187 size_t reg_size = 0;
[9351353]188 int irq = 0;
189
[d2bff2f]190 ret = pci_get_my_registers(device, &reg_base, &reg_size, &irq);
191 CHECK_RET_DEST_FREE_RETURN(ret,
[4125b7d]192 "Failed to get I/O addresses for %" PRIun ": %s.\n",
193 device->handle, str_error(ret));
194 usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
[d2bff2f]195 (void *) reg_base, reg_size, irq);
[9351353]196
197 ret = pci_disable_legacy(device);
[d2bff2f]198 CHECK_RET_DEST_FREE_RETURN(ret,
[dfe4955]199 "Failed to disable legacy USB: %s.\n", str_error(ret));
200
201 const size_t cmd_count = hc_irq_cmd_count();
202 irq_cmd_t irq_cmds[cmd_count];
203 ret =
204 hc_get_irq_commands(irq_cmds, sizeof(irq_cmds), reg_base, reg_size);
205 CHECK_RET_DEST_FREE_RETURN(ret,
206 "Failed to generate IRQ commands: %s.\n", str_error(ret));
207
208 irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = irq_cmds };
209
210 /* Register handler to avoid interrupt lockup */
211 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
212 CHECK_RET_DEST_FREE_RETURN(ret,
213 "Failed to register interrupt handler: %s.\n", str_error(ret));
[9351353]214
[ff34e5a]215 bool interrupts = false;
[9351353]216 ret = pci_enable_interrupts(device);
217 if (ret != EOK) {
[dfe4955]218 usb_log_warning("Failed to enable interrupts: %s."
219 " Falling back to polling.\n", str_error(ret));
[ff34e5a]220 } else {
221 usb_log_debug("Hw interrupts enabled.\n");
222 interrupts = true;
[9351353]223 }
224
[d2bff2f]225 ret = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
226 CHECK_RET_DEST_FREE_RETURN(ret,
[408d3c9]227 "Failed to init uhci_hcd: %s.\n", str_error(ret));
[9d2d444]228
[dfe4955]229 device->driver_data = instance;
230
[9351353]231#define CHECK_RET_FINI_RETURN(ret, message...) \
232if (ret != EOK) { \
[c01cd32]233 hc_fini(&instance->hc); \
[d2bff2f]234 CHECK_RET_DEST_FREE_RETURN(ret, message); \
[9351353]235 return ret; \
[d2bff2f]236} else (void)0
[9351353]237
[d2bff2f]238 ret = ddf_fun_bind(instance->hc_fun);
[408d3c9]239 CHECK_RET_FINI_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
240 str_error(ret));
[9351353]241
[1dc4a5e]242 ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
[3ae5ca8]243 CHECK_RET_FINI_RETURN(ret,
[76ef94e]244 "Failed to add UHCI to HC class: %s.\n", str_error(ret));
245
[33fbe95]246 ret = rh_init(&instance->rh, instance->rh_fun,
[9351353]247 (uintptr_t)instance->hc.registers + 0x10, 4);
248 CHECK_RET_FINI_RETURN(ret,
[408d3c9]249 "Failed to setup UHCI root hub: %s.\n", str_error(ret));
[9351353]250
251 ret = ddf_fun_bind(instance->rh_fun);
252 CHECK_RET_FINI_RETURN(ret,
[408d3c9]253 "Failed to register UHCI root hub: %s.\n", str_error(ret));
[9351353]254
255 return EOK;
256#undef CHECK_RET_FINI_RETURN
257}
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.