| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 | */
|
|---|
| 29 | /** @addtogroup libusbdev
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB device driver framework.
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/dev/driver.h>
|
|---|
| 36 | #include <usb/dev/request.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usb/dev.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 |
|
|---|
| 43 | static int generic_device_add(ddf_dev_t *);
|
|---|
| 44 | static int generic_device_remove(ddf_dev_t *);
|
|---|
| 45 | static int generic_device_gone(ddf_dev_t *);
|
|---|
| 46 |
|
|---|
| 47 | static driver_ops_t generic_driver_ops = {
|
|---|
| 48 | .dev_add = generic_device_add,
|
|---|
| 49 | .dev_remove = generic_device_remove,
|
|---|
| 50 | .dev_gone = generic_device_gone,
|
|---|
| 51 | };
|
|---|
| 52 | static driver_t generic_driver = {
|
|---|
| 53 | .driver_ops = &generic_driver_ops
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static const usb_driver_t *driver = NULL;
|
|---|
| 57 |
|
|---|
| 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 | */
|
|---|
| 65 | int usb_driver_main(const usb_driver_t *drv)
|
|---|
| 66 | {
|
|---|
| 67 | assert(drv != NULL);
|
|---|
| 68 |
|
|---|
| 69 | /* Prepare the generic driver. */
|
|---|
| 70 | generic_driver.name = drv->name;
|
|---|
| 71 |
|
|---|
| 72 | driver = drv;
|
|---|
| 73 |
|
|---|
| 74 | return ddf_driver_main(&generic_driver);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Count number of pipes the driver expects.
|
|---|
| 78 | *
|
|---|
| 79 | * @param drv USB driver.
|
|---|
| 80 | * @return Number of pipes (excluding default control pipe).
|
|---|
| 81 | */
|
|---|
| 82 | static inline size_t count_other_pipes(
|
|---|
| 83 | const usb_endpoint_description_t **endpoints)
|
|---|
| 84 | {
|
|---|
| 85 | size_t count;
|
|---|
| 86 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
|---|
| 87 | return count;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Callback when a new device is supposed to be controlled by this driver.
|
|---|
| 91 | *
|
|---|
| 92 | * This callback is a wrapper for USB specific version of @c device_add.
|
|---|
| 93 | *
|
|---|
| 94 | * @param gen_dev Device structure as prepared by DDF.
|
|---|
| 95 | * @return Error code.
|
|---|
| 96 | */
|
|---|
| 97 | int generic_device_add(ddf_dev_t *gen_dev)
|
|---|
| 98 | {
|
|---|
| 99 | assert(driver);
|
|---|
| 100 | assert(driver->ops);
|
|---|
| 101 | assert(driver->ops->device_add);
|
|---|
| 102 |
|
|---|
| 103 | /* Get place for driver data. */
|
|---|
| 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 | ddf_dev_get_name(gen_dev));
|
|---|
| 108 | return ENOMEM;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /* Initialize generic USB driver data. */
|
|---|
| 112 | const char *err_msg = NULL;
|
|---|
| 113 | int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
|
|---|
| 114 | if (rc != EOK) {
|
|---|
| 115 | usb_log_error("USB device `%s' init failed (%s): %s.\n",
|
|---|
| 116 | ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
|
|---|
| 117 | return rc;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /* Start USB driver specific initialization. */
|
|---|
| 121 | rc = driver->ops->device_add(dev);
|
|---|
| 122 | if (rc != EOK)
|
|---|
| 123 | usb_device_deinit(dev);
|
|---|
| 124 | return rc;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 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 | */
|
|---|
| 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;
|
|---|
| 140 | /* Just tell the driver to stop whatever it is doing */
|
|---|
| 141 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
|---|
| 142 | const int ret = driver->ops->device_rem(usb_dev);
|
|---|
| 143 | if (ret != EOK)
|
|---|
| 144 | return ret;
|
|---|
| 145 | usb_device_deinit(usb_dev);
|
|---|
| 146 | return EOK;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 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 | */
|
|---|
| 156 | int generic_device_gone(ddf_dev_t *gen_dev)
|
|---|
| 157 | {
|
|---|
| 158 | assert(driver);
|
|---|
| 159 | assert(driver->ops);
|
|---|
| 160 | if (driver->ops->device_gone == NULL)
|
|---|
| 161 | return ENOTSUP;
|
|---|
| 162 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
|---|
| 163 | const int ret = driver->ops->device_gone(usb_dev);
|
|---|
| 164 | if (ret == EOK)
|
|---|
| 165 | usb_device_deinit(usb_dev);
|
|---|
| 166 |
|
|---|
| 167 | return ret;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /** Destroy existing pipes of a USB device.
|
|---|
| 171 | *
|
|---|
| 172 | * @param dev Device where to destroy the pipes.
|
|---|
| 173 | */
|
|---|
| 174 | static void destroy_current_pipes(usb_device_t *dev)
|
|---|
| 175 | {
|
|---|
| 176 | usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
|
|---|
| 177 | dev->pipes = NULL;
|
|---|
| 178 | dev->pipes_count = 0;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 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 | *
|
|---|
| 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 | *
|
|---|
| 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,
|
|---|
| 204 | const usb_endpoint_description_t **endpoints)
|
|---|
| 205 | {
|
|---|
| 206 | if (dev->interface_no < 0) {
|
|---|
| 207 | return EINVAL;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /* Destroy existing pipes. */
|
|---|
| 211 | destroy_current_pipes(dev);
|
|---|
| 212 |
|
|---|
| 213 | /* Change the interface itself. */
|
|---|
| 214 | int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
|
|---|
| 215 | alternate_setting);
|
|---|
| 216 | if (rc != EOK) {
|
|---|
| 217 | return rc;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /* Create new pipes. */
|
|---|
| 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);
|
|---|
| 225 |
|
|---|
| 226 | return rc;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /** Retrieve basic descriptors from the device.
|
|---|
| 230 | *
|
|---|
| 231 | * @param[in] ctrl_pipe Control endpoint pipe.
|
|---|
| 232 | * @param[out] descriptors Where to store the descriptors.
|
|---|
| 233 | * @return Error code.
|
|---|
| 234 | */
|
|---|
| 235 | int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
|
|---|
| 236 | usb_device_descriptors_t *descriptors)
|
|---|
| 237 | {
|
|---|
| 238 | assert(descriptors != NULL);
|
|---|
| 239 |
|
|---|
| 240 | descriptors->configuration = NULL;
|
|---|
| 241 |
|
|---|
| 242 | int rc;
|
|---|
| 243 |
|
|---|
| 244 | /* It is worth to start a long transfer. */
|
|---|
| 245 | usb_pipe_start_long_transfer(ctrl_pipe);
|
|---|
| 246 |
|
|---|
| 247 | /* Get the device descriptor. */
|
|---|
| 248 | rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
|
|---|
| 249 | if (rc != EOK) {
|
|---|
| 250 | goto leave;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /* Get the full configuration descriptor. */
|
|---|
| 254 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
|---|
| 255 | ctrl_pipe, 0, (void **) &descriptors->configuration,
|
|---|
| 256 | &descriptors->configuration_size);
|
|---|
| 257 |
|
|---|
| 258 | leave:
|
|---|
| 259 | usb_pipe_end_long_transfer(ctrl_pipe);
|
|---|
| 260 |
|
|---|
| 261 | return rc;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
|---|
| 265 | *
|
|---|
| 266 | * @param[in] descriptors Where to store the descriptors.
|
|---|
| 267 | */
|
|---|
| 268 | void usb_device_release_descriptors(usb_device_descriptors_t *descriptors)
|
|---|
| 269 | {
|
|---|
| 270 | assert(descriptors);
|
|---|
| 271 | free(descriptors->configuration);
|
|---|
| 272 | descriptors->configuration = NULL;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Create pipes for a device.
|
|---|
| 276 | *
|
|---|
| 277 | * This is more or less a wrapper that does following actions:
|
|---|
| 278 | * - allocate and initialize pipes
|
|---|
| 279 | * - map endpoints to the pipes based on the descriptions
|
|---|
| 280 | * - registers endpoints with the host controller
|
|---|
| 281 | *
|
|---|
| 282 | * @param[in] wire Initialized backing connection to the host controller.
|
|---|
| 283 | * @param[in] endpoints Endpoints description, NULL terminated.
|
|---|
| 284 | * @param[in] config_descr Configuration descriptor of active configuration.
|
|---|
| 285 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
|---|
| 286 | * @param[in] interface_no Interface to map from.
|
|---|
| 287 | * @param[in] interface_setting Interface setting (default is usually 0).
|
|---|
| 288 | * @param[out] pipes_ptr Where to store array of created pipes
|
|---|
| 289 | * (not NULL terminated).
|
|---|
| 290 | * @param[out] pipes_count_ptr Where to store number of pipes
|
|---|
| 291 | * (set to NULL if you wish to ignore the count).
|
|---|
| 292 | * @return Error code.
|
|---|
| 293 | */
|
|---|
| 294 | int usb_device_create_pipes(usb_device_connection_t *wire,
|
|---|
| 295 | const usb_endpoint_description_t **endpoints,
|
|---|
| 296 | const uint8_t *config_descr, size_t config_descr_size,
|
|---|
| 297 | int interface_no, int interface_setting,
|
|---|
| 298 | usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
|
|---|
| 299 | {
|
|---|
| 300 | assert(wire != NULL);
|
|---|
| 301 | assert(config_descr != NULL);
|
|---|
| 302 | assert(config_descr_size > 0);
|
|---|
| 303 | assert(pipes_ptr != NULL);
|
|---|
| 304 |
|
|---|
| 305 | size_t i;
|
|---|
| 306 | int rc;
|
|---|
| 307 |
|
|---|
| 308 | const size_t pipe_count = count_other_pipes(endpoints);
|
|---|
| 309 | if (pipe_count == 0) {
|
|---|
| 310 | if (pipes_count_ptr)
|
|---|
| 311 | *pipes_count_ptr = pipe_count;
|
|---|
| 312 | *pipes_ptr = NULL;
|
|---|
| 313 | return EOK;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | usb_endpoint_mapping_t *pipes
|
|---|
| 317 | = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
|---|
| 318 | if (pipes == NULL) {
|
|---|
| 319 | return ENOMEM;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /* Now initialize. */
|
|---|
| 323 | for (i = 0; i < pipe_count; i++) {
|
|---|
| 324 | pipes[i].description = endpoints[i];
|
|---|
| 325 | pipes[i].interface_no = interface_no;
|
|---|
| 326 | pipes[i].interface_setting = interface_setting;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /* Find the mapping from configuration descriptor. */
|
|---|
| 330 | rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
|---|
| 331 | config_descr, config_descr_size, wire);
|
|---|
| 332 | if (rc != EOK) {
|
|---|
| 333 | free(pipes);
|
|---|
| 334 | return rc;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /* Register created pipes. */
|
|---|
| 338 | for (i = 0; i < pipe_count; i++) {
|
|---|
| 339 | if (pipes[i].present) {
|
|---|
| 340 | rc = usb_pipe_register(&pipes[i].pipe,
|
|---|
| 341 | pipes[i].descriptor->poll_interval);
|
|---|
| 342 | if (rc != EOK) {
|
|---|
| 343 | goto rollback_unregister_endpoints;
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | *pipes_ptr = pipes;
|
|---|
| 349 | if (pipes_count_ptr != NULL) {
|
|---|
| 350 | *pipes_count_ptr = pipe_count;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | return EOK;
|
|---|
| 354 |
|
|---|
| 355 | /*
|
|---|
| 356 | * Jump here if something went wrong after endpoints have
|
|---|
| 357 | * been registered.
|
|---|
| 358 | * This is also the target when the registration of
|
|---|
| 359 | * endpoints fails.
|
|---|
| 360 | */
|
|---|
| 361 | rollback_unregister_endpoints:
|
|---|
| 362 | for (i = 0; i < pipe_count; i++) {
|
|---|
| 363 | if (pipes[i].present) {
|
|---|
| 364 | usb_pipe_unregister(&pipes[i].pipe);
|
|---|
| 365 | }
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | free(pipes);
|
|---|
| 369 | return rc;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | /** Destroy pipes previously created by usb_device_create_pipes.
|
|---|
| 373 | *
|
|---|
| 374 | * @param[in] pipes Endpoint mapping to be destroyed.
|
|---|
| 375 | * @param[in] pipes_count Number of endpoints.
|
|---|
| 376 | */
|
|---|
| 377 | void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
|
|---|
| 378 | {
|
|---|
| 379 | /* Destroy the pipes. */
|
|---|
| 380 | for (size_t i = 0; i < pipes_count; ++i) {
|
|---|
| 381 | assert(pipes);
|
|---|
| 382 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
|---|
| 383 | i, pipes[i].present ? "" : "not ");
|
|---|
| 384 | if (pipes[i].present)
|
|---|
| 385 | usb_pipe_unregister(&pipes[i].pipe);
|
|---|
| 386 | }
|
|---|
| 387 | free(pipes);
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /** Initialize new instance of USB device.
|
|---|
| 391 | *
|
|---|
| 392 | * @param[in] usb_dev Pointer to the new device.
|
|---|
| 393 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
|---|
| 394 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
|---|
| 395 | * @param[out] errstr_ptr Where to store description of context
|
|---|
| 396 | * (in case error occurs).
|
|---|
| 397 | * @return Error code.
|
|---|
| 398 | */
|
|---|
| 399 | int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
|---|
| 400 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
|---|
| 401 | {
|
|---|
| 402 | assert(usb_dev != NULL);
|
|---|
| 403 | assert(ddf_dev != NULL);
|
|---|
| 404 |
|
|---|
| 405 | *errstr_ptr = NULL;
|
|---|
| 406 |
|
|---|
| 407 | usb_dev->ddf_dev = ddf_dev;
|
|---|
| 408 | usb_dev->driver_data = NULL;
|
|---|
| 409 | usb_dev->descriptors.configuration = NULL;
|
|---|
| 410 | usb_dev->pipes_count = 0;
|
|---|
| 411 | usb_dev->pipes = NULL;
|
|---|
| 412 |
|
|---|
| 413 | /* Get assigned params */
|
|---|
| 414 | devman_handle_t hc_handle;
|
|---|
| 415 | usb_address_t address;
|
|---|
| 416 |
|
|---|
| 417 | int rc = usb_get_info_by_handle(ddf_dev_get_handle(ddf_dev),
|
|---|
| 418 | &hc_handle, &address, &usb_dev->interface_no);
|
|---|
| 419 | if (rc != EOK) {
|
|---|
| 420 | *errstr_ptr = "device parameters retrieval";
|
|---|
| 421 | return rc;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /* Initialize hc connection. */
|
|---|
| 425 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
|---|
| 426 |
|
|---|
| 427 | /* Initialize backing wire and control pipe. */
|
|---|
| 428 | rc = usb_device_connection_initialize(
|
|---|
| 429 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
|---|
| 430 | if (rc != EOK) {
|
|---|
| 431 | *errstr_ptr = "device connection initialization";
|
|---|
| 432 | return rc;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /* This pipe was registered by the hub driver,
|
|---|
| 436 | * during device initialization. */
|
|---|
| 437 | rc = usb_pipe_initialize_default_control(
|
|---|
| 438 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
|---|
| 439 | if (rc != EOK) {
|
|---|
| 440 | *errstr_ptr = "default control pipe initialization";
|
|---|
| 441 | return rc;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /* Open hc connection for pipe registration. */
|
|---|
| 445 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
|---|
| 446 | if (rc != EOK) {
|
|---|
| 447 | *errstr_ptr = "hc connection open";
|
|---|
| 448 | return rc;
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | /* Retrieve standard descriptors. */
|
|---|
| 452 | rc = usb_device_retrieve_descriptors(
|
|---|
| 453 | &usb_dev->ctrl_pipe, &usb_dev->descriptors);
|
|---|
| 454 | if (rc != EOK) {
|
|---|
| 455 | *errstr_ptr = "descriptor retrieval";
|
|---|
| 456 | usb_hc_connection_close(&usb_dev->hc_conn);
|
|---|
| 457 | return rc;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /* Create alternate interfaces. We will silently ignore failure.
|
|---|
| 461 | * We might either control one interface or an entire device,
|
|---|
| 462 | * it makes no sense to speak about alternate interfaces when
|
|---|
| 463 | * controlling a device. */
|
|---|
| 464 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
|---|
| 465 | usb_dev->descriptors.configuration,
|
|---|
| 466 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
|---|
| 467 | const int alternate_iface =
|
|---|
| 468 | (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
|
|---|
| 469 |
|
|---|
| 470 | /* Create and register other pipes than default control (EP 0) */
|
|---|
| 471 | rc = usb_device_create_pipes(&usb_dev->wire, endpoints,
|
|---|
| 472 | usb_dev->descriptors.configuration,
|
|---|
| 473 | usb_dev->descriptors.configuration_size,
|
|---|
| 474 | usb_dev->interface_no, (int)alternate_iface,
|
|---|
| 475 | &usb_dev->pipes, &usb_dev->pipes_count);
|
|---|
| 476 | if (rc != EOK) {
|
|---|
| 477 | usb_hc_connection_close(&usb_dev->hc_conn);
|
|---|
| 478 | /* Full configuration descriptor is allocated. */
|
|---|
| 479 | usb_device_release_descriptors(&usb_dev->descriptors);
|
|---|
| 480 | /* Alternate interfaces may be allocated */
|
|---|
| 481 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
|---|
| 482 | *errstr_ptr = "pipes initialization";
|
|---|
| 483 | return rc;
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | usb_hc_connection_close(&usb_dev->hc_conn);
|
|---|
| 487 | return EOK;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | /** Clean instance of a USB device.
|
|---|
| 491 | *
|
|---|
| 492 | * @param dev Device to be de-initialized.
|
|---|
| 493 | *
|
|---|
| 494 | * Does not free/destroy supplied pointer.
|
|---|
| 495 | */
|
|---|
| 496 | void usb_device_deinit(usb_device_t *dev)
|
|---|
| 497 | {
|
|---|
| 498 | if (dev) {
|
|---|
| 499 | /* Destroy existing pipes. */
|
|---|
| 500 | destroy_current_pipes(dev);
|
|---|
| 501 | /* Ignore errors and hope for the best. */
|
|---|
| 502 | usb_hc_connection_deinitialize(&dev->hc_conn);
|
|---|
| 503 | usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
|
|---|
| 504 | usb_device_release_descriptors(&dev->descriptors);
|
|---|
| 505 | free(dev->driver_data);
|
|---|
| 506 | dev->driver_data = NULL;
|
|---|
| 507 | }
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | /** Allocate driver specific data.
|
|---|
| 511 | * @param usb_dev usb_device structure.
|
|---|
| 512 | * @param size requested data size.
|
|---|
| 513 | * @return Pointer to the newly allocated space, NULL on failure.
|
|---|
| 514 | */
|
|---|
| 515 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
|---|
| 516 | {
|
|---|
| 517 | assert(usb_dev);
|
|---|
| 518 | assert(usb_dev->driver_data == NULL);
|
|---|
| 519 | return usb_dev->driver_data = calloc(1, size);
|
|---|
| 520 |
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /**
|
|---|
| 524 | * @}
|
|---|
| 525 | */
|
|---|