| 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 drvusbuhci
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver initialization
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <ddf/driver.h>
|
|---|
| 37 | #include <devman.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <io/log.h>
|
|---|
| 40 | #include <pci_dev_iface.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 | #include <usb/debug.h>
|
|---|
| 44 | #include <usb/host/ddf_helpers.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "hc.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "uhci"
|
|---|
| 49 |
|
|---|
| 50 | // TODO: This should be merged to hc_interrupt
|
|---|
| 51 | static void uhci_interrupt(hcd_t *hcd, uint32_t status)
|
|---|
| 52 | {
|
|---|
| 53 | assert(hcd);
|
|---|
| 54 | if (hcd->driver.data)
|
|---|
| 55 | hc_interrupt(hcd->driver.data, status);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | static int uhci_driver_init(hcd_t *hcd, const hw_res_list_parsed_t *res, bool irq)
|
|---|
| 59 | {
|
|---|
| 60 | assert(hcd);
|
|---|
| 61 | assert(hcd->driver.data == NULL);
|
|---|
| 62 |
|
|---|
| 63 | hc_t *instance = malloc(sizeof(hc_t));
|
|---|
| 64 | if (!instance)
|
|---|
| 65 | return ENOMEM;
|
|---|
| 66 |
|
|---|
| 67 | const int ret = hc_init(instance, res, irq);
|
|---|
| 68 | if (ret == EOK)
|
|---|
| 69 | hcd_set_implementation(hcd, instance, hc_schedule, NULL, NULL,
|
|---|
| 70 | uhci_interrupt, hc_status);
|
|---|
| 71 | return ret;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | static void uhci_driver_fini(hcd_t *hcd)
|
|---|
| 75 | {
|
|---|
| 76 | assert(hcd);
|
|---|
| 77 | if (hcd->driver.data)
|
|---|
| 78 | hc_fini(hcd->driver.data);
|
|---|
| 79 |
|
|---|
| 80 | free(hcd->driver.data);
|
|---|
| 81 | hcd_set_implementation(hcd, NULL, NULL, NULL, NULL, NULL, NULL);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static int uhci_dev_add(ddf_dev_t *device);
|
|---|
| 85 |
|
|---|
| 86 | static const driver_ops_t uhci_driver_ops = {
|
|---|
| 87 | .dev_add = uhci_dev_add,
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | static const driver_t uhci_driver = {
|
|---|
| 91 | .name = NAME,
|
|---|
| 92 | .driver_ops = &uhci_driver_ops
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | /** Call the PCI driver with a request to clear legacy support register
|
|---|
| 96 | *
|
|---|
| 97 | * @param[in] device Device asking to disable interrupts
|
|---|
| 98 | * @return Error code.
|
|---|
| 99 | */
|
|---|
| 100 | static int disable_legacy(ddf_dev_t *device)
|
|---|
| 101 | {
|
|---|
| 102 | assert(device);
|
|---|
| 103 |
|
|---|
| 104 | async_sess_t *parent_sess = devman_parent_device_connect(
|
|---|
| 105 | EXCHANGE_SERIALIZE, ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
|---|
| 106 | if (!parent_sess)
|
|---|
| 107 | return ENOMEM;
|
|---|
| 108 |
|
|---|
| 109 | /* See UHCI design guide page 45 for these values.
|
|---|
| 110 | * Write all WC bits in USB legacy register */
|
|---|
| 111 | const int rc = pci_config_space_write_16(parent_sess, 0xc0, 0xaf00);
|
|---|
| 112 |
|
|---|
| 113 | async_hangup(parent_sess);
|
|---|
| 114 | return rc;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /** Initialize a new ddf driver instance for uhci hc and hub.
|
|---|
| 118 | *
|
|---|
| 119 | * @param[in] device DDF instance of the device to initialize.
|
|---|
| 120 | * @return Error code.
|
|---|
| 121 | */
|
|---|
| 122 | int uhci_dev_add(ddf_dev_t *device)
|
|---|
| 123 | {
|
|---|
| 124 | usb_log_debug2("uhci_dev_add() called\n");
|
|---|
| 125 | assert(device);
|
|---|
| 126 |
|
|---|
| 127 | int ret = disable_legacy(device);
|
|---|
| 128 | if (ret != EOK) {
|
|---|
| 129 | usb_log_error("Failed to disable legacy USB: %s.\n",
|
|---|
| 130 | str_error(ret));
|
|---|
| 131 | return ret;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | ret = ddf_hcd_device_setup_all(device, USB_SPEED_FULL,
|
|---|
| 136 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11,
|
|---|
| 137 | ddf_hcd_gen_irq_handler, hc_gen_irq_code,
|
|---|
| 138 | uhci_driver_init, uhci_driver_fini);
|
|---|
| 139 | if (ret != EOK) {
|
|---|
| 140 | usb_log_error("Failed to initialize UHCI driver: %s.\n",
|
|---|
| 141 | str_error(ret));
|
|---|
| 142 | } else {
|
|---|
| 143 | usb_log_info("Controlling new UHCI device '%s'.\n",
|
|---|
| 144 | ddf_dev_get_name(device));
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return ret;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** Initialize global driver structures (NONE).
|
|---|
| 151 | *
|
|---|
| 152 | * @param[in] argc Number of arguments in argv vector (ignored).
|
|---|
| 153 | * @param[in] argv Cmdline argument vector (ignored).
|
|---|
| 154 | * @return Error code.
|
|---|
| 155 | *
|
|---|
| 156 | * Driver debug level is set here.
|
|---|
| 157 | */
|
|---|
| 158 | int main(int argc, char *argv[])
|
|---|
| 159 | {
|
|---|
| 160 | printf(NAME ": HelenOS UHCI driver.\n");
|
|---|
| 161 | log_init(NAME);
|
|---|
| 162 |
|
|---|
| 163 | return ddf_driver_main(&uhci_driver);
|
|---|
| 164 | }
|
|---|
| 165 | /**
|
|---|
| 166 | * @}
|
|---|
| 167 | */
|
|---|