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