| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 | /** @addtogroup drvusbohci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Main routines of OHCI driver.
|
|---|
| 34 | */
|
|---|
| 35 | #include <ddf/driver.h>
|
|---|
| 36 | #include <ddf/interrupt.h>
|
|---|
| 37 | #include <device/hw_res.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 |
|
|---|
| 41 | #include <usb_iface.h>
|
|---|
| 42 | #include <usb/ddfiface.h>
|
|---|
| 43 | #include <usb/debug.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "pci.h"
|
|---|
| 46 | #include "iface.h"
|
|---|
| 47 | #include "ohci_hc.h"
|
|---|
| 48 |
|
|---|
| 49 | static int ohci_add_device(ddf_dev_t *device);
|
|---|
| 50 | /*----------------------------------------------------------------------------*/
|
|---|
| 51 | /** IRQ handling callback, identifies device
|
|---|
| 52 | *
|
|---|
| 53 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 54 | * @param[in] iid (Unused).
|
|---|
| 55 | * @param[in] call Pointer to the call that represents interrupt.
|
|---|
| 56 | */
|
|---|
| 57 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 58 | {
|
|---|
| 59 | assert(dev);
|
|---|
| 60 | ohci_hc_t *hc = (ohci_hc_t*)dev->driver_data;
|
|---|
| 61 | assert(hc);
|
|---|
| 62 | ohci_hc_interrupt(hc, 0);
|
|---|
| 63 | }
|
|---|
| 64 | /*----------------------------------------------------------------------------*/
|
|---|
| 65 | static driver_ops_t ohci_driver_ops = {
|
|---|
| 66 | .add_device = ohci_add_device,
|
|---|
| 67 | };
|
|---|
| 68 | /*----------------------------------------------------------------------------*/
|
|---|
| 69 | static driver_t ohci_driver = {
|
|---|
| 70 | .name = NAME,
|
|---|
| 71 | .driver_ops = &ohci_driver_ops
|
|---|
| 72 | };
|
|---|
| 73 | static ddf_dev_ops_t hc_ops = {
|
|---|
| 74 | .interfaces[USBHC_DEV_IFACE] = &ohci_hc_iface,
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /*----------------------------------------------------------------------------*/
|
|---|
| 78 | /** Initializes a new ddf driver instance of OHCI hcd.
|
|---|
| 79 | *
|
|---|
| 80 | * @param[in] device DDF instance of the device to initialize.
|
|---|
| 81 | * @return Error code.
|
|---|
| 82 | */
|
|---|
| 83 | static int ohci_add_device(ddf_dev_t *device)
|
|---|
| 84 | {
|
|---|
| 85 | assert(device);
|
|---|
| 86 | #define CHECK_RET_RETURN(ret, message...) \
|
|---|
| 87 | if (ret != EOK) { \
|
|---|
| 88 | usb_log_error(message); \
|
|---|
| 89 | return ret; \
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | uintptr_t mem_reg_base = 0;
|
|---|
| 93 | size_t mem_reg_size = 0;
|
|---|
| 94 | int irq = 0;
|
|---|
| 95 |
|
|---|
| 96 | int ret =
|
|---|
| 97 | pci_get_my_registers(device, &mem_reg_base, &mem_reg_size, &irq);
|
|---|
| 98 | CHECK_RET_RETURN(ret,
|
|---|
| 99 | "Failed(%d) to get memory addresses:.\n", ret, device->handle);
|
|---|
| 100 | usb_log_info("Memory mapped regs at 0x%X (size %zu), IRQ %d.\n",
|
|---|
| 101 | mem_reg_base, mem_reg_size, irq);
|
|---|
| 102 |
|
|---|
| 103 | ret = pci_disable_legacy(device);
|
|---|
| 104 | CHECK_RET_RETURN(ret,
|
|---|
| 105 | "Failed(%d) disable legacy USB: %s.\n", ret, str_error(ret));
|
|---|
| 106 |
|
|---|
| 107 | ohci_hc_t *hcd = malloc(sizeof(ohci_hc_t));
|
|---|
| 108 | if (hcd == NULL) {
|
|---|
| 109 | usb_log_error("Failed to allocate OHCI driver.\n");
|
|---|
| 110 | return ENOMEM;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | ddf_fun_t *hc_fun = ddf_fun_create(device, fun_exposed, "ohci-hc");
|
|---|
| 114 | if (hc_fun == NULL) {
|
|---|
| 115 | usb_log_error("Failed to create OHCI function.\n");
|
|---|
| 116 | free(hcd);
|
|---|
| 117 | return ENOMEM;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | bool interrupts = false;
|
|---|
| 121 | ret = pci_enable_interrupts(device);
|
|---|
| 122 | if (ret != EOK) {
|
|---|
| 123 | usb_log_warning(
|
|---|
| 124 | "Failed(%d) to enable interrupts, fall back to polling.\n",
|
|---|
| 125 | ret);
|
|---|
| 126 | } else {
|
|---|
| 127 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 128 | interrupts = true;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | ret = ohci_hc_init(hcd, hc_fun, mem_reg_base, mem_reg_size, interrupts);
|
|---|
| 132 | if (ret != EOK) {
|
|---|
| 133 | usb_log_error("Failed to initialize OHCI driver.\n");
|
|---|
| 134 | free(hcd);
|
|---|
| 135 | return ret;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | ret = register_interrupt_handler(device, irq, irq_handler, NULL);
|
|---|
| 139 |
|
|---|
| 140 | hc_fun->ops = &hc_ops;
|
|---|
| 141 | ret = ddf_fun_bind(hc_fun);
|
|---|
| 142 | if (ret != EOK) {
|
|---|
| 143 | usb_log_error("Failed to bind OHCI function.\n");
|
|---|
| 144 | ddf_fun_destroy(hc_fun);
|
|---|
| 145 | free(hcd);
|
|---|
| 146 | return ret;
|
|---|
| 147 | }
|
|---|
| 148 | hc_fun->driver_data = hcd;
|
|---|
| 149 |
|
|---|
| 150 | /* TODO: register interrupt handler */
|
|---|
| 151 |
|
|---|
| 152 | usb_log_info("Controlling new OHCI device `%s' (handle %llu).\n",
|
|---|
| 153 | device->name, device->handle);
|
|---|
| 154 |
|
|---|
| 155 | return EOK;
|
|---|
| 156 | #undef CHECK_RET_RETURN
|
|---|
| 157 | }
|
|---|
| 158 | /*----------------------------------------------------------------------------*/
|
|---|
| 159 | /** Initializes global driver structures (NONE).
|
|---|
| 160 | *
|
|---|
| 161 | * @param[in] argc Nmber of arguments in argv vector (ignored).
|
|---|
| 162 | * @param[in] argv Cmdline argument vector (ignored).
|
|---|
| 163 | * @return Error code.
|
|---|
| 164 | *
|
|---|
| 165 | * Driver debug level is set here.
|
|---|
| 166 | */
|
|---|
| 167 | int main(int argc, char *argv[])
|
|---|
| 168 | {
|
|---|
| 169 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
|---|
| 170 | sleep(5);
|
|---|
| 171 | return ddf_driver_main(&ohci_driver);
|
|---|
| 172 | }
|
|---|
| 173 | /**
|
|---|
| 174 | * @}
|
|---|
| 175 | */
|
|---|