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