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

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

libusbhost: Add speed and bw parameters to hcd_ddf_setup_device.

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