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