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