[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 |
|
---|
[2489353] | 343 | int usb_device_unmap_ep(usb_endpoint_mapping_t *epm)
|
---|
| 344 | {
|
---|
| 345 | assert(epm);
|
---|
| 346 |
|
---|
| 347 | if (!epm->present)
|
---|
| 348 | return ENOENT;
|
---|
| 349 |
|
---|
| 350 | const int rc = usb_pipe_unregister(&epm->pipe);
|
---|
| 351 | if (rc != EOK)
|
---|
| 352 | return rc;
|
---|
| 353 |
|
---|
| 354 | epm->present = false;
|
---|
| 355 | return EOK;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[45e49e6] | 358 | usb_address_t usb_device_get_address(const usb_device_t *usb_dev)
|
---|
| 359 | {
|
---|
| 360 | assert(usb_dev);
|
---|
| 361 | return usb_dev->depth;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | unsigned usb_device_get_depth(const usb_device_t *usb_dev)
|
---|
| 365 | {
|
---|
| 366 | assert(usb_dev);
|
---|
| 367 | return usb_dev->depth;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | usb_speed_t usb_device_get_speed(const usb_device_t *usb_dev)
|
---|
[129b821f] | 371 | {
|
---|
| 372 | assert(usb_dev);
|
---|
| 373 | return usb_dev->speed;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[45e49e6] | 376 | int usb_device_get_iface_number(const usb_device_t *usb_dev)
|
---|
[8e10ef4] | 377 | {
|
---|
| 378 | assert(usb_dev);
|
---|
| 379 | return usb_dev->interface_no;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[45e49e6] | 382 | devman_handle_t usb_device_get_devman_handle(const usb_device_t *usb_dev)
|
---|
[8e4219ab] | 383 | {
|
---|
| 384 | assert(usb_dev);
|
---|
| 385 | return usb_dev->handle;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[e2dfa86] | 388 | const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
|
---|
[945d66c] | 389 | {
|
---|
| 390 | assert(usb_dev);
|
---|
[e2dfa86] | 391 | return &usb_dev->descriptors;
|
---|
[945d66c] | 392 | }
|
---|
| 393 |
|
---|
[8e10ef4] | 394 | const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
|
---|
| 395 | usb_device_t *usb_dev)
|
---|
| 396 | {
|
---|
| 397 | assert(usb_dev);
|
---|
| 398 | return &usb_dev->alternate_interfaces;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[7363fc1] | 401 | /** Clean instance of a USB device.
|
---|
| 402 | *
|
---|
| 403 | * @param dev Device to be de-initialized.
|
---|
| 404 | *
|
---|
| 405 | * Does not free/destroy supplied pointer.
|
---|
| 406 | */
|
---|
| 407 | static void usb_device_fini(usb_device_t *usb_dev)
|
---|
| 408 | {
|
---|
| 409 | if (usb_dev) {
|
---|
| 410 | /* Destroy existing pipes. */
|
---|
| 411 | usb_device_destroy_pipes(usb_dev);
|
---|
| 412 | /* Ignore errors and hope for the best. */
|
---|
| 413 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
| 414 | usb_device_release_descriptors(usb_dev);
|
---|
| 415 | free(usb_dev->driver_data);
|
---|
| 416 | usb_dev->driver_data = NULL;
|
---|
[4c86c7c] | 417 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
| 418 | usb_dev->bus_session = NULL;
|
---|
[c1b1944] | 419 | }
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[7d9cd62] | 422 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 423 | *
|
---|
[7d9cd62] | 424 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 425 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 426 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 427 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 428 | * (in case error occurs).
|
---|
| 429 | * @return Error code.
|
---|
| 430 | */
|
---|
[fd9b3a67] | 431 | static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
[c280d7e] | 432 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
|
---|
[6ee6e6f] | 433 | {
|
---|
[7d9cd62] | 434 | assert(usb_dev != NULL);
|
---|
[7363fc1] | 435 | assert(errstr_ptr);
|
---|
[6ee6e6f] | 436 |
|
---|
[7fc260ff] | 437 | *errstr_ptr = NULL;
|
---|
| 438 |
|
---|
[7d9cd62] | 439 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 440 | usb_dev->driver_data = NULL;
|
---|
[e2dfa86] | 441 | usb_dev->descriptors.full_config = NULL;
|
---|
| 442 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7d9cd62] | 443 | usb_dev->pipes_count = 0;
|
---|
| 444 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 445 |
|
---|
[c280d7e] | 446 | usb_dev->bus_session = usb_dev_connect(usb_dev->handle);
|
---|
[c0fdc0e] | 447 |
|
---|
[c8c758d] | 448 | if (!usb_dev->bus_session) {
|
---|
| 449 | *errstr_ptr = "device bus session create";
|
---|
| 450 | return ENOMEM;
|
---|
[7fc260ff] | 451 | }
|
---|
| 452 |
|
---|
| 453 | /* This pipe was registered by the hub driver,
|
---|
| 454 | * during device initialization. */
|
---|
[9efad54] | 455 | int rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe, usb_dev->bus_session);
|
---|
[7fc260ff] | 456 | if (rc != EOK) {
|
---|
[7363fc1] | 457 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 458 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 459 | return rc;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 463 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 464 | if (rc != EOK) {
|
---|
[3f2af64] | 465 | *errstr_ptr = "descriptor retrieval";
|
---|
[7363fc1] | 466 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[6ee6e6f] | 467 | return rc;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[7fc260ff] | 470 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 471 | * We might either control one interface or an entire device,
|
---|
| 472 | * it makes no sense to speak about alternate interfaces when
|
---|
| 473 | * controlling a device. */
|
---|
[806a779] | 474 | usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[e2dfa86] | 475 | usb_dev->descriptors.full_config,
|
---|
| 476 | usb_dev->descriptors.full_config_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 477 |
|
---|
[7363fc1] | 478 | if (endpoints) {
|
---|
| 479 | /* Create and register other pipes than default control (EP 0)*/
|
---|
| 480 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
| 481 | if (rc != EOK) {
|
---|
| 482 | usb_device_fini(usb_dev);
|
---|
| 483 | *errstr_ptr = "pipes initialization";
|
---|
| 484 | return rc;
|
---|
| 485 | }
|
---|
[6ee6e6f] | 486 | }
|
---|
| 487 |
|
---|
| 488 | return EOK;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[c280d7e] | 491 | static int usb_device_get_info(async_sess_t *sess, usb_device_t *dev)
|
---|
[70452dd4] | 492 | {
|
---|
[c280d7e] | 493 | assert(dev);
|
---|
[816f5f4] | 494 |
|
---|
[8e4219ab] | 495 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 496 | if (!exch)
|
---|
| 497 | return EPARTY;
|
---|
[816f5f4] | 498 |
|
---|
[c280d7e] | 499 | usb_device_desc_t dev_desc;
|
---|
| 500 | const int ret = usb_get_my_description(exch, &dev_desc);
|
---|
| 501 |
|
---|
[8e4219ab] | 502 | if (ret == EOK) {
|
---|
[c280d7e] | 503 | dev->address = dev_desc.address;
|
---|
[45e49e6] | 504 | dev->depth = dev_desc.depth;
|
---|
[c280d7e] | 505 | dev->speed = dev_desc.speed;
|
---|
| 506 | dev->handle = dev_desc.handle;
|
---|
| 507 | dev->interface_no = dev_desc.iface;
|
---|
[8e4219ab] | 508 | }
|
---|
[816f5f4] | 509 |
|
---|
[8e4219ab] | 510 | async_exchange_end(exch);
|
---|
| 511 | return ret;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[fd9b3a67] | 514 | int usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
| 515 | const usb_endpoint_description_t **desc, const char **err)
|
---|
| 516 | {
|
---|
| 517 | assert(ddf_dev);
|
---|
| 518 | assert(err);
|
---|
[8e4219ab] | 519 |
|
---|
[2bdf92a5] | 520 | async_sess_t *sess = ddf_dev_parent_sess_get(ddf_dev);
|
---|
[086f8e3] | 521 | if (sess == NULL)
|
---|
| 522 | return ENOMEM;
|
---|
[8e4219ab] | 523 |
|
---|
[7363fc1] | 524 | usb_device_t *usb_dev =
|
---|
| 525 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
| 526 | if (usb_dev == NULL) {
|
---|
[fd9b3a67] | 527 | *err = "DDF data alloc";
|
---|
| 528 | return ENOMEM;
|
---|
| 529 | }
|
---|
[816f5f4] | 530 |
|
---|
[c280d7e] | 531 | const int ret = usb_device_get_info(sess, usb_dev);
|
---|
| 532 | if (ret != EOK)
|
---|
| 533 | return ret;
|
---|
| 534 |
|
---|
| 535 | return usb_device_init(usb_dev, ddf_dev, desc, err);
|
---|
[fd9b3a67] | 536 | }
|
---|
| 537 |
|
---|
| 538 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
| 539 | {
|
---|
| 540 | assert(ddf_dev);
|
---|
[7363fc1] | 541 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
| 542 | assert(usb_dev);
|
---|
| 543 | usb_device_fini(usb_dev);
|
---|
[fd9b3a67] | 544 | return;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
[7363fc1] | 547 | usb_device_t * usb_device_create(devman_handle_t handle)
|
---|
| 548 | {
|
---|
[c280d7e] | 549 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
| 550 | if (!usb_dev)
|
---|
| 551 | return NULL;
|
---|
[8e4219ab] | 552 |
|
---|
[b4b534ac] | 553 | async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
[c280d7e] | 554 | int ret = usb_device_get_info(sess, usb_dev);
|
---|
[bd41b192] | 555 | if (sess)
|
---|
| 556 | async_hangup(sess);
|
---|
[4172db4a] | 557 | if (ret != EOK) {
|
---|
| 558 | free(usb_dev);
|
---|
[8e4219ab] | 559 | return NULL;
|
---|
[4172db4a] | 560 | }
|
---|
[8e4219ab] | 561 |
|
---|
[7363fc1] | 562 | const char* dummy = NULL;
|
---|
[c280d7e] | 563 | ret = usb_device_init(usb_dev, NULL, NULL, &dummy);
|
---|
[7363fc1] | 564 | if (ret != EOK) {
|
---|
| 565 | free(usb_dev);
|
---|
| 566 | usb_dev = NULL;
|
---|
| 567 | }
|
---|
| 568 | return usb_dev;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
| 572 | {
|
---|
| 573 | if (usb_dev) {
|
---|
| 574 | usb_device_fini(usb_dev);
|
---|
| 575 | free(usb_dev);
|
---|
[70452dd4] | 576 | }
|
---|
[065064e6] | 577 | }
|
---|
| 578 |
|
---|
[f6b2a76b] | 579 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 580 | {
|
---|
| 581 | assert(usb_dev);
|
---|
[35bc430] | 582 | if (usb_dev->ddf_dev)
|
---|
| 583 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 584 | return NULL;
|
---|
[f6b2a76b] | 585 | }
|
---|
| 586 |
|
---|
[bb512b2d] | 587 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
| 588 | const char* name)
|
---|
| 589 | {
|
---|
| 590 | assert(usb_dev);
|
---|
| 591 | if (usb_dev->ddf_dev)
|
---|
| 592 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
| 593 | return NULL;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[0f4bff8] | 596 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
| 597 | {
|
---|
| 598 | assert(usb_dev);
|
---|
| 599 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 603 | {
|
---|
| 604 | async_exchange_end(exch);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[6e3c005] | 607 | /** Allocate driver specific data.
|
---|
| 608 | * @param usb_dev usb_device structure.
|
---|
| 609 | * @param size requested data size.
|
---|
| 610 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 611 | */
|
---|
[065064e6] | 612 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 613 | {
|
---|
| 614 | assert(usb_dev);
|
---|
| 615 | assert(usb_dev->driver_data == NULL);
|
---|
| 616 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 617 |
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[0f4bff8] | 620 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
| 621 | {
|
---|
| 622 | assert(usb_dev);
|
---|
| 623 | return usb_dev->driver_data;
|
---|
| 624 | }
|
---|
[4ca778b] | 625 |
|
---|
[6105fc0] | 626 | /**
|
---|
| 627 | * @}
|
---|
| 628 | */
|
---|