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