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

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

Remove all traces of uhcirh

  • Property mode set to 100644
File size: 5.2 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
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/ddfiface.h>
43#include <usb/debug.h>
44#include <usb/host/hcd.h>
45#include <usb/host/ddf_helpers.h>
46
47#include "uhci.h"
48
49#include "res.h"
50#include "hc.h"
51
52
53/** IRQ handling callback, forward status from call to diver structure.
54 *
55 * @param[in] dev DDF instance of the device to use.
56 * @param[in] iid (Unused).
57 * @param[in] call Pointer to the call from kernel.
58 */
59static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
60{
61 assert(dev);
62 hcd_t *hcd = dev_to_hcd(dev);
63 if (!hcd || !hcd->private_data) {
64 usb_log_error("Interrupt on not yet initialized device.\n");
65 return;
66 }
67 const uint16_t status = IPC_GET_ARG1(*call);
68 hc_interrupt(hcd->private_data, status);
69}
70
71/** Initialize hc and rh DDF structures and their respective drivers.
72 *
73 * @param[in] device DDF instance of the device to use.
74 *
75 * This function does all the preparatory work for hc and rh drivers:
76 * - gets device's hw resources
77 * - disables UHCI legacy support (PCI config space)
78 * - attempts to enable interrupts
79 * - registers interrupt handler
80 */
81int device_setup_uhci(ddf_dev_t *device)
82{
83 if (!device)
84 return EBADMEM;
85
86#define CHECK_RET_RETURN(ret, message...) \
87if (ret != EOK) { \
88 usb_log_error(message); \
89 return ret; \
90} else (void)0
91
92 uintptr_t reg_base = 0;
93 size_t reg_size = 0;
94 int irq = 0;
95
96 int ret = get_my_registers(device, &reg_base, &reg_size, &irq);
97 CHECK_RET_RETURN(ret, "Failed to get I/O region for %" PRIun ": %s.\n",
98 ddf_dev_get_handle(device), str_error(ret));
99 usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
100 (void *) reg_base, reg_size, irq);
101
102 const size_t ranges_count = hc_irq_pio_range_count();
103 const size_t cmds_count = hc_irq_cmd_count();
104 irq_pio_range_t irq_ranges[ranges_count];
105 irq_cmd_t irq_cmds[cmds_count];
106 ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
107 sizeof(irq_cmds), reg_base, reg_size);
108 CHECK_RET_RETURN(ret, "Failed to generate IRQ commands: %s.\n",
109 str_error(ret));
110
111 irq_code_t irq_code = {
112 .rangecount = ranges_count,
113 .ranges = irq_ranges,
114 .cmdcount = cmds_count,
115 .cmds = irq_cmds
116 };
117
118 /* Register handler to avoid interrupt lockup */
119 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
120 CHECK_RET_RETURN(ret, "Failed to register interrupt handler: %s.\n",
121 str_error(ret));
122
123 ret = disable_legacy(device);
124 CHECK_RET_RETURN(ret, "Failed to disable legacy USB: %s.\n",
125 str_error(ret));
126
127 bool interrupts = false;
128 ret = enable_interrupts(device);
129 if (ret != EOK) {
130 usb_log_warning("Failed to enable interrupts: %s."
131 " Falling back to polling.\n", str_error(ret));
132 } else {
133 usb_log_debug("Hw interrupts enabled.\n");
134 interrupts = true;
135 }
136
137 ddf_fun_t *hc_fun = NULL;
138 ret = hcd_ddf_setup_device(device, &hc_fun, USB_SPEED_FULL,
139 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
140 CHECK_RET_RETURN(ret, "Failed to setup UHCI HCD.\n");
141
142 hc_t *hc = malloc(sizeof(hc_t));
143 ret = hc ? EOK : ENOMEM;
144 CHECK_RET_RETURN(ret, "Failed to allocate UHCI HC structure.\n");
145
146 ret = hc_init(hc, (void*)reg_base, reg_size, interrupts);
147 CHECK_RET_RETURN(ret,
148 "Failed to init uhci_hcd: %s.\n", str_error(ret));
149
150 hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
151
152// TODO: Undo hcd_setup_device
153#define CHECK_RET_FINI_RETURN(ret, message...) \
154if (ret != EOK) { \
155 hc_fini(hc); \
156 CHECK_RET_RETURN(ret, message); \
157 return ret; \
158} else (void)0
159
160 ret = hcd_ddf_setup_root_hub(device, USB_SPEED_FULL);
161 CHECK_RET_FINI_RETURN(ret,
162 "Failed to setup UHCI root hub: %s.\n", str_error(ret));
163
164 return EOK;
165#undef CHECK_RET_FINI_RETURN
166}
167/**
168 * @}
169 */
Note: See TracBrowser for help on using the repository browser.