[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 | {
|
---|
[ab27e01] | 117 | size_t count;
|
---|
[64e3dad] | 118 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
---|
[6105fc0] | 119 | return count;
|
---|
| 120 | }
|
---|
[a76b01b4] | 121 |
|
---|
[0b4e7ca] | 122 | /** Change interface setting of a device.
|
---|
| 123 | * This function selects new alternate setting of an interface by issuing
|
---|
| 124 | * proper USB command to the device and also creates new USB pipes
|
---|
| 125 | * under @c dev->pipes.
|
---|
| 126 | *
|
---|
| 127 | * @warning This function is intended for drivers working at interface level.
|
---|
| 128 | * For drivers controlling the whole device, you need to change interface
|
---|
| 129 | * manually using usb_request_set_interface() and creating new pipes
|
---|
| 130 | * with usb_pipe_initialize_from_configuration().
|
---|
| 131 | *
|
---|
[3f2af64] | 132 | * @warning This is a wrapper function that does several operations that
|
---|
| 133 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
| 134 | * during the SET_INTERFACE request would result in having a device with
|
---|
| 135 | * no pipes at all (except the default control one). That is because the old
|
---|
| 136 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
| 137 | * be created.
|
---|
| 138 | *
|
---|
[0b4e7ca] | 139 | * @param dev USB device.
|
---|
| 140 | * @param alternate_setting Alternate setting to choose.
|
---|
| 141 | * @param endpoints New endpoint descriptions.
|
---|
| 142 | * @return Error code.
|
---|
| 143 | */
|
---|
[5a6cc679] | 144 | errno_t usb_device_select_interface(usb_device_t *usb_dev,
|
---|
[f4138ac] | 145 | uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
|
---|
[0b4e7ca] | 146 | {
|
---|
[f4138ac] | 147 | assert(usb_dev);
|
---|
| 148 |
|
---|
| 149 | if (usb_dev->interface_no < 0) {
|
---|
[0b4e7ca] | 150 | return EINVAL;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /* Change the interface itself. */
|
---|
[5a6cc679] | 154 | errno_t rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
|
---|
[f4138ac] | 155 | usb_dev->interface_no, alternate_setting);
|
---|
[0b4e7ca] | 156 | if (rc != EOK) {
|
---|
| 157 | return rc;
|
---|
| 158 | }
|
---|
[816f5f4] | 159 |
|
---|
[f4138ac] | 160 | /* Change current alternative */
|
---|
| 161 | usb_dev->alternate_interfaces.current = alternate_setting;
|
---|
| 162 |
|
---|
| 163 | /* Destroy existing pipes. */
|
---|
| 164 | usb_device_destroy_pipes(usb_dev);
|
---|
| 165 |
|
---|
[0b4e7ca] | 166 | /* Create new pipes. */
|
---|
[b06d35a] | 167 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
[c1b1944] | 168 |
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /** Retrieve basic descriptors from the device.
|
---|
| 173 | *
|
---|
[4fa0a384] | 174 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
[c1b1944] | 175 | * @param[out] descriptors Where to store the descriptors.
|
---|
| 176 | * @return Error code.
|
---|
| 177 | */
|
---|
[5a6cc679] | 178 | static errno_t usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
[c1b1944] | 179 | {
|
---|
[945d66c] | 180 | assert(usb_dev);
|
---|
[e2dfa86] | 181 | assert(usb_dev->descriptors.full_config == NULL);
|
---|
[4fa0a384] | 182 |
|
---|
[c1b1944] | 183 | /* Get the device descriptor. */
|
---|
[5a6cc679] | 184 | errno_t rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
[945d66c] | 185 | &usb_dev->descriptors.device);
|
---|
[c1b1944] | 186 | if (rc != EOK) {
|
---|
[d93f5afb] | 187 | return rc;
|
---|
[c1b1944] | 188 | }
|
---|
| 189 |
|
---|
| 190 | /* Get the full configuration descriptor. */
|
---|
| 191 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
[945d66c] | 192 | &usb_dev->ctrl_pipe, 0,
|
---|
[0eb2a0f] | 193 | &usb_dev->descriptors.full_config,
|
---|
[e2dfa86] | 194 | &usb_dev->descriptors.full_config_size);
|
---|
[c1b1944] | 195 |
|
---|
[4fa0a384] | 196 |
|
---|
| 197 | return rc;
|
---|
[c1b1944] | 198 | }
|
---|
| 199 |
|
---|
[7fc260ff] | 200 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
| 201 | *
|
---|
| 202 | * @param[in] descriptors Where to store the descriptors.
|
---|
| 203 | */
|
---|
[945d66c] | 204 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
[7fc260ff] | 205 | {
|
---|
[945d66c] | 206 | assert(usb_dev);
|
---|
[e2dfa86] | 207 | free(usb_dev->descriptors.full_config);
|
---|
| 208 | usb_dev->descriptors.full_config = NULL;
|
---|
| 209 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7fc260ff] | 210 | }
|
---|
| 211 |
|
---|
[c1b1944] | 212 | /** Create pipes for a device.
|
---|
| 213 | *
|
---|
| 214 | * This is more or less a wrapper that does following actions:
|
---|
| 215 | * - allocate and initialize pipes
|
---|
| 216 | * - map endpoints to the pipes based on the descriptions
|
---|
| 217 | * - registers endpoints with the host controller
|
---|
| 218 | *
|
---|
| 219 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
| 220 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
| 221 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
| 222 | * @param[in] interface_no Interface to map from.
|
---|
| 223 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
| 224 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
| 225 | * (not NULL terminated).
|
---|
| 226 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
[ab27e01] | 227 | * (set to NULL if you wish to ignore the count).
|
---|
[c1b1944] | 228 | * @return Error code.
|
---|
| 229 | */
|
---|
[5a6cc679] | 230 | errno_t usb_device_create_pipes(usb_device_t *usb_dev,
|
---|
[b06d35a] | 231 | const usb_endpoint_description_t **endpoints)
|
---|
[c1b1944] | 232 | {
|
---|
[b06d35a] | 233 | assert(usb_dev);
|
---|
[e2dfa86] | 234 | assert(usb_dev->descriptors.full_config);
|
---|
[b06d35a] | 235 | assert(usb_dev->pipes == NULL);
|
---|
| 236 | assert(usb_dev->pipes_count == 0);
|
---|
[c1b1944] | 237 |
|
---|
[b06d35a] | 238 | size_t pipe_count = count_pipes(endpoints);
|
---|
[c1b1944] | 239 | if (pipe_count == 0) {
|
---|
| 240 | return EOK;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[b06d35a] | 243 | usb_endpoint_mapping_t *pipes =
|
---|
| 244 | calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
[c1b1944] | 245 | if (pipes == NULL) {
|
---|
| 246 | return ENOMEM;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[441633f] | 249 | /* Now initialize. */
|
---|
[b06d35a] | 250 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 251 | pipes[i].description = endpoints[i];
|
---|
[b06d35a] | 252 | pipes[i].interface_no = usb_dev->interface_no;
|
---|
| 253 | pipes[i].interface_setting =
|
---|
| 254 | usb_dev->alternate_interfaces.current;
|
---|
[c1b1944] | 255 | }
|
---|
| 256 |
|
---|
| 257 | /* Find the mapping from configuration descriptor. */
|
---|
[5a6cc679] | 258 | errno_t rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
[e2dfa86] | 259 | usb_dev->descriptors.full_config,
|
---|
[d93f5afb] | 260 | usb_dev->descriptors.full_config_size,
|
---|
[8582076] | 261 | usb_dev->bus_session);
|
---|
[c1b1944] | 262 | if (rc != EOK) {
|
---|
[441633f] | 263 | free(pipes);
|
---|
| 264 | return rc;
|
---|
[c1b1944] | 265 | }
|
---|
| 266 |
|
---|
[441633f] | 267 | /* Register created pipes. */
|
---|
[9efad54] | 268 | unsigned pipes_registered = 0;
|
---|
[b06d35a] | 269 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 270 | if (pipes[i].present) {
|
---|
[9efad54] | 271 | rc = usb_pipe_register(&pipes[i].pipe, pipes[i].descriptor, pipes[i].companion_descriptor);
|
---|
[c1b1944] | 272 | if (rc != EOK) {
|
---|
| 273 | goto rollback_unregister_endpoints;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
[9efad54] | 276 | pipes_registered++;
|
---|
[c1b1944] | 277 | }
|
---|
| 278 |
|
---|
[b06d35a] | 279 | usb_dev->pipes = pipes;
|
---|
| 280 | usb_dev->pipes_count = pipe_count;
|
---|
[c1b1944] | 281 |
|
---|
| 282 | return EOK;
|
---|
| 283 |
|
---|
| 284 | /*
|
---|
| 285 | * Jump here if something went wrong after endpoints have
|
---|
| 286 | * been registered.
|
---|
| 287 | * This is also the target when the registration of
|
---|
| 288 | * endpoints fails.
|
---|
| 289 | */
|
---|
| 290 | rollback_unregister_endpoints:
|
---|
[9efad54] | 291 | for (size_t i = 0; i < pipes_registered; i++) {
|
---|
[c1b1944] | 292 | if (pipes[i].present) {
|
---|
[bd575647] | 293 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | free(pipes);
|
---|
[0b4e7ca] | 298 | return rc;
|
---|
| 299 | }
|
---|
[159b91f4] | 300 |
|
---|
[c1b1944] | 301 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 302 | *
|
---|
[2dc5a9f] | 303 | * @param[in] usb_dev USB device.
|
---|
[58563585] | 304 | *
|
---|
[c1b1944] | 305 | */
|
---|
[2dc5a9f] | 306 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
[c1b1944] | 307 | {
|
---|
[2dc5a9f] | 308 | assert(usb_dev);
|
---|
| 309 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
[816f5f4] | 310 |
|
---|
[c1b1944] | 311 | /* Destroy the pipes. */
|
---|
[2489353] | 312 | int rc;
|
---|
[2dc5a9f] | 313 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
[a1732929] | 314 | usb_log_debug2("Unregistering pipe %zu: %spresent.",
|
---|
[2dc5a9f] | 315 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
[2489353] | 316 |
|
---|
| 317 | rc = usb_device_unmap_ep(usb_dev->pipes + i);
|
---|
| 318 | if (rc != EOK && rc != ENOENT)
|
---|
| 319 | usb_log_warning("Unregistering pipe %zu failed: %s", i, str_error(rc));
|
---|
[c1b1944] | 320 | }
|
---|
[816f5f4] | 321 |
|
---|
[2dc5a9f] | 322 | free(usb_dev->pipes);
|
---|
| 323 | usb_dev->pipes = NULL;
|
---|
| 324 | usb_dev->pipes_count = 0;
|
---|
[c1b1944] | 325 | }
|
---|
| 326 |
|
---|
[0f4bff8] | 327 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
| 328 | {
|
---|
| 329 | assert(usb_dev);
|
---|
| 330 | return &usb_dev->ctrl_pipe;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[a6a5b25] | 333 | usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
|
---|
| 334 | const usb_endpoint_description_t *desc)
|
---|
| 335 | {
|
---|
| 336 | assert(usb_dev);
|
---|
| 337 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
| 338 | if (usb_dev->pipes[i].description == desc)
|
---|
| 339 | return &usb_dev->pipes[i];
|
---|
| 340 | }
|
---|
| 341 | return NULL;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[2489353] | 344 | int usb_device_unmap_ep(usb_endpoint_mapping_t *epm)
|
---|
| 345 | {
|
---|
| 346 | assert(epm);
|
---|
| 347 |
|
---|
| 348 | if (!epm->present)
|
---|
| 349 | return ENOENT;
|
---|
| 350 |
|
---|
| 351 | const int rc = usb_pipe_unregister(&epm->pipe);
|
---|
| 352 | if (rc != EOK)
|
---|
| 353 | return rc;
|
---|
| 354 |
|
---|
| 355 | epm->present = false;
|
---|
| 356 | return EOK;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[45e49e6] | 359 | usb_address_t usb_device_get_address(const usb_device_t *usb_dev)
|
---|
| 360 | {
|
---|
| 361 | assert(usb_dev);
|
---|
| 362 | return usb_dev->depth;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | unsigned usb_device_get_depth(const usb_device_t *usb_dev)
|
---|
| 366 | {
|
---|
| 367 | assert(usb_dev);
|
---|
| 368 | return usb_dev->depth;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | usb_speed_t usb_device_get_speed(const usb_device_t *usb_dev)
|
---|
[129b821f] | 372 | {
|
---|
| 373 | assert(usb_dev);
|
---|
| 374 | return usb_dev->speed;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[45e49e6] | 377 | int usb_device_get_iface_number(const usb_device_t *usb_dev)
|
---|
[8e10ef4] | 378 | {
|
---|
| 379 | assert(usb_dev);
|
---|
| 380 | return usb_dev->interface_no;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[45e49e6] | 383 | devman_handle_t usb_device_get_devman_handle(const usb_device_t *usb_dev)
|
---|
[8e4219ab] | 384 | {
|
---|
| 385 | assert(usb_dev);
|
---|
| 386 | return usb_dev->handle;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[e2dfa86] | 389 | const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
|
---|
[945d66c] | 390 | {
|
---|
| 391 | assert(usb_dev);
|
---|
[e2dfa86] | 392 | return &usb_dev->descriptors;
|
---|
[945d66c] | 393 | }
|
---|
| 394 |
|
---|
[8e10ef4] | 395 | const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
|
---|
| 396 | usb_device_t *usb_dev)
|
---|
| 397 | {
|
---|
| 398 | assert(usb_dev);
|
---|
| 399 | return &usb_dev->alternate_interfaces;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[7363fc1] | 402 | /** Clean instance of a USB device.
|
---|
| 403 | *
|
---|
| 404 | * @param dev Device to be de-initialized.
|
---|
| 405 | *
|
---|
| 406 | * Does not free/destroy supplied pointer.
|
---|
| 407 | */
|
---|
| 408 | static void usb_device_fini(usb_device_t *usb_dev)
|
---|
| 409 | {
|
---|
| 410 | if (usb_dev) {
|
---|
| 411 | /* Destroy existing pipes. */
|
---|
| 412 | usb_device_destroy_pipes(usb_dev);
|
---|
| 413 | /* Ignore errors and hope for the best. */
|
---|
| 414 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
| 415 | usb_device_release_descriptors(usb_dev);
|
---|
| 416 | free(usb_dev->driver_data);
|
---|
| 417 | usb_dev->driver_data = NULL;
|
---|
[4c86c7c] | 418 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
| 419 | usb_dev->bus_session = NULL;
|
---|
[c1b1944] | 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[7d9cd62] | 423 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 424 | *
|
---|
[7d9cd62] | 425 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 426 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 427 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 428 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 429 | * (in case error occurs).
|
---|
| 430 | * @return Error code.
|
---|
| 431 | */
|
---|
[5a6cc679] | 432 | static errno_t usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
[c280d7e] | 433 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 434 | {
|
---|
[7d9cd62] | 435 | assert(usb_dev != NULL);
|
---|
[7363fc1] | 436 | assert(errstr_ptr);
|
---|
[6ee6e6f] | 437 |
|
---|
[7fc260ff] | 438 | *errstr_ptr = NULL;
|
---|
| 439 |
|
---|
[7d9cd62] | 440 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 441 | usb_dev->driver_data = NULL;
|
---|
[e2dfa86] | 442 | usb_dev->descriptors.full_config = NULL;
|
---|
| 443 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7d9cd62] | 444 | usb_dev->pipes_count = 0;
|
---|
| 445 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 446 |
|
---|
[c280d7e] | 447 | usb_dev->bus_session = usb_dev_connect(usb_dev->handle);
|
---|
[c0fdc0e] | 448 |
|
---|
[c8c758d] | 449 | if (!usb_dev->bus_session) {
|
---|
| 450 | *errstr_ptr = "device bus session create";
|
---|
| 451 | return ENOMEM;
|
---|
[7fc260ff] | 452 | }
|
---|
| 453 |
|
---|
| 454 | /* This pipe was registered by the hub driver,
|
---|
| 455 | * during device initialization. */
|
---|
[5a6cc679] | 456 | errno_t rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe, usb_dev->bus_session);
|
---|
[7fc260ff] | 457 | if (rc != EOK) {
|
---|
[7363fc1] | 458 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 459 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 460 | return rc;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 464 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 465 | if (rc != EOK) {
|
---|
[3f2af64] | 466 | *errstr_ptr = "descriptor retrieval";
|
---|
[7363fc1] | 467 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[6ee6e6f] | 468 | return rc;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
[7fc260ff] | 471 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 472 | * We might either control one interface or an entire device,
|
---|
| 473 | * it makes no sense to speak about alternate interfaces when
|
---|
| 474 | * controlling a device. */
|
---|
[806a779] | 475 | usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[e2dfa86] | 476 | usb_dev->descriptors.full_config,
|
---|
| 477 | usb_dev->descriptors.full_config_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 478 |
|
---|
[7363fc1] | 479 | if (endpoints) {
|
---|
| 480 | /* Create and register other pipes than default control (EP 0)*/
|
---|
| 481 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
| 482 | if (rc != EOK) {
|
---|
| 483 | usb_device_fini(usb_dev);
|
---|
| 484 | *errstr_ptr = "pipes initialization";
|
---|
| 485 | return rc;
|
---|
| 486 | }
|
---|
[6ee6e6f] | 487 | }
|
---|
| 488 |
|
---|
| 489 | return EOK;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[5a6cc679] | 492 | static errno_t usb_device_get_info(async_sess_t *sess, usb_device_t *dev)
|
---|
[70452dd4] | 493 | {
|
---|
[c280d7e] | 494 | assert(dev);
|
---|
[816f5f4] | 495 |
|
---|
[8e4219ab] | 496 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 497 | if (!exch)
|
---|
| 498 | return EPARTY;
|
---|
[816f5f4] | 499 |
|
---|
[c280d7e] | 500 | usb_device_desc_t dev_desc;
|
---|
[5a6cc679] | 501 | const errno_t ret = usb_get_my_description(exch, &dev_desc);
|
---|
[c280d7e] | 502 |
|
---|
[8e4219ab] | 503 | if (ret == EOK) {
|
---|
[c280d7e] | 504 | dev->address = dev_desc.address;
|
---|
[45e49e6] | 505 | dev->depth = dev_desc.depth;
|
---|
[c280d7e] | 506 | dev->speed = dev_desc.speed;
|
---|
| 507 | dev->handle = dev_desc.handle;
|
---|
| 508 | dev->interface_no = dev_desc.iface;
|
---|
[8e4219ab] | 509 | }
|
---|
[816f5f4] | 510 |
|
---|
[8e4219ab] | 511 | async_exchange_end(exch);
|
---|
| 512 | return ret;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
[5a6cc679] | 515 | errno_t usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
[fd9b3a67] | 516 | const usb_endpoint_description_t **desc, const char **err)
|
---|
| 517 | {
|
---|
| 518 | assert(ddf_dev);
|
---|
| 519 | assert(err);
|
---|
[8e4219ab] | 520 |
|
---|
[2bdf92a5] | 521 | async_sess_t *sess = ddf_dev_parent_sess_get(ddf_dev);
|
---|
[086f8e3] | 522 | if (sess == NULL)
|
---|
| 523 | return ENOMEM;
|
---|
[8e4219ab] | 524 |
|
---|
[7363fc1] | 525 | usb_device_t *usb_dev =
|
---|
| 526 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
| 527 | if (usb_dev == NULL) {
|
---|
[fd9b3a67] | 528 | *err = "DDF data alloc";
|
---|
| 529 | return ENOMEM;
|
---|
| 530 | }
|
---|
[816f5f4] | 531 |
|
---|
[5a6cc679] | 532 | const errno_t ret = usb_device_get_info(sess, usb_dev);
|
---|
[c280d7e] | 533 | if (ret != EOK)
|
---|
| 534 | return ret;
|
---|
| 535 |
|
---|
| 536 | return usb_device_init(usb_dev, ddf_dev, desc, err);
|
---|
[fd9b3a67] | 537 | }
|
---|
| 538 |
|
---|
| 539 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
| 540 | {
|
---|
| 541 | assert(ddf_dev);
|
---|
[7363fc1] | 542 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
| 543 | assert(usb_dev);
|
---|
| 544 | usb_device_fini(usb_dev);
|
---|
[fd9b3a67] | 545 | return;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
[7363fc1] | 548 | usb_device_t * usb_device_create(devman_handle_t handle)
|
---|
| 549 | {
|
---|
[c280d7e] | 550 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
| 551 | if (!usb_dev)
|
---|
| 552 | return NULL;
|
---|
[8e4219ab] | 553 |
|
---|
[b4b534ac] | 554 | async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
[5a6cc679] | 555 | errno_t ret = usb_device_get_info(sess, usb_dev);
|
---|
[bd41b192] | 556 | if (sess)
|
---|
| 557 | async_hangup(sess);
|
---|
[4172db4a] | 558 | if (ret != EOK) {
|
---|
| 559 | free(usb_dev);
|
---|
[8e4219ab] | 560 | return NULL;
|
---|
[4172db4a] | 561 | }
|
---|
[8e4219ab] | 562 |
|
---|
[7363fc1] | 563 | const char* dummy = NULL;
|
---|
[c280d7e] | 564 | ret = usb_device_init(usb_dev, NULL, NULL, &dummy);
|
---|
[7363fc1] | 565 | if (ret != EOK) {
|
---|
| 566 | free(usb_dev);
|
---|
| 567 | usb_dev = NULL;
|
---|
| 568 | }
|
---|
| 569 | return usb_dev;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
| 573 | {
|
---|
| 574 | if (usb_dev) {
|
---|
| 575 | usb_device_fini(usb_dev);
|
---|
| 576 | free(usb_dev);
|
---|
[70452dd4] | 577 | }
|
---|
[065064e6] | 578 | }
|
---|
| 579 |
|
---|
[f6b2a76b] | 580 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 581 | {
|
---|
| 582 | assert(usb_dev);
|
---|
[35bc430] | 583 | if (usb_dev->ddf_dev)
|
---|
| 584 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 585 | return NULL;
|
---|
[f6b2a76b] | 586 | }
|
---|
| 587 |
|
---|
[bb512b2d] | 588 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
| 589 | const char* name)
|
---|
| 590 | {
|
---|
| 591 | assert(usb_dev);
|
---|
| 592 | if (usb_dev->ddf_dev)
|
---|
| 593 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
| 594 | return NULL;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[0f4bff8] | 597 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
| 598 | {
|
---|
| 599 | assert(usb_dev);
|
---|
| 600 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 604 | {
|
---|
| 605 | async_exchange_end(exch);
|
---|
| 606 | }
|
---|
| 607 |
|
---|
[6e3c005] | 608 | /** Allocate driver specific data.
|
---|
| 609 | * @param usb_dev usb_device structure.
|
---|
| 610 | * @param size requested data size.
|
---|
| 611 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 612 | */
|
---|
[065064e6] | 613 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 614 | {
|
---|
| 615 | assert(usb_dev);
|
---|
| 616 | assert(usb_dev->driver_data == NULL);
|
---|
| 617 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 618 |
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[0f4bff8] | 621 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
| 622 | {
|
---|
| 623 | assert(usb_dev);
|
---|
| 624 | return usb_dev->driver_data;
|
---|
| 625 | }
|
---|
[4ca778b] | 626 |
|
---|
[6105fc0] | 627 | /**
|
---|
| 628 | * @}
|
---|
| 629 | */
|
---|