source: mainline/uspace/drv/bus/usb/ohci/ohci.c@ 4cf5b8e0

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

usb: Move HC driver implementation functions to a separate structure.

  • Property mode set to 100644
File size: 4.7 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 "res.h"
46#include "hc.h"
47
48
49
50/** IRQ handling callback, identifies device
51 *
52 * @param[in] dev DDF instance of the device to use.
53 * @param[in] iid (Unused).
54 * @param[in] call Pointer to the call that represents interrupt.
55 */
56static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
57{
58 assert(dev);
59 hcd_t *hcd = dev_to_hcd(dev);
60 if (!hcd || !hcd->driver.data) {
61 usb_log_warning("Interrupt on device that is not ready.\n");
62 return;
63 }
64
65 const uint16_t status = IPC_GET_ARG1(*call);
66 hc_interrupt(hcd->driver.data, status);
67}
68
69/** Initialize hc and rh ddf structures and their respective drivers.
70 *
71 * @param[in] device DDF instance of the device to use.
72 * @param[in] instance OHCI structure to use.
73 *
74 * This function does all the preparatory work for hc and rh drivers:
75 * - gets device hw resources
76 * - disables OHCI legacy support
77 * - asks for interrupt
78 * - registers interrupt handler
79 */
80int device_setup_ohci(ddf_dev_t *device)
81{
82
83 addr_range_t regs;
84 int irq = 0;
85
86 int ret = get_my_registers(device, &regs, &irq);
87 if (ret != EOK) {
88 usb_log_error("Failed to get register memory addresses "
89 "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
90 str_error(ret));
91 return ret;
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 = 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 ret;
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.