[6105fc0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[4291215] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
[e0a5d4c] | 4 | * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek, Michal Staruch
|
---|
[6105fc0] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
[58563585] | 30 |
|
---|
[160b75e] | 31 | /** @addtogroup libusbdev
|
---|
[6105fc0] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | * USB device driver framework.
|
---|
| 36 | */
|
---|
[87619045] | 37 |
|
---|
| 38 | #include <usb_iface.h>
|
---|
[c01987c] | 39 | #include <usb/dev/alternate_ifaces.h>
|
---|
[87619045] | 40 | #include <usb/dev/device.h>
|
---|
[c01987c] | 41 | #include <usb/dev/pipes.h>
|
---|
[7d521e24] | 42 | #include <usb/dev/request.h>
|
---|
[6105fc0] | 43 | #include <usb/debug.h>
|
---|
[c01987c] | 44 | #include <usb/descriptor.h>
|
---|
| 45 | #include <usb/usb.h>
|
---|
[6105fc0] | 46 |
|
---|
| 47 | #include <assert.h>
|
---|
[c01987c] | 48 | #include <async.h>
|
---|
| 49 | #include <devman.h>
|
---|
| 50 | #include <errno.h>
|
---|
[2489353] | 51 | #include <str_error.h>
|
---|
[c01987c] | 52 | #include <stdlib.h>
|
---|
| 53 |
|
---|
| 54 | #include <ddf/driver.h>
|
---|
[6105fc0] | 55 |
|
---|
[fd9b3a67] | 56 | /** USB device structure. */
|
---|
[05b59393] | 57 | struct usb_device {
|
---|
[d93f5afb] | 58 | /** Connection to device on USB bus */
|
---|
| 59 | usb_dev_session_t *bus_session;
|
---|
[816f5f4] | 60 |
|
---|
[d93f5afb] | 61 | /** devman handle */
|
---|
| 62 | devman_handle_t handle;
|
---|
[816f5f4] | 63 |
|
---|
[fd9b3a67] | 64 | /** The default control pipe. */
|
---|
| 65 | usb_pipe_t ctrl_pipe;
|
---|
[816f5f4] | 66 |
|
---|
[fd9b3a67] | 67 | /** Other endpoint pipes.
|
---|
[58563585] | 68 | *
|
---|
[fd9b3a67] | 69 | * This is an array of other endpoint pipes in the same order as
|
---|
| 70 | * in usb_driver_t.
|
---|
| 71 | */
|
---|
| 72 | usb_endpoint_mapping_t *pipes;
|
---|
[816f5f4] | 73 |
|
---|
[fd9b3a67] | 74 | /** Number of other endpoint pipes. */
|
---|
| 75 | size_t pipes_count;
|
---|
[816f5f4] | 76 |
|
---|
[c280d7e] | 77 | /** USB address of this device */
|
---|
| 78 | usb_address_t address;
|
---|
| 79 |
|
---|
[45e49e6] | 80 | /** Depth in the USB hub hiearchy */
|
---|
| 81 | unsigned depth;
|
---|
| 82 |
|
---|
[c280d7e] | 83 | /** USB speed of this device */
|
---|
| 84 | usb_speed_t speed;
|
---|
| 85 |
|
---|
[fd9b3a67] | 86 | /** Current interface.
|
---|
[58563585] | 87 | *
|
---|
[fd9b3a67] | 88 | * Usually, drivers operate on single interface only.
|
---|
| 89 | * This item contains the value of the interface or -1 for any.
|
---|
| 90 | */
|
---|
| 91 | int interface_no;
|
---|
[816f5f4] | 92 |
|
---|
[fd9b3a67] | 93 | /** Alternative interfaces. */
|
---|
| 94 | usb_alternate_interfaces_t alternate_interfaces;
|
---|
[816f5f4] | 95 |
|
---|
[fd9b3a67] | 96 | /** Some useful descriptors for USB device. */
|
---|
[e2dfa86] | 97 | usb_device_descriptors_t descriptors;
|
---|
[816f5f4] | 98 |
|
---|
[fd9b3a67] | 99 | /** Generic DDF device backing this one. DO NOT TOUCH! */
|
---|
| 100 | ddf_dev_t *ddf_dev;
|
---|
[816f5f4] | 101 |
|
---|
[fd9b3a67] | 102 | /** Custom driver data.
|
---|
[58563585] | 103 | *
|
---|
[fd9b3a67] | 104 | * Do not use the entry in generic device, that is already used
|
---|
| 105 | * by the framework.
|
---|
| 106 | */
|
---|
| 107 | void *driver_data;
|
---|
[05b59393] | 108 | };
|
---|
[a76b01b4] | 109 |
|
---|
[69334af] | 110 | /** Count number of pipes the driver expects.
|
---|
| 111 | *
|
---|
| 112 | * @param drv USB driver.
|
---|
| 113 | * @return Number of pipes (excluding default control pipe).
|
---|
| 114 | */
|
---|
[b06d35a] | 115 | static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
|
---|
[6105fc0] | 116 | {
|
---|
[850fd32] | 117 | size_t count = 0;
|
---|
| 118 | while (endpoints != NULL && endpoints[count] != NULL)
|
---|
| 119 | ++count;
|
---|
[6105fc0] | 120 | return count;
|
---|
| 121 | }
|
---|
[a76b01b4] | 122 |
|
---|
[0b4e7ca] | 123 | /** Change interface setting of a device.
|
---|
| 124 | * This function selects new alternate setting of an interface by issuing
|
---|
| 125 | * proper USB command to the device and also creates new USB pipes
|
---|
| 126 | * under @c dev->pipes.
|
---|
| 127 | *
|
---|
| 128 | * @warning This function is intended for drivers working at interface level.
|
---|
| 129 | * For drivers controlling the whole device, you need to change interface
|
---|
| 130 | * manually using usb_request_set_interface() and creating new pipes
|
---|
| 131 | * with usb_pipe_initialize_from_configuration().
|
---|
| 132 | *
|
---|
[3f2af64] | 133 | * @warning This is a wrapper function that does several operations that
|
---|
| 134 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
| 135 | * during the SET_INTERFACE request would result in having a device with
|
---|
| 136 | * no pipes at all (except the default control one). That is because the old
|
---|
| 137 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
| 138 | * be created.
|
---|
| 139 | *
|
---|
[0b4e7ca] | 140 | * @param dev USB device.
|
---|
| 141 | * @param alternate_setting Alternate setting to choose.
|
---|
| 142 | * @param endpoints New endpoint descriptions.
|
---|
| 143 | * @return Error code.
|
---|
| 144 | */
|
---|
[5a6cc679] | 145 | errno_t usb_device_select_interface(usb_device_t *usb_dev,
|
---|
[f4138ac] | 146 | uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
|
---|
[0b4e7ca] | 147 | {
|
---|
[f4138ac] | 148 | assert(usb_dev);
|
---|
| 149 |
|
---|
| 150 | if (usb_dev->interface_no < 0) {
|
---|
[0b4e7ca] | 151 | return EINVAL;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /* Change the interface itself. */
|
---|
[5a6cc679] | 155 | errno_t rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
|
---|
[f4138ac] | 156 | usb_dev->interface_no, alternate_setting);
|
---|
[0b4e7ca] | 157 | if (rc != EOK) {
|
---|
| 158 | return rc;
|
---|
| 159 | }
|
---|
[816f5f4] | 160 |
|
---|
[f4138ac] | 161 | /* Change current alternative */
|
---|
| 162 | usb_dev->alternate_interfaces.current = alternate_setting;
|
---|
| 163 |
|
---|
| 164 | /* Destroy existing pipes. */
|
---|
| 165 | usb_device_destroy_pipes(usb_dev);
|
---|
| 166 |
|
---|
[0b4e7ca] | 167 | /* Create new pipes. */
|
---|
[b06d35a] | 168 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
[c1b1944] | 169 |
|
---|
| 170 | return rc;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** Retrieve basic descriptors from the device.
|
---|
| 174 | *
|
---|
[4fa0a384] | 175 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
[c1b1944] | 176 | * @param[out] descriptors Where to store the descriptors.
|
---|
| 177 | * @return Error code.
|
---|
| 178 | */
|
---|
[5a6cc679] | 179 | static errno_t usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
[c1b1944] | 180 | {
|
---|
[945d66c] | 181 | assert(usb_dev);
|
---|
[e2dfa86] | 182 | assert(usb_dev->descriptors.full_config == NULL);
|
---|
[4fa0a384] | 183 |
|
---|
[c1b1944] | 184 | /* Get the device descriptor. */
|
---|
[5a6cc679] | 185 | errno_t rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
[945d66c] | 186 | &usb_dev->descriptors.device);
|
---|
[c1b1944] | 187 | if (rc != EOK) {
|
---|
[d93f5afb] | 188 | return rc;
|
---|
[c1b1944] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /* Get the full configuration descriptor. */
|
---|
| 192 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
[945d66c] | 193 | &usb_dev->ctrl_pipe, 0,
|
---|
[0eb2a0f] | 194 | &usb_dev->descriptors.full_config,
|
---|
[e2dfa86] | 195 | &usb_dev->descriptors.full_config_size);
|
---|
[c1b1944] | 196 |
|
---|
[4fa0a384] | 197 |
|
---|
| 198 | return rc;
|
---|
[c1b1944] | 199 | }
|
---|
| 200 |
|
---|
[7fc260ff] | 201 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
| 202 | *
|
---|
| 203 | * @param[in] descriptors Where to store the descriptors.
|
---|
| 204 | */
|
---|
[945d66c] | 205 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
[7fc260ff] | 206 | {
|
---|
[945d66c] | 207 | assert(usb_dev);
|
---|
[e2dfa86] | 208 | free(usb_dev->descriptors.full_config);
|
---|
| 209 | usb_dev->descriptors.full_config = NULL;
|
---|
| 210 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7fc260ff] | 211 | }
|
---|
| 212 |
|
---|
[c1b1944] | 213 | /** Create pipes for a device.
|
---|
| 214 | *
|
---|
| 215 | * This is more or less a wrapper that does following actions:
|
---|
| 216 | * - allocate and initialize pipes
|
---|
| 217 | * - map endpoints to the pipes based on the descriptions
|
---|
| 218 | * - registers endpoints with the host controller
|
---|
| 219 | *
|
---|
| 220 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
| 221 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
| 222 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
| 223 | * @param[in] interface_no Interface to map from.
|
---|
| 224 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
| 225 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
| 226 | * (not NULL terminated).
|
---|
| 227 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
[ab27e01] | 228 | * (set to NULL if you wish to ignore the count).
|
---|
[c1b1944] | 229 | * @return Error code.
|
---|
| 230 | */
|
---|
[5a6cc679] | 231 | errno_t usb_device_create_pipes(usb_device_t *usb_dev,
|
---|
[b06d35a] | 232 | const usb_endpoint_description_t **endpoints)
|
---|
[c1b1944] | 233 | {
|
---|
[b06d35a] | 234 | assert(usb_dev);
|
---|
[e2dfa86] | 235 | assert(usb_dev->descriptors.full_config);
|
---|
[b06d35a] | 236 | assert(usb_dev->pipes == NULL);
|
---|
| 237 | assert(usb_dev->pipes_count == 0);
|
---|
[c1b1944] | 238 |
|
---|
[b06d35a] | 239 | size_t pipe_count = count_pipes(endpoints);
|
---|
[c1b1944] | 240 | if (pipe_count == 0) {
|
---|
| 241 | return EOK;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[b06d35a] | 244 | usb_endpoint_mapping_t *pipes =
|
---|
| 245 | calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
[c1b1944] | 246 | if (pipes == NULL) {
|
---|
| 247 | return ENOMEM;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[441633f] | 250 | /* Now initialize. */
|
---|
[b06d35a] | 251 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 252 | pipes[i].description = endpoints[i];
|
---|
[b06d35a] | 253 | pipes[i].interface_no = usb_dev->interface_no;
|
---|
| 254 | pipes[i].interface_setting =
|
---|
| 255 | usb_dev->alternate_interfaces.current;
|
---|
[c1b1944] | 256 | }
|
---|
| 257 |
|
---|
| 258 | /* Find the mapping from configuration descriptor. */
|
---|
[5a6cc679] | 259 | errno_t rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
[e2dfa86] | 260 | usb_dev->descriptors.full_config,
|
---|
[d93f5afb] | 261 | usb_dev->descriptors.full_config_size,
|
---|
[8582076] | 262 | usb_dev->bus_session);
|
---|
[c1b1944] | 263 | if (rc != EOK) {
|
---|
[441633f] | 264 | free(pipes);
|
---|
| 265 | return rc;
|
---|
[c1b1944] | 266 | }
|
---|
| 267 |
|
---|
[441633f] | 268 | /* Register created pipes. */
|
---|
[9efad54] | 269 | unsigned pipes_registered = 0;
|
---|
[b06d35a] | 270 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 271 | if (pipes[i].present) {
|
---|
[9efad54] | 272 | rc = usb_pipe_register(&pipes[i].pipe, pipes[i].descriptor, pipes[i].companion_descriptor);
|
---|
[c1b1944] | 273 | if (rc != EOK) {
|
---|
| 274 | goto rollback_unregister_endpoints;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
[9efad54] | 277 | pipes_registered++;
|
---|
[c1b1944] | 278 | }
|
---|
| 279 |
|
---|
[b06d35a] | 280 | usb_dev->pipes = pipes;
|
---|
| 281 | usb_dev->pipes_count = pipe_count;
|
---|
[c1b1944] | 282 |
|
---|
| 283 | return EOK;
|
---|
| 284 |
|
---|
| 285 | /*
|
---|
| 286 | * Jump here if something went wrong after endpoints have
|
---|
| 287 | * been registered.
|
---|
| 288 | * This is also the target when the registration of
|
---|
| 289 | * endpoints fails.
|
---|
| 290 | */
|
---|
| 291 | rollback_unregister_endpoints:
|
---|
[9efad54] | 292 | for (size_t i = 0; i < pipes_registered; i++) {
|
---|
[c1b1944] | 293 | if (pipes[i].present) {
|
---|
[bd575647] | 294 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | free(pipes);
|
---|
[0b4e7ca] | 299 | return rc;
|
---|
| 300 | }
|
---|
[159b91f4] | 301 |
|
---|
[c1b1944] | 302 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 303 | *
|
---|
[2dc5a9f] | 304 | * @param[in] usb_dev USB device.
|
---|
[58563585] | 305 | *
|
---|
[c1b1944] | 306 | */
|
---|
[2dc5a9f] | 307 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
[c1b1944] | 308 | {
|
---|
[2dc5a9f] | 309 | assert(usb_dev);
|
---|
| 310 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
[816f5f4] | 311 |
|
---|
[c1b1944] | 312 | /* Destroy the pipes. */
|
---|
[2489353] | 313 | int rc;
|
---|
[2dc5a9f] | 314 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
[a1732929] | 315 | usb_log_debug2("Unregistering pipe %zu: %spresent.",
|
---|
[2dc5a9f] | 316 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
[2489353] | 317 |
|
---|
| 318 | rc = usb_device_unmap_ep(usb_dev->pipes + i);
|
---|
| 319 | if (rc != EOK && rc != ENOENT)
|
---|
| 320 | usb_log_warning("Unregistering pipe %zu failed: %s", i, str_error(rc));
|
---|
[c1b1944] | 321 | }
|
---|
[816f5f4] | 322 |
|
---|
[2dc5a9f] | 323 | free(usb_dev->pipes);
|
---|
| 324 | usb_dev->pipes = NULL;
|
---|
| 325 | usb_dev->pipes_count = 0;
|
---|
[c1b1944] | 326 | }
|
---|
| 327 |
|
---|
[0f4bff8] | 328 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
| 329 | {
|
---|
| 330 | assert(usb_dev);
|
---|
| 331 | return &usb_dev->ctrl_pipe;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[a6a5b25] | 334 | usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
|
---|
| 335 | const usb_endpoint_description_t *desc)
|
---|
| 336 | {
|
---|
| 337 | assert(usb_dev);
|
---|
| 338 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
| 339 | if (usb_dev->pipes[i].description == desc)
|
---|
| 340 | return &usb_dev->pipes[i];
|
---|
| 341 | }
|
---|
| 342 | return NULL;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[2489353] | 345 | int usb_device_unmap_ep(usb_endpoint_mapping_t *epm)
|
---|
| 346 | {
|
---|
| 347 | assert(epm);
|
---|
| 348 |
|
---|
| 349 | if (!epm->present)
|
---|
| 350 | return ENOENT;
|
---|
| 351 |
|
---|
| 352 | const int rc = usb_pipe_unregister(&epm->pipe);
|
---|
| 353 | if (rc != EOK)
|
---|
| 354 | return rc;
|
---|
| 355 |
|
---|
| 356 | epm->present = false;
|
---|
| 357 | return EOK;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[45e49e6] | 360 | usb_address_t usb_device_get_address(const usb_device_t *usb_dev)
|
---|
| 361 | {
|
---|
| 362 | assert(usb_dev);
|
---|
| 363 | return usb_dev->depth;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | unsigned usb_device_get_depth(const usb_device_t *usb_dev)
|
---|
| 367 | {
|
---|
| 368 | assert(usb_dev);
|
---|
| 369 | return usb_dev->depth;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | usb_speed_t usb_device_get_speed(const usb_device_t *usb_dev)
|
---|
[129b821f] | 373 | {
|
---|
| 374 | assert(usb_dev);
|
---|
| 375 | return usb_dev->speed;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[45e49e6] | 378 | int usb_device_get_iface_number(const usb_device_t *usb_dev)
|
---|
[8e10ef4] | 379 | {
|
---|
| 380 | assert(usb_dev);
|
---|
| 381 | return usb_dev->interface_no;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[45e49e6] | 384 | devman_handle_t usb_device_get_devman_handle(const usb_device_t *usb_dev)
|
---|
[8e4219ab] | 385 | {
|
---|
| 386 | assert(usb_dev);
|
---|
| 387 | return usb_dev->handle;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[e2dfa86] | 390 | const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
|
---|
[945d66c] | 391 | {
|
---|
| 392 | assert(usb_dev);
|
---|
[e2dfa86] | 393 | return &usb_dev->descriptors;
|
---|
[945d66c] | 394 | }
|
---|
| 395 |
|
---|
[3bacee1] | 396 | const usb_alternate_interfaces_t *usb_device_get_alternative_ifaces(
|
---|
[8e10ef4] | 397 | usb_device_t *usb_dev)
|
---|
| 398 | {
|
---|
| 399 | assert(usb_dev);
|
---|
| 400 | return &usb_dev->alternate_interfaces;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[7363fc1] | 403 | /** Clean instance of a USB device.
|
---|
| 404 | *
|
---|
| 405 | * @param dev Device to be de-initialized.
|
---|
| 406 | *
|
---|
| 407 | * Does not free/destroy supplied pointer.
|
---|
| 408 | */
|
---|
| 409 | static void usb_device_fini(usb_device_t *usb_dev)
|
---|
| 410 | {
|
---|
| 411 | if (usb_dev) {
|
---|
| 412 | /* Destroy existing pipes. */
|
---|
| 413 | usb_device_destroy_pipes(usb_dev);
|
---|
| 414 | /* Ignore errors and hope for the best. */
|
---|
| 415 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
| 416 | usb_device_release_descriptors(usb_dev);
|
---|
| 417 | free(usb_dev->driver_data);
|
---|
| 418 | usb_dev->driver_data = NULL;
|
---|
[4c86c7c] | 419 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
| 420 | usb_dev->bus_session = NULL;
|
---|
[c1b1944] | 421 | }
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[7d9cd62] | 424 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 425 | *
|
---|
[7d9cd62] | 426 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 427 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 428 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 429 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 430 | * (in case error occurs).
|
---|
| 431 | * @return Error code.
|
---|
| 432 | */
|
---|
[5a6cc679] | 433 | static errno_t usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
[c280d7e] | 434 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 435 | {
|
---|
[7d9cd62] | 436 | assert(usb_dev != NULL);
|
---|
[7363fc1] | 437 | assert(errstr_ptr);
|
---|
[6ee6e6f] | 438 |
|
---|
[7fc260ff] | 439 | *errstr_ptr = NULL;
|
---|
| 440 |
|
---|
[7d9cd62] | 441 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 442 | usb_dev->driver_data = NULL;
|
---|
[e2dfa86] | 443 | usb_dev->descriptors.full_config = NULL;
|
---|
| 444 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7d9cd62] | 445 | usb_dev->pipes_count = 0;
|
---|
| 446 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 447 |
|
---|
[c280d7e] | 448 | usb_dev->bus_session = usb_dev_connect(usb_dev->handle);
|
---|
[c0fdc0e] | 449 |
|
---|
[c8c758d] | 450 | if (!usb_dev->bus_session) {
|
---|
| 451 | *errstr_ptr = "device bus session create";
|
---|
| 452 | return ENOMEM;
|
---|
[7fc260ff] | 453 | }
|
---|
| 454 |
|
---|
| 455 | /* This pipe was registered by the hub driver,
|
---|
| 456 | * during device initialization. */
|
---|
[5a6cc679] | 457 | errno_t rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe, usb_dev->bus_session);
|
---|
[7fc260ff] | 458 | if (rc != EOK) {
|
---|
[7363fc1] | 459 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 460 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 461 | return rc;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 465 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 466 | if (rc != EOK) {
|
---|
[3f2af64] | 467 | *errstr_ptr = "descriptor retrieval";
|
---|
[7363fc1] | 468 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[6ee6e6f] | 469 | return rc;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[7fc260ff] | 472 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 473 | * We might either control one interface or an entire device,
|
---|
| 474 | * it makes no sense to speak about alternate interfaces when
|
---|
| 475 | * controlling a device. */
|
---|
[806a779] | 476 | usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[e2dfa86] | 477 | usb_dev->descriptors.full_config,
|
---|
| 478 | usb_dev->descriptors.full_config_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 479 |
|
---|
[7363fc1] | 480 | if (endpoints) {
|
---|
| 481 | /* Create and register other pipes than default control (EP 0)*/
|
---|
| 482 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
| 483 | if (rc != EOK) {
|
---|
| 484 | usb_device_fini(usb_dev);
|
---|
| 485 | *errstr_ptr = "pipes initialization";
|
---|
| 486 | return rc;
|
---|
| 487 | }
|
---|
[6ee6e6f] | 488 | }
|
---|
| 489 |
|
---|
| 490 | return EOK;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[5a6cc679] | 493 | static errno_t usb_device_get_info(async_sess_t *sess, usb_device_t *dev)
|
---|
[70452dd4] | 494 | {
|
---|
[c280d7e] | 495 | assert(dev);
|
---|
[816f5f4] | 496 |
|
---|
[8e4219ab] | 497 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 498 | if (!exch)
|
---|
| 499 | return EPARTY;
|
---|
[816f5f4] | 500 |
|
---|
[c280d7e] | 501 | usb_device_desc_t dev_desc;
|
---|
[5a6cc679] | 502 | const errno_t ret = usb_get_my_description(exch, &dev_desc);
|
---|
[c280d7e] | 503 |
|
---|
[8e4219ab] | 504 | if (ret == EOK) {
|
---|
[c280d7e] | 505 | dev->address = dev_desc.address;
|
---|
[45e49e6] | 506 | dev->depth = dev_desc.depth;
|
---|
[c280d7e] | 507 | dev->speed = dev_desc.speed;
|
---|
| 508 | dev->handle = dev_desc.handle;
|
---|
| 509 | dev->interface_no = dev_desc.iface;
|
---|
[8e4219ab] | 510 | }
|
---|
[816f5f4] | 511 |
|
---|
[8e4219ab] | 512 | async_exchange_end(exch);
|
---|
| 513 | return ret;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[5a6cc679] | 516 | errno_t usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
[fd9b3a67] | 517 | const usb_endpoint_description_t **desc, const char **err)
|
---|
| 518 | {
|
---|
| 519 | assert(ddf_dev);
|
---|
| 520 | assert(err);
|
---|
[8e4219ab] | 521 |
|
---|
[2bdf92a5] | 522 | async_sess_t *sess = ddf_dev_parent_sess_get(ddf_dev);
|
---|
[086f8e3] | 523 | if (sess == NULL)
|
---|
| 524 | return ENOMEM;
|
---|
[8e4219ab] | 525 |
|
---|
[7363fc1] | 526 | usb_device_t *usb_dev =
|
---|
| 527 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
| 528 | if (usb_dev == NULL) {
|
---|
[fd9b3a67] | 529 | *err = "DDF data alloc";
|
---|
| 530 | return ENOMEM;
|
---|
| 531 | }
|
---|
[816f5f4] | 532 |
|
---|
[5a6cc679] | 533 | const errno_t ret = usb_device_get_info(sess, usb_dev);
|
---|
[c280d7e] | 534 | if (ret != EOK)
|
---|
| 535 | return ret;
|
---|
| 536 |
|
---|
| 537 | return usb_device_init(usb_dev, ddf_dev, desc, err);
|
---|
[fd9b3a67] | 538 | }
|
---|
| 539 |
|
---|
| 540 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
| 541 | {
|
---|
| 542 | assert(ddf_dev);
|
---|
[7363fc1] | 543 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
| 544 | assert(usb_dev);
|
---|
| 545 | usb_device_fini(usb_dev);
|
---|
[fd9b3a67] | 546 | return;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[3bacee1] | 549 | usb_device_t *usb_device_create(devman_handle_t handle)
|
---|
[7363fc1] | 550 | {
|
---|
[c280d7e] | 551 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
| 552 | if (!usb_dev)
|
---|
| 553 | return NULL;
|
---|
[8e4219ab] | 554 |
|
---|
[b4b534ac] | 555 | async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
[5a6cc679] | 556 | errno_t ret = usb_device_get_info(sess, usb_dev);
|
---|
[bd41b192] | 557 | if (sess)
|
---|
| 558 | async_hangup(sess);
|
---|
[4172db4a] | 559 | if (ret != EOK) {
|
---|
| 560 | free(usb_dev);
|
---|
[8e4219ab] | 561 | return NULL;
|
---|
[4172db4a] | 562 | }
|
---|
[8e4219ab] | 563 |
|
---|
[3bacee1] | 564 | const char *dummy = NULL;
|
---|
[c280d7e] | 565 | ret = usb_device_init(usb_dev, NULL, NULL, &dummy);
|
---|
[7363fc1] | 566 | if (ret != EOK) {
|
---|
| 567 | free(usb_dev);
|
---|
| 568 | usb_dev = NULL;
|
---|
| 569 | }
|
---|
| 570 | return usb_dev;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
| 574 | {
|
---|
| 575 | if (usb_dev) {
|
---|
| 576 | usb_device_fini(usb_dev);
|
---|
| 577 | free(usb_dev);
|
---|
[70452dd4] | 578 | }
|
---|
[065064e6] | 579 | }
|
---|
| 580 |
|
---|
[f6b2a76b] | 581 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 582 | {
|
---|
| 583 | assert(usb_dev);
|
---|
[35bc430] | 584 | if (usb_dev->ddf_dev)
|
---|
| 585 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 586 | return NULL;
|
---|
[f6b2a76b] | 587 | }
|
---|
| 588 |
|
---|
[bb512b2d] | 589 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
[3bacee1] | 590 | const char *name)
|
---|
[bb512b2d] | 591 | {
|
---|
| 592 | assert(usb_dev);
|
---|
| 593 | if (usb_dev->ddf_dev)
|
---|
| 594 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
| 595 | return NULL;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[3bacee1] | 598 | async_exch_t *usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
[0f4bff8] | 599 | {
|
---|
| 600 | assert(usb_dev);
|
---|
| 601 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 605 | {
|
---|
| 606 | async_exchange_end(exch);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[6e3c005] | 609 | /** Allocate driver specific data.
|
---|
| 610 | * @param usb_dev usb_device structure.
|
---|
| 611 | * @param size requested data size.
|
---|
| 612 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 613 | */
|
---|
[3bacee1] | 614 | void *usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
[065064e6] | 615 | {
|
---|
| 616 | assert(usb_dev);
|
---|
| 617 | assert(usb_dev->driver_data == NULL);
|
---|
| 618 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 619 |
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[3bacee1] | 622 | void *usb_device_data_get(usb_device_t *usb_dev)
|
---|
[0f4bff8] | 623 | {
|
---|
| 624 | assert(usb_dev);
|
---|
| 625 | return usb_dev->driver_data;
|
---|
| 626 | }
|
---|
[4ca778b] | 627 |
|
---|
[6105fc0] | 628 | /**
|
---|
| 629 | * @}
|
---|
| 630 | */
|
---|