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