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