[e40b9f00] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[e40b9f00] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Functions needed by hub drivers.
|
---|
| 34 | */
|
---|
[77ad86c] | 35 |
|
---|
[7d521e24] | 36 | #include <usb/dev/hub.h>
|
---|
| 37 | #include <usb/dev/pipes.h>
|
---|
| 38 | #include <usb/dev/request.h>
|
---|
| 39 | #include <usb/dev/recognise.h>
|
---|
[fb422312] | 40 | #include <usb/debug.h>
|
---|
[e40b9f00] | 41 | #include <errno.h>
|
---|
[eb1a2f4] | 42 | #include <assert.h>
|
---|
[b6c9e1e] | 43 | #include <usb/debug.h>
|
---|
[5f94a0c] | 44 | #include <time.h>
|
---|
[79ae36dd] | 45 | #include <async.h>
|
---|
[e40b9f00] | 46 |
|
---|
[6e3c005] | 47 | /** How much time to wait between attempts to get the default address.
|
---|
[1c258d1] | 48 | * The value is based on typical value for port reset + some overhead.
|
---|
| 49 | */
|
---|
[6e3c005] | 50 | #define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
|
---|
[e40b9f00] | 51 |
|
---|
| 52 | /** Inform host controller about new device.
|
---|
| 53 | *
|
---|
| 54 | * @param connection Opened connection to host controller.
|
---|
| 55 | * @param attached_device Information about the new device.
|
---|
| 56 | * @return Error code.
|
---|
| 57 | */
|
---|
[6e3c005] | 58 | int usb_hub_register_device(usb_hc_connection_t *connection,
|
---|
[4501e207] | 59 | const usb_hub_attached_device_t *attached_device)
|
---|
[e40b9f00] | 60 | {
|
---|
[6e3c005] | 61 | assert(connection);
|
---|
[02fc5c4] | 62 | if (attached_device == NULL || attached_device->fun == NULL)
|
---|
[6e3c005] | 63 | return EBADMEM;
|
---|
| 64 | return usb_hc_bind_address(connection,
|
---|
[56fd7cf] | 65 | attached_device->address, ddf_fun_get_handle(attached_device->fun));
|
---|
[e40b9f00] | 66 | }
|
---|
| 67 |
|
---|
[3238506] | 68 | /** Change address of connected device.
|
---|
| 69 | * This function automatically updates the backing connection to point to
|
---|
| 70 | * the new address. It also unregisterrs the old endpoint and registers
|
---|
| 71 | * a new one.
|
---|
| 72 | * This creates whole bunch of problems:
|
---|
| 73 | * 1. All pipes using this wire are broken because they are not
|
---|
| 74 | * registered for new address
|
---|
| 75 | * 2. All other pipes for this device are using wrong address,
|
---|
| 76 | * possibly targeting completely different device
|
---|
| 77 | *
|
---|
| 78 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
| 79 | * @param new_address New USB address to be set (in native endianness).
|
---|
| 80 | * @return Error code.
|
---|
| 81 | */
|
---|
[7267fa1] | 82 | static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
|
---|
[9f9b31ad] | 83 | {
|
---|
[3238506] | 84 | if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
|
---|
| 85 | return EINVAL;
|
---|
[9f9b31ad] | 86 | }
|
---|
[3238506] | 87 | assert(pipe);
|
---|
| 88 | assert(pipe->wire != NULL);
|
---|
| 89 |
|
---|
| 90 | const uint16_t addr = uint16_host2usb((uint16_t)new_address);
|
---|
| 91 |
|
---|
| 92 | int rc = usb_control_request_set(pipe,
|
---|
| 93 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 94 | USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
|
---|
[9f9b31ad] | 95 |
|
---|
| 96 | if (rc != EOK) {
|
---|
[3238506] | 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /* TODO: prevent others from accessing the wire now. */
|
---|
[bd575647] | 101 | if (usb_pipe_unregister(pipe) != EOK) {
|
---|
[3238506] | 102 | usb_log_warning(
|
---|
| 103 | "Failed to unregister the old pipe on address change.\n");
|
---|
[9f9b31ad] | 104 | }
|
---|
[6e3c005] | 105 | /* Address changed. We can release the old one, thus
|
---|
| 106 | * allowing other to us it. */
|
---|
| 107 | usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address);
|
---|
[7267fa1] | 108 |
|
---|
[3238506] | 109 | /* The address is already changed so set it in the wire */
|
---|
| 110 | pipe->wire->address = new_address;
|
---|
[bd575647] | 111 | rc = usb_pipe_register(pipe, 0);
|
---|
[3238506] | 112 | if (rc != EOK)
|
---|
| 113 | return EADDRNOTAVAIL;
|
---|
[9f9b31ad] | 114 |
|
---|
[3238506] | 115 | return EOK;
|
---|
[9f9b31ad] | 116 | }
|
---|
| 117 |
|
---|
[6d8c5725] | 118 | /** Wrapper for registering attached device to the hub.
|
---|
| 119 | *
|
---|
[a6add7a] | 120 | * The @p enable_port function is expected to enable signaling on given
|
---|
[6d8c5725] | 121 | * port.
|
---|
[32ec5671] | 122 | * The argument can have arbitrary meaning and it is not touched at all
|
---|
| 123 | * by this function (it is passed as is to the @p enable_port function).
|
---|
[6d8c5725] | 124 | *
|
---|
| 125 | * If the @p enable_port fails (i.e. does not return EOK), the device
|
---|
[a6add7a] | 126 | * addition is canceled.
|
---|
[6d8c5725] | 127 | * The return value is then returned (it is good idea to use different
|
---|
| 128 | * error codes than those listed as return codes by this function itself).
|
---|
| 129 | *
|
---|
[61727bf] | 130 | * The @p connection representing connection with host controller does not
|
---|
| 131 | * need to be started.
|
---|
| 132 | * This function duplicates the connection to allow simultaneous calls of
|
---|
| 133 | * this function (i.e. from different fibrils).
|
---|
| 134 | *
|
---|
[bc1c6fb] | 135 | * @param[in] parent Parent device (i.e. the hub device).
|
---|
[90d7033] | 136 | * @param[in] connection Connection to host controller. Must be non-null.
|
---|
[bc1c6fb] | 137 | * @param[in] dev_speed New device speed.
|
---|
| 138 | * @param[in] enable_port Function for enabling signaling through the port the
|
---|
[6d8c5725] | 139 | * device is attached to.
|
---|
[bc1c6fb] | 140 | * @param[in] arg Any data argument to @p enable_port.
|
---|
[6d8c5725] | 141 | * @param[out] assigned_address USB address of the device.
|
---|
[10059a68] | 142 | * @param[in] dev_ops Child device ops. Will use default if not provided.
|
---|
[bc1c6fb] | 143 | * @param[in] new_dev_data Arbitrary pointer to be stored in the child
|
---|
[59c163c] | 144 | * as @c driver_data. Will allocate and assign usb_hub_attached_device_t
|
---|
| 145 | * structure if NULL.
|
---|
[bc1c6fb] | 146 | * @param[out] new_fun Storage where pointer to allocated child function
|
---|
[90d7033] | 147 | * will be written. Must be non-null.
|
---|
[6d8c5725] | 148 | * @return Error code.
|
---|
[90d7033] | 149 | * @retval EINVAL Either connection or new_fun is a NULL pointer.
|
---|
[6d8c5725] | 150 | * @retval ENOENT Connection to HC not opened.
|
---|
| 151 | * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
|
---|
| 152 | * @retval EBUSY Failed reserving default USB address.
|
---|
| 153 | * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
|
---|
| 154 | * @retval ESTALL Problem communication with device (either SET_ADDRESS
|
---|
| 155 | * request or requests for descriptors when creating match ids).
|
---|
| 156 | */
|
---|
[ffa96c2] | 157 | int usb_hc_new_device_wrapper(ddf_dev_t *parent, ddf_fun_t *fun,
|
---|
[7267fa1] | 158 | usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
|
---|
[32ec5671] | 159 | int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
|
---|
[ffa96c2] | 160 | ddf_dev_ops_t *dev_ops)
|
---|
[6d8c5725] | 161 | {
|
---|
[ffa96c2] | 162 | if (hc_conn == NULL)
|
---|
[90d7033] | 163 | return EINVAL;
|
---|
| 164 |
|
---|
[61727bf] | 165 | int rc;
|
---|
[5f94a0c] | 166 | struct timeval start_time;
|
---|
| 167 |
|
---|
| 168 | rc = gettimeofday(&start_time, NULL);
|
---|
| 169 | if (rc != EOK) {
|
---|
| 170 | return rc;
|
---|
| 171 | }
|
---|
[61727bf] | 172 |
|
---|
[7267fa1] | 173 | /* We are gona do a lot of communication better open it in advance. */
|
---|
| 174 | rc = usb_hc_connection_open(hc_conn);
|
---|
[61727bf] | 175 | if (rc != EOK) {
|
---|
| 176 | return rc;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[7267fa1] | 179 | /* Request a new address. */
|
---|
[67f55e7b] | 180 | usb_address_t dev_addr =
|
---|
[7267fa1] | 181 | usb_hc_request_address(hc_conn, 0, false, dev_speed);
|
---|
[6d8c5725] | 182 | if (dev_addr < 0) {
|
---|
[fb422312] | 183 | rc = EADDRNOTAVAIL;
|
---|
| 184 | goto close_connection;
|
---|
[6d8c5725] | 185 | }
|
---|
| 186 |
|
---|
[7267fa1] | 187 | /* Initialize connection to device. */
|
---|
[6d8c5725] | 188 | usb_device_connection_t dev_conn;
|
---|
[3538b0e] | 189 | rc = usb_device_connection_initialize(
|
---|
| 190 | &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
|
---|
[6d8c5725] | 191 | if (rc != EOK) {
|
---|
| 192 | rc = ENOTCONN;
|
---|
[98064795] | 193 | goto leave_release_free_address;
|
---|
[6d8c5725] | 194 | }
|
---|
| 195 |
|
---|
[3538b0e] | 196 | /* Initialize control pipe on default address. Don't register yet. */
|
---|
[a372663] | 197 | usb_pipe_t ctrl_pipe;
|
---|
[3238506] | 198 | rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
|
---|
[6d8c5725] | 199 | if (rc != EOK) {
|
---|
| 200 | rc = ENOTCONN;
|
---|
[98064795] | 201 | goto leave_release_free_address;
|
---|
[6d8c5725] | 202 | }
|
---|
[9f9b31ad] | 203 |
|
---|
[3538b0e] | 204 | /*
|
---|
| 205 | * The default address request might fail.
|
---|
| 206 | * That means that someone else is already using that address.
|
---|
| 207 | * We will simply wait and try again.
|
---|
| 208 | * (Someone else already wants to add a new device.)
|
---|
| 209 | */
|
---|
[98064795] | 210 | do {
|
---|
[7267fa1] | 211 | rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
|
---|
[062165f] | 212 | true, dev_speed);
|
---|
[3238506] | 213 | if (rc == ENOENT) {
|
---|
[98064795] | 214 | /* Do not overheat the CPU ;-). */
|
---|
[6e3c005] | 215 | async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
|
---|
[98064795] | 216 | }
|
---|
[062165f] | 217 | } while (rc == ENOENT);
|
---|
[3238506] | 218 | if (rc < 0) {
|
---|
[062165f] | 219 | goto leave_release_free_address;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[3538b0e] | 222 | /* Register control pipe on default address. 0 means no interval. */
|
---|
[bd575647] | 223 | rc = usb_pipe_register(&ctrl_pipe, 0);
|
---|
[062165f] | 224 | if (rc != EOK) {
|
---|
| 225 | rc = ENOTCONN;
|
---|
| 226 | goto leave_release_default_address;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[5f94a0c] | 229 | struct timeval end_time;
|
---|
| 230 | rc = gettimeofday(&end_time, NULL);
|
---|
| 231 | if (rc != EOK) {
|
---|
| 232 | goto leave_release_default_address;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /* According to the USB spec part 9.1.2 host allows 100ms time for
|
---|
| 236 | * the insertion process to complete. According to 7.1.7.1 this is the
|
---|
| 237 | * time between attach detected and port reset. However, the setup done
|
---|
| 238 | * above might use much of this time so we should only wait to fill
|
---|
| 239 | * up the 100ms quota*/
|
---|
[3238506] | 240 | const suseconds_t elapsed = tv_sub(&end_time, &start_time);
|
---|
[5f94a0c] | 241 | if (elapsed < 100000) {
|
---|
| 242 | async_usleep(100000 - elapsed);
|
---|
| 243 | }
|
---|
[98064795] | 244 |
|
---|
[3238506] | 245 | /* Endpoint is registered. We can enable the port and change address. */
|
---|
[32ec5671] | 246 | rc = enable_port(arg);
|
---|
[9f9b31ad] | 247 | if (rc != EOK) {
|
---|
| 248 | goto leave_release_default_address;
|
---|
| 249 | }
|
---|
[5f94a0c] | 250 | /* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
|
---|
| 251 | * 10ms for reset recovery. Device response to any bus transactions
|
---|
| 252 | * addressed to the default device address during the reset recovery
|
---|
| 253 | * time is undefined.
|
---|
| 254 | */
|
---|
| 255 | async_usleep(10000);
|
---|
[98064795] | 256 |
|
---|
[3238506] | 257 | /* Get max_packet_size value. */
|
---|
[3954a63b] | 258 | rc = usb_pipe_probe_default_control(&ctrl_pipe);
|
---|
[206f71a] | 259 | if (rc != EOK) {
|
---|
[98064795] | 260 | rc = ESTALL;
|
---|
[206f71a] | 261 | goto leave_release_default_address;
|
---|
| 262 | }
|
---|
[6d8c5725] | 263 |
|
---|
[7267fa1] | 264 | rc = usb_request_set_address(&ctrl_pipe, dev_addr);
|
---|
[6d8c5725] | 265 | if (rc != EOK) {
|
---|
| 266 | rc = ESTALL;
|
---|
[c6394aa] | 267 | goto leave_release_default_address;
|
---|
[6d8c5725] | 268 | }
|
---|
| 269 |
|
---|
[9f9b31ad] | 270 |
|
---|
[3238506] | 271 | /* Register the device with devman. */
|
---|
[6d8c5725] | 272 | /* FIXME: create device_register that will get opened ctrl pipe. */
|
---|
[2179cf95] | 273 | rc = usb_device_register_child_in_devman(&ctrl_pipe,
|
---|
[ffa96c2] | 274 | parent, fun, dev_ops);
|
---|
[6d8c5725] | 275 | if (rc != EOK) {
|
---|
| 276 | goto leave_release_free_address;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[90d7033] | 279 | const usb_hub_attached_device_t new_device = {
|
---|
[6d8c5725] | 280 | .address = dev_addr,
|
---|
[ffa96c2] | 281 | .fun = fun,
|
---|
[6d8c5725] | 282 | };
|
---|
[59c163c] | 283 |
|
---|
| 284 |
|
---|
[3238506] | 285 | /* Inform the host controller about the handle. */
|
---|
[6e3c005] | 286 | rc = usb_hub_register_device(hc_conn, &new_device);
|
---|
[6d8c5725] | 287 | if (rc != EOK) {
|
---|
[90d7033] | 288 | /* The child function is already created. */
|
---|
[6d8c5725] | 289 | rc = EDESTADDRREQ;
|
---|
| 290 | goto leave_release_free_address;
|
---|
| 291 | }
|
---|
[fb422312] | 292 |
|
---|
[6d8c5725] | 293 | if (assigned_address != NULL) {
|
---|
| 294 | *assigned_address = dev_addr;
|
---|
| 295 | }
|
---|
[3238506] | 296 |
|
---|
[fb422312] | 297 | rc = EOK;
|
---|
| 298 | goto close_connection;
|
---|
[6d8c5725] | 299 |
|
---|
| 300 | /*
|
---|
| 301 | * Error handling (like nested exceptions) starts here.
|
---|
| 302 | * Completely ignoring errors here.
|
---|
| 303 | */
|
---|
| 304 | leave_release_default_address:
|
---|
[6e3c005] | 305 | if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
|
---|
| 306 | usb_log_warning("%s: Failed to release defaut address.\n",
|
---|
[3538b0e] | 307 | __FUNCTION__);
|
---|
[6d8c5725] | 308 |
|
---|
| 309 | leave_release_free_address:
|
---|
[07a7a97d] | 310 | /* This might be either 0:0 or dev_addr:0 */
|
---|
[bd575647] | 311 | if (usb_pipe_unregister(&ctrl_pipe) != EOK)
|
---|
[07a7a97d] | 312 | usb_log_warning("%s: Failed to unregister default pipe.\n",
|
---|
| 313 | __FUNCTION__);
|
---|
[61727bf] | 314 |
|
---|
[6e3c005] | 315 | if (usb_hc_release_address(hc_conn, dev_addr) != EOK)
|
---|
| 316 | usb_log_warning("%s: Failed to release address: %d.\n",
|
---|
| 317 | __FUNCTION__, dev_addr);
|
---|
[3238506] | 318 |
|
---|
[fb422312] | 319 | close_connection:
|
---|
[7267fa1] | 320 | if (usb_hc_connection_close(hc_conn) != EOK)
|
---|
[3238506] | 321 | usb_log_warning("%s: Failed to close hc connection.\n",
|
---|
[10059a68] | 322 | __FUNCTION__);
|
---|
[6d8c5725] | 323 |
|
---|
| 324 | return rc;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[e40b9f00] | 327 | /**
|
---|
| 328 | * @}
|
---|
| 329 | */
|
---|