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