| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky, 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 | /** @addtogroup usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <driver.h>
|
|---|
| 35 | #include <usb_iface.h>
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "iface.h"
|
|---|
| 42 | #include "pci.h"
|
|---|
| 43 | #include "root_hub.h"
|
|---|
| 44 | #include "uhci.h"
|
|---|
| 45 |
|
|---|
| 46 | #define NAME "uhci-hcd"
|
|---|
| 47 |
|
|---|
| 48 | static int uhci_add_device(device_t *device);
|
|---|
| 49 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle);
|
|---|
| 50 | /*----------------------------------------------------------------------------*/
|
|---|
| 51 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
|
|---|
| 52 | {
|
|---|
| 53 | /* This shall be called only for the UHCI itself. */
|
|---|
| 54 | assert(dev->parent == NULL);
|
|---|
| 55 |
|
|---|
| 56 | *handle = dev->handle;
|
|---|
| 57 | return EOK;
|
|---|
| 58 | }
|
|---|
| 59 | /*----------------------------------------------------------------------------*/
|
|---|
| 60 | static usb_iface_t hc_usb_iface = {
|
|---|
| 61 | .get_hc_handle = usb_iface_get_hc_handle
|
|---|
| 62 | };
|
|---|
| 63 | /*----------------------------------------------------------------------------*/
|
|---|
| 64 | static device_ops_t uhci_ops = {
|
|---|
| 65 | .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
|
|---|
| 66 | .interfaces[USBHC_DEV_IFACE] = &uhci_iface
|
|---|
| 67 | };
|
|---|
| 68 | /*----------------------------------------------------------------------------*/
|
|---|
| 69 | static driver_ops_t uhci_driver_ops = {
|
|---|
| 70 | .add_device = uhci_add_device,
|
|---|
| 71 | };
|
|---|
| 72 | /*----------------------------------------------------------------------------*/
|
|---|
| 73 | static driver_t uhci_driver = {
|
|---|
| 74 | .name = NAME,
|
|---|
| 75 | .driver_ops = &uhci_driver_ops
|
|---|
| 76 | };
|
|---|
| 77 | /*----------------------------------------------------------------------------*/
|
|---|
| 78 | static void irq_handler(device_t *device, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 79 | {
|
|---|
| 80 | assert(device);
|
|---|
| 81 | uhci_t *hc = dev_to_uhci(device);
|
|---|
| 82 | usb_log_info("LOL HARDWARE INTERRUPT: %p.\n", hc);
|
|---|
| 83 | assert(hc);
|
|---|
| 84 | uhci_interrupt(hc);
|
|---|
| 85 | }
|
|---|
| 86 | /*----------------------------------------------------------------------------*/
|
|---|
| 87 | static int uhci_add_device(device_t *device)
|
|---|
| 88 | {
|
|---|
| 89 | assert(device);
|
|---|
| 90 |
|
|---|
| 91 | usb_log_info("uhci_add_device() called\n");
|
|---|
| 92 | device->ops = &uhci_ops;
|
|---|
| 93 |
|
|---|
| 94 | uintptr_t io_reg_base;
|
|---|
| 95 | size_t io_reg_size;
|
|---|
| 96 | int irq;
|
|---|
| 97 |
|
|---|
| 98 | int ret =
|
|---|
| 99 | pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
|
|---|
| 100 |
|
|---|
| 101 | if (ret != EOK) {
|
|---|
| 102 | usb_log_error(
|
|---|
| 103 | "Failed(%d) to get I/O registers addresses for device:.\n",
|
|---|
| 104 | ret, device->handle);
|
|---|
| 105 | return ret;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
|
|---|
| 109 | io_reg_base, io_reg_size, irq);
|
|---|
| 110 |
|
|---|
| 111 | ret = register_interrupt_handler(device, irq, irq_handler, NULL);
|
|---|
| 112 | usb_log_debug("Registered interrupt handler %d.\n", ret);
|
|---|
| 113 |
|
|---|
| 114 | uhci_t *uhci_hc = malloc(sizeof(uhci_t));
|
|---|
| 115 | if (!uhci_hc) {
|
|---|
| 116 | usb_log_error("Failed to allocate memory for uhci hcd driver.\n");
|
|---|
| 117 | return ENOMEM;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | ret = uhci_init(uhci_hc, (void*)io_reg_base, io_reg_size);
|
|---|
| 121 | if (ret != EOK) {
|
|---|
| 122 | usb_log_error("Failed to init uhci-hcd.\n");
|
|---|
| 123 | return ret;
|
|---|
| 124 | }
|
|---|
| 125 | device_t *rh;
|
|---|
| 126 | ret = setup_root_hub(&rh, device);
|
|---|
| 127 | if (ret != EOK) {
|
|---|
| 128 | usb_log_error("Failed to setup uhci root hub.\n");
|
|---|
| 129 | /* TODO: destroy uhci here */
|
|---|
| 130 | return ret;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | ret = child_device_register(rh, device);
|
|---|
| 134 | if (ret != EOK) {
|
|---|
| 135 | usb_log_error("Failed to register root hub.\n");
|
|---|
| 136 | /* TODO: destroy uhci here */
|
|---|
| 137 | return ret;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | device->driver_data = uhci_hc;
|
|---|
| 141 |
|
|---|
| 142 | return EOK;
|
|---|
| 143 | }
|
|---|
| 144 | /*----------------------------------------------------------------------------*/
|
|---|
| 145 | int main(int argc, char *argv[])
|
|---|
| 146 | {
|
|---|
| 147 | sleep(3);
|
|---|
| 148 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
|---|
| 149 |
|
|---|
| 150 | return driver_main(&uhci_driver);
|
|---|
| 151 | }
|
|---|
| 152 | /**
|
|---|
| 153 | * @}
|
|---|
| 154 | */
|
|---|