| 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 |
|
|---|
| 29 | /** @addtogroup drvusbuhci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver initialization
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <ddf/driver.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <io/log.h>
|
|---|
| 40 | #include <io/logctl.h>
|
|---|
| 41 | #include <pci_dev_iface.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <usb/debug.h>
|
|---|
| 45 | #include <usb/host/ddf_helpers.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "hc.h"
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "uhci"
|
|---|
| 50 |
|
|---|
| 51 | static int uhci_driver_init(hcd_t *, const hw_res_list_parsed_t *, ddf_dev_t *);
|
|---|
| 52 | static int uhci_driver_start(hcd_t *, bool);
|
|---|
| 53 | static void uhci_driver_fini(hcd_t *);
|
|---|
| 54 | static int disable_legacy(hcd_t *, ddf_dev_t *);
|
|---|
| 55 |
|
|---|
| 56 | static const ddf_hc_driver_t uhci_hc_driver = {
|
|---|
| 57 | .claim = disable_legacy,
|
|---|
| 58 | .irq_code_gen = uhci_hc_gen_irq_code,
|
|---|
| 59 | .init = uhci_driver_init,
|
|---|
| 60 | .start = uhci_driver_start,
|
|---|
| 61 | .setup_root_hub = hcd_setup_virtual_root_hub,
|
|---|
| 62 | .fini = uhci_driver_fini,
|
|---|
| 63 | .name = "UHCI",
|
|---|
| 64 | .ops = {
|
|---|
| 65 | .schedule = uhci_hc_schedule,
|
|---|
| 66 | .irq_hook = uhci_hc_interrupt,
|
|---|
| 67 | .status_hook = uhci_hc_status,
|
|---|
| 68 | },
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | static int uhci_driver_init(hcd_t *hcd, const hw_res_list_parsed_t *res, ddf_dev_t *device)
|
|---|
| 72 | {
|
|---|
| 73 | int err;
|
|---|
| 74 |
|
|---|
| 75 | assert(hcd);
|
|---|
| 76 | assert(hcd_get_driver_data(hcd) == NULL);
|
|---|
| 77 |
|
|---|
| 78 | hc_t *instance = malloc(sizeof(hc_t));
|
|---|
| 79 | if (!instance)
|
|---|
| 80 | return ENOMEM;
|
|---|
| 81 |
|
|---|
| 82 | if ((err = hc_init(instance, res)) != EOK)
|
|---|
| 83 | goto err;
|
|---|
| 84 |
|
|---|
| 85 | hcd_set_implementation(hcd, instance, &uhci_hc_driver.ops, &instance->bus.base);
|
|---|
| 86 |
|
|---|
| 87 | return EOK;
|
|---|
| 88 |
|
|---|
| 89 | err:
|
|---|
| 90 | free(instance);
|
|---|
| 91 | return err;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | static int uhci_driver_start(hcd_t *hcd, bool interrupts)
|
|---|
| 95 | {
|
|---|
| 96 | assert(hcd);
|
|---|
| 97 | hc_t *hc = hcd_get_driver_data(hcd);
|
|---|
| 98 |
|
|---|
| 99 | hc->hw_interrupts = interrupts;
|
|---|
| 100 | hc_start(hc);
|
|---|
| 101 | return EOK;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | static void uhci_driver_fini(hcd_t *hcd)
|
|---|
| 105 | {
|
|---|
| 106 | assert(hcd);
|
|---|
| 107 | hc_t *hc = hcd_get_driver_data(hcd);
|
|---|
| 108 | if (hc)
|
|---|
| 109 | hc_fini(hc);
|
|---|
| 110 |
|
|---|
| 111 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
|---|
| 112 | free(hc);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Call the PCI driver with a request to clear legacy support register
|
|---|
| 116 | *
|
|---|
| 117 | * @param[in] device Device asking to disable interrupts
|
|---|
| 118 | * @return Error code.
|
|---|
| 119 | */
|
|---|
| 120 | static int disable_legacy(hcd_t *hcd, ddf_dev_t *device)
|
|---|
| 121 | {
|
|---|
| 122 | assert(device);
|
|---|
| 123 |
|
|---|
| 124 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
|---|
| 125 | if (parent_sess == NULL)
|
|---|
| 126 | return ENOMEM;
|
|---|
| 127 |
|
|---|
| 128 | /* See UHCI design guide page 45 for these values.
|
|---|
| 129 | * Write all WC bits in USB legacy register */
|
|---|
| 130 | return pci_config_space_write_16(parent_sess, 0xc0, 0xaf00);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Initialize a new ddf driver instance for uhci hc and hub.
|
|---|
| 134 | *
|
|---|
| 135 | * @param[in] device DDF instance of the device to initialize.
|
|---|
| 136 | * @return Error code.
|
|---|
| 137 | */
|
|---|
| 138 | static int uhci_dev_add(ddf_dev_t *device)
|
|---|
| 139 | {
|
|---|
| 140 | usb_log_debug2("uhci_dev_add() called\n");
|
|---|
| 141 | assert(device);
|
|---|
| 142 | return hcd_ddf_add_hc(device, &uhci_hc_driver);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | static int uhci_fun_online(ddf_fun_t *fun)
|
|---|
| 146 | {
|
|---|
| 147 | return hcd_ddf_device_online(fun);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | static int uhci_fun_offline(ddf_fun_t *fun)
|
|---|
| 151 | {
|
|---|
| 152 | return hcd_ddf_device_offline(fun);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | static const driver_ops_t uhci_driver_ops = {
|
|---|
| 156 | .dev_add = uhci_dev_add,
|
|---|
| 157 | .fun_online = uhci_fun_online,
|
|---|
| 158 | .fun_offline = uhci_fun_offline
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | static const driver_t uhci_driver = {
|
|---|
| 162 | .name = NAME,
|
|---|
| 163 | .driver_ops = &uhci_driver_ops
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | /** Initialize global driver structures (NONE).
|
|---|
| 168 | *
|
|---|
| 169 | * @param[in] argc Number of arguments in argv vector (ignored).
|
|---|
| 170 | * @param[in] argv Cmdline argument vector (ignored).
|
|---|
| 171 | * @return Error code.
|
|---|
| 172 | *
|
|---|
| 173 | * Driver debug level is set here.
|
|---|
| 174 | */
|
|---|
| 175 | int main(int argc, char *argv[])
|
|---|
| 176 | {
|
|---|
| 177 | printf(NAME ": HelenOS UHCI driver.\n");
|
|---|
| 178 | log_init(NAME);
|
|---|
| 179 | logctl_set_log_level(NAME, LVL_DEBUG2);
|
|---|
| 180 | return ddf_driver_main(&uhci_driver);
|
|---|
| 181 | }
|
|---|
| 182 | /**
|
|---|
| 183 | * @}
|
|---|
| 184 | */
|
|---|