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