| 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 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 | #include <fibril_synch.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/usb.h> /* usb_address_t */
|
|---|
| 39 | #include <usb/usbdevice.h>
|
|---|
| 40 | #include <usb/hub.h>
|
|---|
| 41 | #include <usb/request.h>
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 | #include <usb/recognise.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "port.h"
|
|---|
| 46 | #include "port_status.h"
|
|---|
| 47 |
|
|---|
| 48 | static int uhci_port_new_device(uhci_port_t *port, uint16_t status);
|
|---|
| 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 int uhci_port_check(void *port);
|
|---|
| 52 | static int new_device_enable_port(int portno, void *arg);
|
|---|
| 53 |
|
|---|
| 54 | int uhci_port_init(
|
|---|
| 55 | uhci_port_t *port, port_status_t *address, unsigned number,
|
|---|
| 56 | unsigned usec, ddf_dev_t *rh, int parent_phone)
|
|---|
| 57 | {
|
|---|
| 58 | assert(port);
|
|---|
| 59 | port->address = address;
|
|---|
| 60 | port->number = number;
|
|---|
| 61 | port->wait_period_usec = usec;
|
|---|
| 62 | port->attached_device = 0;
|
|---|
| 63 | port->rh = rh;
|
|---|
| 64 | int rc = usb_hc_connection_initialize_from_device(
|
|---|
| 65 | &port->hc_connection, rh);
|
|---|
| 66 | if (rc != EOK) {
|
|---|
| 67 | usb_log_error("Failed to initialize connection to HC.");
|
|---|
| 68 | return rc;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | port->checker = fibril_create(uhci_port_check, port);
|
|---|
| 72 | if (port->checker == 0) {
|
|---|
| 73 | usb_log_error(": failed to launch root hub fibril.");
|
|---|
| 74 | return ENOMEM;
|
|---|
| 75 | }
|
|---|
| 76 | fibril_add_ready(port->checker);
|
|---|
| 77 | usb_log_debug(
|
|---|
| 78 | "Added fibril for port %d: %p.\n", number, port->checker);
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 | /*----------------------------------------------------------------------------*/
|
|---|
| 82 | void uhci_port_fini(uhci_port_t *port)
|
|---|
| 83 | {
|
|---|
| 84 | // TODO: destroy fibril
|
|---|
| 85 | // TODO: hangup phone
|
|---|
| 86 | // fibril_teardown(port->checker);
|
|---|
| 87 | return;
|
|---|
| 88 | }
|
|---|
| 89 | /*----------------------------------------------------------------------------*/
|
|---|
| 90 | int uhci_port_check(void *port)
|
|---|
| 91 | {
|
|---|
| 92 | uhci_port_t *port_instance = port;
|
|---|
| 93 | assert(port_instance);
|
|---|
| 94 |
|
|---|
| 95 | while (1) {
|
|---|
| 96 | /* read register value */
|
|---|
| 97 | port_status_t port_status =
|
|---|
| 98 | port_status_read(port_instance->address);
|
|---|
| 99 |
|
|---|
| 100 | /* debug print */
|
|---|
| 101 | static fibril_mutex_t dbg_mtx = FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
|
|---|
| 102 | fibril_mutex_lock(&dbg_mtx);
|
|---|
| 103 | usb_log_debug("Port %d status at %p: 0x%04x.\n",
|
|---|
| 104 | port_instance->number, port_instance->address, port_status);
|
|---|
| 105 | print_port_status(port_status);
|
|---|
| 106 | fibril_mutex_unlock(&dbg_mtx);
|
|---|
| 107 |
|
|---|
| 108 | if (port_status & STATUS_CONNECTED_CHANGED) {
|
|---|
| 109 | usb_log_debug("Change detected on port %d.\n", port_instance->number);
|
|---|
| 110 | int rc = usb_hc_connection_open(
|
|---|
| 111 | &port_instance->hc_connection);
|
|---|
| 112 | if (rc != EOK) {
|
|---|
| 113 | usb_log_error("Failed to connect to HC.");
|
|---|
| 114 | goto next;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /* remove any old device */
|
|---|
| 118 | if (port_instance->attached_device) {
|
|---|
| 119 | usb_log_debug("Removing device on port %d.\n",
|
|---|
| 120 | port_instance->number);
|
|---|
| 121 | uhci_port_remove_device(port_instance);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | port_status_write(port_instance->address, STATUS_CONNECTED_CHANGED);
|
|---|
| 125 | usb_log_debug("Change status erased on port %d.\n",
|
|---|
| 126 | port_instance->number);
|
|---|
| 127 |
|
|---|
| 128 | if (port_status & STATUS_CONNECTED) {
|
|---|
| 129 | /* new device */
|
|---|
| 130 | uhci_port_new_device(port_instance, port_status);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | rc = usb_hc_connection_close(
|
|---|
| 134 | &port_instance->hc_connection);
|
|---|
| 135 | if (rc != EOK) {
|
|---|
| 136 | usb_log_error("Failed to disconnect from HC.");
|
|---|
| 137 | goto next;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | next:
|
|---|
| 141 | async_usleep(port_instance->wait_period_usec);
|
|---|
| 142 | }
|
|---|
| 143 | return EOK;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Callback for enabling port during adding a new device.
|
|---|
| 147 | *
|
|---|
| 148 | * @param portno Port number (unused).
|
|---|
| 149 | * @param arg Pointer to uhci_port_t of port with the new device.
|
|---|
| 150 | * @return Error code.
|
|---|
| 151 | */
|
|---|
| 152 | static int new_device_enable_port(int portno, void *arg)
|
|---|
| 153 | {
|
|---|
| 154 | uhci_port_t *port = (uhci_port_t *) arg;
|
|---|
| 155 |
|
|---|
| 156 | usb_log_debug("new_device_enable_port(%d)\n", port->number);
|
|---|
| 157 |
|
|---|
| 158 | /*
|
|---|
| 159 | * The host then waits for at least 100 ms to allow completion of
|
|---|
| 160 | * an insertion process and for power at the device to become stable.
|
|---|
| 161 | */
|
|---|
| 162 | async_usleep(100000);
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | /* The hub maintains the reset signal to that port for 10 ms
|
|---|
| 166 | * (See Section 11.5.1.5)
|
|---|
| 167 | */
|
|---|
| 168 | {
|
|---|
| 169 | usb_log_debug("Reset Signal start on port %d.\n",
|
|---|
| 170 | port->number);
|
|---|
| 171 | port_status_t port_status =
|
|---|
| 172 | port_status_read(port->address);
|
|---|
| 173 | port_status |= STATUS_IN_RESET;
|
|---|
| 174 | port_status_write(port->address, port_status);
|
|---|
| 175 | async_usleep(10000);
|
|---|
| 176 | port_status =
|
|---|
| 177 | port_status_read(port->address);
|
|---|
| 178 | port_status &= ~STATUS_IN_RESET;
|
|---|
| 179 | port_status_write(port->address, port_status);
|
|---|
| 180 | usb_log_debug("Reset Signal stop on port %d.\n",
|
|---|
| 181 | port->number);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /* Enable the port. */
|
|---|
| 185 | uhci_port_set_enabled(port, true);
|
|---|
| 186 |
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /*----------------------------------------------------------------------------*/
|
|---|
| 191 | static int uhci_port_new_device(uhci_port_t *port, uint16_t status)
|
|---|
| 192 | {
|
|---|
| 193 | assert(port);
|
|---|
| 194 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
|---|
| 195 |
|
|---|
| 196 | usb_log_info("Detected new device on port %u.\n", port->number);
|
|---|
| 197 |
|
|---|
| 198 | usb_address_t dev_addr;
|
|---|
| 199 | int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
|---|
| 200 | ((status & STATUS_LOW_SPEED) != 0) ? USB_SPEED_LOW : USB_SPEED_FULL,
|
|---|
| 201 | new_device_enable_port, port->number, port,
|
|---|
| 202 | &dev_addr, &port->attached_device, NULL, NULL, NULL);
|
|---|
| 203 | if (rc != EOK) {
|
|---|
| 204 | usb_log_error("Failed adding new device on port %u: %s.\n",
|
|---|
| 205 | port->number, str_error(rc));
|
|---|
| 206 | uhci_port_set_enabled(port, false);
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | usb_log_info("New device on port %u has address %d (handle %zu).\n",
|
|---|
| 211 | port->number, dev_addr, port->attached_device);
|
|---|
| 212 |
|
|---|
| 213 | return EOK;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /*----------------------------------------------------------------------------*/
|
|---|
| 217 | static int uhci_port_remove_device(uhci_port_t *port)
|
|---|
| 218 | {
|
|---|
| 219 | usb_log_error("Don't know how to remove device %#x.\n",
|
|---|
| 220 | (unsigned int)port->attached_device);
|
|---|
| 221 | // uhci_port_set_enabled(port, false);
|
|---|
| 222 | return EOK;
|
|---|
| 223 | }
|
|---|
| 224 | /*----------------------------------------------------------------------------*/
|
|---|
| 225 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
|---|
| 226 | {
|
|---|
| 227 | assert(port);
|
|---|
| 228 |
|
|---|
| 229 | /* read register value */
|
|---|
| 230 | port_status_t port_status
|
|---|
| 231 | = port_status_read(port->address);
|
|---|
| 232 |
|
|---|
| 233 | /* enable port: register write */
|
|---|
| 234 | if (enabled) {
|
|---|
| 235 | port_status |= STATUS_ENABLED;
|
|---|
| 236 | } else {
|
|---|
| 237 | port_status &= ~STATUS_ENABLED;
|
|---|
| 238 | }
|
|---|
| 239 | port_status_write(port->address, port_status);
|
|---|
| 240 |
|
|---|
| 241 | usb_log_info("%s port %d.\n",
|
|---|
| 242 | enabled ? "Enabled" : "Disabled", port->number);
|
|---|
| 243 | return EOK;
|
|---|
| 244 | }
|
|---|
| 245 | /*----------------------------------------------------------------------------*/
|
|---|
| 246 | /**
|
|---|
| 247 | * @}
|
|---|
| 248 | */
|
|---|