| [c7137738] | 1 | /*
|
|---|
| [c56dbe0] | 2 | * Copyright (c) 2011 Vojtech Horky, Jan Vesely
|
|---|
| [c7137738] | 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 | */
|
|---|
| [c56dbe0] | 28 | /** @addtogroup usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| [eb1a2f4] | 34 | #include <ddf/driver.h>
|
|---|
| 35 | #include <ddf/interrupt.h>
|
|---|
| [fb78ae72] | 36 | #include <device/hw_res.h>
|
|---|
| [c56dbe0] | 37 | #include <errno.h>
|
|---|
| [ab414a6] | 38 | #include <str_error.h>
|
|---|
| [c56dbe0] | 39 |
|
|---|
| [ab414a6] | 40 | #include <usb_iface.h>
|
|---|
| 41 | #include <usb/ddfiface.h>
|
|---|
| [afcd86e] | 42 | #include <usb/debug.h>
|
|---|
| 43 |
|
|---|
| [3515533] | 44 | #include "iface.h"
|
|---|
| [1256a0a] | 45 | #include "pci.h"
|
|---|
| 46 | #include "root_hub.h"
|
|---|
| [4317827] | 47 | #include "uhci.h"
|
|---|
| [c7137738] | 48 |
|
|---|
| [afcd86e] | 49 | #define NAME "uhci-hcd"
|
|---|
| 50 |
|
|---|
| [eb1a2f4] | 51 | static int uhci_add_device(ddf_dev_t *device);
|
|---|
| [36a4738] | 52 | /*----------------------------------------------------------------------------*/
|
|---|
| 53 | static driver_ops_t uhci_driver_ops = {
|
|---|
| 54 | .add_device = uhci_add_device,
|
|---|
| 55 | };
|
|---|
| 56 | /*----------------------------------------------------------------------------*/
|
|---|
| 57 | static driver_t uhci_driver = {
|
|---|
| 58 | .name = NAME,
|
|---|
| 59 | .driver_ops = &uhci_driver_ops
|
|---|
| 60 | };
|
|---|
| 61 | /*----------------------------------------------------------------------------*/
|
|---|
| [eb1a2f4] | 62 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| [36a4738] | 63 | {
|
|---|
| [eb1a2f4] | 64 | assert(dev);
|
|---|
| 65 | uhci_t *hc = dev_to_uhci(dev);
|
|---|
| [733a9a8] | 66 | uint16_t status = IPC_GET_ARG1(*call);
|
|---|
| [2736b13e] | 67 | assert(hc);
|
|---|
| [733a9a8] | 68 | uhci_interrupt(hc, status);
|
|---|
| [36a4738] | 69 | }
|
|---|
| 70 | /*----------------------------------------------------------------------------*/
|
|---|
| [86341e2] | 71 | static int uhci_add_device(ddf_dev_t *device)
|
|---|
| 72 | {
|
|---|
| 73 | assert(device);
|
|---|
| 74 | uhci_t *hcd = NULL;
|
|---|
| 75 | #define CHECK_RET_FREE_HC_RETURN(ret, message...) \
|
|---|
| [b9d910f] | 76 | if (ret != EOK) { \
|
|---|
| 77 | usb_log_error(message); \
|
|---|
| [86341e2] | 78 | if (hcd != NULL) \
|
|---|
| 79 | free(hcd); \
|
|---|
| [b9d910f] | 80 | return ret; \
|
|---|
| 81 | }
|
|---|
| [c7137738] | 82 |
|
|---|
| [afcd86e] | 83 | usb_log_info("uhci_add_device() called\n");
|
|---|
| [eb1a2f4] | 84 |
|
|---|
| [cabda7f] | 85 | uintptr_t io_reg_base = 0;
|
|---|
| 86 | size_t io_reg_size = 0;
|
|---|
| 87 | int irq = 0;
|
|---|
| [ea991e84] | 88 |
|
|---|
| [890af992] | 89 | int ret =
|
|---|
| 90 | pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
|
|---|
| [86341e2] | 91 | CHECK_RET_FREE_HC_RETURN(ret,
|
|---|
| [fb78ae72] | 92 | "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
|
|---|
| [afcd86e] | 93 | usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
|
|---|
| [ea991e84] | 94 | io_reg_base, io_reg_size, irq);
|
|---|
| 95 |
|
|---|
| [ab414a6] | 96 | ret = pci_disable_legacy(device);
|
|---|
| 97 | CHECK_RET_FREE_HC_RETURN(ret,
|
|---|
| 98 | "Failed(%d) disable legacy USB: %s.\n", ret, str_error(ret));
|
|---|
| 99 |
|
|---|
| [b375bb8] | 100 | #if 0
|
|---|
| 101 | ret = pci_enable_interrupts(device);
|
|---|
| 102 | if (ret != EOK) {
|
|---|
| 103 | usb_log_warning(
|
|---|
| 104 | "Failed(%d) to enable interrupts, fall back to polling.\n",
|
|---|
| 105 | ret);
|
|---|
| 106 | }
|
|---|
| 107 | #endif
|
|---|
| [acce4fc] | 108 |
|
|---|
| [86341e2] | 109 | hcd = malloc(sizeof(uhci_t));
|
|---|
| 110 | ret = (hcd != NULL) ? EOK : ENOMEM;
|
|---|
| [b375bb8] | 111 | CHECK_RET_FREE_HC_RETURN(ret,
|
|---|
| 112 | "Failed(%d) to allocate memory for uhci hcd.\n", ret);
|
|---|
| [1256a0a] | 113 |
|
|---|
| [86341e2] | 114 | ret = uhci_init(hcd, device, (void*)io_reg_base, io_reg_size);
|
|---|
| 115 | CHECK_RET_FREE_HC_RETURN(ret, "Failed(%d) to init uhci-hcd.\n",
|
|---|
| 116 | ret);
|
|---|
| 117 | #undef CHECK_RET_FREE_HC_RETURN
|
|---|
| [b9d910f] | 118 |
|
|---|
| [eb1a2f4] | 119 | /*
|
|---|
| [86341e2] | 120 | * We might free hcd, but that does not matter since no one
|
|---|
| [eb1a2f4] | 121 | * else would access driver_data anyway.
|
|---|
| 122 | */
|
|---|
| [86341e2] | 123 | device->driver_data = hcd;
|
|---|
| 124 |
|
|---|
| 125 | ddf_fun_t *rh = NULL;
|
|---|
| 126 | #define CHECK_RET_FINI_FREE_RETURN(ret, message...) \
|
|---|
| 127 | if (ret != EOK) { \
|
|---|
| 128 | usb_log_error(message); \
|
|---|
| 129 | if (hcd != NULL) {\
|
|---|
| 130 | uhci_fini(hcd); \
|
|---|
| 131 | free(hcd); \
|
|---|
| 132 | } \
|
|---|
| 133 | if (rh != NULL) \
|
|---|
| 134 | free(rh); \
|
|---|
| 135 | return ret; \
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| [b375bb8] | 138 | /* It does no harm if we register this on polling */
|
|---|
| [b9d910f] | 139 | ret = register_interrupt_handler(device, irq, irq_handler,
|
|---|
| [86341e2] | 140 | &hcd->interrupt_code);
|
|---|
| [b375bb8] | 141 | CHECK_RET_FINI_FREE_RETURN(ret,
|
|---|
| 142 | "Failed(%d) to register interrupt handler.\n", ret);
|
|---|
| [86341e2] | 143 |
|
|---|
| [1256a0a] | 144 | ret = setup_root_hub(&rh, device);
|
|---|
| [b375bb8] | 145 | CHECK_RET_FINI_FREE_RETURN(ret,
|
|---|
| 146 | "Failed(%d) to setup UHCI root hub.\n", ret);
|
|---|
| [86341e2] | 147 | rh->driver_data = hcd->ddf_instance;
|
|---|
| [1256a0a] | 148 |
|
|---|
| [eb1a2f4] | 149 | ret = ddf_fun_bind(rh);
|
|---|
| [b375bb8] | 150 | CHECK_RET_FINI_FREE_RETURN(ret,
|
|---|
| 151 | "Failed(%d) to register UHCI root hub.\n", ret);
|
|---|
| [1256a0a] | 152 |
|
|---|
| 153 | return EOK;
|
|---|
| [86341e2] | 154 | #undef CHECK_RET_FINI_FREE_RETURN
|
|---|
| [c7137738] | 155 | }
|
|---|
| [36a4738] | 156 | /*----------------------------------------------------------------------------*/
|
|---|
| [c7137738] | 157 | int main(int argc, char *argv[])
|
|---|
| 158 | {
|
|---|
| [36a4738] | 159 | sleep(3);
|
|---|
| [cabda7f] | 160 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
|---|
| [c7137738] | 161 |
|
|---|
| [eb1a2f4] | 162 | return ddf_driver_main(&uhci_driver);
|
|---|
| [c7137738] | 163 | }
|
|---|
| [c56dbe0] | 164 | /**
|
|---|
| 165 | * @}
|
|---|
| 166 | */
|
|---|