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