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