| 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 | #include <fibril_synch.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/usb.h> /* usb_address_t */
|
|---|
| 39 | #include <usb/usbdevice.h>
|
|---|
| 40 | #include <usb/hub.h>
|
|---|
| 41 | #include <usb/request.h>
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 | #include <usb/recognise.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "port.h"
|
|---|
| 46 | #include "port_status.h"
|
|---|
| 47 |
|
|---|
| 48 | static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
|
|---|
| 49 | static int uhci_port_remove_device(uhci_port_t *port);
|
|---|
| 50 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
|---|
| 51 | static int uhci_port_check(void *port);
|
|---|
| 52 | static int uhci_port_reset_enable(int portno, void *arg);
|
|---|
| 53 | /*----------------------------------------------------------------------------*/
|
|---|
| 54 | /** Initializes UHCI root hub port instance.
|
|---|
| 55 | *
|
|---|
| 56 | * @param[in] port Memory structure to use.
|
|---|
| 57 | * @param[in] addr Address of I/O register.
|
|---|
| 58 | * @param[in] number Port number.
|
|---|
| 59 | * @param[in] usec Polling interval.
|
|---|
| 60 | * @param[in] rh Pointer to ddf instance fo the root hub driver.
|
|---|
| 61 | * @return Error code.
|
|---|
| 62 | *
|
|---|
| 63 | * Starts the polling fibril.
|
|---|
| 64 | */
|
|---|
| 65 | int uhci_port_init(uhci_port_t *port,
|
|---|
| 66 | port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
|
|---|
| 67 | {
|
|---|
| 68 | assert(port);
|
|---|
| 69 |
|
|---|
| 70 | port->address = address;
|
|---|
| 71 | port->number = number;
|
|---|
| 72 | port->wait_period_usec = usec;
|
|---|
| 73 | port->attached_device = 0;
|
|---|
| 74 | port->rh = rh;
|
|---|
| 75 |
|
|---|
| 76 | int rc = usb_hc_connection_initialize_from_device(
|
|---|
| 77 | &port->hc_connection, rh);
|
|---|
| 78 | if (rc != EOK) {
|
|---|
| 79 | usb_log_error("Failed to initialize connection to HC.");
|
|---|
| 80 | return rc;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | port->checker = fibril_create(uhci_port_check, port);
|
|---|
| 84 | if (port->checker == 0) {
|
|---|
| 85 | usb_log_error("Port(%p - %d): failed to launch root hub fibril.",
|
|---|
| 86 | port->address, port->number);
|
|---|
| 87 | return ENOMEM;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | fibril_add_ready(port->checker);
|
|---|
| 91 | usb_log_debug("Port(%p - %d): Added fibril. %x\n",
|
|---|
| 92 | port->address, port->number, port->checker);
|
|---|
| 93 | return EOK;
|
|---|
| 94 | }
|
|---|
| 95 | /*----------------------------------------------------------------------------*/
|
|---|
| 96 | /** Finishes UHCI root hub port instance.
|
|---|
| 97 | *
|
|---|
| 98 | * @param[in] port Memory structure to use.
|
|---|
| 99 | *
|
|---|
| 100 | * Stops the polling fibril.
|
|---|
| 101 | */
|
|---|
| 102 | void uhci_port_fini(uhci_port_t *port)
|
|---|
| 103 | {
|
|---|
| 104 | /* TODO: Kill fibril here */
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 | /*----------------------------------------------------------------------------*/
|
|---|
| 108 | /** Periodically checks port status and reports new devices.
|
|---|
| 109 | *
|
|---|
| 110 | * @param[in] port Memory structure to use.
|
|---|
| 111 | * @return Error code.
|
|---|
| 112 | */
|
|---|
| 113 | int uhci_port_check(void *port)
|
|---|
| 114 | {
|
|---|
| 115 | uhci_port_t *instance = port;
|
|---|
| 116 | assert(instance);
|
|---|
| 117 |
|
|---|
| 118 | /* Iteration count, for debug purposes only */
|
|---|
| 119 | unsigned count = 0;
|
|---|
| 120 |
|
|---|
| 121 | while (1) {
|
|---|
| 122 | async_usleep(instance->wait_period_usec);
|
|---|
| 123 |
|
|---|
| 124 | /* read register value */
|
|---|
| 125 | port_status_t port_status = port_status_read(instance->address);
|
|---|
| 126 |
|
|---|
| 127 | /* debug print mutex */
|
|---|
| 128 | static fibril_mutex_t dbg_mtx =
|
|---|
| 129 | FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
|
|---|
| 130 | fibril_mutex_lock(&dbg_mtx);
|
|---|
| 131 | usb_log_debug2("Port(%p - %d): Status: %#04x. === %u\n",
|
|---|
| 132 | instance->address, instance->number, port_status, count++);
|
|---|
| 133 | // print_port_status(port_status);
|
|---|
| 134 | fibril_mutex_unlock(&dbg_mtx);
|
|---|
| 135 |
|
|---|
| 136 | if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
|
|---|
| 137 | continue;
|
|---|
| 138 |
|
|---|
| 139 | usb_log_debug("Port(%p - %d): Connected change detected: %x.\n",
|
|---|
| 140 | instance->address, instance->number, port_status);
|
|---|
| 141 |
|
|---|
| 142 | int rc =
|
|---|
| 143 | usb_hc_connection_open(&instance->hc_connection);
|
|---|
| 144 | if (rc != EOK) {
|
|---|
| 145 | usb_log_error("Port(%p - %d): Failed to connect to HC.",
|
|---|
| 146 | instance->address, instance->number);
|
|---|
| 147 | continue;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /* Remove any old device */
|
|---|
| 151 | if (instance->attached_device) {
|
|---|
| 152 | usb_log_debug2("Port(%p - %d): Removing device.\n",
|
|---|
| 153 | instance->address, instance->number);
|
|---|
| 154 | uhci_port_remove_device(instance);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if ((port_status & STATUS_CONNECTED) != 0) {
|
|---|
| 158 | /* New device */
|
|---|
| 159 | const usb_speed_t speed =
|
|---|
| 160 | ((port_status & STATUS_LOW_SPEED) != 0) ?
|
|---|
| 161 | USB_SPEED_LOW : USB_SPEED_FULL;
|
|---|
| 162 | uhci_port_new_device(instance, speed);
|
|---|
| 163 | } else {
|
|---|
| 164 | /* Write one to WC bits, to ack changes */
|
|---|
| 165 | port_status_write(instance->address, port_status);
|
|---|
| 166 | usb_log_debug("Port(%p - %d): Change status ACK.\n",
|
|---|
| 167 | instance->address, instance->number);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | rc = usb_hc_connection_close(&instance->hc_connection);
|
|---|
| 171 | if (rc != EOK) {
|
|---|
| 172 | usb_log_error("Port(%p - %d): Failed to disconnect.",
|
|---|
| 173 | instance->address, instance->number);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | return EOK;
|
|---|
| 177 | }
|
|---|
| 178 | /*----------------------------------------------------------------------------*/
|
|---|
| 179 | /** Callback for enabling port during adding a new device.
|
|---|
| 180 | *
|
|---|
| 181 | * @param portno Port number (unused).
|
|---|
| 182 | * @param arg Pointer to uhci_port_t of port with the new device.
|
|---|
| 183 | * @return Error code.
|
|---|
| 184 | */
|
|---|
| 185 | int uhci_port_reset_enable(int portno, void *arg)
|
|---|
| 186 | {
|
|---|
| 187 | uhci_port_t *port = (uhci_port_t *) arg;
|
|---|
| 188 |
|
|---|
| 189 | usb_log_debug2("Port(%p - %d): new_device_enable_port.\n",
|
|---|
| 190 | port->address, port->number);
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * The host then waits for at least 100 ms to allow completion of
|
|---|
| 194 | * an insertion process and for power at the device to become stable.
|
|---|
| 195 | */
|
|---|
| 196 | async_usleep(100000);
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | /* The hub maintains the reset signal to that port for 10 ms
|
|---|
| 200 | * (See Section 11.5.1.5)
|
|---|
| 201 | */
|
|---|
| 202 | {
|
|---|
| 203 | usb_log_debug("Port(%p - %d): Reset Signal start.\n",
|
|---|
| 204 | port->address, port->number);
|
|---|
| 205 | port_status_t port_status =
|
|---|
| 206 | port_status_read(port->address);
|
|---|
| 207 | port_status |= STATUS_IN_RESET;
|
|---|
| 208 | port_status_write(port->address, port_status);
|
|---|
| 209 | async_usleep(10000);
|
|---|
| 210 | port_status = port_status_read(port->address);
|
|---|
| 211 | port_status &= ~STATUS_IN_RESET;
|
|---|
| 212 | port_status_write(port->address, port_status);
|
|---|
| 213 | usb_log_debug("Port(%p - %d): Reset Signal stop.\n",
|
|---|
| 214 | port->address, port->number);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /* Enable the port. */
|
|---|
| 218 | uhci_port_set_enabled(port, true);
|
|---|
| 219 | return EOK;
|
|---|
| 220 | }
|
|---|
| 221 | /*----------------------------------------------------------------------------*/
|
|---|
| 222 | /** Initializes and reports connected device.
|
|---|
| 223 | *
|
|---|
| 224 | * @param[in] port Memory structure to use.
|
|---|
| 225 | * @param[in] speed Detected speed.
|
|---|
| 226 | * @return Error code.
|
|---|
| 227 | *
|
|---|
| 228 | * Uses libUSB function to do the actual work.
|
|---|
| 229 | */
|
|---|
| 230 | int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
|
|---|
| 231 | {
|
|---|
| 232 | assert(port);
|
|---|
| 233 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
|---|
| 234 |
|
|---|
| 235 | usb_log_info("Port(%p-%d): Detected new device.\n",
|
|---|
| 236 | port->address, port->number);
|
|---|
| 237 |
|
|---|
| 238 | usb_address_t dev_addr;
|
|---|
| 239 | int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
|---|
| 240 | speed, uhci_port_reset_enable, port->number, port,
|
|---|
| 241 | &dev_addr, &port->attached_device, NULL, NULL, NULL);
|
|---|
| 242 |
|
|---|
| 243 | if (rc != EOK) {
|
|---|
| 244 | usb_log_error("Port(%p-%d): Failed(%d) to add device: %s.\n",
|
|---|
| 245 | port->address, port->number, rc, str_error(rc));
|
|---|
| 246 | uhci_port_set_enabled(port, false);
|
|---|
| 247 | return rc;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | usb_log_info("Port(%p-%d): New device has address %d (handle %zu).\n",
|
|---|
| 251 | port->address, port->number, dev_addr, port->attached_device);
|
|---|
| 252 |
|
|---|
| 253 | return EOK;
|
|---|
| 254 | }
|
|---|
| 255 | /*----------------------------------------------------------------------------*/
|
|---|
| 256 | /** Removes device.
|
|---|
| 257 | *
|
|---|
| 258 | * @param[in] port Memory structure to use.
|
|---|
| 259 | * @return Error code.
|
|---|
| 260 | *
|
|---|
| 261 | * Does not work DDF does not support device removal.
|
|---|
| 262 | */
|
|---|
| 263 | int uhci_port_remove_device(uhci_port_t *port)
|
|---|
| 264 | {
|
|---|
| 265 | usb_log_error("Port(%p-%d): Don't know how to remove device %#x.\n",
|
|---|
| 266 | port->address, port->number, (unsigned int)port->attached_device);
|
|---|
| 267 | return EOK;
|
|---|
| 268 | }
|
|---|
| 269 | /*----------------------------------------------------------------------------*/
|
|---|
| 270 | /** Enables and disables port.
|
|---|
| 271 | *
|
|---|
| 272 | * @param[in] port Memory structure to use.
|
|---|
| 273 | * @return Error code. (Always EOK)
|
|---|
| 274 | */
|
|---|
| 275 | int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
|---|
| 276 | {
|
|---|
| 277 | assert(port);
|
|---|
| 278 |
|
|---|
| 279 | /* Read register value */
|
|---|
| 280 | port_status_t port_status = port_status_read(port->address);
|
|---|
| 281 |
|
|---|
| 282 | /* Set enabled bit */
|
|---|
| 283 | if (enabled) {
|
|---|
| 284 | port_status |= STATUS_ENABLED;
|
|---|
| 285 | } else {
|
|---|
| 286 | port_status &= ~STATUS_ENABLED;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /* Write new value. */
|
|---|
| 290 | port_status_write(port->address, port_status);
|
|---|
| 291 |
|
|---|
| 292 | usb_log_info("Port(%p-%d): %sabled port.\n",
|
|---|
| 293 | port->address, port->number, enabled ? "En" : "Dis");
|
|---|
| 294 | return EOK;
|
|---|
| 295 | }
|
|---|
| 296 | /*----------------------------------------------------------------------------*/
|
|---|
| 297 | /**
|
|---|
| 298 | * @}
|
|---|
| 299 | */
|
|---|