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