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