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