[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>
|
---|
[1a38701] | 38 | #include <usb/dev.h>
|
---|
[7d521e24] | 39 | #include <usb/dev/dp.h>
|
---|
[6105fc0] | 40 | #include <errno.h>
|
---|
[69334af] | 41 | #include <str_error.h>
|
---|
[6105fc0] | 42 | #include <assert.h>
|
---|
| 43 |
|
---|
[1a4ea01d] | 44 | static int generic_device_add(ddf_dev_t *);
|
---|
[96646a6] | 45 | static int generic_device_remove(ddf_dev_t *);
|
---|
| 46 | static int generic_device_gone(ddf_dev_t *);
|
---|
[6105fc0] | 47 |
|
---|
| 48 | static driver_ops_t generic_driver_ops = {
|
---|
[0c0f823b] | 49 | .dev_add = generic_device_add,
|
---|
[96646a6] | 50 | .dev_remove = generic_device_remove,
|
---|
| 51 | .dev_gone = generic_device_gone,
|
---|
[6105fc0] | 52 | };
|
---|
| 53 | static driver_t generic_driver = {
|
---|
| 54 | .driver_ops = &generic_driver_ops
|
---|
| 55 | };
|
---|
| 56 |
|
---|
[49bd7ae2] | 57 | static const usb_driver_t *driver = NULL;
|
---|
[6105fc0] | 58 |
|
---|
[69334af] | 59 |
|
---|
| 60 | /** Main routine of USB device driver.
|
---|
| 61 | *
|
---|
| 62 | * Under normal conditions, this function never returns.
|
---|
| 63 | *
|
---|
| 64 | * @param drv USB device driver structure.
|
---|
| 65 | * @return Task exit status.
|
---|
| 66 | */
|
---|
[882580a] | 67 | int usb_driver_main(const usb_driver_t *drv)
|
---|
[6105fc0] | 68 | {
|
---|
[69334af] | 69 | assert(drv != NULL);
|
---|
| 70 |
|
---|
[6105fc0] | 71 | /* Prepare the generic driver. */
|
---|
| 72 | generic_driver.name = drv->name;
|
---|
| 73 |
|
---|
| 74 | driver = drv;
|
---|
| 75 |
|
---|
| 76 | return ddf_driver_main(&generic_driver);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[69334af] | 79 | /** Count number of pipes the driver expects.
|
---|
| 80 | *
|
---|
| 81 | * @param drv USB driver.
|
---|
| 82 | * @return Number of pipes (excluding default control pipe).
|
---|
| 83 | */
|
---|
[ab27e01] | 84 | static inline size_t count_other_pipes(
|
---|
| 85 | const usb_endpoint_description_t **endpoints)
|
---|
[6105fc0] | 86 | {
|
---|
[ab27e01] | 87 | size_t count;
|
---|
| 88 | for (count = 0; endpoints && endpoints[count] != NULL; ++count);
|
---|
[6105fc0] | 89 | return count;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[69334af] | 92 | /** Initialize endpoint pipes, excluding default control one.
|
---|
| 93 | *
|
---|
| 94 | * @param drv The device driver.
|
---|
| 95 | * @param dev Device to be initialized.
|
---|
| 96 | * @return Error code.
|
---|
| 97 | */
|
---|
[b803845] | 98 | static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
|
---|
[c1b1944] | 99 | usb_device_t *dev, int alternate_setting)
|
---|
[6105fc0] | 100 | {
|
---|
[ab27e01] | 101 | assert(dev);
|
---|
| 102 |
|
---|
[6ee6e6f] | 103 | if (endpoints == NULL) {
|
---|
| 104 | dev->pipes = NULL;
|
---|
| 105 | dev->pipes_count = 0;
|
---|
| 106 | return EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[c1b1944] | 109 | usb_endpoint_mapping_t *pipes;
|
---|
| 110 | size_t pipes_count;
|
---|
[6105fc0] | 111 |
|
---|
[c1b1944] | 112 | int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
|
---|
[e484f3b] | 113 | dev->descriptors.configuration, dev->descriptors.configuration_size,
|
---|
[7c95d6f5] | 114 | dev->interface_no, alternate_setting, &pipes, &pipes_count);
|
---|
[6105fc0] | 115 |
|
---|
[5fd22d8] | 116 | if (rc != EOK) {
|
---|
[c1b1944] | 117 | return rc;
|
---|
[5fd22d8] | 118 | }
|
---|
| 119 |
|
---|
[c1b1944] | 120 | dev->pipes = pipes;
|
---|
| 121 | dev->pipes_count = pipes_count;
|
---|
[0b4e7ca] | 122 |
|
---|
[6105fc0] | 123 | return EOK;
|
---|
| 124 | }
|
---|
[51f033ce] | 125 | /*----------------------------------------------------------------------------*/
|
---|
| 126 | /** Callback when a new device is supposed to be controlled by this driver.
|
---|
[69334af] | 127 | *
|
---|
[51f033ce] | 128 | * This callback is a wrapper for USB specific version of @c device_add.
|
---|
[69334af] | 129 | *
|
---|
| 130 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 131 | * @return Error code.
|
---|
| 132 | */
|
---|
[1a4ea01d] | 133 | int generic_device_add(ddf_dev_t *gen_dev)
|
---|
[6105fc0] | 134 | {
|
---|
| 135 | assert(driver);
|
---|
| 136 | assert(driver->ops);
|
---|
[1a4ea01d] | 137 | assert(driver->ops->device_add);
|
---|
[6105fc0] | 138 |
|
---|
[7d9cd62] | 139 | usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
|
---|
| 140 | if (dev == NULL) {
|
---|
| 141 | usb_log_error("USB device `%s' structure allocation failed.\n",
|
---|
| 142 | gen_dev->name);
|
---|
| 143 | return ENOMEM;
|
---|
| 144 | }
|
---|
[6ee6e6f] | 145 | const char *err_msg = NULL;
|
---|
[7d9cd62] | 146 | int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
|
---|
[69334af] | 147 | if (rc != EOK) {
|
---|
[7d9cd62] | 148 | usb_log_error("USB device `%s' init failed (%s): %s.\n",
|
---|
[6ee6e6f] | 149 | gen_dev->name, err_msg, str_error(rc));
|
---|
[69334af] | 150 | return rc;
|
---|
[6105fc0] | 151 | }
|
---|
| 152 |
|
---|
[065064e6] | 153 | rc = driver->ops->device_add(dev);
|
---|
| 154 | if (rc != EOK)
|
---|
[9c5fd7a] | 155 | usb_device_deinit(dev);
|
---|
[065064e6] | 156 | return rc;
|
---|
[6105fc0] | 157 | }
|
---|
[96646a6] | 158 | /*----------------------------------------------------------------------------*/
|
---|
[51f033ce] | 159 | /** Callback when a device is supposed to be removed from the system.
|
---|
| 160 | *
|
---|
| 161 | * This callback is a wrapper for USB specific version of @c device_remove.
|
---|
| 162 | *
|
---|
| 163 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 164 | * @return Error code.
|
---|
| 165 | */
|
---|
[96646a6] | 166 | int generic_device_remove(ddf_dev_t *gen_dev)
|
---|
| 167 | {
|
---|
| 168 | assert(driver);
|
---|
| 169 | assert(driver->ops);
|
---|
| 170 | if (driver->ops->device_rem == NULL)
|
---|
| 171 | return ENOTSUP;
|
---|
[844f4ef] | 172 | /* Just tell the driver to stop whatever it is doing, keep structures */
|
---|
[c0fdc0e] | 173 | const int ret = driver->ops->device_rem(gen_dev->driver_data);
|
---|
| 174 | if (ret != EOK)
|
---|
| 175 | return ret;
|
---|
| 176 | return ENOTSUP;
|
---|
[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
|
---|
[ab27e01] | 332 | * (set to NULL if you wish to ignore the count).
|
---|
[c1b1944] | 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) {
|
---|
[ab27e01] | 353 | if (pipes_count_ptr)
|
---|
| 354 | *pipes_count_ptr = pipe_count;
|
---|
[c1b1944] | 355 | *pipes_ptr = NULL;
|
---|
| 356 | return EOK;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | usb_endpoint_mapping_t *pipes
|
---|
[ab27e01] | 360 | = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
[c1b1944] | 361 | if (pipes == NULL) {
|
---|
| 362 | return ENOMEM;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /* Now allocate and fully initialize. */
|
---|
| 366 | for (i = 0; i < pipe_count; i++) {
|
---|
| 367 | pipes[i].description = endpoints[i];
|
---|
| 368 | pipes[i].interface_no = interface_no;
|
---|
| 369 | pipes[i].interface_setting = interface_setting;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /* Find the mapping from configuration descriptor. */
|
---|
| 373 | rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
| 374 | config_descr, config_descr_size, wire);
|
---|
| 375 | if (rc != EOK) {
|
---|
| 376 | goto rollback_free_only;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | for (i = 0; i < pipe_count; i++) {
|
---|
| 380 | if (pipes[i].present) {
|
---|
[b77931d] | 381 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
[bd575647] | 382 | pipes[i].descriptor->poll_interval);
|
---|
[c1b1944] | 383 | if (rc != EOK) {
|
---|
| 384 | goto rollback_unregister_endpoints;
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | *pipes_ptr = pipes;
|
---|
| 390 | if (pipes_count_ptr != NULL) {
|
---|
| 391 | *pipes_count_ptr = pipe_count;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | return EOK;
|
---|
| 395 |
|
---|
| 396 | /*
|
---|
| 397 | * Jump here if something went wrong after endpoints have
|
---|
| 398 | * been registered.
|
---|
| 399 | * This is also the target when the registration of
|
---|
| 400 | * endpoints fails.
|
---|
| 401 | */
|
---|
| 402 | rollback_unregister_endpoints:
|
---|
| 403 | for (i = 0; i < pipe_count; i++) {
|
---|
| 404 | if (pipes[i].present) {
|
---|
[bd575647] | 405 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /*
|
---|
| 410 | * Jump here if something went wrong before some actual communication
|
---|
| 411 | * with HC. Then the only thing that needs to be done is to free
|
---|
| 412 | * allocated memory.
|
---|
| 413 | */
|
---|
| 414 | rollback_free_only:
|
---|
| 415 | free(pipes);
|
---|
[0b4e7ca] | 416 |
|
---|
| 417 | return rc;
|
---|
| 418 | }
|
---|
[159b91f4] | 419 |
|
---|
[c1b1944] | 420 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 421 | *
|
---|
| 422 | * @param[in] dev Generic DDF device backing the USB one.
|
---|
| 423 | * @param[in] pipes Endpoint mapping to be destroyed.
|
---|
| 424 | * @param[in] pipes_count Number of endpoints.
|
---|
| 425 | */
|
---|
[8e5ce07] | 426 | int usb_device_destroy_pipes(const ddf_dev_t *dev,
|
---|
[c1b1944] | 427 | usb_endpoint_mapping_t *pipes, size_t pipes_count)
|
---|
| 428 | {
|
---|
| 429 | assert(dev != NULL);
|
---|
| 430 |
|
---|
| 431 | if (pipes_count == 0) {
|
---|
[5917859c] | 432 | assert(pipes == NULL);
|
---|
[c1b1944] | 433 | return EOK;
|
---|
| 434 | }
|
---|
[5917859c] | 435 | assert(pipes != NULL);
|
---|
[c1b1944] | 436 |
|
---|
| 437 | int rc;
|
---|
| 438 |
|
---|
| 439 | /* Prepare connection to HC to allow endpoint unregistering. */
|
---|
| 440 | usb_hc_connection_t hc_conn;
|
---|
| 441 | rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
|
---|
| 442 | if (rc != EOK) {
|
---|
| 443 | return rc;
|
---|
| 444 | }
|
---|
[94bfc84] | 445 | /* Open connection to hc for pipe unregister. */
|
---|
[c1b1944] | 446 | rc = usb_hc_connection_open(&hc_conn);
|
---|
| 447 | if (rc != EOK) {
|
---|
| 448 | return rc;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | /* Destroy the pipes. */
|
---|
| 452 | size_t i;
|
---|
| 453 | for (i = 0; i < pipes_count; i++) {
|
---|
[1526c174] | 454 | usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
|
---|
| 455 | i, pipes[i].present ? "" : "not ");
|
---|
| 456 | if (pipes[i].present)
|
---|
[bd575647] | 457 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 458 | }
|
---|
| 459 |
|
---|
[94bfc84] | 460 | usb_hc_connection_close(&hc_conn);
|
---|
[c1b1944] | 461 | free(pipes);
|
---|
| 462 |
|
---|
| 463 | return EOK;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[7d9cd62] | 466 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 467 | *
|
---|
[7d9cd62] | 468 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 469 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 470 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 471 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 472 | * (in case error occurs).
|
---|
| 473 | * @return Error code.
|
---|
| 474 | */
|
---|
[7d9cd62] | 475 | int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
| 476 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 477 | {
|
---|
[7d9cd62] | 478 | assert(usb_dev != NULL);
|
---|
[6ee6e6f] | 479 | assert(ddf_dev != NULL);
|
---|
| 480 |
|
---|
[7fc260ff] | 481 | *errstr_ptr = NULL;
|
---|
| 482 |
|
---|
[7d9cd62] | 483 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 484 | usb_dev->driver_data = NULL;
|
---|
| 485 | usb_dev->descriptors.configuration = NULL;
|
---|
| 486 | usb_dev->pipes_count = 0;
|
---|
| 487 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 488 |
|
---|
[c24c157d] | 489 | /* Get assigned params */
|
---|
| 490 | devman_handle_t hc_handle;
|
---|
| 491 | usb_address_t address;
|
---|
| 492 |
|
---|
| 493 | int rc = usb_get_info_by_handle(ddf_dev->handle,
|
---|
| 494 | &hc_handle, &address, &usb_dev->interface_no);
|
---|
| 495 | if (rc != EOK) {
|
---|
| 496 | *errstr_ptr = "device parameters retrieval";
|
---|
| 497 | return rc;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[c0fdc0e] | 500 | /* Initialize hc connection. */
|
---|
[c24c157d] | 501 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
---|
[c0fdc0e] | 502 |
|
---|
[3f2af64] | 503 | /* Initialize backing wire and control pipe. */
|
---|
[c24c157d] | 504 | rc = usb_device_connection_initialize(
|
---|
[bd575647] | 505 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
---|
[3f2af64] | 506 | if (rc != EOK) {
|
---|
[7fc260ff] | 507 | *errstr_ptr = "device connection initialization";
|
---|
| 508 | return rc;
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | /* This pipe was registered by the hub driver,
|
---|
| 512 | * during device initialization. */
|
---|
[c0fdc0e] | 513 | rc = usb_pipe_initialize_default_control(
|
---|
| 514 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
---|
[7fc260ff] | 515 | if (rc != EOK) {
|
---|
| 516 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 517 | return rc;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[6e3c005] | 520 | /* Open hc connection for pipe registration. */
|
---|
[c0fdc0e] | 521 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
---|
| 522 | if (rc != EOK) {
|
---|
| 523 | *errstr_ptr = "hc connection open";
|
---|
| 524 | return rc;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
[3f2af64] | 527 | /* Retrieve standard descriptors. */
|
---|
[c0fdc0e] | 528 | rc = usb_device_retrieve_descriptors(
|
---|
| 529 | &usb_dev->ctrl_pipe, &usb_dev->descriptors);
|
---|
[6ee6e6f] | 530 | if (rc != EOK) {
|
---|
[3f2af64] | 531 | *errstr_ptr = "descriptor retrieval";
|
---|
[c0fdc0e] | 532 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 533 | return rc;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[7fc260ff] | 536 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 537 | * We might either control one interface or an entire device,
|
---|
| 538 | * it makes no sense to speak about alternate interfaces when
|
---|
| 539 | * controlling a device. */
|
---|
[ab27e01] | 540 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[904dcc6] | 541 | usb_dev->descriptors.configuration,
|
---|
| 542 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
---|
[ab27e01] | 543 | const int alternate_iface =
|
---|
| 544 | (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
|
---|
[6ee6e6f] | 545 |
|
---|
[7fc260ff] | 546 | /* TODO Add comment here. */
|
---|
[ab27e01] | 547 | rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface);
|
---|
[6ee6e6f] | 548 | if (rc != EOK) {
|
---|
[c0fdc0e] | 549 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[7d9cd62] | 550 | /* Full configuration descriptor is allocated. */
|
---|
[7fc260ff] | 551 | usb_device_release_descriptors(&usb_dev->descriptors);
|
---|
[7d9cd62] | 552 | /* Alternate interfaces may be allocated */
|
---|
[904dcc6] | 553 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
[6ee6e6f] | 554 | *errstr_ptr = "pipes initialization";
|
---|
| 555 | return rc;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[c0fdc0e] | 558 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 559 | return EOK;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
[7d9cd62] | 562 | /** Clean instance of a USB device.
|
---|
[70452dd4] | 563 | *
|
---|
[9c5fd7a] | 564 | * @param dev Device to be de-initialized.
|
---|
| 565 | *
|
---|
| 566 | * Does not free/destroy supplied pointer.
|
---|
[70452dd4] | 567 | */
|
---|
[9c5fd7a] | 568 | void usb_device_deinit(usb_device_t *dev)
|
---|
[70452dd4] | 569 | {
|
---|
[7d9cd62] | 570 | if (dev) {
|
---|
| 571 | /* Ignore errors and hope for the best. */
|
---|
| 572 | destroy_current_pipes(dev);
|
---|
[70452dd4] | 573 |
|
---|
[904dcc6] | 574 | usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
|
---|
[7fc260ff] | 575 | usb_device_release_descriptors(&dev->descriptors);
|
---|
[7d9cd62] | 576 | free(dev->driver_data);
|
---|
[70452dd4] | 577 | }
|
---|
[065064e6] | 578 | }
|
---|
| 579 |
|
---|
[6e3c005] | 580 | /** Allocate driver specific data.
|
---|
| 581 | * @param usb_dev usb_device structure.
|
---|
| 582 | * @param size requested data size.
|
---|
| 583 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 584 | */
|
---|
[065064e6] | 585 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 586 | {
|
---|
| 587 | assert(usb_dev);
|
---|
| 588 | assert(usb_dev->driver_data == NULL);
|
---|
| 589 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 590 |
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[6105fc0] | 593 | /**
|
---|
| 594 | * @}
|
---|
| 595 | */
|
---|