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