| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 <errno.h>
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/usb.h> /* usb_address_t */
|
|---|
| 37 | #include <usb/usbdrv.h> /* usb_drv_* */
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "port.h"
|
|---|
| 41 | #include "port_status.h"
|
|---|
| 42 |
|
|---|
| 43 | static int uhci_port_new_device(uhci_port_t *port);
|
|---|
| 44 | static int uhci_port_remove_device(uhci_port_t *port);
|
|---|
| 45 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
|---|
| 46 | static int uhci_port_check(void *port);
|
|---|
| 47 |
|
|---|
| 48 | int uhci_port_init(
|
|---|
| 49 | uhci_port_t *port, port_status_t *address, unsigned number,
|
|---|
| 50 | unsigned usec, device_t *rh, int parent_phone)
|
|---|
| 51 | {
|
|---|
| 52 | assert(port);
|
|---|
| 53 | port->address = address;
|
|---|
| 54 | port->number = number;
|
|---|
| 55 | port->wait_period_usec = usec;
|
|---|
| 56 | port->attached_device = 0;
|
|---|
| 57 | port->rh = rh;
|
|---|
| 58 | port->hc_phone = parent_phone;
|
|---|
| 59 |
|
|---|
| 60 | port->checker = fibril_create(uhci_port_check, port);
|
|---|
| 61 | if (port->checker == 0) {
|
|---|
| 62 | usb_log_error(": failed to launch root hub fibril.");
|
|---|
| 63 | return ENOMEM;
|
|---|
| 64 | }
|
|---|
| 65 | fibril_add_ready(port->checker);
|
|---|
| 66 | usb_log_debug(
|
|---|
| 67 | "Added fibril for port %d: %p.\n", number, port->checker);
|
|---|
| 68 | return EOK;
|
|---|
| 69 | }
|
|---|
| 70 | /*----------------------------------------------------------------------------*/
|
|---|
| 71 | void uhci_port_fini(uhci_port_t *port)
|
|---|
| 72 | {
|
|---|
| 73 | // TODO: destroy fibril
|
|---|
| 74 | // TODO: hangup phone
|
|---|
| 75 | // fibril_teardown(port->checker);
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 | /*----------------------------------------------------------------------------*/
|
|---|
| 79 | int uhci_port_check(void *port)
|
|---|
| 80 | {
|
|---|
| 81 | uhci_port_t *port_instance = port;
|
|---|
| 82 | assert(port_instance);
|
|---|
| 83 |
|
|---|
| 84 | while (1) {
|
|---|
| 85 | usb_log_debug("Port(%d) status address %p:\n",
|
|---|
| 86 | port_instance->number, port_instance->address);
|
|---|
| 87 |
|
|---|
| 88 | /* read register value */
|
|---|
| 89 | port_status_t port_status =
|
|---|
| 90 | port_status_read(port_instance->address);
|
|---|
| 91 |
|
|---|
| 92 | /* debug print */
|
|---|
| 93 | usb_log_info("Port(%d) status %#.4x\n",
|
|---|
| 94 | port_instance->number, port_status);
|
|---|
| 95 | print_port_status(port_status);
|
|---|
| 96 |
|
|---|
| 97 | if (port_status & STATUS_CONNECTED_CHANGED) {
|
|---|
| 98 | if (port_status & STATUS_CONNECTED) {
|
|---|
| 99 | /* new device */
|
|---|
| 100 | uhci_port_new_device(port_instance);
|
|---|
| 101 | } else {
|
|---|
| 102 | uhci_port_remove_device(port_instance);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | async_usleep(port_instance->wait_period_usec);
|
|---|
| 106 | }
|
|---|
| 107 | return EOK;
|
|---|
| 108 | }
|
|---|
| 109 | /*----------------------------------------------------------------------------*/
|
|---|
| 110 | static int uhci_port_new_device(uhci_port_t *port)
|
|---|
| 111 | {
|
|---|
| 112 | assert(port);
|
|---|
| 113 | assert(port->hc_phone);
|
|---|
| 114 |
|
|---|
| 115 | usb_log_info("Adding new device on port %d.\n", port->number);
|
|---|
| 116 |
|
|---|
| 117 | /* get address of the future device */
|
|---|
| 118 | const usb_address_t usb_address = usb_drv_request_address(port->hc_phone);
|
|---|
| 119 |
|
|---|
| 120 | if (usb_address <= 0) {
|
|---|
| 121 | usb_log_error("Recieved invalid address(%d).\n", usb_address);
|
|---|
| 122 | return usb_address;
|
|---|
| 123 | }
|
|---|
| 124 | usb_log_debug("Sucessfully obtained address %d for port %d.\n",
|
|---|
| 125 | usb_address, port->number);
|
|---|
| 126 |
|
|---|
| 127 | /* get default address */
|
|---|
| 128 | int ret = usb_drv_reserve_default_address(port->hc_phone);
|
|---|
| 129 | if (ret != EOK) {
|
|---|
| 130 | usb_log_error("Failed to reserve default address on port %d.\n",
|
|---|
| 131 | port->number);
|
|---|
| 132 | int ret2 =
|
|---|
| 133 | usb_drv_release_address(port->hc_phone, usb_address);
|
|---|
| 134 | if (ret2 != EOK) {
|
|---|
| 135 | usb_log_fatal("Failed to return requested address on port %d.\n",
|
|---|
| 136 | port->number);
|
|---|
| 137 | return ret2;
|
|---|
| 138 | }
|
|---|
| 139 | usb_log_debug("Successfully returned reserved address on port %d.\n",
|
|---|
| 140 | port->number);
|
|---|
| 141 | return ret;
|
|---|
| 142 | }
|
|---|
| 143 | usb_log_debug("Sucessfully obtained default address for port %d.\n",
|
|---|
| 144 | port->number);
|
|---|
| 145 |
|
|---|
| 146 | /*
|
|---|
| 147 | * the host then waits for at least 100 ms to allow completion of
|
|---|
| 148 | * an insertion process and for power at the device to become stable.
|
|---|
| 149 | */
|
|---|
| 150 | async_usleep(100000);
|
|---|
| 151 |
|
|---|
| 152 | /* enable port */
|
|---|
| 153 | uhci_port_set_enabled(port, true);
|
|---|
| 154 |
|
|---|
| 155 | /* The hub maintains the reset signal to that port for 10 ms
|
|---|
| 156 | * (See Section 11.5.1.5)
|
|---|
| 157 | */
|
|---|
| 158 | {
|
|---|
| 159 | usb_log_debug("Reset Signal start on port %d.\n",
|
|---|
| 160 | port->number);
|
|---|
| 161 | port_status_t port_status =
|
|---|
| 162 | port_status_read(port->address);
|
|---|
| 163 | port_status |= STATUS_IN_RESET;
|
|---|
| 164 | port_status_write(port->address, port_status);
|
|---|
| 165 | async_usleep(10000);
|
|---|
| 166 | port_status =
|
|---|
| 167 | port_status_read(port->address);
|
|---|
| 168 | port_status &= ~STATUS_IN_RESET;
|
|---|
| 169 | port_status_write(port->address, port_status);
|
|---|
| 170 | usb_log_debug("Reset Signal stop on port %d.\n",
|
|---|
| 171 | port->number);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /* assign address to device */
|
|---|
| 175 | ret = usb_drv_req_set_address(port->hc_phone, 0, usb_address);
|
|---|
| 176 |
|
|---|
| 177 | if (ret != EOK) { /* address assigning went wrong */
|
|---|
| 178 | usb_log_error("Failed(%d) to assign address to the device.\n", ret);
|
|---|
| 179 | uhci_port_set_enabled(port, false);
|
|---|
| 180 | int release = usb_drv_release_default_address(port->hc_phone);
|
|---|
| 181 | if (release != EOK) {
|
|---|
| 182 | usb_log_error("Failed to release default address on port %d.\n",
|
|---|
| 183 | port->number);
|
|---|
| 184 | return release;
|
|---|
| 185 | }
|
|---|
| 186 | usb_log_debug("Sucessfully released default address on port %d.\n",
|
|---|
| 187 | port->number);
|
|---|
| 188 | return ret;
|
|---|
| 189 | }
|
|---|
| 190 | usb_log_debug("Sucessfully assigned address %d for port %d.\n",
|
|---|
| 191 | usb_address, port->number);
|
|---|
| 192 |
|
|---|
| 193 | /* release default address */
|
|---|
| 194 | ret = usb_drv_release_default_address(port->hc_phone);
|
|---|
| 195 | if (ret != EOK) {
|
|---|
| 196 | usb_log_error("Failed to release default address on port %d.\n",
|
|---|
| 197 | port->number);
|
|---|
| 198 | return ret;
|
|---|
| 199 | }
|
|---|
| 200 | usb_log_debug("Sucessfully released default address on port %d.\n",
|
|---|
| 201 | port->number);
|
|---|
| 202 |
|
|---|
| 203 | /* communicate and possibly report to devman */
|
|---|
| 204 | assert(port->attached_device == 0);
|
|---|
| 205 |
|
|---|
| 206 | ret = usb_drv_register_child_in_devman(port->hc_phone, port->rh,
|
|---|
| 207 | usb_address, &port->attached_device);
|
|---|
| 208 |
|
|---|
| 209 | if (ret != EOK) { /* something went wrong */
|
|---|
| 210 | usb_log_error("Failed(%d) in usb_drv_register_child.\n", ret);
|
|---|
| 211 | uhci_port_set_enabled(port, false);
|
|---|
| 212 | return ENOMEM;
|
|---|
| 213 | }
|
|---|
| 214 | usb_log_info("Sucessfully added device on port(%d) address(%d) handle %d.\n",
|
|---|
| 215 | port->number, usb_address, port->attached_device);
|
|---|
| 216 |
|
|---|
| 217 | ret =
|
|---|
| 218 | usb_drv_bind_address(port->hc_phone, usb_address, port->attached_device);
|
|---|
| 219 | // TODO: proper error check here
|
|---|
| 220 | assert(ret == EOK);
|
|---|
| 221 |
|
|---|
| 222 | return EOK;
|
|---|
| 223 | }
|
|---|
| 224 | /*----------------------------------------------------------------------------*/
|
|---|
| 225 | static int uhci_port_remove_device(uhci_port_t *port)
|
|---|
| 226 | {
|
|---|
| 227 | usb_log_error("Don't know how to remove device %#x.\n",
|
|---|
| 228 | (unsigned int)port->attached_device);
|
|---|
| 229 | // uhci_port_set_enabled(port, false);
|
|---|
| 230 | return EOK;
|
|---|
| 231 | }
|
|---|
| 232 | /*----------------------------------------------------------------------------*/
|
|---|
| 233 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
|---|
| 234 | {
|
|---|
| 235 | assert(port);
|
|---|
| 236 |
|
|---|
| 237 | /* read register value */
|
|---|
| 238 | port_status_t port_status
|
|---|
| 239 | = port_status_read(port->address);
|
|---|
| 240 |
|
|---|
| 241 | /* enable port: register write */
|
|---|
| 242 | if (enabled) {
|
|---|
| 243 | port_status |= STATUS_ENABLED;
|
|---|
| 244 | } else {
|
|---|
| 245 | port_status &= ~STATUS_ENABLED;
|
|---|
| 246 | }
|
|---|
| 247 | port_status_write(port->address, port_status);
|
|---|
| 248 |
|
|---|
| 249 | usb_log_info("%s port %d.\n",
|
|---|
| 250 | enabled ? "Enabled" : "Disabled", port->number);
|
|---|
| 251 | return EOK;
|
|---|
| 252 | }
|
|---|
| 253 | /*----------------------------------------------------------------------------*/
|
|---|
| 254 | /**
|
|---|
| 255 | * @}
|
|---|
| 256 | */
|
|---|