[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,
|
---|
[e2dfa86] | 169 | (void**)&usb_dev->descriptors.full_config,
|
---|
| 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,
|
---|
| 239 | usb_dev->descriptors.full_config_size, &usb_dev->wire);
|
---|
[c1b1944] | 240 | if (rc != EOK) {
|
---|
[441633f] | 241 | free(pipes);
|
---|
| 242 | return rc;
|
---|
[c1b1944] | 243 | }
|
---|
| 244 |
|
---|
[441633f] | 245 | /* Register created pipes. */
|
---|
[b06d35a] | 246 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 247 | if (pipes[i].present) {
|
---|
[b77931d] | 248 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
[bd575647] | 249 | pipes[i].descriptor->poll_interval);
|
---|
[c1b1944] | 250 | if (rc != EOK) {
|
---|
| 251 | goto rollback_unregister_endpoints;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[b06d35a] | 256 | usb_dev->pipes = pipes;
|
---|
| 257 | usb_dev->pipes_count = pipe_count;
|
---|
[c1b1944] | 258 |
|
---|
| 259 | return EOK;
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * Jump here if something went wrong after endpoints have
|
---|
| 263 | * been registered.
|
---|
| 264 | * This is also the target when the registration of
|
---|
| 265 | * endpoints fails.
|
---|
| 266 | */
|
---|
| 267 | rollback_unregister_endpoints:
|
---|
[b06d35a] | 268 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
[c1b1944] | 269 | if (pipes[i].present) {
|
---|
[bd575647] | 270 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
[c1b1944] | 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | free(pipes);
|
---|
[0b4e7ca] | 275 | return rc;
|
---|
| 276 | }
|
---|
[159b91f4] | 277 |
|
---|
[c1b1944] | 278 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
| 279 | *
|
---|
[2dc5a9f] | 280 | * @param[in] usb_dev USB device.
|
---|
[c1b1944] | 281 | */
|
---|
[2dc5a9f] | 282 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
[c1b1944] | 283 | {
|
---|
[2dc5a9f] | 284 | assert(usb_dev);
|
---|
| 285 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
[c1b1944] | 286 | /* Destroy the pipes. */
|
---|
[2dc5a9f] | 287 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
[8a01a0b] | 288 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
---|
[2dc5a9f] | 289 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
| 290 | if (usb_dev->pipes[i].present)
|
---|
| 291 | usb_pipe_unregister(&usb_dev->pipes[i].pipe);
|
---|
[c1b1944] | 292 | }
|
---|
[2dc5a9f] | 293 | free(usb_dev->pipes);
|
---|
| 294 | usb_dev->pipes = NULL;
|
---|
| 295 | usb_dev->pipes_count = 0;
|
---|
[c1b1944] | 296 | }
|
---|
| 297 |
|
---|
[0f4bff8] | 298 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
| 299 | {
|
---|
| 300 | assert(usb_dev);
|
---|
| 301 | return &usb_dev->ctrl_pipe;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[a6a5b25] | 304 | usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
|
---|
| 305 | const usb_endpoint_description_t *desc)
|
---|
| 306 | {
|
---|
| 307 | assert(usb_dev);
|
---|
| 308 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
| 309 | if (usb_dev->pipes[i].description == desc)
|
---|
| 310 | return &usb_dev->pipes[i];
|
---|
| 311 | }
|
---|
| 312 | return NULL;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | usb_endpoint_mapping_t * usb_device_get_mapped_ep(
|
---|
| 316 | usb_device_t *usb_dev, usb_endpoint_t ep)
|
---|
| 317 | {
|
---|
| 318 | assert(usb_dev);
|
---|
| 319 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
| 320 | if (usb_dev->pipes[i].pipe.endpoint_no == ep)
|
---|
| 321 | return &usb_dev->pipes[i];
|
---|
| 322 | }
|
---|
| 323 | return NULL;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[8e10ef4] | 326 | int usb_device_get_iface_number(usb_device_t *usb_dev)
|
---|
| 327 | {
|
---|
| 328 | assert(usb_dev);
|
---|
| 329 | return usb_dev->interface_no;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[e2dfa86] | 332 | const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
|
---|
[945d66c] | 333 | {
|
---|
| 334 | assert(usb_dev);
|
---|
[e2dfa86] | 335 | return &usb_dev->descriptors;
|
---|
[945d66c] | 336 | }
|
---|
| 337 |
|
---|
[8e10ef4] | 338 | const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
|
---|
| 339 | usb_device_t *usb_dev)
|
---|
| 340 | {
|
---|
| 341 | assert(usb_dev);
|
---|
| 342 | return &usb_dev->alternate_interfaces;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[35bc430] | 345 | static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
|
---|
| 346 | usb_address_t *address, int *iface_no)
|
---|
| 347 | {
|
---|
| 348 | assert(usb_dev);
|
---|
| 349 |
|
---|
| 350 | int ret = EOK;
|
---|
| 351 | async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
|
---|
| 352 | if (!exch)
|
---|
| 353 | ret = ENOMEM;
|
---|
| 354 |
|
---|
| 355 | if (ret == EOK && address)
|
---|
| 356 | ret = usb_get_my_address(exch, address);
|
---|
| 357 |
|
---|
| 358 | if (ret == EOK && handle)
|
---|
| 359 | ret = usb_get_hc_handle(exch, handle);
|
---|
| 360 |
|
---|
| 361 | if (ret == EOK && iface_no) {
|
---|
| 362 | ret = usb_get_my_interface(exch, iface_no);
|
---|
| 363 | if (ret == ENOTSUP) {
|
---|
| 364 | ret = EOK;
|
---|
| 365 | *iface_no = -1;
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | async_exchange_end(exch);
|
---|
| 370 | return ret;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
[7363fc1] | 373 | /** Clean instance of a USB device.
|
---|
| 374 | *
|
---|
| 375 | * @param dev Device to be de-initialized.
|
---|
| 376 | *
|
---|
| 377 | * Does not free/destroy supplied pointer.
|
---|
| 378 | */
|
---|
| 379 | static void usb_device_fini(usb_device_t *usb_dev)
|
---|
| 380 | {
|
---|
| 381 | if (usb_dev) {
|
---|
| 382 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
| 383 | /* Destroy existing pipes. */
|
---|
| 384 | usb_device_destroy_pipes(usb_dev);
|
---|
| 385 | /* Ignore errors and hope for the best. */
|
---|
| 386 | usb_hc_connection_deinitialize(&usb_dev->hc_conn);
|
---|
| 387 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
| 388 | usb_device_release_descriptors(usb_dev);
|
---|
| 389 | free(usb_dev->driver_data);
|
---|
| 390 | usb_dev->driver_data = NULL;
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[7d9cd62] | 394 | /** Initialize new instance of USB device.
|
---|
[6ee6e6f] | 395 | *
|
---|
[7d9cd62] | 396 | * @param[in] usb_dev Pointer to the new device.
|
---|
[6ee6e6f] | 397 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
| 398 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
| 399 | * @param[out] errstr_ptr Where to store description of context
|
---|
| 400 | * (in case error occurs).
|
---|
| 401 | * @return Error code.
|
---|
| 402 | */
|
---|
[fd9b3a67] | 403 | static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
[7363fc1] | 404 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
|
---|
| 405 | devman_handle_t handle)
|
---|
[6ee6e6f] | 406 | {
|
---|
[7d9cd62] | 407 | assert(usb_dev != NULL);
|
---|
[7363fc1] | 408 | assert(errstr_ptr);
|
---|
[6ee6e6f] | 409 |
|
---|
[7fc260ff] | 410 | *errstr_ptr = NULL;
|
---|
| 411 |
|
---|
[7d9cd62] | 412 | usb_dev->ddf_dev = ddf_dev;
|
---|
| 413 | usb_dev->driver_data = NULL;
|
---|
[e2dfa86] | 414 | usb_dev->descriptors.full_config = NULL;
|
---|
| 415 | usb_dev->descriptors.full_config_size = 0;
|
---|
[7d9cd62] | 416 | usb_dev->pipes_count = 0;
|
---|
| 417 | usb_dev->pipes = NULL;
|
---|
[6ee6e6f] | 418 |
|
---|
[7363fc1] | 419 | if (ddf_dev)
|
---|
| 420 | usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
|
---|
| 421 | else
|
---|
| 422 | usb_dev->bus_session = usb_dev_connect(handle);
|
---|
| 423 |
|
---|
[c8c758d] | 424 | if (!usb_dev->bus_session) {
|
---|
| 425 | *errstr_ptr = "device bus session create";
|
---|
| 426 | return ENOMEM;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[c24c157d] | 429 | /* Get assigned params */
|
---|
| 430 | devman_handle_t hc_handle;
|
---|
| 431 | usb_address_t address;
|
---|
| 432 |
|
---|
[35bc430] | 433 | int rc = usb_dev_get_info(usb_dev,
|
---|
[c24c157d] | 434 | &hc_handle, &address, &usb_dev->interface_no);
|
---|
| 435 | if (rc != EOK) {
|
---|
[7363fc1] | 436 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[c24c157d] | 437 | *errstr_ptr = "device parameters retrieval";
|
---|
| 438 | return rc;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[c0fdc0e] | 441 | /* Initialize hc connection. */
|
---|
[c24c157d] | 442 | usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
|
---|
[c0fdc0e] | 443 |
|
---|
[3f2af64] | 444 | /* Initialize backing wire and control pipe. */
|
---|
[c24c157d] | 445 | rc = usb_device_connection_initialize(
|
---|
[bd575647] | 446 | &usb_dev->wire, &usb_dev->hc_conn, address);
|
---|
[3f2af64] | 447 | if (rc != EOK) {
|
---|
[7363fc1] | 448 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 449 | *errstr_ptr = "device connection initialization";
|
---|
| 450 | return rc;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /* This pipe was registered by the hub driver,
|
---|
| 454 | * during device initialization. */
|
---|
[c0fdc0e] | 455 | rc = usb_pipe_initialize_default_control(
|
---|
| 456 | &usb_dev->ctrl_pipe, &usb_dev->wire);
|
---|
[7fc260ff] | 457 | if (rc != EOK) {
|
---|
[7363fc1] | 458 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[7fc260ff] | 459 | *errstr_ptr = "default control pipe initialization";
|
---|
[3f2af64] | 460 | return rc;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[6e3c005] | 463 | /* Open hc connection for pipe registration. */
|
---|
[c0fdc0e] | 464 | rc = usb_hc_connection_open(&usb_dev->hc_conn);
|
---|
| 465 | if (rc != EOK) {
|
---|
[7363fc1] | 466 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[c0fdc0e] | 467 | *errstr_ptr = "hc connection open";
|
---|
| 468 | return rc;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
[3f2af64] | 471 | /* Retrieve standard descriptors. */
|
---|
[945d66c] | 472 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
[6ee6e6f] | 473 | if (rc != EOK) {
|
---|
[3f2af64] | 474 | *errstr_ptr = "descriptor retrieval";
|
---|
[c0fdc0e] | 475 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[7363fc1] | 476 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
[6ee6e6f] | 477 | return rc;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[7fc260ff] | 480 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
| 481 | * We might either control one interface or an entire device,
|
---|
| 482 | * it makes no sense to speak about alternate interfaces when
|
---|
| 483 | * controlling a device. */
|
---|
[ab27e01] | 484 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
[e2dfa86] | 485 | usb_dev->descriptors.full_config,
|
---|
| 486 | usb_dev->descriptors.full_config_size, usb_dev->interface_no);
|
---|
[6ee6e6f] | 487 |
|
---|
[7363fc1] | 488 | if (endpoints) {
|
---|
| 489 | /* Create and register other pipes than default control (EP 0)*/
|
---|
| 490 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
| 491 | if (rc != EOK) {
|
---|
| 492 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
| 493 | usb_device_fini(usb_dev);
|
---|
| 494 | *errstr_ptr = "pipes initialization";
|
---|
| 495 | return rc;
|
---|
| 496 | }
|
---|
[6ee6e6f] | 497 | }
|
---|
| 498 |
|
---|
[c0fdc0e] | 499 | usb_hc_connection_close(&usb_dev->hc_conn);
|
---|
[6ee6e6f] | 500 | return EOK;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[fd9b3a67] | 503 | int usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
| 504 | const usb_endpoint_description_t **desc, const char **err)
|
---|
| 505 | {
|
---|
| 506 | assert(ddf_dev);
|
---|
| 507 | assert(err);
|
---|
[7363fc1] | 508 | usb_device_t *usb_dev =
|
---|
| 509 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
| 510 | if (usb_dev == NULL) {
|
---|
[fd9b3a67] | 511 | *err = "DDF data alloc";
|
---|
| 512 | return ENOMEM;
|
---|
| 513 | }
|
---|
[7363fc1] | 514 | return usb_device_init(usb_dev, ddf_dev, desc, err, 0);
|
---|
[fd9b3a67] | 515 | }
|
---|
| 516 |
|
---|
| 517 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
| 518 | {
|
---|
| 519 | assert(ddf_dev);
|
---|
[7363fc1] | 520 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
| 521 | assert(usb_dev);
|
---|
| 522 | usb_device_fini(usb_dev);
|
---|
[fd9b3a67] | 523 | return;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[7363fc1] | 526 | usb_device_t * usb_device_create(devman_handle_t handle)
|
---|
| 527 | {
|
---|
| 528 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
| 529 | if (!usb_dev)
|
---|
| 530 | return NULL;
|
---|
| 531 | const char* dummy = NULL;
|
---|
| 532 | const int ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle);
|
---|
| 533 | if (ret != EOK) {
|
---|
| 534 | free(usb_dev);
|
---|
| 535 | usb_dev = NULL;
|
---|
| 536 | }
|
---|
| 537 | return usb_dev;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
| 541 | {
|
---|
| 542 | if (usb_dev) {
|
---|
| 543 | usb_device_fini(usb_dev);
|
---|
| 544 | free(usb_dev);
|
---|
| 545 | }
|
---|
| 546 | }
|
---|
| 547 |
|
---|
[f6b2a76b] | 548 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
| 549 | {
|
---|
| 550 | assert(usb_dev);
|
---|
[35bc430] | 551 | if (usb_dev->ddf_dev)
|
---|
| 552 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
| 553 | return NULL;
|
---|
[f6b2a76b] | 554 | }
|
---|
| 555 |
|
---|
[bb512b2d] | 556 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
| 557 | const char* name)
|
---|
| 558 | {
|
---|
| 559 | assert(usb_dev);
|
---|
| 560 | if (usb_dev->ddf_dev)
|
---|
| 561 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
| 562 | return NULL;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
[0f4bff8] | 565 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
| 566 | {
|
---|
| 567 | assert(usb_dev);
|
---|
| 568 | return async_exchange_begin(usb_dev->bus_session);
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
| 572 | {
|
---|
| 573 | async_exchange_end(exch);
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[6e3c005] | 576 | /** Allocate driver specific data.
|
---|
| 577 | * @param usb_dev usb_device structure.
|
---|
| 578 | * @param size requested data size.
|
---|
| 579 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
| 580 | */
|
---|
[065064e6] | 581 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
| 582 | {
|
---|
| 583 | assert(usb_dev);
|
---|
| 584 | assert(usb_dev->driver_data == NULL);
|
---|
| 585 | return usb_dev->driver_data = calloc(1, size);
|
---|
[70452dd4] | 586 |
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[0f4bff8] | 589 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
| 590 | {
|
---|
| 591 | assert(usb_dev);
|
---|
| 592 | return usb_dev->driver_data;
|
---|
| 593 | }
|
---|
[6105fc0] | 594 | /**
|
---|
| 595 | * @}
|
---|
| 596 | */
|
---|