[6105fc0] | 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
|
---|
[6105fc0] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device driver framework.
|
---|
| 34 | */
|
---|
[7d521e24] | 35 | #include <usb/dev/driver.h>
|
---|
| 36 | #include <usb/dev/request.h>
|
---|
[6105fc0] | 37 | #include <usb/debug.h>
|
---|
[7d521e24] | 38 | #include <usb/dev/dp.h>
|
---|
[6105fc0] | 39 | #include <errno.h>
|
---|
[69334af] | 40 | #include <str_error.h>
|
---|
[6105fc0] | 41 | #include <assert.h>
|
---|
| 42 |
|
---|
[1a4ea01d] | 43 | static int generic_device_add(ddf_dev_t *);
|
---|
[96646a6] | 44 | static int generic_device_remove(ddf_dev_t *);
|
---|
| 45 | static int generic_device_gone(ddf_dev_t *);
|
---|
[6105fc0] | 46 |
|
---|
| 47 | static driver_ops_t generic_driver_ops = {
|
---|
[96646a6] | 48 | .add_device = generic_device_add,
|
---|
| 49 | .dev_remove = generic_device_remove,
|
---|
| 50 | .dev_gone = generic_device_gone,
|
---|
[6105fc0] | 51 | };
|
---|
| 52 | static driver_t generic_driver = {
|
---|
| 53 | .driver_ops = &generic_driver_ops
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[49bd7ae2] | 56 | static const usb_driver_t *driver = NULL;
|
---|
[6105fc0] | 57 |
|
---|
[69334af] | 58 |
|
---|
| 59 | /** Main routine of USB device driver.
|
---|
| 60 | *
|
---|
| 61 | * Under normal conditions, this function never returns.
|
---|
| 62 | *
|
---|
| 63 | * @param drv USB device driver structure.
|
---|
| 64 | * @return Task exit status.
|
---|
| 65 | */
|
---|
[882580a] | 66 | int usb_driver_main(const usb_driver_t *drv)
|
---|
[6105fc0] | 67 | {
|
---|
[69334af] | 68 | assert(drv != NULL);
|
---|
| 69 |
|
---|
[6105fc0] | 70 | /* Prepare the generic driver. */
|
---|
| 71 | generic_driver.name = drv->name;
|
---|
| 72 |
|
---|
| 73 | driver = drv;
|
---|
| 74 |
|
---|
| 75 | return ddf_driver_main(&generic_driver);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[69334af] | 78 | /** Count number of pipes the driver expects.
|
---|
| 79 | *
|
---|
| 80 | * @param drv USB driver.
|
---|
| 81 | * @return Number of pipes (excluding default control pipe).
|
---|
| 82 | */
|
---|
[b803845] | 83 | static size_t count_other_pipes(const usb_endpoint_description_t **endpoints)
|
---|
[6105fc0] | 84 | {
|
---|
| 85 | size_t count = 0;
|
---|
[0b4e7ca] | 86 | if (endpoints == NULL) {
|
---|
[6105fc0] | 87 | return 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[0b4e7ca] | 90 | while (endpoints[count] != NULL) {
|
---|
[6105fc0] | 91 | count++;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return count;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[69334af] | 97 | /** Initialize endpoint pipes, excluding default control one.
|
---|
| 98 | *
|
---|
| 99 | * @param drv The device driver.
|
---|
| 100 | * @param dev Device to be initialized.
|
---|
| 101 | * @return Error code.
|
---|
| 102 | */
|
---|
[b803845] | 103 | static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
|
---|
[c1b1944] | 104 | usb_device_t *dev, int alternate_setting)
|
---|
[6105fc0] | 105 | {
|
---|
[6ee6e6f] | 106 | if (endpoints == NULL) {
|
---|
| 107 | dev->pipes = NULL;
|
---|
| 108 | dev->pipes_count = 0;
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[c1b1944] | 112 | usb_endpoint_mapping_t *pipes;
|
---|
| 113 | size_t pipes_count;
|
---|
[6105fc0] | 114 |
|
---|
[c1b1944] | 115 | int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
|
---|
[e484f3b] | 116 | dev->descriptors.configuration, dev->descriptors.configuration_size,
|
---|
[7c95d6f5] | 117 | dev->interface_no, alternate_setting, &pipes, &pipes_count);
|
---|
[6105fc0] | 118 |
|
---|
[5fd22d8] | 119 | if (rc != EOK) {
|
---|
[c1b1944] | 120 | return rc;
|
---|
[5fd22d8] | 121 | }
|
---|
| 122 |
|
---|
[c1b1944] | 123 | dev->pipes = pipes;
|
---|
| 124 | dev->pipes_count = pipes_count;
|
---|
[0b4e7ca] | 125 |
|
---|
[6105fc0] | 126 | return EOK;
|
---|
| 127 | }
|
---|
[51f033ce] | 128 | /*----------------------------------------------------------------------------*/
|
---|
| 129 | /** Callback when a new device is supposed to be controlled by this driver.
|
---|
[69334af] | 130 | *
|
---|
[51f033ce] | 131 | * This callback is a wrapper for USB specific version of @c device_add.
|
---|
[69334af] | 132 | *
|
---|
| 133 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 134 | * @return Error code.
|
---|
| 135 | */
|
---|
[1a4ea01d] | 136 | int generic_device_add(ddf_dev_t *gen_dev)
|
---|
[6105fc0] | 137 | {
|
---|
| 138 | assert(driver);
|
---|
| 139 | assert(driver->ops);
|
---|
[1a4ea01d] | 140 | assert(driver->ops->device_add);
|
---|
[6105fc0] | 141 |
|
---|
[7d9cd62] | 142 | usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
|
---|
| 143 | if (dev == NULL) {
|
---|
| 144 | usb_log_error("USB device `%s' structure allocation failed.\n",
|
---|
| 145 | gen_dev->name);
|
---|
| 146 | return ENOMEM;
|
---|
| 147 | }
|
---|
[6ee6e6f] | 148 | const char *err_msg = NULL;
|
---|
[7d9cd62] | 149 | int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
|
---|
[69334af] | 150 | if (rc != EOK) {
|
---|
[7d9cd62] | 151 | usb_log_error("USB device `%s' init failed (%s): %s.\n",
|
---|
[6ee6e6f] | 152 | gen_dev->name, err_msg, str_error(rc));
|
---|
[69334af] | 153 | return rc;
|
---|
[6105fc0] | 154 | }
|
---|
| 155 |
|
---|
[065064e6] | 156 | rc = driver->ops->device_add(dev);
|
---|
| 157 | if (rc != EOK)
|
---|
[9c5fd7a] | 158 | usb_device_deinit(dev);
|
---|
[065064e6] | 159 | return rc;
|
---|
[6105fc0] | 160 | }
|
---|
[96646a6] | 161 | /*----------------------------------------------------------------------------*/
|
---|
[51f033ce] | 162 | /** Callback when a device is supposed to be removed from the system.
|
---|
| 163 | *
|
---|
| 164 | * This callback is a wrapper for USB specific version of @c device_remove.
|
---|
| 165 | *
|
---|
| 166 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 167 | * @return Error code.
|
---|
| 168 | */
|
---|
[96646a6] | 169 | int generic_device_remove(ddf_dev_t *gen_dev)
|
---|
| 170 | {
|
---|
| 171 | assert(driver);
|
---|
| 172 | assert(driver->ops);
|
---|
| 173 | if (driver->ops->device_rem == NULL)
|
---|
| 174 | return ENOTSUP;
|
---|
[844f4ef] | 175 | /* Just tell the driver to stop whatever it is doing, keep structures */
|
---|
| 176 | return driver->ops->device_rem(gen_dev->driver_data);
|
---|
[96646a6] | 177 | }
|
---|
| 178 | /*----------------------------------------------------------------------------*/
|
---|
[51f033ce] | 179 | /** Callback when a device was removed from the system.
|
---|
| 180 | *
|
---|
| 181 | * This callback is a wrapper for USB specific version of @c device_gone.
|
---|
| 182 | *
|
---|
| 183 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 184 | * @return Error code.
|
---|
| 185 | */
|
---|
[96646a6] | 186 | int generic_device_gone(ddf_dev_t *gen_dev)
|
---|
| 187 | {
|
---|
| 188 | assert(driver);
|
---|
| 189 | assert(driver->ops);
|
---|
[844f4ef] | 190 | if (driver->ops->device_gone == NULL)
|
---|
| 191 | return ENOTSUP;
|
---|
[065064e6] | 192 | usb_device_t *usb_dev = gen_dev->driver_data;
|
---|
| 193 | const int ret = driver->ops->device_gone(usb_dev);
|
---|
[844f4ef] | 194 | if (ret == EOK)
|
---|
[9c5fd7a] | 195 | usb_device_deinit(usb_dev);
|
---|
[96646a6] | 196 |
|
---|
[844f4ef] | 197 | return ret;
|
---|
[96646a6] | 198 | }
|
---|
| 199 | /*----------------------------------------------------------------------------*/
|
---|
[0b4e7ca] | 200 | /** Destroy existing pipes of a USB device.
|
---|
| 201 | *
|
---|
| 202 | * @param dev Device where to destroy the pipes.
|
---|
| 203 | * @return Error code.
|
---|
| 204 | */
|
---|
| 205 | static int destroy_current_pipes(usb_device_t *dev)
|
---|
| 206 | {
|
---|
[c1b1944] | 207 | int rc = usb_device_destroy_pipes(dev->ddf_dev,
|
---|
| 208 | dev->pipes, dev->pipes_count);
|
---|
[0b4e7ca] | 209 | if (rc != EOK) {
|
---|
| 210 | return rc;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | dev->pipes = NULL;
|
---|
| 214 | dev->pipes_count = 0;
|
---|
| 215 |
|
---|
| 216 | return EOK;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Change interface setting of a device.
|
---|
| 220 | * This function selects new alternate setting of an interface by issuing
|
---|
| 221 | * proper USB command to the device and also creates new USB pipes
|
---|
| 222 | * under @c dev->pipes.
|
---|
| 223 | *
|
---|
| 224 | * @warning This function is intended for drivers working at interface level.
|
---|
| 225 | * For drivers controlling the whole device, you need to change interface
|
---|
| 226 | * manually using usb_request_set_interface() and creating new pipes
|
---|
| 227 | * with usb_pipe_initialize_from_configuration().
|
---|
| 228 | *
|
---|
[3f2af64] | 229 | * @warning This is a wrapper function that does several operations that
|
---|
| 230 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
| 231 | * during the SET_INTERFACE request would result in having a device with
|
---|
| 232 | * no pipes at all (except the default control one). That is because the old
|
---|
| 233 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
| 234 | * be created.
|
---|
| 235 | *
|
---|
[0b4e7ca] | 236 | * @param dev USB device.
|
---|
| 237 | * @param alternate_setting Alternate setting to choose.
|
---|
| 238 | * @param endpoints New endpoint descriptions.
|
---|
| 239 | * @return Error code.
|
---|
| 240 | */
|
---|
| 241 | int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
|
---|
[b803845] | 242 | const usb_endpoint_description_t **endpoints)
|
---|
[0b4e7ca] | 243 | {
|
---|
| 244 | if (dev->interface_no < 0) {
|
---|
| 245 | return EINVAL;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | int rc;
|
---|
| 249 |
|
---|
| 250 | /* Destroy existing pipes. */
|
---|
| 251 | rc = destroy_current_pipes(dev);
|
---|
| 252 | if (rc != EOK) {
|
---|
| 253 | return rc;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /* Change the interface itself. */
|
---|
| 257 | rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
|
---|
| 258 | alternate_setting);
|
---|
| 259 | if (rc != EOK) {
|
---|
| 260 | return rc;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /* Create new pipes. */
|
---|
[c1b1944] | 264 | rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
|
---|
| 265 |
|
---|
| 266 | return rc;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /** Retrieve basic descriptors from the device.
|
---|
| 270 | *
|
---|
[4fa0a384] | 271 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
[c1b1944] | 272 | * @param[out] descriptors Where to store the descriptors.
|
---|
| 273 | * @return Error code.
|
---|
| 274 | */
|
---|
| 275 | int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
|
---|
| 276 | usb_device_descriptors_t *descriptors)
|
---|
| 277 | {
|
---|
| 278 | assert(descriptors != NULL);
|
---|
| 279 |
|
---|
| 280 | descriptors->configuration = NULL;
|
---|
| 281 |
|
---|
| 282 | int rc;
|
---|
| 283 |
|
---|
[4fa0a384] | 284 | /* It is worth to start a long transfer. */
|
---|
[2c2cbcf] | 285 | usb_pipe_start_long_transfer(ctrl_pipe);
|
---|
[4fa0a384] | 286 |
|
---|
[c1b1944] | 287 | /* Get the device descriptor. */
|
---|
| 288 | rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
|
---|
| 289 | if (rc != EOK) {
|
---|
[4fa0a384] | 290 | goto leave;
|
---|
[c1b1944] | 291 | }
|
---|
| 292 |
|
---|
| 293 | /* Get the full configuration descriptor. */
|
---|
| 294 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
| 295 | ctrl_pipe, 0, (void **) &descriptors->configuration,
|
---|
| 296 | &descriptors->configuration_size);
|
---|
| 297 |
|
---|
[4fa0a384] | 298 | leave:
|
---|
| 299 | usb_pipe_end_long_transfer(ctrl_pipe);
|
---|
| 300 |
|
---|
| 301 | return rc;
|
---|
[c1b1944] | 302 | }
|
---|
| 303 |
|
---|
[7fc260ff] | 304 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
| 305 | *
|
---|
| 306 | * @param[in] descriptors Where to store the descriptors.
|
---|
| 307 | */
|
---|
| 308 | void usb_device_release_descriptors(usb_device_descriptors_t *descriptors)
|
---|
| 309 | {
|
---|
| 310 | assert(descriptors);
|
---|
| 311 | free(descriptors->configuration);
|
---|
| 312 | descriptors->configuration = NULL;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[c1b1944] | 315 | /** Create pipes for a device.
|
---|
| 316 | *
|
---|
| 317 | * This is more or less a wrapper that does following actions:
|
---|
| 318 | * - allocate and initialize pipes
|
---|
| 319 | * - map endpoints to the pipes based on the descriptions
|
---|
| 320 | * - registers endpoints with the host controller
|
---|
| 321 | *
|
---|
| 322 | * @param[in] dev Generic DDF device backing the USB one.
|
---|
| 323 | * @param[in] wire Initialized backing connection to the host controller.
|
---|
| 324 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
| 325 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
| 326 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
| 327 | * @param[in] interface_no Interface to map from.
|
---|
| 328 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
| 329 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
| 330 | * (not NULL terminated).
|
---|
| 331 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
| 332 | * (set to if you wish to ignore the count).
|
---|
| 333 | * @return Error code.
|
---|
| 334 | */
|
---|
[8e5ce07] | 335 | int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
|
---|
[b803845] | 336 | const usb_endpoint_description_t **endpoints,
|
---|
[7c95d6f5] | 337 | const uint8_t *config_descr, size_t config_descr_size,
|
---|
[c1b1944] | 338 | int interface_no, int interface_setting,
|
---|
| 339 | usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
|
---|
| 340 | {
|
---|
| 341 | assert(dev != NULL);
|
---|
| 342 | assert(wire != NULL);
|
---|
| 343 | assert(endpoints != NULL);
|
---|
| 344 | assert(config_descr != NULL);
|
---|
| 345 | assert(config_descr_size > 0);
|
---|
| 346 | assert(pipes_ptr != NULL);
|
---|
| 347 |
|
---|
| 348 | size_t i;
|
---|
| 349 | int rc;
|
---|
| 350 |
|
---|
[5917859c] | 351 | const size_t pipe_count = count_other_pipes(endpoints);
|
---|
[c1b1944] | 352 | if (pipe_count == 0) {
|
---|
[5917859c] | 353 | *pipes_count_ptr = pipe_count;
|
---|
[c1b1944] | 354 | *pipes_ptr = NULL;
|
---|
| 355 | return EOK;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | usb_endpoint_mapping_t *pipes
|
---|
| 359 | = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
|
---|
| 360 | if (pipes == NULL) {
|
---|
| 361 | return ENOMEM;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /* Initialize to NULL to allow smooth rollback. */
|
---|
| 365 | for (i = 0; i < pipe_count; i++) {
|
---|
| 366 | pipes[i].pipe = NULL;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | /* Now allocate and fully initialize. */
|
---|
| 370 | for (i = 0; i < pipe_count; i++) {
|
---|
| 371 | pipes[i].pipe = malloc(sizeof(usb_pipe_t));
|
---|
| 372 | if (pipes[i].pipe == NULL) {
|
---|
| 373 | rc = ENOMEM;
|
---|
| 374 | goto rollback_free_only;
|
---|
| 375 | }
|
---|
| 376 | pipes[i].description = endpoints[i];
|
---|
| 377 | pipes[i].interface_no = interface_no;
|
---|
| 378 | pipes[i].interface_setting = interface_setting;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /* Find the mapping from configuration descriptor. */
|
---|
| 382 | rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
| 383 | config_descr, config_descr_size, wire);
|
---|
| 384 | if (rc != EOK) {
|
---|
| 385 | goto rollback_free_only;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /* Register the endpoints with HC. */
|
---|
| 389 | usb_hc_connection_t hc_conn;
|
---|
| 390 | rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
|
---|
| 391 | if (rc != EOK) {
|
---|
| 392 | goto rollback_free_only;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | rc = usb_hc_connection_open(&hc_conn);
|
---|
| 396 | if (rc != EOK) {
|
---|
| 397 | goto rollback_free_only;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | for (i = 0; i < pipe_count; i++) {
|
---|
| 401 | if (pipes[i].present) {
|
---|
| 402 | rc = usb_pipe_register(pipes[i].pipe,
|
---|
| 403 | pipes[i].descriptor->poll_interval, &hc_conn);
|
---|
| 404 | if (rc != EOK) {
|
---|
| 405 | goto rollback_unregister_endpoints;
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[fb422312] | 410 | if (usb_hc_connection_close(&hc_conn) != EOK)
|
---|
| 411 | usb_log_warning("usb_device_create_pipes(): "
|
---|
| 412 | "Failed to close connection.\n");
|
---|
[c1b1944] | 413 |
|
---|
| 414 | *pipes_ptr = pipes;
|
---|
| 415 | if (pipes_count_ptr != NULL) {
|
---|
| 416 | *pipes_count_ptr = pipe_count;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | return EOK;
|
---|
| 420 |
|
---|
| 421 | /*
|
---|
| 422 | * Jump here if something went wrong after endpoints have
|
---|
| 423 | * been registered.
|
---|
| 424 | * This is also the target when the registration of
|
---|
| 425 | * endpoints fails.
|
---|
| 426 | */
|
---|
| 427 | rollback_unregister_endpoints:
|
---|
| 428 | for (i = 0; i < pipe_count; i++) {
|
---|
| 429 | if (pipes[i].present) {
|
---|
| 430 | usb_pipe_unregister(pipes[i].pipe, &hc_conn);
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[fb422312] | 434 | if (usb_hc_connection_close(&hc_conn) != EOK)
|
---|
| 435 | usb_log_warning("usb_device_create_pipes(): "
|
---|
| 436 | "Failed to close connection.\n");
|
---|
[c1b1944] | 437 |
|
---|
| 438 | /*
|
---|
| 439 | * Jump here if something went wrong before some actual communication
|
---|
| 440 | * with HC. Then the only thing that needs to be done is to free
|
---|
| 441 | * allocated memory.
|
---|
| 442 | */
|
---|
| 443 | rollback_free_only:
|
---|
| 444 | for (i = 0; i < pipe_count; i++) {
|
---|
| 445 | if (pipes[i].pipe != NULL) {
|
---|
| 446 | free(pipes[i].pipe);
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 | free(pipes);
|
---|
[0b4e7ca] | 450 |
|
---|
| 451 | return rc;
|
---|
| 452 | }
|
---|
[159b91f4] | 453 |
|
---|
[c1b1944] | 454 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 455 | *
|
---|
| 456 | * @param[in] dev Generic DDF device backing the USB one.
|
---|
| 457 | * @param[in] pipes Endpoint mapping to be destroyed.
|
---|
| 458 | * @param[in] pipes_count Number of endpoints.
|
---|
| 459 | */
|
---|
[8e5ce07] | 460 | int usb_device_destroy_pipes(const ddf_dev_t *dev,
|
---|
[c1b1944] | 461 | usb_endpoint_mapping_t *pipes, size_t pipes_count)
|
---|
| 462 | {
|
---|
| 463 | assert(dev != NULL);
|
---|
| 464 |
|
---|
| 465 | if (pipes_count == 0) {
|
---|
[5917859c] | 466 | assert(pipes == NULL);
|
---|
[c1b1944] | 467 | return EOK;
|
---|
| 468 | }
|
---|
[5917859c] | 469 | assert(pipes != NULL);
|
---|
[c1b1944] | 470 |
|
---|
| 471 | int rc;
|
---|
| 472 |
|
---|
| 473 | /* Prepare connection to HC to allow endpoint unregistering. */
|
---|
| 474 | usb_hc_connection_t hc_conn;
|
---|
| 475 | rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
|
---|
| 476 | if (rc != EOK) {
|
---|
| 477 | return rc;
|
---|
| 478 | }
|
---|
| 479 | rc = usb_hc_connection_open(&hc_conn);
|
---|
| 480 | if (rc != EOK) {
|
---|
| 481 | return rc;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /* Destroy the pipes. */
|
---|
| 485 | size_t i;
|
---|
| 486 | for (i = 0; i < pipes_count; i++) {
|
---|
[1526c174] | 487 | usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
|
---|
| 488 | i, pipes[i].present ? "" : "not ");
|
---|
| 489 | if (pipes[i].present)
|
---|
| 490 | usb_pipe_unregister(pipes[i].pipe, &hc_conn);
|
---|
[c1b1944] | 491 | free(pipes[i].pipe);
|
---|
| 492 | }
|
---|
| 493 |
|
---|
[fb422312] | 494 | if (usb_hc_connection_close(&hc_conn) != EOK)
|
---|
| 495 | usb_log_warning("usb_device_destroy_pipes(): "
|
---|
| 496 | "Failed to close connection.\n");
|
---|
[c1b1944] | 497 |
|
---|
| 498 | free(pipes);
|
---|
| 499 |
|
---|
| 500 | return EOK;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[7d9cd62] | 503 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 504 | *
|
---|
[7d9cd62] | 505 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 506 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 507 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 508 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 509 | * (in case error occurs).
|
---|
| 510 | * @return Error code.
|
---|
| 511 | */
|
---|
[7d9cd62] | 512 | int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
| 513 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 514 | {
|
---|
[7d9cd62] | 515 | assert(usb_dev != NULL);
|
---|
[6ee6e6f] | 516 | assert(ddf_dev != NULL);
|
---|
| 517 |
|
---|
[7fc260ff] | 518 | *errstr_ptr = NULL;
|
---|
| 519 |
|
---|
[7d9cd62] | 520 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 521 | usb_dev->driver_data = NULL;
|
---|
| 522 | usb_dev->descriptors.configuration = NULL;
|
---|
| 523 | usb_dev->pipes_count = 0;
|
---|
| 524 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 525 |
|
---|
[3f2af64] | 526 | /* Initialize backing wire and control pipe. */
|
---|
[7fc260ff] | 527 | int rc = usb_device_connection_initialize_from_device(
|
---|
| 528 | &usb_dev->wire, ddf_dev);
|
---|
[3f2af64] | 529 | if (rc != EOK) {
|
---|
[7fc260ff] | 530 | *errstr_ptr = "device connection initialization";
|
---|
| 531 | return rc;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | /* This pipe was registered by the hub driver,
|
---|
| 535 | * during device initialization. */
|
---|
| 536 | rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe,
|
---|
| 537 | &usb_dev->wire);
|
---|
| 538 | if (rc != EOK) {
|
---|
| 539 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 540 | return rc;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | /* Get our interface. */
|
---|
[7d9cd62] | 544 | usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev);
|
---|
[3f2af64] | 545 |
|
---|
| 546 | /* Retrieve standard descriptors. */
|
---|
[7d9cd62] | 547 | rc = usb_device_retrieve_descriptors(&usb_dev->ctrl_pipe,
|
---|
| 548 | &usb_dev->descriptors);
|
---|
[6ee6e6f] | 549 | if (rc != EOK) {
|
---|
[3f2af64] | 550 | *errstr_ptr = "descriptor retrieval";
|
---|
[6ee6e6f] | 551 | return rc;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
[7fc260ff] | 554 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 555 | * We might either control one interface or an entire device,
|
---|
| 556 | * it makes no sense to speak about alternate interfaces when
|
---|
| 557 | * controlling a device. */
|
---|
[904dcc6] | 558 | usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
| 559 | usb_dev->descriptors.configuration,
|
---|
| 560 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 561 |
|
---|
[7fc260ff] | 562 | /* TODO Add comment here. */
|
---|
[7d9cd62] | 563 | rc = initialize_other_pipes(endpoints, usb_dev, 0);
|
---|
[6ee6e6f] | 564 | if (rc != EOK) {
|
---|
[7d9cd62] | 565 | /* Full configuration descriptor is allocated. */
|
---|
[7fc260ff] | 566 | usb_device_release_descriptors(&usb_dev->descriptors);
|
---|
[7d9cd62] | 567 | /* Alternate interfaces may be allocated */
|
---|
[904dcc6] | 568 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
[6ee6e6f] | 569 | *errstr_ptr = "pipes initialization";
|
---|
| 570 | return rc;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | return EOK;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[7d9cd62] | 576 | /** Clean instance of a USB device.
|
---|
[70452dd4] | 577 | *
|
---|
[9c5fd7a] | 578 | * @param dev Device to be de-initialized.
|
---|
| 579 | *
|
---|
| 580 | * Does not free/destroy supplied pointer.
|
---|
[70452dd4] | 581 | */
|
---|
[9c5fd7a] | 582 | void usb_device_deinit(usb_device_t *dev)
|
---|
[70452dd4] | 583 | {
|
---|
[7d9cd62] | 584 | if (dev) {
|
---|
| 585 | /* Ignore errors and hope for the best. */
|
---|
| 586 | destroy_current_pipes(dev);
|
---|
[70452dd4] | 587 |
|
---|
[904dcc6] | 588 | usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
|
---|
[7fc260ff] | 589 | usb_device_release_descriptors(&dev->descriptors);
|
---|
[7d9cd62] | 590 | free(dev->driver_data);
|
---|
[70452dd4] | 591 | }
|
---|
[065064e6] | 592 | }
|
---|
| 593 |
|
---|
| 594 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 595 | {
|
---|
| 596 | assert(usb_dev);
|
---|
| 597 | assert(usb_dev->driver_data == NULL);
|
---|
| 598 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 599 |
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[6105fc0] | 602 | /**
|
---|
| 603 | * @}
|
---|
| 604 | */
|
---|