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