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