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