[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 | */
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[6105fc0] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device driver framework.
|
---|
| 34 | */
|
---|
[87619045] | 35 |
|
---|
| 36 | #include <usb_iface.h>
|
---|
| 37 | #include <usb/dev/device.h>
|
---|
[7d521e24] | 38 | #include <usb/dev/request.h>
|
---|
[6105fc0] | 39 | #include <usb/debug.h>
|
---|
[1a38701] | 40 | #include <usb/dev.h>
|
---|
[6105fc0] | 41 | #include <errno.h>
|
---|
[69334af] | 42 | #include <str_error.h>
|
---|
[6105fc0] | 43 | #include <assert.h>
|
---|
| 44 |
|
---|
[fd9b3a67] | 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 |
|
---|
[69334af] | 91 | /** Count number of pipes the driver expects.
|
---|
| 92 | *
|
---|
| 93 | * @param drv USB driver.
|
---|
| 94 | * @return Number of pipes (excluding default control pipe).
|
---|
| 95 | */
|
---|
[b06d35a] | 96 | static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
|
---|
[6105fc0] | 97 | {
|
---|
[ab27e01] | 98 | size_t count;
|
---|
[64e3dad] | 99 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
---|
[6105fc0] | 100 | return count;
|
---|
| 101 | }
|
---|
[a76b01b4] | 102 |
|
---|
[0b4e7ca] | 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 | *
|
---|
[3f2af64] | 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 | *
|
---|
[0b4e7ca] | 120 | * @param dev USB device.
|
---|
| 121 | * @param alternate_setting Alternate setting to choose.
|
---|
| 122 | * @param endpoints New endpoint descriptions.
|
---|
| 123 | * @return Error code.
|
---|
| 124 | */
|
---|
[f4138ac] | 125 | int usb_device_select_interface(usb_device_t *usb_dev,
|
---|
| 126 | uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
|
---|
[0b4e7ca] | 127 | {
|
---|
[f4138ac] | 128 | assert(usb_dev);
|
---|
| 129 |
|
---|
| 130 | if (usb_dev->interface_no < 0) {
|
---|
[0b4e7ca] | 131 | return EINVAL;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /* Change the interface itself. */
|
---|
[f4138ac] | 135 | int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
|
---|
| 136 | usb_dev->interface_no, alternate_setting);
|
---|
[0b4e7ca] | 137 | if (rc != EOK) {
|
---|
| 138 | return rc;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[f4138ac] | 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 |
|
---|
[0b4e7ca] | 147 | /* Create new pipes. */
|
---|
[b06d35a] | 148 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
[c1b1944] | 149 |
|
---|
| 150 | return rc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /** Retrieve basic descriptors from the device.
|
---|
| 154 | *
|
---|
[4fa0a384] | 155 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
[c1b1944] | 156 | * @param[out] descriptors Where to store the descriptors.
|
---|
| 157 | * @return Error code.
|
---|
| 158 | */
|
---|
[945d66c] | 159 | static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
[c1b1944] | 160 | {
|
---|
[945d66c] | 161 | assert(usb_dev);
|
---|
| 162 | assert(usb_dev->descriptors.configuration == NULL);
|
---|
[c1b1944] | 163 |
|
---|
[4fa0a384] | 164 | /* It is worth to start a long transfer. */
|
---|
[945d66c] | 165 | usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
|
---|
[4fa0a384] | 166 |
|
---|
[c1b1944] | 167 | /* Get the device descriptor. */
|
---|
[945d66c] | 168 | int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
| 169 | &usb_dev->descriptors.device);
|
---|
[c1b1944] | 170 | if (rc != EOK) {
|
---|
[4fa0a384] | 171 | goto leave;
|
---|
[c1b1944] | 172 | }
|
---|
| 173 |
|
---|
| 174 | /* Get the full configuration descriptor. */
|
---|
| 175 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
[945d66c] | 176 | &usb_dev->ctrl_pipe, 0,
|
---|
| 177 | (void **) &usb_dev->descriptors.configuration,
|
---|
| 178 | &usb_dev->descriptors.configuration_size);
|
---|
[c1b1944] | 179 |
|
---|
[4fa0a384] | 180 | leave:
|
---|
[945d66c] | 181 | usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
|
---|
[4fa0a384] | 182 |
|
---|
| 183 | return rc;
|
---|
[c1b1944] | 184 | }
|
---|
| 185 |
|
---|
[7fc260ff] | 186 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
| 187 | *
|
---|
| 188 | * @param[in] descriptors Where to store the descriptors.
|
---|
| 189 | */
|
---|
[945d66c] | 190 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
[7fc260ff] | 191 | {
|
---|
[945d66c] | 192 | assert(usb_dev);
|
---|
| 193 | free(usb_dev->descriptors.configuration);
|
---|
| 194 | usb_dev->descriptors.configuration = NULL;
|
---|
| 195 | usb_dev->descriptors.configuration_size = 0;
|
---|
[7fc260ff] | 196 | }
|
---|
| 197 |
|
---|
[c1b1944] | 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
|
---|
[ab27e01] | 214 | * (set to NULL if you wish to ignore the count).
|
---|
[c1b1944] | 215 | * @return Error code.
|
---|
| 216 | */
|
---|
[b06d35a] | 217 | int usb_device_create_pipes(usb_device_t *usb_dev,
|
---|
| 218 | const usb_endpoint_description_t **endpoints)
|
---|
[c1b1944] | 219 | {
|
---|
[b06d35a] | 220 | assert(usb_dev);
|
---|
| 221 | assert(usb_dev->descriptors.configuration);
|
---|
| 222 | assert(usb_dev->pipes == NULL);
|
---|
| 223 | assert(usb_dev->pipes_count == 0);
|
---|
[c1b1944] | 224 |
|
---|
[b06d35a] | 225 | size_t pipe_count = count_pipes(endpoints);
|
---|
[c1b1944] | 226 | if (pipe_count == 0) {
|
---|
| 227 | return EOK;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[b06d35a] | 230 | usb_endpoint_mapping_t *pipes =
|
---|
| 231 | calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
[c1b1944] | 232 | if (pipes == NULL) {
|
---|
| 233 | return ENOMEM;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[441633f] | 236 | /* Now initialize. */
|
---|
[b06d35a] | 237 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 238 | pipes[i].description = endpoints[i];
|
---|
[b06d35a] | 239 | pipes[i].interface_no = usb_dev->interface_no;
|
---|
| 240 | pipes[i].interface_setting =
|
---|
| 241 | usb_dev->alternate_interfaces.current;
|
---|
[c1b1944] | 242 | }
|
---|
| 243 |
|
---|
| 244 | /* Find the mapping from configuration descriptor. */
|
---|
[b06d35a] | 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);
|
---|
[c1b1944] | 248 | if (rc != EOK) {
|
---|
[441633f] | 249 | free(pipes);
|
---|
| 250 | return rc;
|
---|
[c1b1944] | 251 | }
|
---|
| 252 |
|
---|
[441633f] | 253 | /* Register created pipes. */
|
---|
[b06d35a] | 254 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 255 | if (pipes[i].present) {
|
---|
[b77931d] | 256 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
[bd575647] | 257 | pipes[i].descriptor->poll_interval);
|
---|
[c1b1944] | 258 | if (rc != EOK) {
|
---|
| 259 | goto rollback_unregister_endpoints;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[b06d35a] | 264 | usb_dev->pipes = pipes;
|
---|
| 265 | usb_dev->pipes_count = pipe_count;
|
---|
[c1b1944] | 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:
|
---|
[b06d35a] | 276 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 277 | if (pipes[i].present) {
|
---|
[bd575647] | 278 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | free(pipes);
|
---|
[0b4e7ca] | 283 | return rc;
|
---|
| 284 | }
|
---|
[159b91f4] | 285 |
|
---|
[c1b1944] | 286 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 287 | *
|
---|
[2dc5a9f] | 288 | * @param[in] usb_dev USB device.
|
---|
[c1b1944] | 289 | */
|
---|
[2dc5a9f] | 290 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
[c1b1944] | 291 | {
|
---|
[2dc5a9f] | 292 | assert(usb_dev);
|
---|
| 293 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
[c1b1944] | 294 | /* Destroy the pipes. */
|
---|
[2dc5a9f] | 295 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
[8a01a0b] | 296 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
---|
[2dc5a9f] | 297 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
| 298 | if (usb_dev->pipes[i].present)
|
---|
| 299 | usb_pipe_unregister(&usb_dev->pipes[i].pipe);
|
---|
[c1b1944] | 300 | }
|
---|
[2dc5a9f] | 301 | free(usb_dev->pipes);
|
---|
| 302 | usb_dev->pipes = NULL;
|
---|
| 303 | usb_dev->pipes_count = 0;
|
---|
[c1b1944] | 304 | }
|
---|
| 305 |
|
---|
[0f4bff8] | 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 |
|
---|
[a6a5b25] | 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 |
|
---|
[8e10ef4] | 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 |
|
---|
[945d66c] | 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 |
|
---|
[8e10ef4] | 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 |
|
---|
[35bc430] | 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 |
|
---|
[7363fc1] | 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 |
|
---|
[7d9cd62] | 412 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 413 | *
|
---|
[7d9cd62] | 414 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 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 | */
|
---|
[fd9b3a67] | 421 | static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
[7363fc1] | 422 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
|
---|
| 423 | devman_handle_t handle)
|
---|
[6ee6e6f] | 424 | {
|
---|
[7d9cd62] | 425 | assert(usb_dev != NULL);
|
---|
[6ee6e6f] | 426 | assert(ddf_dev != NULL);
|
---|
[7363fc1] | 427 | assert(errstr_ptr);
|
---|
[6ee6e6f] | 428 |
|
---|
[7fc260ff] | 429 | *errstr_ptr = NULL;
|
---|
| 430 |
|
---|
[7d9cd62] | 431 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 432 | usb_dev->driver_data = NULL;
|
---|
| 433 | usb_dev->descriptors.configuration = NULL;
|
---|
| 434 | usb_dev->pipes_count = 0;
|
---|
| 435 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 436 |
|
---|
[7363fc1] | 437 | if (ddf_dev)
|
---|
| 438 | usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
|
---|
| 439 | else
|
---|
| 440 | usb_dev->bus_session = usb_dev_connect(handle);
|
---|
| 441 |
|
---|
[c8c758d] | 442 | if (!usb_dev->bus_session) {
|
---|
| 443 | *errstr_ptr = "device bus session create";
|
---|
| 444 | return ENOMEM;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[c24c157d] | 447 | /* Get assigned params */
|
---|
| 448 | devman_handle_t hc_handle;
|
---|
| 449 | usb_address_t address;
|
---|
| 450 |
|
---|
[35bc430] | 451 | int rc = usb_dev_get_info(usb_dev,
|
---|
[c24c157d] | 452 | &hc_handle, &address, &usb_dev->interface_no);
|
---|
| 453 | if (rc != EOK) {
|
---|
[7363fc1] | 454 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[c24c157d] | 455 | *errstr_ptr = "device parameters retrieval";
|
---|
| 456 | return rc;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[c0fdc0e] | 459 | /* Initialize hc connection. */
|
---|
[c24c157d] | 460 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
---|
[c0fdc0e] | 461 |
|
---|
[3f2af64] | 462 | /* Initialize backing wire and control pipe. */
|
---|
[c24c157d] | 463 | rc = usb_device_connection_initialize(
|
---|
[bd575647] | 464 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
---|
[3f2af64] | 465 | if (rc != EOK) {
|
---|
[7363fc1] | 466 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 467 | *errstr_ptr = "device connection initialization";
|
---|
| 468 | return rc;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | /* This pipe was registered by the hub driver,
|
---|
| 472 | * during device initialization. */
|
---|
[c0fdc0e] | 473 | rc = usb_pipe_initialize_default_control(
|
---|
| 474 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
---|
[7fc260ff] | 475 | if (rc != EOK) {
|
---|
[7363fc1] | 476 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 477 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 478 | return rc;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[6e3c005] | 481 | /* Open hc connection for pipe registration. */
|
---|
[c0fdc0e] | 482 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
---|
| 483 | if (rc != EOK) {
|
---|
[7363fc1] | 484 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[c0fdc0e] | 485 | *errstr_ptr = "hc connection open";
|
---|
| 486 | return rc;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
[3f2af64] | 489 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 490 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 491 | if (rc != EOK) {
|
---|
[3f2af64] | 492 | *errstr_ptr = "descriptor retrieval";
|
---|
[c0fdc0e] | 493 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[7363fc1] | 494 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[6ee6e6f] | 495 | return rc;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[7fc260ff] | 498 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 499 | * We might either control one interface or an entire device,
|
---|
| 500 | * it makes no sense to speak about alternate interfaces when
|
---|
| 501 | * controlling a device. */
|
---|
[ab27e01] | 502 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[904dcc6] | 503 | usb_dev->descriptors.configuration,
|
---|
| 504 | usb_dev->descriptors.configuration_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 505 |
|
---|
[7363fc1] | 506 | if (endpoints) {
|
---|
| 507 | /* Create and register other pipes than default control (EP 0)*/
|
---|
| 508 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
| 509 | if (rc != EOK) {
|
---|
| 510 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
| 511 | usb_device_fini(usb_dev);
|
---|
| 512 | *errstr_ptr = "pipes initialization";
|
---|
| 513 | return rc;
|
---|
| 514 | }
|
---|
[6ee6e6f] | 515 | }
|
---|
| 516 |
|
---|
[c0fdc0e] | 517 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 518 | return EOK;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[fd9b3a67] | 521 | int usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
| 522 | const usb_endpoint_description_t **desc, const char **err)
|
---|
| 523 | {
|
---|
| 524 | assert(ddf_dev);
|
---|
| 525 | assert(err);
|
---|
[7363fc1] | 526 | usb_device_t *usb_dev =
|
---|
| 527 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
| 528 | if (usb_dev == NULL) {
|
---|
[fd9b3a67] | 529 | *err = "DDF data alloc";
|
---|
| 530 | return ENOMEM;
|
---|
| 531 | }
|
---|
[7363fc1] | 532 | return usb_device_init(usb_dev, ddf_dev, desc, err, 0);
|
---|
[fd9b3a67] | 533 | }
|
---|
| 534 |
|
---|
| 535 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
| 536 | {
|
---|
| 537 | assert(ddf_dev);
|
---|
[7363fc1] | 538 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
| 539 | assert(usb_dev);
|
---|
| 540 | usb_device_fini(usb_dev);
|
---|
[fd9b3a67] | 541 | return;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[7363fc1] | 544 | usb_device_t * usb_device_create(devman_handle_t handle)
|
---|
| 545 | {
|
---|
| 546 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
| 547 | if (!usb_dev)
|
---|
| 548 | return NULL;
|
---|
| 549 | const char* dummy = NULL;
|
---|
| 550 | const int ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle);
|
---|
| 551 | if (ret != EOK) {
|
---|
| 552 | free(usb_dev);
|
---|
| 553 | usb_dev = NULL;
|
---|
| 554 | }
|
---|
| 555 | return usb_dev;
|
---|
| 556 |
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
| 560 | {
|
---|
| 561 | if (usb_dev) {
|
---|
| 562 | usb_device_fini(usb_dev);
|
---|
| 563 | free(usb_dev);
|
---|
| 564 | }
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[f6b2a76b] | 567 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 568 | {
|
---|
| 569 | assert(usb_dev);
|
---|
[35bc430] | 570 | if (usb_dev->ddf_dev)
|
---|
| 571 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 572 | return NULL;
|
---|
[f6b2a76b] | 573 | }
|
---|
| 574 |
|
---|
[bb512b2d] | 575 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
| 576 | const char* name)
|
---|
| 577 | {
|
---|
| 578 | assert(usb_dev);
|
---|
| 579 | if (usb_dev->ddf_dev)
|
---|
| 580 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
| 581 | return NULL;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[0f4bff8] | 584 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
| 585 | {
|
---|
| 586 | assert(usb_dev);
|
---|
| 587 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 591 | {
|
---|
| 592 | async_exchange_end(exch);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[6e3c005] | 595 | /** Allocate driver specific data.
|
---|
| 596 | * @param usb_dev usb_device structure.
|
---|
| 597 | * @param size requested data size.
|
---|
| 598 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 599 | */
|
---|
[065064e6] | 600 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 601 | {
|
---|
| 602 | assert(usb_dev);
|
---|
| 603 | assert(usb_dev->driver_data == NULL);
|
---|
| 604 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 605 |
|
---|
| 606 | }
|
---|
| 607 |
|
---|
[0f4bff8] | 608 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
| 609 | {
|
---|
| 610 | assert(usb_dev);
|
---|
| 611 | return usb_dev->driver_data;
|
---|
| 612 | }
|
---|
[6105fc0] | 613 | /**
|
---|
| 614 | * @}
|
---|
| 615 | */
|
---|