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

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

usbmid: Use initialized parent to get address and hc handle.

Remove forwarding helpers from libusb.
Remove unused headers.

  • 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/debug.h>
[a720ff6]43#include <usb/host/hcd.h>
[53332b5b]44#include <usb/host/ddf_helpers.h>
[9351353]45
46#include "uhci.h"
47
[795448f]48#include "res.h"
[d2bff2f]49#include "hc.h"
50
[76fbd9a]51
[9d2d444]52/** IRQ handling callback, forward status from call to diver structure.
[9351353]53 *
54 * @param[in] dev DDF instance of the device to use.
55 * @param[in] iid (Unused).
[9d2d444]56 * @param[in] call Pointer to the call from kernel.
[9351353]57 */
58static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
59{
60 assert(dev);
[a720ff6]61 hcd_t *hcd = dev_to_hcd(dev);
62 if (!hcd || !hcd->private_data) {
[dfe4955]63 usb_log_error("Interrupt on not yet initialized device.\n");
64 return;
65 }
[2df648c2]66 const uint16_t status = IPC_GET_ARG1(*call);
[a720ff6]67 hc_interrupt(hcd->private_data, status);
[9351353]68}
[76fbd9a]69
[9d2d444]70/** Initialize hc and rh DDF structures and their respective drivers.
[17ceb72]71 *
72 * @param[in] device DDF instance of the device to use.
73 *
74 * This function does all the preparatory work for hc and rh drivers:
[9d2d444]75 * - gets device's hw resources
76 * - disables UHCI legacy support (PCI config space)
[23f40280]77 * - attempts to enable interrupts
[17ceb72]78 * - registers interrupt handler
79 */
[d2bff2f]80int device_setup_uhci(ddf_dev_t *device)
[9351353]81{
[1664a26]82 if (!device)
83 return EBADMEM;
84
[a720ff6]85#define CHECK_RET_RETURN(ret, message...) \
[9351353]86if (ret != EOK) { \
[d2bff2f]87 usb_log_error(message); \
[9351353]88 return ret; \
[d2bff2f]89} else (void)0
90
91 uintptr_t reg_base = 0;
92 size_t reg_size = 0;
[9351353]93 int irq = 0;
94
[a720ff6]95 int ret = get_my_registers(device, &reg_base, &reg_size, &irq);
96 CHECK_RET_RETURN(ret, "Failed to get I/O region for %" PRIun ": %s.\n",
[56fd7cf]97 ddf_dev_get_handle(device), str_error(ret));
[4125b7d]98 usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
[d2bff2f]99 (void *) reg_base, reg_size, irq);
[9351353]100
[d57122c]101 const size_t ranges_count = hc_irq_pio_range_count();
102 const size_t cmds_count = hc_irq_cmd_count();
103 irq_pio_range_t irq_ranges[ranges_count];
104 irq_cmd_t irq_cmds[cmds_count];
105 ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
106 sizeof(irq_cmds), reg_base, reg_size);
[a720ff6]107 CHECK_RET_RETURN(ret, "Failed to generate IRQ commands: %s.\n",
108 str_error(ret));
[dfe4955]109
[d57122c]110 irq_code_t irq_code = {
111 .rangecount = ranges_count,
112 .ranges = irq_ranges,
113 .cmdcount = cmds_count,
114 .cmds = irq_cmds
115 };
[dfe4955]116
117 /* Register handler to avoid interrupt lockup */
118 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
[a720ff6]119 CHECK_RET_RETURN(ret, "Failed to register interrupt handler: %s.\n",
120 str_error(ret));
121
122 ret = disable_legacy(device);
123 CHECK_RET_RETURN(ret, "Failed to disable legacy USB: %s.\n",
124 str_error(ret));
[9351353]125
[ff34e5a]126 bool interrupts = false;
[795448f]127 ret = enable_interrupts(device);
[9351353]128 if (ret != EOK) {
[dfe4955]129 usb_log_warning("Failed to enable interrupts: %s."
130 " Falling back to polling.\n", str_error(ret));
[ff34e5a]131 } else {
132 usb_log_debug("Hw interrupts enabled.\n");
133 interrupts = true;
[9351353]134 }
135
[a720ff6]136 ddf_fun_t *hc_fun = NULL;
[0ef03d7]137 ret = hcd_ddf_setup_device(device, &hc_fun, USB_SPEED_FULL,
138 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
[a720ff6]139 CHECK_RET_RETURN(ret, "Failed to setup UHCI HCD.\n");
140
141 hc_t *hc = malloc(sizeof(hc_t));
142 ret = hc ? EOK : ENOMEM;
143 CHECK_RET_RETURN(ret, "Failed to allocate UHCI HC structure.\n");
144
145 ret = hc_init(hc, (void*)reg_base, reg_size, interrupts);
146 CHECK_RET_RETURN(ret,
[408d3c9]147 "Failed to init uhci_hcd: %s.\n", str_error(ret));
[9d2d444]148
[a720ff6]149 hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
150
151// TODO: Undo hcd_setup_device
[9351353]152#define CHECK_RET_FINI_RETURN(ret, message...) \
153if (ret != EOK) { \
[a720ff6]154 hc_fini(hc); \
155 CHECK_RET_RETURN(ret, message); \
[9351353]156 return ret; \
[d2bff2f]157} else (void)0
[9351353]158
[3848fec]159 ret = hcd_ddf_setup_root_hub(device, USB_SPEED_FULL);
[9351353]160 CHECK_RET_FINI_RETURN(ret,
[408d3c9]161 "Failed to setup UHCI root hub: %s.\n", str_error(ret));
[9351353]162
163 return EOK;
164#undef CHECK_RET_FINI_RETURN
165}
166/**
167 * @}
168 */
Note: See TracBrowser for help on using the repository browser.