[6105fc0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libusb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device driver framework.
|
---|
| 34 | */
|
---|
| 35 | #include <usb/devdrv.h>
|
---|
| 36 | #include <usb/request.h>
|
---|
| 37 | #include <usb/debug.h>
|
---|
[7526e3d9] | 38 | #include <usb/dp.h>
|
---|
[6105fc0] | 39 | #include <errno.h>
|
---|
[69334af] | 40 | #include <str_error.h>
|
---|
[6105fc0] | 41 | #include <assert.h>
|
---|
| 42 |
|
---|
| 43 | static int generic_add_device(ddf_dev_t *);
|
---|
| 44 |
|
---|
| 45 | static driver_ops_t generic_driver_ops = {
|
---|
| 46 | .add_device = generic_add_device
|
---|
| 47 | };
|
---|
| 48 | static driver_t generic_driver = {
|
---|
| 49 | .driver_ops = &generic_driver_ops
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | static usb_driver_t *driver = NULL;
|
---|
| 53 |
|
---|
[69334af] | 54 |
|
---|
| 55 | /** Main routine of USB device driver.
|
---|
| 56 | *
|
---|
| 57 | * Under normal conditions, this function never returns.
|
---|
| 58 | *
|
---|
| 59 | * @param drv USB device driver structure.
|
---|
| 60 | * @return Task exit status.
|
---|
| 61 | */
|
---|
[6105fc0] | 62 | int usb_driver_main(usb_driver_t *drv)
|
---|
| 63 | {
|
---|
[69334af] | 64 | assert(drv != NULL);
|
---|
| 65 |
|
---|
[6105fc0] | 66 | /* Prepare the generic driver. */
|
---|
| 67 | generic_driver.name = drv->name;
|
---|
| 68 |
|
---|
| 69 | driver = drv;
|
---|
| 70 |
|
---|
| 71 | return ddf_driver_main(&generic_driver);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[69334af] | 74 | /** Log out of memory error on given device.
|
---|
| 75 | *
|
---|
| 76 | * @param dev Device causing the trouble.
|
---|
| 77 | */
|
---|
| 78 | static void usb_log_oom(ddf_dev_t *dev)
|
---|
| 79 | {
|
---|
| 80 | usb_log_error("Out of memory when adding device `%s'.\n",
|
---|
| 81 | dev->name);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Count number of pipes the driver expects.
|
---|
| 85 | *
|
---|
| 86 | * @param drv USB driver.
|
---|
| 87 | * @return Number of pipes (excluding default control pipe).
|
---|
| 88 | */
|
---|
[6105fc0] | 89 | static size_t count_other_pipes(usb_driver_t *drv)
|
---|
| 90 | {
|
---|
| 91 | size_t count = 0;
|
---|
| 92 | if (drv->endpoints == NULL) {
|
---|
| 93 | return 0;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | while (drv->endpoints[count] != NULL) {
|
---|
| 97 | count++;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return count;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[69334af] | 103 | /** Initialize endpoint pipes, excluding default control one.
|
---|
| 104 | *
|
---|
| 105 | * @param drv The device driver.
|
---|
| 106 | * @param dev Device to be initialized.
|
---|
| 107 | * @return Error code.
|
---|
| 108 | */
|
---|
[6105fc0] | 109 | static int initialize_other_pipes(usb_driver_t *drv, usb_device_t *dev)
|
---|
| 110 | {
|
---|
[69334af] | 111 | int rc;
|
---|
[6105fc0] | 112 |
|
---|
| 113 | size_t pipe_count = count_other_pipes(drv);
|
---|
| 114 | dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
|
---|
[69334af] | 115 | if (dev->pipes == NULL) {
|
---|
| 116 | usb_log_oom(dev->ddf_dev);
|
---|
| 117 | return ENOMEM;
|
---|
| 118 | }
|
---|
[6105fc0] | 119 |
|
---|
| 120 | size_t i;
|
---|
[69334af] | 121 |
|
---|
| 122 | /* Initialize to NULL first for rollback purposes. */
|
---|
| 123 | for (i = 0; i < pipe_count; i++) {
|
---|
| 124 | dev->pipes[i].pipe = NULL;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[6105fc0] | 127 | for (i = 0; i < pipe_count; i++) {
|
---|
[a372663] | 128 | dev->pipes[i].pipe = malloc(sizeof(usb_pipe_t));
|
---|
[69334af] | 129 | if (dev->pipes[i].pipe == NULL) {
|
---|
| 130 | usb_log_oom(dev->ddf_dev);
|
---|
| 131 | rc = ENOMEM;
|
---|
| 132 | goto rollback;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[6105fc0] | 135 | dev->pipes[i].description = drv->endpoints[i];
|
---|
[d71691d] | 136 | dev->pipes[i].interface_no = dev->interface_no;
|
---|
[6105fc0] | 137 | }
|
---|
| 138 |
|
---|
[e484f3b] | 139 | rc = usb_pipe_initialize_from_configuration(dev->pipes, pipe_count,
|
---|
| 140 | dev->descriptors.configuration, dev->descriptors.configuration_size,
|
---|
| 141 | &dev->wire);
|
---|
[69334af] | 142 | if (rc != EOK) {
|
---|
| 143 | usb_log_error("Failed initializing USB endpoints: %s.\n",
|
---|
| 144 | str_error(rc));
|
---|
| 145 | goto rollback;
|
---|
| 146 | }
|
---|
[6105fc0] | 147 |
|
---|
[5fd22d8] | 148 | /* Register the endpoints. */
|
---|
| 149 | usb_hc_connection_t hc_conn;
|
---|
| 150 | rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev);
|
---|
| 151 | if (rc != EOK) {
|
---|
| 152 | usb_log_error(
|
---|
| 153 | "Failed initializing connection to host controller: %s.\n",
|
---|
| 154 | str_error(rc));
|
---|
| 155 | goto rollback;
|
---|
| 156 | }
|
---|
| 157 | rc = usb_hc_connection_open(&hc_conn);
|
---|
| 158 | if (rc != EOK) {
|
---|
| 159 | usb_log_error("Failed to connect to host controller: %s.\n",
|
---|
| 160 | str_error(rc));
|
---|
| 161 | goto rollback;
|
---|
| 162 | }
|
---|
| 163 | for (i = 0; i < pipe_count; i++) {
|
---|
| 164 | if (dev->pipes[i].present) {
|
---|
[3954a63b] | 165 | rc = usb_pipe_register(dev->pipes[i].pipe,
|
---|
[5fd22d8] | 166 | dev->pipes[i].descriptor->poll_interval,
|
---|
| 167 | &hc_conn);
|
---|
| 168 | /* Ignore error when operation not supported by HC. */
|
---|
| 169 | if ((rc != EOK) && (rc != ENOTSUP)) {
|
---|
| 170 | /* FIXME: what shall we do? */
|
---|
| 171 | dev->pipes[i].present = false;
|
---|
| 172 | free(dev->pipes[i].pipe);
|
---|
| 173 | dev->pipes[i].pipe = NULL;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | /* Ignoring errors here. */
|
---|
| 178 | usb_hc_connection_close(&hc_conn);
|
---|
| 179 |
|
---|
[6105fc0] | 180 | return EOK;
|
---|
[69334af] | 181 |
|
---|
| 182 | rollback:
|
---|
| 183 | for (i = 0; i < pipe_count; i++) {
|
---|
| 184 | if (dev->pipes[i].pipe != NULL) {
|
---|
| 185 | free(dev->pipes[i].pipe);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | free(dev->pipes);
|
---|
| 189 |
|
---|
| 190 | return rc;
|
---|
[6105fc0] | 191 | }
|
---|
| 192 |
|
---|
[69334af] | 193 | /** Initialize all endpoint pipes.
|
---|
| 194 | *
|
---|
| 195 | * @param drv The driver.
|
---|
| 196 | * @param dev The device to be initialized.
|
---|
| 197 | * @return Error code.
|
---|
| 198 | */
|
---|
[09daa8b] | 199 | static int initialize_pipes(usb_device_t *dev)
|
---|
[69334af] | 200 | {
|
---|
| 201 | int rc;
|
---|
| 202 |
|
---|
| 203 | rc = usb_device_connection_initialize_from_device(&dev->wire,
|
---|
| 204 | dev->ddf_dev);
|
---|
| 205 | if (rc != EOK) {
|
---|
| 206 | usb_log_error(
|
---|
| 207 | "Failed initializing connection on device `%s'. %s.\n",
|
---|
| 208 | dev->ddf_dev->name, str_error(rc));
|
---|
| 209 | return rc;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[3954a63b] | 212 | rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
|
---|
[69334af] | 213 | &dev->wire);
|
---|
| 214 | if (rc != EOK) {
|
---|
| 215 | usb_log_error("Failed to initialize default control pipe " \
|
---|
| 216 | "on device `%s': %s.\n",
|
---|
| 217 | dev->ddf_dev->name, str_error(rc));
|
---|
| 218 | return rc;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[3954a63b] | 221 | rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
|
---|
[206f71a] | 222 | if (rc != EOK) {
|
---|
| 223 | usb_log_error(
|
---|
| 224 | "Probing default control pipe on device `%s' failed: %s.\n",
|
---|
| 225 | dev->ddf_dev->name, str_error(rc));
|
---|
| 226 | return rc;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[bb18a59] | 229 | /* Get our interface. */
|
---|
| 230 | dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
|
---|
| 231 |
|
---|
[69334af] | 232 | /*
|
---|
[e484f3b] | 233 | * For further actions, we need open session on default control pipe.
|
---|
[69334af] | 234 | */
|
---|
[3954a63b] | 235 | rc = usb_pipe_start_session(&dev->ctrl_pipe);
|
---|
[69334af] | 236 | if (rc != EOK) {
|
---|
| 237 | usb_log_error("Failed to start an IPC session: %s.\n",
|
---|
| 238 | str_error(rc));
|
---|
| 239 | return rc;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[e484f3b] | 242 | /* Get the device descriptor. */
|
---|
| 243 | rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
|
---|
| 244 | &dev->descriptors.device);
|
---|
| 245 | if (rc != EOK) {
|
---|
| 246 | usb_log_error("Failed to retrieve device descriptor: %s.\n",
|
---|
| 247 | str_error(rc));
|
---|
| 248 | return rc;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /* Get the full configuration descriptor. */
|
---|
| 252 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
| 253 | &dev->ctrl_pipe, 0, (void **) &dev->descriptors.configuration,
|
---|
| 254 | &dev->descriptors.configuration_size);
|
---|
| 255 | if (rc != EOK) {
|
---|
| 256 | usb_log_error("Failed retrieving configuration descriptor: %s.\n",
|
---|
| 257 | dev->ddf_dev->name, str_error(rc));
|
---|
| 258 | return rc;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[69334af] | 261 | if (driver->endpoints != NULL) {
|
---|
| 262 | rc = initialize_other_pipes(driver, dev);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /* No checking here. */
|
---|
[3954a63b] | 266 | usb_pipe_end_session(&dev->ctrl_pipe);
|
---|
[69334af] | 267 |
|
---|
[e484f3b] | 268 | /* Rollback actions. */
|
---|
| 269 | if (rc != EOK) {
|
---|
| 270 | if (dev->descriptors.configuration != NULL) {
|
---|
| 271 | free(dev->descriptors.configuration);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[69334af] | 275 | return rc;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[7526e3d9] | 278 | /** Count number of alternate settings of a interface.
|
---|
| 279 | *
|
---|
| 280 | * @param config_descr Full configuration descriptor.
|
---|
| 281 | * @param config_descr_size Size of @p config_descr in bytes.
|
---|
| 282 | * @param interface_no Interface number.
|
---|
| 283 | * @return Number of alternate interfaces for @p interface_no interface.
|
---|
| 284 | */
|
---|
| 285 | static size_t count_alternate_interfaces(uint8_t *config_descr,
|
---|
| 286 | size_t config_descr_size, int interface_no)
|
---|
| 287 | {
|
---|
| 288 | assert(config_descr != NULL);
|
---|
| 289 | usb_dp_parser_t dp_parser = {
|
---|
| 290 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 291 | };
|
---|
| 292 | usb_dp_parser_data_t dp_data = {
|
---|
| 293 | .data = config_descr,
|
---|
| 294 | .size = config_descr_size,
|
---|
| 295 | .arg = NULL
|
---|
| 296 | };
|
---|
| 297 |
|
---|
| 298 | size_t alternate_count = 0;
|
---|
| 299 |
|
---|
| 300 | uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
| 301 | &dp_data, config_descr);
|
---|
| 302 | while (iface_ptr != NULL) {
|
---|
| 303 | usb_standard_interface_descriptor_t *iface
|
---|
| 304 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 305 | if (iface->descriptor_type == USB_DESCTYPE_INTERFACE) {
|
---|
| 306 | if (iface->interface_number == interface_no) {
|
---|
| 307 | alternate_count++;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 311 | config_descr, iface_ptr);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | return alternate_count;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /** Initialize structures related to alternate interfaces.
|
---|
| 318 | *
|
---|
| 319 | * @param dev Device where alternate settings shall be initialized.
|
---|
| 320 | * @return Error code.
|
---|
| 321 | */
|
---|
| 322 | static int initialize_alternate_interfaces(usb_device_t *dev)
|
---|
| 323 | {
|
---|
| 324 | if (dev->interface_no < 0) {
|
---|
| 325 | dev->alternate_interfaces = NULL;
|
---|
| 326 | return EOK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | usb_alternate_interfaces_t *alternates
|
---|
| 330 | = malloc(sizeof(usb_alternate_interfaces_t));
|
---|
| 331 |
|
---|
| 332 | if (alternates == NULL) {
|
---|
| 333 | return ENOMEM;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | alternates->alternative_count
|
---|
| 337 | = count_alternate_interfaces(dev->descriptors.configuration,
|
---|
| 338 | dev->descriptors.configuration_size, dev->interface_no);
|
---|
| 339 |
|
---|
| 340 | if (alternates->alternative_count == 0) {
|
---|
| 341 | free(alternates);
|
---|
| 342 | return ENOENT;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | alternates->alternatives = malloc(alternates->alternative_count
|
---|
| 346 | * sizeof(usb_alternate_interface_descriptors_t));
|
---|
| 347 | if (alternates->alternatives == NULL) {
|
---|
| 348 | free(alternates);
|
---|
| 349 | return ENOMEM;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | alternates->current = 0;
|
---|
| 353 |
|
---|
| 354 | usb_dp_parser_t dp_parser = {
|
---|
| 355 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 356 | };
|
---|
| 357 | usb_dp_parser_data_t dp_data = {
|
---|
| 358 | .data = dev->descriptors.configuration,
|
---|
| 359 | .size = dev->descriptors.configuration_size,
|
---|
| 360 | .arg = NULL
|
---|
| 361 | };
|
---|
| 362 |
|
---|
| 363 | usb_alternate_interface_descriptors_t *cur_alt_iface
|
---|
| 364 | = &alternates->alternatives[0];
|
---|
| 365 |
|
---|
| 366 | uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
| 367 | &dp_data, dp_data.data);
|
---|
| 368 | while (iface_ptr != NULL) {
|
---|
| 369 | usb_standard_interface_descriptor_t *iface
|
---|
| 370 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 371 | if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
|
---|
| 372 | || (iface->interface_number != dev->interface_no)) {
|
---|
| 373 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
|
---|
| 374 | &dp_data,
|
---|
| 375 | dp_data.data, iface_ptr);
|
---|
| 376 | continue;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | cur_alt_iface->interface = iface;
|
---|
| 380 | cur_alt_iface->nested_descriptors = iface_ptr + sizeof(*iface);
|
---|
| 381 |
|
---|
| 382 | /* Find next interface to count size of nested descriptors. */
|
---|
| 383 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 384 | dp_data.data, iface_ptr);
|
---|
| 385 | if (iface_ptr == NULL) {
|
---|
| 386 | uint8_t *next = dp_data.data + dp_data.size;
|
---|
| 387 | cur_alt_iface->nested_descriptors_size
|
---|
| 388 | = next - cur_alt_iface->nested_descriptors;
|
---|
| 389 | } else {
|
---|
| 390 | cur_alt_iface->nested_descriptors_size
|
---|
| 391 | = iface_ptr - cur_alt_iface->nested_descriptors;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | cur_alt_iface++;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | dev->alternate_interfaces = alternates;
|
---|
| 398 |
|
---|
| 399 | return EOK;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[69334af] | 402 | /** Callback when new device is supposed to be controlled by this driver.
|
---|
| 403 | *
|
---|
| 404 | * This callback is a wrapper for USB specific version of @c add_device.
|
---|
| 405 | *
|
---|
| 406 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 407 | * @return Error code.
|
---|
| 408 | */
|
---|
[6105fc0] | 409 | int generic_add_device(ddf_dev_t *gen_dev)
|
---|
| 410 | {
|
---|
| 411 | assert(driver);
|
---|
| 412 | assert(driver->ops);
|
---|
| 413 | assert(driver->ops->add_device);
|
---|
| 414 |
|
---|
| 415 | int rc;
|
---|
| 416 |
|
---|
| 417 | usb_device_t *dev = malloc(sizeof(usb_device_t));
|
---|
[69334af] | 418 | if (dev == NULL) {
|
---|
| 419 | usb_log_error("Out of memory when adding device `%s'.\n",
|
---|
| 420 | gen_dev->name);
|
---|
| 421 | return ENOMEM;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[6105fc0] | 424 |
|
---|
| 425 | dev->ddf_dev = gen_dev;
|
---|
[69334af] | 426 | dev->ddf_dev->driver_data = dev;
|
---|
[6105fc0] | 427 | dev->driver_data = NULL;
|
---|
[e484f3b] | 428 | dev->descriptors.configuration = NULL;
|
---|
[6105fc0] | 429 |
|
---|
[09daa8b] | 430 | rc = initialize_pipes(dev);
|
---|
[69334af] | 431 | if (rc != EOK) {
|
---|
| 432 | free(dev);
|
---|
| 433 | return rc;
|
---|
[6105fc0] | 434 | }
|
---|
| 435 |
|
---|
[7526e3d9] | 436 | (void) initialize_alternate_interfaces(dev);
|
---|
| 437 |
|
---|
[6105fc0] | 438 | return driver->ops->add_device(dev);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | /**
|
---|
| 442 | * @}
|
---|
| 443 | */
|
---|