[6105fc0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[4291215] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
[6105fc0] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 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>
|
---|
[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 | /** Main routine of USB device driver.
|
---|
| 59 | *
|
---|
| 60 | * Under normal conditions, this function never returns.
|
---|
| 61 | *
|
---|
| 62 | * @param drv USB device driver structure.
|
---|
| 63 | * @return Task exit status.
|
---|
| 64 | */
|
---|
[882580a] | 65 | int usb_driver_main(const usb_driver_t *drv)
|
---|
[6105fc0] | 66 | {
|
---|
[69334af] | 67 | assert(drv != NULL);
|
---|
| 68 |
|
---|
[6105fc0] | 69 | /* Prepare the generic driver. */
|
---|
| 70 | generic_driver.name = drv->name;
|
---|
| 71 |
|
---|
| 72 | driver = drv;
|
---|
| 73 |
|
---|
| 74 | return ddf_driver_main(&generic_driver);
|
---|
| 75 | }
|
---|
[a76b01b4] | 76 |
|
---|
[69334af] | 77 | /** Count number of pipes the driver expects.
|
---|
| 78 | *
|
---|
| 79 | * @param drv USB driver.
|
---|
| 80 | * @return Number of pipes (excluding default control pipe).
|
---|
| 81 | */
|
---|
[ab27e01] | 82 | static inline size_t count_other_pipes(
|
---|
| 83 | const usb_endpoint_description_t **endpoints)
|
---|
[6105fc0] | 84 | {
|
---|
[ab27e01] | 85 | size_t count;
|
---|
[64e3dad] | 86 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
---|
[6105fc0] | 87 | return count;
|
---|
| 88 | }
|
---|
[a76b01b4] | 89 |
|
---|
[51f033ce] | 90 | /** Callback when a new device is supposed to be controlled by this driver.
|
---|
[69334af] | 91 | *
|
---|
[51f033ce] | 92 | * This callback is a wrapper for USB specific version of @c device_add.
|
---|
[69334af] | 93 | *
|
---|
| 94 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 95 | * @return Error code.
|
---|
| 96 | */
|
---|
[1a4ea01d] | 97 | int generic_device_add(ddf_dev_t *gen_dev)
|
---|
[6105fc0] | 98 | {
|
---|
| 99 | assert(driver);
|
---|
| 100 | assert(driver->ops);
|
---|
[1a4ea01d] | 101 | assert(driver->ops->device_add);
|
---|
[6105fc0] | 102 |
|
---|
[54cbda2] | 103 | /* Get place for driver data. */
|
---|
[7d9cd62] | 104 | usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
|
---|
| 105 | if (dev == NULL) {
|
---|
| 106 | usb_log_error("USB device `%s' structure allocation failed.\n",
|
---|
[56fd7cf] | 107 | ddf_dev_get_name(gen_dev));
|
---|
[7d9cd62] | 108 | return ENOMEM;
|
---|
| 109 | }
|
---|
[54cbda2] | 110 |
|
---|
| 111 | /* Initialize generic USB driver data. */
|
---|
[6ee6e6f] | 112 | const char *err_msg = NULL;
|
---|
[7d9cd62] | 113 | int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
|
---|
[69334af] | 114 | if (rc != EOK) {
|
---|
[7d9cd62] | 115 | usb_log_error("USB device `%s' init failed (%s): %s.\n",
|
---|
[56fd7cf] | 116 | ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
|
---|
[69334af] | 117 | return rc;
|
---|
[6105fc0] | 118 | }
|
---|
| 119 |
|
---|
[54cbda2] | 120 | /* Start USB driver specific initialization. */
|
---|
[065064e6] | 121 | rc = driver->ops->device_add(dev);
|
---|
| 122 | if (rc != EOK)
|
---|
[9c5fd7a] | 123 | usb_device_deinit(dev);
|
---|
[065064e6] | 124 | return rc;
|
---|
[6105fc0] | 125 | }
|
---|
[a76b01b4] | 126 |
|
---|
[51f033ce] | 127 | /** Callback when a device is supposed to be removed from the system.
|
---|
| 128 | *
|
---|
| 129 | * This callback is a wrapper for USB specific version of @c device_remove.
|
---|
| 130 | *
|
---|
| 131 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 132 | * @return Error code.
|
---|
| 133 | */
|
---|
[96646a6] | 134 | int generic_device_remove(ddf_dev_t *gen_dev)
|
---|
| 135 | {
|
---|
| 136 | assert(driver);
|
---|
| 137 | assert(driver->ops);
|
---|
| 138 | if (driver->ops->device_rem == NULL)
|
---|
| 139 | return ENOTSUP;
|
---|
[54cbda2] | 140 | /* Just tell the driver to stop whatever it is doing */
|
---|
[56fd7cf] | 141 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
[54cbda2] | 142 | const int ret = driver->ops->device_rem(usb_dev);
|
---|
[c0fdc0e] | 143 | if (ret != EOK)
|
---|
| 144 | return ret;
|
---|
[54cbda2] | 145 | usb_device_deinit(usb_dev);
|
---|
| 146 | return EOK;
|
---|
[96646a6] | 147 | }
|
---|
[a76b01b4] | 148 |
|
---|
[51f033ce] | 149 | /** Callback when a device was removed from the system.
|
---|
| 150 | *
|
---|
| 151 | * This callback is a wrapper for USB specific version of @c device_gone.
|
---|
| 152 | *
|
---|
| 153 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 154 | * @return Error code.
|
---|
| 155 | */
|
---|
[96646a6] | 156 | int generic_device_gone(ddf_dev_t *gen_dev)
|
---|
| 157 | {
|
---|
| 158 | assert(driver);
|
---|
| 159 | assert(driver->ops);
|
---|
[844f4ef] | 160 | if (driver->ops->device_gone == NULL)
|
---|
| 161 | return ENOTSUP;
|
---|
[56fd7cf] | 162 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
[065064e6] | 163 | const int ret = driver->ops->device_gone(usb_dev);
|
---|
[844f4ef] | 164 | if (ret == EOK)
|
---|
[9c5fd7a] | 165 | usb_device_deinit(usb_dev);
|
---|
[96646a6] | 166 |
|
---|
[844f4ef] | 167 | return ret;
|
---|
[96646a6] | 168 | }
|
---|
[a76b01b4] | 169 |
|
---|
[0b4e7ca] | 170 | /** Destroy existing pipes of a USB device.
|
---|
| 171 | *
|
---|
| 172 | * @param dev Device where to destroy the pipes.
|
---|
| 173 | */
|
---|
[6883abfa] | 174 | static void destroy_current_pipes(usb_device_t *dev)
|
---|
[0b4e7ca] | 175 | {
|
---|
[6883abfa] | 176 | usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
|
---|
[0b4e7ca] | 177 | dev->pipes = NULL;
|
---|
| 178 | dev->pipes_count = 0;
|
---|
| 179 | }
|
---|
[a76b01b4] | 180 |
|
---|
[0b4e7ca] | 181 | /** Change interface setting of a device.
|
---|
| 182 | * This function selects new alternate setting of an interface by issuing
|
---|
| 183 | * proper USB command to the device and also creates new USB pipes
|
---|
| 184 | * under @c dev->pipes.
|
---|
| 185 | *
|
---|
| 186 | * @warning This function is intended for drivers working at interface level.
|
---|
| 187 | * For drivers controlling the whole device, you need to change interface
|
---|
| 188 | * manually using usb_request_set_interface() and creating new pipes
|
---|
| 189 | * with usb_pipe_initialize_from_configuration().
|
---|
| 190 | *
|
---|
[3f2af64] | 191 | * @warning This is a wrapper function that does several operations that
|
---|
| 192 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
| 193 | * during the SET_INTERFACE request would result in having a device with
|
---|
| 194 | * no pipes at all (except the default control one). That is because the old
|
---|
| 195 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
| 196 | * be created.
|
---|
| 197 | *
|
---|
[0b4e7ca] | 198 | * @param dev USB device.
|
---|
| 199 | * @param alternate_setting Alternate setting to choose.
|
---|
| 200 | * @param endpoints New endpoint descriptions.
|
---|
| 201 | * @return Error code.
|
---|
| 202 | */
|
---|
| 203 | int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
|
---|
[b803845] | 204 | const usb_endpoint_description_t **endpoints)
|
---|
[0b4e7ca] | 205 | {
|
---|
| 206 | if (dev->interface_no < 0) {
|
---|
| 207 | return EINVAL;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /* Destroy existing pipes. */
|
---|
[6883abfa] | 211 | destroy_current_pipes(dev);
|
---|
[0b4e7ca] | 212 |
|
---|
| 213 | /* Change the interface itself. */
|
---|
[441633f] | 214 | int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
|
---|
[0b4e7ca] | 215 | alternate_setting);
|
---|
| 216 | if (rc != EOK) {
|
---|
| 217 | return rc;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /* Create new pipes. */
|
---|
[66ee26a] | 221 | rc = usb_device_create_pipes(&dev->wire, endpoints,
|
---|
| 222 | dev->descriptors.configuration, dev->descriptors.configuration_size,
|
---|
| 223 | dev->interface_no, (int)alternate_setting,
|
---|
| 224 | &dev->pipes, &dev->pipes_count);
|
---|
[c1b1944] | 225 |
|
---|
| 226 | return rc;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /** Retrieve basic descriptors from the device.
|
---|
| 230 | *
|
---|
[4fa0a384] | 231 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
[c1b1944] | 232 | * @param[out] descriptors Where to store the descriptors.
|
---|
| 233 | * @return Error code.
|
---|
| 234 | */
|
---|
[945d66c] | 235 | static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
[c1b1944] | 236 | {
|
---|
[945d66c] | 237 | assert(usb_dev);
|
---|
| 238 | assert(usb_dev->descriptors.configuration == NULL);
|
---|
[c1b1944] | 239 |
|
---|
[4fa0a384] | 240 | /* It is worth to start a long transfer. */
|
---|
[945d66c] | 241 | usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
|
---|
[4fa0a384] | 242 |
|
---|
[c1b1944] | 243 | /* Get the device descriptor. */
|
---|
[945d66c] | 244 | int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
| 245 | &usb_dev->descriptors.device);
|
---|
[c1b1944] | 246 | if (rc != EOK) {
|
---|
[4fa0a384] | 247 | goto leave;
|
---|
[c1b1944] | 248 | }
|
---|
| 249 |
|
---|
| 250 | /* Get the full configuration descriptor. */
|
---|
| 251 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
[945d66c] | 252 | &usb_dev->ctrl_pipe, 0,
|
---|
| 253 | (void **) &usb_dev->descriptors.configuration,
|
---|
| 254 | &usb_dev->descriptors.configuration_size);
|
---|
[c1b1944] | 255 |
|
---|
[4fa0a384] | 256 | leave:
|
---|
[945d66c] | 257 | usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
|
---|
[4fa0a384] | 258 |
|
---|
| 259 | return rc;
|
---|
[c1b1944] | 260 | }
|
---|
| 261 |
|
---|
[7fc260ff] | 262 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
| 263 | *
|
---|
| 264 | * @param[in] descriptors Where to store the descriptors.
|
---|
| 265 | */
|
---|
[945d66c] | 266 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
[7fc260ff] | 267 | {
|
---|
[945d66c] | 268 | assert(usb_dev);
|
---|
| 269 | free(usb_dev->descriptors.configuration);
|
---|
| 270 | usb_dev->descriptors.configuration = NULL;
|
---|
| 271 | usb_dev->descriptors.configuration_size = 0;
|
---|
[7fc260ff] | 272 | }
|
---|
| 273 |
|
---|
[c1b1944] | 274 | /** Create pipes for a device.
|
---|
| 275 | *
|
---|
| 276 | * This is more or less a wrapper that does following actions:
|
---|
| 277 | * - allocate and initialize pipes
|
---|
| 278 | * - map endpoints to the pipes based on the descriptions
|
---|
| 279 | * - registers endpoints with the host controller
|
---|
| 280 | *
|
---|
| 281 | * @param[in] wire Initialized backing connection to the host controller.
|
---|
| 282 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
| 283 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
| 284 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
| 285 | * @param[in] interface_no Interface to map from.
|
---|
| 286 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
| 287 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
| 288 | * (not NULL terminated).
|
---|
| 289 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
[ab27e01] | 290 | * (set to NULL if you wish to ignore the count).
|
---|
[c1b1944] | 291 | * @return Error code.
|
---|
| 292 | */
|
---|
[0cfb05e] | 293 | int usb_device_create_pipes(usb_device_connection_t *wire,
|
---|
[b803845] | 294 | const usb_endpoint_description_t **endpoints,
|
---|
[7c95d6f5] | 295 | const uint8_t *config_descr, size_t config_descr_size,
|
---|
[c1b1944] | 296 | int interface_no, int interface_setting,
|
---|
| 297 | usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
|
---|
| 298 | {
|
---|
| 299 | assert(wire != NULL);
|
---|
| 300 | assert(config_descr != NULL);
|
---|
| 301 | assert(config_descr_size > 0);
|
---|
| 302 | assert(pipes_ptr != NULL);
|
---|
| 303 |
|
---|
| 304 | size_t i;
|
---|
| 305 | int rc;
|
---|
| 306 |
|
---|
[5917859c] | 307 | const size_t pipe_count = count_other_pipes(endpoints);
|
---|
[c1b1944] | 308 | if (pipe_count == 0) {
|
---|
[ab27e01] | 309 | if (pipes_count_ptr)
|
---|
| 310 | *pipes_count_ptr = pipe_count;
|
---|
[c1b1944] | 311 | *pipes_ptr = NULL;
|
---|
| 312 | return EOK;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | usb_endpoint_mapping_t *pipes
|
---|
[ab27e01] | 316 | = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
[c1b1944] | 317 | if (pipes == NULL) {
|
---|
| 318 | return ENOMEM;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[441633f] | 321 | /* Now initialize. */
|
---|
[c1b1944] | 322 | for (i = 0; i < pipe_count; i++) {
|
---|
| 323 | pipes[i].description = endpoints[i];
|
---|
| 324 | pipes[i].interface_no = interface_no;
|
---|
| 325 | pipes[i].interface_setting = interface_setting;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /* Find the mapping from configuration descriptor. */
|
---|
| 329 | rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
| 330 | config_descr, config_descr_size, wire);
|
---|
| 331 | if (rc != EOK) {
|
---|
[441633f] | 332 | free(pipes);
|
---|
| 333 | return rc;
|
---|
[c1b1944] | 334 | }
|
---|
| 335 |
|
---|
[441633f] | 336 | /* Register created pipes. */
|
---|
[c1b1944] | 337 | for (i = 0; i < pipe_count; i++) {
|
---|
| 338 | if (pipes[i].present) {
|
---|
[b77931d] | 339 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
[bd575647] | 340 | pipes[i].descriptor->poll_interval);
|
---|
[c1b1944] | 341 | if (rc != EOK) {
|
---|
| 342 | goto rollback_unregister_endpoints;
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | *pipes_ptr = pipes;
|
---|
| 348 | if (pipes_count_ptr != NULL) {
|
---|
| 349 | *pipes_count_ptr = pipe_count;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | return EOK;
|
---|
| 353 |
|
---|
| 354 | /*
|
---|
| 355 | * Jump here if something went wrong after endpoints have
|
---|
| 356 | * been registered.
|
---|
| 357 | * This is also the target when the registration of
|
---|
| 358 | * endpoints fails.
|
---|
| 359 | */
|
---|
| 360 | rollback_unregister_endpoints:
|
---|
| 361 | for (i = 0; i < pipe_count; i++) {
|
---|
| 362 | if (pipes[i].present) {
|
---|
[bd575647] | 363 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | free(pipes);
|
---|
[0b4e7ca] | 368 | return rc;
|
---|
| 369 | }
|
---|
[159b91f4] | 370 |
|
---|
[c1b1944] | 371 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 372 | *
|
---|
| 373 | * @param[in] pipes Endpoint mapping to be destroyed.
|
---|
| 374 | * @param[in] pipes_count Number of endpoints.
|
---|
| 375 | */
|
---|
[6883abfa] | 376 | void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
|
---|
[c1b1944] | 377 | {
|
---|
| 378 | /* Destroy the pipes. */
|
---|
[8a01a0b] | 379 | for (size_t i = 0; i < pipes_count; ++i) {
|
---|
[6883abfa] | 380 | assert(pipes);
|
---|
[8a01a0b] | 381 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
---|
[1526c174] | 382 | i, pipes[i].present ? "" : "not ");
|
---|
| 383 | if (pipes[i].present)
|
---|
[bd575647] | 384 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 385 | }
|
---|
| 386 | free(pipes);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[0f4bff8] | 389 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
| 390 | {
|
---|
| 391 | assert(usb_dev);
|
---|
| 392 | return &usb_dev->ctrl_pipe;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[945d66c] | 395 | const usb_standard_device_descriptor_t *
|
---|
| 396 | usb_device_get_device_descriptor(usb_device_t *usb_dev)
|
---|
| 397 | {
|
---|
| 398 | assert(usb_dev);
|
---|
| 399 | return &usb_dev->descriptors.device;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | const void * usb_device_get_configuration_descriptor(
|
---|
| 403 | usb_device_t *usb_dev, size_t *size)
|
---|
| 404 | {
|
---|
| 405 | assert(usb_dev);
|
---|
| 406 | if (size)
|
---|
| 407 | *size = usb_dev->descriptors.configuration_size;
|
---|
| 408 | return usb_dev->descriptors.configuration;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[7d9cd62] | 411 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 412 | *
|
---|
[7d9cd62] | 413 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 414 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 415 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 416 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 417 | * (in case error occurs).
|
---|
| 418 | * @return Error code.
|
---|
| 419 | */
|
---|
[7d9cd62] | 420 | int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
| 421 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 422 | {
|
---|
[7d9cd62] | 423 | assert(usb_dev != NULL);
|
---|
[6ee6e6f] | 424 | assert(ddf_dev != NULL);
|
---|
| 425 |
|
---|
[7fc260ff] | 426 | *errstr_ptr = NULL;
|
---|
| 427 |
|
---|
[7d9cd62] | 428 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 429 | usb_dev->driver_data = NULL;
|
---|
| 430 | usb_dev->descriptors.configuration = NULL;
|
---|
| 431 | usb_dev->pipes_count = 0;
|
---|
| 432 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 433 |
|
---|
[94fbf78] | 434 | usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
|
---|
[c8c758d] | 435 | if (!usb_dev->bus_session) {
|
---|
| 436 | *errstr_ptr = "device bus session create";
|
---|
| 437 | return ENOMEM;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[c24c157d] | 440 | /* Get assigned params */
|
---|
| 441 | devman_handle_t hc_handle;
|
---|
| 442 | usb_address_t address;
|
---|
| 443 |
|
---|
[56fd7cf] | 444 | int rc = usb_get_info_by_handle(ddf_dev_get_handle(ddf_dev),
|
---|
[c24c157d] | 445 | &hc_handle, &address, &usb_dev->interface_no);
|
---|
| 446 | if (rc != EOK) {
|
---|
| 447 | *errstr_ptr = "device parameters retrieval";
|
---|
| 448 | return rc;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[c0fdc0e] | 451 | /* Initialize hc connection. */
|
---|
[c24c157d] | 452 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
---|
[c0fdc0e] | 453 |
|
---|
[3f2af64] | 454 | /* Initialize backing wire and control pipe. */
|
---|
[c24c157d] | 455 | rc = usb_device_connection_initialize(
|
---|
[bd575647] | 456 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
---|
[3f2af64] | 457 | if (rc != EOK) {
|
---|
[7fc260ff] | 458 | *errstr_ptr = "device connection initialization";
|
---|
| 459 | return rc;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | /* This pipe was registered by the hub driver,
|
---|
| 463 | * during device initialization. */
|
---|
[c0fdc0e] | 464 | rc = usb_pipe_initialize_default_control(
|
---|
| 465 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
---|
[7fc260ff] | 466 | if (rc != EOK) {
|
---|
| 467 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 468 | return rc;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
[6e3c005] | 471 | /* Open hc connection for pipe registration. */
|
---|
[c0fdc0e] | 472 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
---|
| 473 | if (rc != EOK) {
|
---|
| 474 | *errstr_ptr = "hc connection open";
|
---|
| 475 | return rc;
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[3f2af64] | 478 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 479 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 480 | if (rc != EOK) {
|
---|
[3f2af64] | 481 | *errstr_ptr = "descriptor retrieval";
|
---|
[c0fdc0e] | 482 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 483 | return rc;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[7fc260ff] | 486 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 487 | * We might either control one interface or an entire device,
|
---|
| 488 | * it makes no sense to speak about alternate interfaces when
|
---|
| 489 | * controlling a device. */
|
---|
[ab27e01] | 490 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[904dcc6] | 491 | usb_dev->descriptors.configuration,
|
---|
| 492 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
---|
[ab27e01] | 493 | const int alternate_iface =
|
---|
| 494 | (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
|
---|
[6ee6e6f] | 495 |
|
---|
[66ee26a] | 496 | /* Create and register other pipes than default control (EP 0) */
|
---|
| 497 | rc = usb_device_create_pipes(&usb_dev->wire, endpoints,
|
---|
| 498 | usb_dev->descriptors.configuration,
|
---|
| 499 | usb_dev->descriptors.configuration_size,
|
---|
| 500 | usb_dev->interface_no, (int)alternate_iface,
|
---|
| 501 | &usb_dev->pipes, &usb_dev->pipes_count);
|
---|
[6ee6e6f] | 502 | if (rc != EOK) {
|
---|
[c0fdc0e] | 503 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[7d9cd62] | 504 | /* Full configuration descriptor is allocated. */
|
---|
[945d66c] | 505 | usb_device_release_descriptors(usb_dev);
|
---|
[7d9cd62] | 506 | /* Alternate interfaces may be allocated */
|
---|
[904dcc6] | 507 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
[6ee6e6f] | 508 | *errstr_ptr = "pipes initialization";
|
---|
| 509 | return rc;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[c0fdc0e] | 512 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 513 | return EOK;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[7d9cd62] | 516 | /** Clean instance of a USB device.
|
---|
[70452dd4] | 517 | *
|
---|
[9c5fd7a] | 518 | * @param dev Device to be de-initialized.
|
---|
| 519 | *
|
---|
| 520 | * Does not free/destroy supplied pointer.
|
---|
[70452dd4] | 521 | */
|
---|
[9c5fd7a] | 522 | void usb_device_deinit(usb_device_t *dev)
|
---|
[70452dd4] | 523 | {
|
---|
[7d9cd62] | 524 | if (dev) {
|
---|
[c8c758d] | 525 | usb_dev_session_close(dev->bus_session);
|
---|
[8e3742f9] | 526 | /* Destroy existing pipes. */
|
---|
[7d9cd62] | 527 | destroy_current_pipes(dev);
|
---|
[8e3742f9] | 528 | /* Ignore errors and hope for the best. */
|
---|
| 529 | usb_hc_connection_deinitialize(&dev->hc_conn);
|
---|
[904dcc6] | 530 | usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
|
---|
[945d66c] | 531 | usb_device_release_descriptors(dev);
|
---|
[7d9cd62] | 532 | free(dev->driver_data);
|
---|
[8e3742f9] | 533 | dev->driver_data = NULL;
|
---|
[70452dd4] | 534 | }
|
---|
[065064e6] | 535 | }
|
---|
| 536 |
|
---|
[f6b2a76b] | 537 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 538 | {
|
---|
| 539 | assert(usb_dev);
|
---|
| 540 | assert(usb_dev->ddf_dev);
|
---|
| 541 | //TODO Handle case without ddf_dev
|
---|
| 542 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[0f4bff8] | 545 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
| 546 | {
|
---|
| 547 | assert(usb_dev);
|
---|
| 548 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 552 | {
|
---|
| 553 | async_exchange_end(exch);
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[6e3c005] | 556 | /** Allocate driver specific data.
|
---|
| 557 | * @param usb_dev usb_device structure.
|
---|
| 558 | * @param size requested data size.
|
---|
| 559 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 560 | */
|
---|
[065064e6] | 561 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 562 | {
|
---|
| 563 | assert(usb_dev);
|
---|
| 564 | assert(usb_dev->driver_data == NULL);
|
---|
| 565 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 566 |
|
---|
| 567 | }
|
---|
| 568 |
|
---|
[0f4bff8] | 569 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
| 570 | {
|
---|
| 571 | assert(usb_dev);
|
---|
| 572 | return usb_dev->driver_data;
|
---|
| 573 | }
|
---|
[6105fc0] | 574 | /**
|
---|
| 575 | * @}
|
---|
| 576 | */
|
---|