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