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
RevLine 
[9351353]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 */
[56fd7cf]35
36/* XXX Fix this */
37#define _DDF_DATA_IMPLANT
38
[9351353]39#include <errno.h>
40#include <str_error.h>
41#include <ddf/interrupt.h>
42#include <usb/ddfiface.h>
43#include <usb/debug.h>
[a720ff6]44#include <usb/host/hcd.h>
[53332b5b]45#include <usb/host/ddf_helpers.h>
[9351353]46
47#include "uhci.h"
48
[795448f]49#include "res.h"
[d2bff2f]50#include "hc.h"
51#include "root_hub.h"
52
[76fbd9a]53
[9d2d444]54/** IRQ handling callback, forward status from call to diver structure.
[9351353]55 *
56 * @param[in] dev DDF instance of the device to use.
57 * @param[in] iid (Unused).
[9d2d444]58 * @param[in] call Pointer to the call from kernel.
[9351353]59 */
60static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
61{
62 assert(dev);
[a720ff6]63 hcd_t *hcd = dev_to_hcd(dev);
64 if (!hcd || !hcd->private_data) {
[dfe4955]65 usb_log_error("Interrupt on not yet initialized device.\n");
66 return;
67 }
[2df648c2]68 const uint16_t status = IPC_GET_ARG1(*call);
[a720ff6]69 hc_interrupt(hcd->private_data, status);
[9351353]70}
[76fbd9a]71
[9d2d444]72/** Initialize hc and rh DDF structures and their respective drivers.
[17ceb72]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:
[9d2d444]77 * - gets device's hw resources
78 * - disables UHCI legacy support (PCI config space)
[23f40280]79 * - attempts to enable interrupts
[17ceb72]80 * - registers interrupt handler
81 */
[d2bff2f]82int device_setup_uhci(ddf_dev_t *device)
[9351353]83{
[1664a26]84 if (!device)
85 return EBADMEM;
86
[a720ff6]87#define CHECK_RET_RETURN(ret, message...) \
[9351353]88if (ret != EOK) { \
[d2bff2f]89 usb_log_error(message); \
[9351353]90 return ret; \
[d2bff2f]91} else (void)0
92
93 uintptr_t reg_base = 0;
94 size_t reg_size = 0;
[9351353]95 int irq = 0;
96
[a720ff6]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",
[56fd7cf]99 ddf_dev_get_handle(device), str_error(ret));
[4125b7d]100 usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
[d2bff2f]101 (void *) reg_base, reg_size, irq);
[9351353]102
[d57122c]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);
[a720ff6]109 CHECK_RET_RETURN(ret, "Failed to generate IRQ commands: %s.\n",
110 str_error(ret));
[dfe4955]111
[d57122c]112 irq_code_t irq_code = {
113 .rangecount = ranges_count,
114 .ranges = irq_ranges,
115 .cmdcount = cmds_count,
116 .cmds = irq_cmds
117 };
[dfe4955]118
119 /* Register handler to avoid interrupt lockup */
120 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
[a720ff6]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));
[9351353]127
[ff34e5a]128 bool interrupts = false;
[795448f]129 ret = enable_interrupts(device);
[9351353]130 if (ret != EOK) {
[dfe4955]131 usb_log_warning("Failed to enable interrupts: %s."
132 " Falling back to polling.\n", str_error(ret));
[ff34e5a]133 } else {
134 usb_log_debug("Hw interrupts enabled.\n");
135 interrupts = true;
[9351353]136 }
137
[a720ff6]138 ddf_fun_t *hc_fun = NULL;
[0ef03d7]139 ret = hcd_ddf_setup_device(device, &hc_fun, USB_SPEED_FULL,
140 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
[a720ff6]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,
[408d3c9]149 "Failed to init uhci_hcd: %s.\n", str_error(ret));
[9d2d444]150
[a720ff6]151 hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
152
153// TODO: Undo hcd_setup_device
[9351353]154#define CHECK_RET_FINI_RETURN(ret, message...) \
155if (ret != EOK) { \
[a720ff6]156 hc_fini(hc); \
157 CHECK_RET_RETURN(ret, message); \
[9351353]158 return ret; \
[d2bff2f]159} else (void)0
[9351353]160
[a720ff6]161 ret = rh_init(device, (uintptr_t)hc->registers + 0x10, 4,
162 ddf_fun_get_handle(hc_fun));
[9351353]163 CHECK_RET_FINI_RETURN(ret,
[408d3c9]164 "Failed to setup UHCI root hub: %s.\n", str_error(ret));
[9351353]165
166 return EOK;
167#undef CHECK_RET_FINI_RETURN
168}
169/**
170 * @}
171 */
Note: See TracBrowser for help on using the repository browser.