source: mainline/uspace/drv/bus/usb/ohci/ohci.c@ 8d40181

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

ohci,uhci,ehci: Use library provided 'get_registers' function

  • Property mode set to 100644
File size: 4.9 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 drvusbohci
30 * @{
31 */
32/** @file
33 * @brief OHCI driver
34 */
35
36#include <errno.h>
37#include <str_error.h>
38#include <ddf/interrupt.h>
39#include <usb/usb.h>
40#include <usb/debug.h>
41
42#include <usb/host/ddf_helpers.h>
43
44#include "ohci.h"
45#include "hc.h"
46
47
48/** IRQ handling callback, identifies device
49 *
50 * @param[in] dev DDF instance of the device to use.
51 * @param[in] iid (Unused).
52 * @param[in] call Pointer to the call that represents interrupt.
53 */
54static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
55{
56 assert(dev);
57 hcd_t *hcd = dev_to_hcd(dev);
58 if (!hcd || !hcd->driver.data) {
59 usb_log_warning("Interrupt on device that is not ready.\n");
60 return;
61 }
62
63 const uint16_t status = IPC_GET_ARG1(*call);
64 hc_interrupt(hcd->driver.data, status);
65}
66
67/** Initialize hc and rh ddf structures and their respective drivers.
68 *
69 * @param[in] device DDF instance of the device to use.
70 * @param[in] instance OHCI structure to use.
71 *
72 * This function does all the preparatory work for hc and rh drivers:
73 * - gets device hw resources
74 * - disables OHCI legacy support
75 * - asks for interrupt
76 * - registers interrupt handler
77 */
78int device_setup_ohci(ddf_dev_t *device)
79{
80 hw_res_list_parsed_t hw_res;
81 int ret = hcd_ddf_get_registers(device, &hw_res);
82 if (ret != EOK ||
83 hw_res.irqs.count != 1 || hw_res.mem_ranges.count != 1) {
84 usb_log_error("Failed to get register memory addresses "
85 "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
86 str_error(ret));
87 return ret;
88 }
89 addr_range_t regs = hw_res.mem_ranges.ranges[0];
90 const int irq = hw_res.irqs.irqs[0];
91 hw_res_list_parsed_clean(&hw_res);
92
93
94 usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
95 RNGABSPTR(regs), RNGSZ(regs), irq);
96
97 /* Initialize generic HCD driver */
98 ret = hcd_ddf_setup_hc(device, USB_SPEED_FULL,
99 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
100 if (ret != EOK) {
101 usb_log_error("Failedd to setup generic hcd: %s.",
102 str_error(ret));
103 return ret;
104 }
105
106 ret = hc_register_irq_handler(device, &regs, irq, irq_handler);
107 if (ret != EOK) {
108 usb_log_error("Failed to register interrupt handler: %s.\n",
109 str_error(ret));
110 hcd_ddf_clean_hc(device);
111 return ret;
112 }
113
114 /* Try to enable interrupts */
115 bool interrupts = false;
116 ret = hcd_ddf_enable_interrupts(device);
117 if (ret != EOK) {
118 usb_log_warning("Failed to enable interrupts: %s."
119 " Falling back to polling\n", str_error(ret));
120 /* We don't need that handler */
121 unregister_interrupt_handler(device, irq);
122 } else {
123 usb_log_debug("Hw interrupts enabled.\n");
124 interrupts = true;
125 }
126
127
128 hc_t *hc_impl = malloc(sizeof(hc_t));
129 if (!hc_impl) {
130 usb_log_error("Failed to allocate driver structure.\n");
131 hcd_ddf_clean_hc(device);
132 unregister_interrupt_handler(device, irq);
133 return ENOMEM;
134 }
135
136 /* Initialize OHCI HC */
137 ret = hc_init(hc_impl, &regs, interrupts);
138 if (ret != EOK) {
139 usb_log_error("Failed to init hc: %s.\n", str_error(ret));
140 hcd_ddf_clean_hc(device);
141 unregister_interrupt_handler(device, irq);
142 return ret;
143 }
144
145 /* Connect OHCI to generic HCD */
146 hcd_set_implementation(dev_to_hcd(device), hc_impl,
147 hc_schedule, ohci_endpoint_init, ohci_endpoint_fini);
148
149 /* HC should be running OK. We can add root hub */
150 ret = hcd_ddf_setup_root_hub(device);
151 if (ret != EOK) {
152 usb_log_error("Failed to registter OHCI root hub: %s.\n",
153 str_error(ret));
154 hcd_ddf_clean_hc(device);
155 unregister_interrupt_handler(device, irq);
156 return ret;
157 }
158
159 return ret;
160}
161/**
162 * @}
163 */
Note: See TracBrowser for help on using the repository browser.