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