[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>
|
---|
[4687fcd4] | 36 | #include <usb_iface.h>
|
---|
[063ead6f] | 37 | #include <usb/ddfiface.h>
|
---|
[fb78ae72] | 38 | #include <device/hw_res.h>
|
---|
[3515533] | 39 |
|
---|
[c56dbe0] | 40 | #include <errno.h>
|
---|
| 41 |
|
---|
[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);
|
---|
[71ed4849] | 52 |
|
---|
[36a4738] | 53 | /*----------------------------------------------------------------------------*/
|
---|
| 54 | static driver_ops_t uhci_driver_ops = {
|
---|
| 55 | .add_device = uhci_add_device,
|
---|
| 56 | };
|
---|
| 57 | /*----------------------------------------------------------------------------*/
|
---|
| 58 | static driver_t uhci_driver = {
|
---|
| 59 | .name = NAME,
|
---|
| 60 | .driver_ops = &uhci_driver_ops
|
---|
| 61 | };
|
---|
| 62 | /*----------------------------------------------------------------------------*/
|
---|
[eb1a2f4] | 63 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
[36a4738] | 64 | {
|
---|
[eb1a2f4] | 65 | assert(dev);
|
---|
| 66 | uhci_t *hc = dev_to_uhci(dev);
|
---|
[733a9a8] | 67 | uint16_t status = IPC_GET_ARG1(*call);
|
---|
[2736b13e] | 68 | assert(hc);
|
---|
[733a9a8] | 69 | uhci_interrupt(hc, status);
|
---|
[36a4738] | 70 | }
|
---|
| 71 | /*----------------------------------------------------------------------------*/
|
---|
[b9d910f] | 72 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
| 73 | if (ret != EOK) { \
|
---|
| 74 | usb_log_error(message); \
|
---|
| 75 | return ret; \
|
---|
| 76 | }
|
---|
[c7137738] | 77 |
|
---|
[eb1a2f4] | 78 | static int uhci_add_device(ddf_dev_t *device)
|
---|
[c7137738] | 79 | {
|
---|
[afcd86e] | 80 | assert(device);
|
---|
| 81 |
|
---|
| 82 | usb_log_info("uhci_add_device() called\n");
|
---|
[eb1a2f4] | 83 |
|
---|
[c7137738] | 84 |
|
---|
[ea991e84] | 85 | uintptr_t io_reg_base;
|
---|
| 86 | size_t io_reg_size;
|
---|
| 87 | int irq;
|
---|
| 88 |
|
---|
[890af992] | 89 | int ret =
|
---|
| 90 | pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
|
---|
[ea991e84] | 91 |
|
---|
[b9d910f] | 92 | CHECK_RET_RETURN(ret,
|
---|
[fb78ae72] | 93 | "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
|
---|
[afcd86e] | 94 | usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
|
---|
[ea991e84] | 95 | io_reg_base, io_reg_size, irq);
|
---|
| 96 |
|
---|
[fb78ae72] | 97 | ret = pci_enable_interrupts(device);
|
---|
| 98 | CHECK_RET_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
|
---|
[acce4fc] | 99 |
|
---|
[5944244] | 100 | uhci_t *uhci_hc = malloc(sizeof(uhci_t));
|
---|
[b9d910f] | 101 | ret = (uhci_hc != NULL) ? EOK : ENOMEM;
|
---|
| 102 | CHECK_RET_RETURN(ret, "Failed to allocate memory for uhci hcd driver.\n");
|
---|
[1256a0a] | 103 |
|
---|
[eb1a2f4] | 104 | ret = uhci_init(uhci_hc, device, (void*)io_reg_base, io_reg_size);
|
---|
[1256a0a] | 105 | if (ret != EOK) {
|
---|
[afcd86e] | 106 | usb_log_error("Failed to init uhci-hcd.\n");
|
---|
[b9d910f] | 107 | free(uhci_hc);
|
---|
[1256a0a] | 108 | return ret;
|
---|
| 109 | }
|
---|
[b9d910f] | 110 |
|
---|
[eb1a2f4] | 111 | /*
|
---|
| 112 | * We might free uhci_hc, but that does not matter since no one
|
---|
| 113 | * else would access driver_data anyway.
|
---|
| 114 | */
|
---|
| 115 | device->driver_data = uhci_hc;
|
---|
| 116 |
|
---|
[b9d910f] | 117 | ret = register_interrupt_handler(device, irq, irq_handler,
|
---|
| 118 | &uhci_hc->interrupt_code);
|
---|
| 119 | if (ret != EOK) {
|
---|
| 120 | usb_log_error("Failed to register interrupt handler.\n");
|
---|
| 121 | uhci_fini(uhci_hc);
|
---|
| 122 | free(uhci_hc);
|
---|
[1256a0a] | 123 | return ret;
|
---|
| 124 | }
|
---|
[b9d910f] | 125 |
|
---|
[eb1a2f4] | 126 | ddf_fun_t *rh;
|
---|
[1256a0a] | 127 | ret = setup_root_hub(&rh, device);
|
---|
| 128 | if (ret != EOK) {
|
---|
[afcd86e] | 129 | usb_log_error("Failed to setup uhci root hub.\n");
|
---|
[b9d910f] | 130 | uhci_fini(uhci_hc);
|
---|
| 131 | free(uhci_hc);
|
---|
[1256a0a] | 132 | return ret;
|
---|
| 133 | }
|
---|
[eb1a2f4] | 134 | rh->driver_data = uhci_hc->ddf_instance;
|
---|
[1256a0a] | 135 |
|
---|
[eb1a2f4] | 136 | ret = ddf_fun_bind(rh);
|
---|
[1256a0a] | 137 | if (ret != EOK) {
|
---|
[afcd86e] | 138 | usb_log_error("Failed to register root hub.\n");
|
---|
[b9d910f] | 139 | uhci_fini(uhci_hc);
|
---|
| 140 | free(uhci_hc);
|
---|
| 141 | free(rh);
|
---|
[1256a0a] | 142 | return ret;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return EOK;
|
---|
[c7137738] | 146 | }
|
---|
[36a4738] | 147 | /*----------------------------------------------------------------------------*/
|
---|
[c7137738] | 148 | int main(int argc, char *argv[])
|
---|
| 149 | {
|
---|
[36a4738] | 150 | sleep(3);
|
---|
[eb1a2f4] | 151 | usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
|
---|
[c7137738] | 152 |
|
---|
[eb1a2f4] | 153 | return ddf_driver_main(&uhci_driver);
|
---|
[c7137738] | 154 | }
|
---|
[c56dbe0] | 155 | /**
|
---|
| 156 | * @}
|
---|
| 157 | */
|
---|