source: mainline/uspace/drv/uhci-hcd/main.c@ 269bd9b4

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

Doxygen and other comments

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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/** @addtogroup usb
29 * @{
30 */
31/** @file
32 * @brief UHCI driver
33 */
34#include <ddf/driver.h>
35#include <ddf/interrupt.h>
36#include <device/hw_res.h>
37#include <errno.h>
38#include <str_error.h>
39
40#include <usb_iface.h>
41#include <usb/ddfiface.h>
42#include <usb/debug.h>
43
44#include "iface.h"
45#include "pci.h"
46#include "root_hub.h"
47#include "uhci.h"
48
49#define NAME "uhci-hcd"
50
51static int uhci_add_device(ddf_dev_t *device);
52/*----------------------------------------------------------------------------*/
53static driver_ops_t uhci_driver_ops = {
54 .add_device = uhci_add_device,
55};
56/*----------------------------------------------------------------------------*/
57static driver_t uhci_driver = {
58 .name = NAME,
59 .driver_ops = &uhci_driver_ops
60};
61/*----------------------------------------------------------------------------*/
62/** IRQ handling callback, identifies devic
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 uhci_t *hc = dev_to_uhci(dev);
72 uint16_t status = IPC_GET_ARG1(*call);
73 assert(hc);
74 uhci_interrupt(hc, status);
75}
76/*----------------------------------------------------------------------------*/
77/** Initializes a new ddf driver instance of UHCI hcd.
78 *
79 * @param[in] device DDF instance of the device to initialize.
80 * @return Error code.
81 *
82 * Gets and initialies hardware resources, disables any legacy support,
83 * and reports root hub device.
84 */
85int uhci_add_device(ddf_dev_t *device)
86{
87 assert(device);
88 uhci_t *hcd = NULL;
89#define CHECK_RET_FREE_HC_RETURN(ret, message...) \
90if (ret != EOK) { \
91 usb_log_error(message); \
92 if (hcd != NULL) \
93 free(hcd); \
94 return ret; \
95}
96
97 usb_log_info("uhci_add_device() called\n");
98
99 uintptr_t io_reg_base = 0;
100 size_t io_reg_size = 0;
101 int irq = 0;
102
103 int ret =
104 pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
105 CHECK_RET_FREE_HC_RETURN(ret,
106 "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
107 usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
108 io_reg_base, io_reg_size, irq);
109
110 ret = pci_disable_legacy(device);
111 CHECK_RET_FREE_HC_RETURN(ret,
112 "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
113
114#if 0
115 ret = pci_enable_interrupts(device);
116 if (ret != EOK) {
117 usb_log_warning(
118 "Failed(%d) to enable interrupts, fall back to polling.\n",
119 ret);
120 }
121#endif
122
123 hcd = malloc(sizeof(uhci_t));
124 ret = (hcd != NULL) ? EOK : ENOMEM;
125 CHECK_RET_FREE_HC_RETURN(ret,
126 "Failed(%d) to allocate memory for uhci hcd.\n", ret);
127
128 ret = uhci_init(hcd, device, (void*)io_reg_base, io_reg_size);
129 CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to init uhci-hcd.\n", ret);
130#undef CHECK_RET_FREE_HC_RETURN
131
132 /*
133 * We might free hcd, but that does not matter since no one
134 * else would access driver_data anyway.
135 */
136 device->driver_data = hcd;
137
138 ddf_fun_t *rh = NULL;
139#define CHECK_RET_FINI_FREE_RETURN(ret, message...) \
140if (ret != EOK) { \
141 usb_log_error(message); \
142 if (hcd != NULL) {\
143 uhci_fini(hcd); \
144 free(hcd); \
145 } \
146 if (rh != NULL) \
147 free(rh); \
148 return ret; \
149}
150
151 /* It does no harm if we register this on polling */
152 ret = register_interrupt_handler(device, irq, irq_handler,
153 &hcd->interrupt_code);
154 CHECK_RET_FINI_FREE_RETURN(ret,
155 "Failed(%d) to register interrupt handler.\n", ret);
156
157 ret = setup_root_hub(&rh, device);
158 CHECK_RET_FINI_FREE_RETURN(ret,
159 "Failed(%d) to setup UHCI root hub.\n", ret);
160 rh->driver_data = hcd->ddf_instance;
161
162 ret = ddf_fun_bind(rh);
163 CHECK_RET_FINI_FREE_RETURN(ret,
164 "Failed(%d) to register UHCI root hub.\n", ret);
165
166 return EOK;
167#undef CHECK_RET_FINI_FREE_RETURN
168}
169/*----------------------------------------------------------------------------*/
170/** Initializes global driver structures (NONE).
171 *
172 * @param[in] argc Nmber of arguments in argv vector (ignored).
173 * @param[in] argv Cmdline argument vector (ignored).
174 * @return Error code.
175 *
176 * Driver debug level is set here.
177 */
178int main(int argc, char *argv[])
179{
180 sleep(3);
181 usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
182
183 return ddf_driver_main(&uhci_driver);
184}
185/**
186 * @}
187 */
Note: See TracBrowser for help on using the repository browser.