| 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 drvusbuhcirh
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI root hub port routines
|
|---|
| 33 | */
|
|---|
| 34 | #include <libarch/ddi.h> /* pio_read and pio_write */
|
|---|
| 35 | #include <fibril_synch.h> /* async_usleep */
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <usb/usb.h> /* usb_address_t */
|
|---|
| 41 | #include <usb/dev/hub.h> /* usb_hc_new_device_wrapper */
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "port.h"
|
|---|
| 45 |
|
|---|
| 46 | static int uhci_port_check(void *port);
|
|---|
| 47 | static int uhci_port_reset_enable(int portno, void *arg);
|
|---|
| 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 void uhci_port_print_status(
|
|---|
| 52 | uhci_port_t *port, const port_status_t value);
|
|---|
| 53 |
|
|---|
| 54 | /** Register reading helper function.
|
|---|
| 55 | *
|
|---|
| 56 | * @param[in] port Structure to use.
|
|---|
| 57 | * @return Error code. (Always EOK)
|
|---|
| 58 | */
|
|---|
| 59 | static inline port_status_t uhci_port_read_status(uhci_port_t *port)
|
|---|
| 60 | {
|
|---|
| 61 | assert(port);
|
|---|
| 62 | return pio_read_16(port->address);
|
|---|
| 63 | }
|
|---|
| 64 | /*----------------------------------------------------------------------------*/
|
|---|
| 65 | /** Register writing helper function.
|
|---|
| 66 | *
|
|---|
| 67 | * @param[in] port Structure to use.
|
|---|
| 68 | * @param[in] val New register value.
|
|---|
| 69 | * @return Error code. (Always EOK)
|
|---|
| 70 | */
|
|---|
| 71 | static inline void uhci_port_write_status(uhci_port_t *port, port_status_t val)
|
|---|
| 72 | {
|
|---|
| 73 | assert(port);
|
|---|
| 74 | pio_write_16(port->address, val);
|
|---|
| 75 | }
|
|---|
| 76 | /*----------------------------------------------------------------------------*/
|
|---|
| 77 | /** Initialize UHCI root hub port instance.
|
|---|
| 78 | *
|
|---|
| 79 | * @param[in] port Memory structure to use.
|
|---|
| 80 | * @param[in] address Address of I/O register.
|
|---|
| 81 | * @param[in] number Port number.
|
|---|
| 82 | * @param[in] usec Polling interval.
|
|---|
| 83 | * @param[in] rh Pointer to ddf instance of the root hub driver.
|
|---|
| 84 | * @return Error code.
|
|---|
| 85 | *
|
|---|
| 86 | * Creates and starts the polling fibril.
|
|---|
| 87 | */
|
|---|
| 88 | int uhci_port_init(uhci_port_t *port,
|
|---|
| 89 | port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
|
|---|
| 90 | {
|
|---|
| 91 | assert(port);
|
|---|
| 92 | char *id_string;
|
|---|
| 93 | asprintf(&id_string, "Port (%p - %u)", port, number);
|
|---|
| 94 | if (id_string == NULL) {
|
|---|
| 95 | return ENOMEM;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | port->id_string = id_string;
|
|---|
| 99 | port->address = address;
|
|---|
| 100 | port->number = number;
|
|---|
| 101 | port->wait_period_usec = usec;
|
|---|
| 102 | port->attached_device = 0;
|
|---|
| 103 | port->rh = rh;
|
|---|
| 104 |
|
|---|
| 105 | int ret =
|
|---|
| 106 | usb_hc_connection_initialize_from_device(&port->hc_connection, rh);
|
|---|
| 107 | if (ret != EOK) {
|
|---|
| 108 | usb_log_error("%s: failed to initialize connection to HC.",
|
|---|
| 109 | port->id_string);
|
|---|
| 110 | free(id_string);
|
|---|
| 111 | return ret;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | port->checker = fibril_create(uhci_port_check, port);
|
|---|
| 115 | if (port->checker == 0) {
|
|---|
| 116 | usb_log_error("%s: failed to create polling fibril.",
|
|---|
| 117 | port->id_string);
|
|---|
| 118 | free(id_string);
|
|---|
| 119 | return ENOMEM;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | fibril_add_ready(port->checker);
|
|---|
| 123 | usb_log_debug("%s: Started polling fibril (%" PRIun ").\n",
|
|---|
| 124 | port->id_string, port->checker);
|
|---|
| 125 | return EOK;
|
|---|
| 126 | }
|
|---|
| 127 | /*----------------------------------------------------------------------------*/
|
|---|
| 128 | /** Cleanup UHCI root hub port instance.
|
|---|
| 129 | *
|
|---|
| 130 | * @param[in] port Memory structure to use.
|
|---|
| 131 | *
|
|---|
| 132 | * Stops the polling fibril.
|
|---|
| 133 | */
|
|---|
| 134 | void uhci_port_fini(uhci_port_t *port)
|
|---|
| 135 | {
|
|---|
| 136 | assert(port);
|
|---|
| 137 | free(port->id_string);
|
|---|
| 138 | // TODO: Kill fibril here
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 | /*----------------------------------------------------------------------------*/
|
|---|
| 142 | /** Periodically checks port status and reports new devices.
|
|---|
| 143 | *
|
|---|
| 144 | * @param[in] port Port structure to use.
|
|---|
| 145 | * @return Error code.
|
|---|
| 146 | */
|
|---|
| 147 | int uhci_port_check(void *port)
|
|---|
| 148 | {
|
|---|
| 149 | uhci_port_t *instance = port;
|
|---|
| 150 | assert(instance);
|
|---|
| 151 |
|
|---|
| 152 | while (1) {
|
|---|
| 153 | async_usleep(instance->wait_period_usec);
|
|---|
| 154 |
|
|---|
| 155 | /* Read register value */
|
|---|
| 156 | const port_status_t port_status =
|
|---|
| 157 | uhci_port_read_status(instance);
|
|---|
| 158 |
|
|---|
| 159 | /* Print the value if it's interesting */
|
|---|
| 160 | if (port_status & ~STATUS_ALWAYS_ONE)
|
|---|
| 161 | uhci_port_print_status(instance, port_status);
|
|---|
| 162 |
|
|---|
| 163 | if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
|
|---|
| 164 | continue;
|
|---|
| 165 |
|
|---|
| 166 | usb_log_debug("%s: Connected change detected: %x.\n",
|
|---|
| 167 | instance->id_string, port_status);
|
|---|
| 168 |
|
|---|
| 169 | /* Remove any old device */
|
|---|
| 170 | if (instance->attached_device) {
|
|---|
| 171 | usb_log_debug2("%s: Removing device.\n",
|
|---|
| 172 | instance->id_string);
|
|---|
| 173 | uhci_port_remove_device(instance);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | int ret =
|
|---|
| 177 | usb_hc_connection_open(&instance->hc_connection);
|
|---|
| 178 | if (ret != EOK) {
|
|---|
| 179 | usb_log_error("%s: Failed to connect to HC.",
|
|---|
| 180 | instance->id_string);
|
|---|
| 181 | continue;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | if ((port_status & STATUS_CONNECTED) != 0) {
|
|---|
| 185 | /* New device */
|
|---|
| 186 | const usb_speed_t speed =
|
|---|
| 187 | ((port_status & STATUS_LOW_SPEED) != 0) ?
|
|---|
| 188 | USB_SPEED_LOW : USB_SPEED_FULL;
|
|---|
| 189 | uhci_port_new_device(instance, speed);
|
|---|
| 190 | } else {
|
|---|
| 191 | /* Write one to WC bits, to ack changes */
|
|---|
| 192 | uhci_port_write_status(instance, port_status);
|
|---|
| 193 | usb_log_debug("%s: status change ACK.\n",
|
|---|
| 194 | instance->id_string);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | ret = usb_hc_connection_close(&instance->hc_connection);
|
|---|
| 198 | if (ret != EOK) {
|
|---|
| 199 | usb_log_error("%s: Failed to disconnect.",
|
|---|
| 200 | instance->id_string);
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | return EOK;
|
|---|
| 204 | }
|
|---|
| 205 | /*----------------------------------------------------------------------------*/
|
|---|
| 206 | /** Callback for enabling port during adding a new device.
|
|---|
| 207 | *
|
|---|
| 208 | * @param portno Port number (unused).
|
|---|
| 209 | * @param arg Pointer to uhci_port_t of port with the new device.
|
|---|
| 210 | * @return Error code.
|
|---|
| 211 | *
|
|---|
| 212 | * Resets and enables the ub port.
|
|---|
| 213 | */
|
|---|
| 214 | int uhci_port_reset_enable(int portno, void *arg)
|
|---|
| 215 | {
|
|---|
| 216 | uhci_port_t *port = arg;
|
|---|
| 217 | assert(port);
|
|---|
| 218 |
|
|---|
| 219 | usb_log_debug2("%s: new_device_enable_port.\n", port->id_string);
|
|---|
| 220 |
|
|---|
| 221 | /*
|
|---|
| 222 | * Resets from root ports should be nominally 50ms (USB spec 7.1.7.3)
|
|---|
| 223 | */
|
|---|
| 224 | {
|
|---|
| 225 | usb_log_debug("%s: Reset Signal start.\n", port->id_string);
|
|---|
| 226 | port_status_t port_status = uhci_port_read_status(port);
|
|---|
| 227 | port_status |= STATUS_IN_RESET;
|
|---|
| 228 | uhci_port_write_status(port, port_status);
|
|---|
| 229 | async_usleep(50000);
|
|---|
| 230 | port_status = uhci_port_read_status(port);
|
|---|
| 231 | port_status &= ~STATUS_IN_RESET;
|
|---|
| 232 | uhci_port_write_status(port, port_status);
|
|---|
| 233 | while (uhci_port_read_status(port) & STATUS_IN_RESET);
|
|---|
| 234 | }
|
|---|
| 235 | /* PIO delay, should not be longer than 3ms as the device might
|
|---|
| 236 | * enter suspend state. */
|
|---|
| 237 | udelay(10);
|
|---|
| 238 | /* Enable the port. */
|
|---|
| 239 | uhci_port_set_enabled(port, true);
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 | /*----------------------------------------------------------------------------*/
|
|---|
| 243 | /** Initialize and report connected device.
|
|---|
| 244 | *
|
|---|
| 245 | * @param[in] port Port structure to use.
|
|---|
| 246 | * @param[in] speed Detected speed.
|
|---|
| 247 | * @return Error code.
|
|---|
| 248 | *
|
|---|
| 249 | * Uses libUSB function to do the actual work.
|
|---|
| 250 | */
|
|---|
| 251 | int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
|
|---|
| 252 | {
|
|---|
| 253 | assert(port);
|
|---|
| 254 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
|---|
| 255 |
|
|---|
| 256 | usb_log_debug("%s: Detected new device.\n", port->id_string);
|
|---|
| 257 |
|
|---|
| 258 | int ret, count = 0;
|
|---|
| 259 | usb_address_t dev_addr;
|
|---|
| 260 | do {
|
|---|
| 261 | ret = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
|---|
| 262 | speed, uhci_port_reset_enable, port->number, port,
|
|---|
| 263 | &dev_addr, &port->attached_device, NULL, NULL, NULL);
|
|---|
| 264 | } while (ret != EOK && ++count < 4);
|
|---|
| 265 |
|
|---|
| 266 | if (ret != EOK) {
|
|---|
| 267 | usb_log_error("%s: Failed(%d) to add device: %s.\n",
|
|---|
| 268 | port->id_string, ret, str_error(ret));
|
|---|
| 269 | uhci_port_set_enabled(port, false);
|
|---|
| 270 | return ret;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | usb_log_info("New device at port %u, address %d (handle %" PRIun ").\n",
|
|---|
| 274 | port->number, dev_addr, port->attached_device);
|
|---|
| 275 | return EOK;
|
|---|
| 276 | }
|
|---|
| 277 | /*----------------------------------------------------------------------------*/
|
|---|
| 278 | /** Remove device.
|
|---|
| 279 | *
|
|---|
| 280 | * @param[in] port Memory structure to use.
|
|---|
| 281 | * @return Error code.
|
|---|
| 282 | *
|
|---|
| 283 | * Does not work, DDF does not support device removal.
|
|---|
| 284 | * Does not even free used USB address (it would be dangerous if tis driver
|
|---|
| 285 | * is still running).
|
|---|
| 286 | */
|
|---|
| 287 | int uhci_port_remove_device(uhci_port_t *port)
|
|---|
| 288 | {
|
|---|
| 289 | usb_log_error("%s: Don't know how to remove device %" PRIun ".\n",
|
|---|
| 290 | port->id_string, port->attached_device);
|
|---|
| 291 | port->attached_device = 0;
|
|---|
| 292 | return ENOTSUP;
|
|---|
| 293 | }
|
|---|
| 294 | /*----------------------------------------------------------------------------*/
|
|---|
| 295 | /** Enable or disable root hub port.
|
|---|
| 296 | *
|
|---|
| 297 | * @param[in] port Port structure to use.
|
|---|
| 298 | * @param[in] enabled Port status to set.
|
|---|
| 299 | * @return Error code. (Always EOK)
|
|---|
| 300 | */
|
|---|
| 301 | int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
|---|
| 302 | {
|
|---|
| 303 | assert(port);
|
|---|
| 304 |
|
|---|
| 305 | /* Read register value */
|
|---|
| 306 | port_status_t port_status = uhci_port_read_status(port);
|
|---|
| 307 |
|
|---|
| 308 | /* Set enabled bit */
|
|---|
| 309 | if (enabled) {
|
|---|
| 310 | port_status |= STATUS_ENABLED;
|
|---|
| 311 | } else {
|
|---|
| 312 | port_status &= ~STATUS_ENABLED;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /* Write new value. */
|
|---|
| 316 | uhci_port_write_status(port, port_status);
|
|---|
| 317 |
|
|---|
| 318 | /* Wait for port to become enabled */
|
|---|
| 319 | do {
|
|---|
| 320 | port_status = uhci_port_read_status(port);
|
|---|
| 321 | } while ((port_status & STATUS_CONNECTED) &&
|
|---|
| 322 | !(port_status & STATUS_ENABLED));
|
|---|
| 323 |
|
|---|
| 324 | usb_log_debug("%s: %sabled port.\n",
|
|---|
| 325 | port->id_string, enabled ? "En" : "Dis");
|
|---|
| 326 | return EOK;
|
|---|
| 327 | }
|
|---|
| 328 | /*----------------------------------------------------------------------------*/
|
|---|
| 329 | /** Print the port status value in a human friendly way
|
|---|
| 330 | *
|
|---|
| 331 | * @param[in] port Port structure to use.
|
|---|
| 332 | * @param[in] value Port register value to print.
|
|---|
| 333 | * @return Error code. (Always EOK)
|
|---|
| 334 | */
|
|---|
| 335 | void uhci_port_print_status(uhci_port_t *port, const port_status_t value)
|
|---|
| 336 | {
|
|---|
| 337 | assert(port);
|
|---|
| 338 | usb_log_debug2("%s Port status(%#x):%s%s%s%s%s%s%s%s%s%s%s.\n",
|
|---|
| 339 | port->id_string, value,
|
|---|
| 340 | (value & STATUS_SUSPEND) ? " SUSPENDED," : "",
|
|---|
| 341 | (value & STATUS_RESUME) ? " IN RESUME," : "",
|
|---|
| 342 | (value & STATUS_IN_RESET) ? " IN RESET," : "",
|
|---|
| 343 | (value & STATUS_LINE_D_MINUS) ? " VD-," : "",
|
|---|
| 344 | (value & STATUS_LINE_D_PLUS) ? " VD+," : "",
|
|---|
| 345 | (value & STATUS_LOW_SPEED) ? " LOWSPEED," : "",
|
|---|
| 346 | (value & STATUS_ENABLED_CHANGED) ? " ENABLED-CHANGE," : "",
|
|---|
| 347 | (value & STATUS_ENABLED) ? " ENABLED," : "",
|
|---|
| 348 | (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "",
|
|---|
| 349 | (value & STATUS_CONNECTED) ? " CONNECTED," : "",
|
|---|
| 350 | (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR: NO ALWAYS ONE"
|
|---|
| 351 | );
|
|---|
| 352 | }
|
|---|
| 353 | /**
|
|---|
| 354 | * @}
|
|---|
| 355 | */
|
|---|