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