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

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

ohci, uhci: Don't unregister interrupt handler on setup failure.

This is handled by the library helper.

  • Property mode set to 100644
File size: 5.4 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#include <assert.h>
37#include <ddf/driver.h>
38#include <ddf/interrupt.h>
39#include <device/hw_res_parsed.h>
40#include <device/pci.h>
41#include <devman.h>
42#include <errno.h>
43#include <stdbool.h>
44#include <stdlib.h>
45#include <str_error.h>
46#include <sys/types.h>
47
48#include <usb/debug.h>
49#include <usb/usb.h>
50#include <usb/host/ddf_helpers.h>
51#include <usb/host/hcd.h>
52#include <usb/host/usb_bus.h>
53
54#include "uhci.h"
55
56#include "hc.h"
57
58
59/** IRQ handling callback, forward status from call to diver structure.
60 *
61 * @param[in] dev DDF instance of the device to use.
62 * @param[in] iid (Unused).
63 * @param[in] call Pointer to the call from kernel.
64 */
65static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
66{
67 assert(dev);
68 hcd_t *hcd = dev_to_hcd(dev);
69 if (!hcd || !hcd->driver.data) {
70 usb_log_error("Interrupt on not yet initialized device.\n");
71 return;
72 }
73 const uint16_t status = IPC_GET_ARG1(*call);
74 hc_interrupt(hcd->driver.data, status);
75}
76
77/** Call the PCI driver with a request to clear legacy support register
78 *
79 * @param[in] device Device asking to disable interrupts
80 * @return Error code.
81 */
82static int disable_legacy(ddf_dev_t *device)
83{
84 assert(device);
85
86 async_sess_t *parent_sess = devman_parent_device_connect(
87 EXCHANGE_SERIALIZE, ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
88 if (!parent_sess)
89 return ENOMEM;
90
91 /* See UHCI design guide page 45 for these values.
92 * Write all WC bits in USB legacy register */
93 const int rc = pci_config_space_write_16(parent_sess, 0xc0, 0xaf00);
94
95 async_hangup(parent_sess);
96 return rc;
97}
98
99/** Initialize hc and rh DDF structures and their respective drivers.
100 *
101 * @param[in] device DDF instance of the device to use.
102 *
103 * This function does all the preparatory work for hc and rh drivers:
104 * - gets device's hw resources
105 * - disables UHCI legacy support (PCI config space)
106 * - attempts to enable interrupts
107 * - registers interrupt handler
108 */
109int device_setup_uhci(ddf_dev_t *device)
110{
111 assert(device);
112
113 hw_res_list_parsed_t hw_res;
114 int ret = hcd_ddf_get_registers(device, &hw_res);
115 if (ret != EOK ||
116 hw_res.irqs.count != 1 || hw_res.io_ranges.count != 1) {
117 usb_log_error("Failed to get register memory addresses "
118 "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
119 str_error(ret));
120 return ret;
121 }
122 addr_range_t regs = hw_res.io_ranges.ranges[0];
123 const int irq = hw_res.irqs.irqs[0];
124 hw_res_list_parsed_clean(&hw_res);
125
126 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
127 RNGABSPTR(regs), RNGSZ(regs), irq);
128
129 ret = hcd_ddf_setup_hc(device, USB_SPEED_FULL,
130 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
131 if (ret != EOK) {
132 usb_log_error("Failed to setup generic HCD.\n");
133 return ret;
134 }
135
136 hc_t *hc = malloc(sizeof(hc_t));
137 if (!hc) {
138 usb_log_error("Failed to allocate UHCI HC structure.\n");
139 ret = ENOMEM;
140 goto ddf_hc_clean;
141 }
142
143 bool interrupts = false;
144 ret = hcd_ddf_setup_interrupts(device, &regs, irq, irq_handler,
145 hc_gen_irq_code);
146 if (ret != EOK) {
147 usb_log_warning("Failed to enable interrupts: %s."
148 " Falling back to polling.\n", str_error(ret));
149 } else {
150 usb_log_debug("Hw interrupts enabled.\n");
151 interrupts = true;
152 }
153
154 ret = disable_legacy(device);
155 if (ret != EOK) {
156 usb_log_error("Failed to disable legacy USB: %s.\n",
157 str_error(ret));
158 goto irq_unregister;
159 }
160
161 ret = hc_init(hc, &regs, interrupts);
162 if (ret != EOK) {
163 usb_log_error("Failed to init uhci_hcd: %s.\n", str_error(ret));
164 goto irq_unregister;
165 // TODO This is unfortunate, we have neither legacy nor real USB
166 }
167
168 hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
169
170 /*
171 * Creating root hub registers a new USB device so HC
172 * needs to be ready at this time.
173 */
174 ret = hcd_ddf_setup_root_hub(device);
175 if (ret != EOK) {
176 usb_log_error("Failed to setup UHCI root hub: %s.\n",
177 str_error(ret));
178 hc_fini(hc);
179irq_unregister:
180 unregister_interrupt_handler(device, irq);
181 free(hc);
182ddf_hc_clean:
183 hcd_ddf_clean_hc(device);
184 }
185 return ret;
186}
187/**
188 * @}
189 */
Note: See TracBrowser for help on using the repository browser.