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