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