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