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

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

usb host: Use all hw resources when generating irq code.

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