| 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 drvusbehci | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** @file | 
|---|
| 33 | * Main routines of EHCI 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 "ehci.h" | 
|---|
| 47 |  | 
|---|
| 48 | static int ehci_add_device(ddf_dev_t *device); | 
|---|
| 49 | /*----------------------------------------------------------------------------*/ | 
|---|
| 50 | static driver_ops_t ehci_driver_ops = { | 
|---|
| 51 | .add_device = ehci_add_device, | 
|---|
| 52 | }; | 
|---|
| 53 | /*----------------------------------------------------------------------------*/ | 
|---|
| 54 | static driver_t ehci_driver = { | 
|---|
| 55 | .name = NAME, | 
|---|
| 56 | .driver_ops = &ehci_driver_ops | 
|---|
| 57 | }; | 
|---|
| 58 | static ddf_dev_ops_t hc_ops = { | 
|---|
| 59 | .interfaces[USBHC_DEV_IFACE] = &ehci_hc_iface, | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 | /*----------------------------------------------------------------------------*/ | 
|---|
| 63 | /** Initializes a new ddf driver instance of EHCI hcd. | 
|---|
| 64 | * | 
|---|
| 65 | * @param[in] device DDF instance of the device to initialize. | 
|---|
| 66 | * @return Error code. | 
|---|
| 67 | */ | 
|---|
| 68 | static int ehci_add_device(ddf_dev_t *device) | 
|---|
| 69 | { | 
|---|
| 70 | assert(device); | 
|---|
| 71 | #define CHECK_RET_RETURN(ret, message...) \ | 
|---|
| 72 | if (ret != EOK) { \ | 
|---|
| 73 | usb_log_error(message); \ | 
|---|
| 74 | return ret; \ | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | uintptr_t mem_reg_base = 0; | 
|---|
| 78 | size_t mem_reg_size = 0; | 
|---|
| 79 | int irq = 0; | 
|---|
| 80 |  | 
|---|
| 81 | int ret = | 
|---|
| 82 | pci_get_my_registers(device, &mem_reg_base, &mem_reg_size, &irq); | 
|---|
| 83 | CHECK_RET_RETURN(ret, | 
|---|
| 84 | "Failed(%d) to get memory addresses:.\n", ret, device->handle); | 
|---|
| 85 | usb_log_info("Memory mapped regs at 0x%X (size %zu), IRQ %d.\n", | 
|---|
| 86 | mem_reg_base, mem_reg_size, irq); | 
|---|
| 87 |  | 
|---|
| 88 | ret = pci_disable_legacy(device); | 
|---|
| 89 | CHECK_RET_RETURN(ret, | 
|---|
| 90 | "Failed(%d) disable legacy USB: %s.\n", ret, str_error(ret)); | 
|---|
| 91 |  | 
|---|
| 92 | ddf_fun_t *hc_fun = ddf_fun_create(device, fun_exposed, "ehci-hc"); | 
|---|
| 93 | if (hc_fun == NULL) { | 
|---|
| 94 | usb_log_error("Failed to create EHCI function.\n"); | 
|---|
| 95 | return ENOMEM; | 
|---|
| 96 | } | 
|---|
| 97 | hc_fun->ops = &hc_ops; | 
|---|
| 98 | ret = ddf_fun_bind(hc_fun); | 
|---|
| 99 |  | 
|---|
| 100 | CHECK_RET_RETURN(ret, | 
|---|
| 101 | "Failed to bind EHCI function: %s.\n", | 
|---|
| 102 | str_error(ret)); | 
|---|
| 103 |  | 
|---|
| 104 | usb_log_info("Controlling new EHCI device `%s' (handle %llu).\n", | 
|---|
| 105 | device->name, device->handle); | 
|---|
| 106 |  | 
|---|
| 107 | return EOK; | 
|---|
| 108 | #undef CHECK_RET_RETURN | 
|---|
| 109 | } | 
|---|
| 110 | /*----------------------------------------------------------------------------*/ | 
|---|
| 111 | /** Initializes global driver structures (NONE). | 
|---|
| 112 | * | 
|---|
| 113 | * @param[in] argc Nmber of arguments in argv vector (ignored). | 
|---|
| 114 | * @param[in] argv Cmdline argument vector (ignored). | 
|---|
| 115 | * @return Error code. | 
|---|
| 116 | * | 
|---|
| 117 | * Driver debug level is set here. | 
|---|
| 118 | */ | 
|---|
| 119 | int main(int argc, char *argv[]) | 
|---|
| 120 | { | 
|---|
| 121 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME); | 
|---|
| 122 | return ddf_driver_main(&ehci_driver); | 
|---|
| 123 | } | 
|---|
| 124 | /** | 
|---|
| 125 | * @} | 
|---|
| 126 | */ | 
|---|