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