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