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