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