[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;
|
---|
[159b91f4] | 137 | dev->pipes[i].interface_setting = 0;
|
---|
[6105fc0] | 138 | }
|
---|
| 139 |
|
---|
[e484f3b] | 140 | rc = usb_pipe_initialize_from_configuration(dev->pipes, pipe_count,
|
---|
| 141 | dev->descriptors.configuration, dev->descriptors.configuration_size,
|
---|
| 142 | &dev->wire);
|
---|
[69334af] | 143 | if (rc != EOK) {
|
---|
| 144 | usb_log_error("Failed initializing USB endpoints: %s.\n",
|
---|
| 145 | str_error(rc));
|
---|
| 146 | goto rollback;
|
---|
| 147 | }
|
---|
[6105fc0] | 148 |
|
---|
[5fd22d8] | 149 | /* Register the endpoints. */
|
---|
| 150 | usb_hc_connection_t hc_conn;
|
---|
| 151 | rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev);
|
---|
| 152 | if (rc != EOK) {
|
---|
| 153 | usb_log_error(
|
---|
| 154 | "Failed initializing connection to host controller: %s.\n",
|
---|
| 155 | str_error(rc));
|
---|
| 156 | goto rollback;
|
---|
| 157 | }
|
---|
| 158 | rc = usb_hc_connection_open(&hc_conn);
|
---|
| 159 | if (rc != EOK) {
|
---|
| 160 | usb_log_error("Failed to connect to host controller: %s.\n",
|
---|
| 161 | str_error(rc));
|
---|
| 162 | goto rollback;
|
---|
| 163 | }
|
---|
| 164 | for (i = 0; i < pipe_count; i++) {
|
---|
| 165 | if (dev->pipes[i].present) {
|
---|
[3954a63b] | 166 | rc = usb_pipe_register(dev->pipes[i].pipe,
|
---|
[5fd22d8] | 167 | dev->pipes[i].descriptor->poll_interval,
|
---|
| 168 | &hc_conn);
|
---|
| 169 | /* Ignore error when operation not supported by HC. */
|
---|
| 170 | if ((rc != EOK) && (rc != ENOTSUP)) {
|
---|
| 171 | /* FIXME: what shall we do? */
|
---|
| 172 | dev->pipes[i].present = false;
|
---|
| 173 | free(dev->pipes[i].pipe);
|
---|
| 174 | dev->pipes[i].pipe = NULL;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | /* Ignoring errors here. */
|
---|
| 179 | usb_hc_connection_close(&hc_conn);
|
---|
| 180 |
|
---|
[6105fc0] | 181 | return EOK;
|
---|
[69334af] | 182 |
|
---|
| 183 | rollback:
|
---|
| 184 | for (i = 0; i < pipe_count; i++) {
|
---|
| 185 | if (dev->pipes[i].pipe != NULL) {
|
---|
| 186 | free(dev->pipes[i].pipe);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | free(dev->pipes);
|
---|
| 190 |
|
---|
| 191 | return rc;
|
---|
[6105fc0] | 192 | }
|
---|
| 193 |
|
---|
[69334af] | 194 | /** Initialize all endpoint pipes.
|
---|
| 195 | *
|
---|
| 196 | * @param drv The driver.
|
---|
| 197 | * @param dev The device to be initialized.
|
---|
| 198 | * @return Error code.
|
---|
| 199 | */
|
---|
[09daa8b] | 200 | static int initialize_pipes(usb_device_t *dev)
|
---|
[69334af] | 201 | {
|
---|
| 202 | int rc;
|
---|
| 203 |
|
---|
| 204 | rc = usb_device_connection_initialize_from_device(&dev->wire,
|
---|
| 205 | dev->ddf_dev);
|
---|
| 206 | if (rc != EOK) {
|
---|
| 207 | usb_log_error(
|
---|
| 208 | "Failed initializing connection on device `%s'. %s.\n",
|
---|
| 209 | dev->ddf_dev->name, str_error(rc));
|
---|
| 210 | return rc;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[3954a63b] | 213 | rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
|
---|
[69334af] | 214 | &dev->wire);
|
---|
| 215 | if (rc != EOK) {
|
---|
| 216 | usb_log_error("Failed to initialize default control pipe " \
|
---|
| 217 | "on device `%s': %s.\n",
|
---|
| 218 | dev->ddf_dev->name, str_error(rc));
|
---|
| 219 | return rc;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[3954a63b] | 222 | rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
|
---|
[206f71a] | 223 | if (rc != EOK) {
|
---|
| 224 | usb_log_error(
|
---|
| 225 | "Probing default control pipe on device `%s' failed: %s.\n",
|
---|
| 226 | dev->ddf_dev->name, str_error(rc));
|
---|
| 227 | return rc;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[bb18a59] | 230 | /* Get our interface. */
|
---|
| 231 | dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
|
---|
| 232 |
|
---|
[69334af] | 233 | /*
|
---|
[e484f3b] | 234 | * For further actions, we need open session on default control pipe.
|
---|
[69334af] | 235 | */
|
---|
[3954a63b] | 236 | rc = usb_pipe_start_session(&dev->ctrl_pipe);
|
---|
[69334af] | 237 | if (rc != EOK) {
|
---|
| 238 | usb_log_error("Failed to start an IPC session: %s.\n",
|
---|
| 239 | str_error(rc));
|
---|
| 240 | return rc;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[e484f3b] | 243 | /* Get the device descriptor. */
|
---|
| 244 | rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
|
---|
| 245 | &dev->descriptors.device);
|
---|
| 246 | if (rc != EOK) {
|
---|
| 247 | usb_log_error("Failed to retrieve device descriptor: %s.\n",
|
---|
| 248 | str_error(rc));
|
---|
| 249 | return rc;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /* Get the full configuration descriptor. */
|
---|
| 253 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
| 254 | &dev->ctrl_pipe, 0, (void **) &dev->descriptors.configuration,
|
---|
| 255 | &dev->descriptors.configuration_size);
|
---|
| 256 | if (rc != EOK) {
|
---|
| 257 | usb_log_error("Failed retrieving configuration descriptor: %s.\n",
|
---|
| 258 | dev->ddf_dev->name, str_error(rc));
|
---|
| 259 | return rc;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[69334af] | 262 | if (driver->endpoints != NULL) {
|
---|
| 263 | rc = initialize_other_pipes(driver, dev);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /* No checking here. */
|
---|
[3954a63b] | 267 | usb_pipe_end_session(&dev->ctrl_pipe);
|
---|
[69334af] | 268 |
|
---|
[e484f3b] | 269 | /* Rollback actions. */
|
---|
| 270 | if (rc != EOK) {
|
---|
| 271 | if (dev->descriptors.configuration != NULL) {
|
---|
| 272 | free(dev->descriptors.configuration);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[69334af] | 276 | return rc;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[7526e3d9] | 279 | /** Count number of alternate settings of a interface.
|
---|
| 280 | *
|
---|
| 281 | * @param config_descr Full configuration descriptor.
|
---|
| 282 | * @param config_descr_size Size of @p config_descr in bytes.
|
---|
| 283 | * @param interface_no Interface number.
|
---|
| 284 | * @return Number of alternate interfaces for @p interface_no interface.
|
---|
| 285 | */
|
---|
| 286 | static size_t count_alternate_interfaces(uint8_t *config_descr,
|
---|
| 287 | size_t config_descr_size, int interface_no)
|
---|
| 288 | {
|
---|
| 289 | assert(config_descr != NULL);
|
---|
| 290 | usb_dp_parser_t dp_parser = {
|
---|
| 291 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 292 | };
|
---|
| 293 | usb_dp_parser_data_t dp_data = {
|
---|
| 294 | .data = config_descr,
|
---|
| 295 | .size = config_descr_size,
|
---|
| 296 | .arg = NULL
|
---|
| 297 | };
|
---|
| 298 |
|
---|
| 299 | size_t alternate_count = 0;
|
---|
| 300 |
|
---|
| 301 | uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
| 302 | &dp_data, config_descr);
|
---|
| 303 | while (iface_ptr != NULL) {
|
---|
| 304 | usb_standard_interface_descriptor_t *iface
|
---|
| 305 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 306 | if (iface->descriptor_type == USB_DESCTYPE_INTERFACE) {
|
---|
| 307 | if (iface->interface_number == interface_no) {
|
---|
| 308 | alternate_count++;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 312 | config_descr, iface_ptr);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | return alternate_count;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /** Initialize structures related to alternate interfaces.
|
---|
| 319 | *
|
---|
| 320 | * @param dev Device where alternate settings shall be initialized.
|
---|
| 321 | * @return Error code.
|
---|
| 322 | */
|
---|
| 323 | static int initialize_alternate_interfaces(usb_device_t *dev)
|
---|
| 324 | {
|
---|
| 325 | if (dev->interface_no < 0) {
|
---|
| 326 | dev->alternate_interfaces = NULL;
|
---|
| 327 | return EOK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | usb_alternate_interfaces_t *alternates
|
---|
| 331 | = malloc(sizeof(usb_alternate_interfaces_t));
|
---|
| 332 |
|
---|
| 333 | if (alternates == NULL) {
|
---|
| 334 | return ENOMEM;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | alternates->alternative_count
|
---|
| 338 | = count_alternate_interfaces(dev->descriptors.configuration,
|
---|
| 339 | dev->descriptors.configuration_size, dev->interface_no);
|
---|
| 340 |
|
---|
| 341 | if (alternates->alternative_count == 0) {
|
---|
| 342 | free(alternates);
|
---|
| 343 | return ENOENT;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | alternates->alternatives = malloc(alternates->alternative_count
|
---|
| 347 | * sizeof(usb_alternate_interface_descriptors_t));
|
---|
| 348 | if (alternates->alternatives == NULL) {
|
---|
| 349 | free(alternates);
|
---|
| 350 | return ENOMEM;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | alternates->current = 0;
|
---|
| 354 |
|
---|
| 355 | usb_dp_parser_t dp_parser = {
|
---|
| 356 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 357 | };
|
---|
| 358 | usb_dp_parser_data_t dp_data = {
|
---|
| 359 | .data = dev->descriptors.configuration,
|
---|
| 360 | .size = dev->descriptors.configuration_size,
|
---|
| 361 | .arg = NULL
|
---|
| 362 | };
|
---|
| 363 |
|
---|
| 364 | usb_alternate_interface_descriptors_t *cur_alt_iface
|
---|
| 365 | = &alternates->alternatives[0];
|
---|
| 366 |
|
---|
| 367 | uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
| 368 | &dp_data, dp_data.data);
|
---|
| 369 | while (iface_ptr != NULL) {
|
---|
| 370 | usb_standard_interface_descriptor_t *iface
|
---|
| 371 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 372 | if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
|
---|
| 373 | || (iface->interface_number != dev->interface_no)) {
|
---|
| 374 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
|
---|
| 375 | &dp_data,
|
---|
| 376 | dp_data.data, iface_ptr);
|
---|
| 377 | continue;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | cur_alt_iface->interface = iface;
|
---|
| 381 | cur_alt_iface->nested_descriptors = iface_ptr + sizeof(*iface);
|
---|
| 382 |
|
---|
| 383 | /* Find next interface to count size of nested descriptors. */
|
---|
| 384 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 385 | dp_data.data, iface_ptr);
|
---|
| 386 | if (iface_ptr == NULL) {
|
---|
| 387 | uint8_t *next = dp_data.data + dp_data.size;
|
---|
| 388 | cur_alt_iface->nested_descriptors_size
|
---|
| 389 | = next - cur_alt_iface->nested_descriptors;
|
---|
| 390 | } else {
|
---|
| 391 | cur_alt_iface->nested_descriptors_size
|
---|
| 392 | = iface_ptr - cur_alt_iface->nested_descriptors;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | cur_alt_iface++;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | dev->alternate_interfaces = alternates;
|
---|
| 399 |
|
---|
| 400 | return EOK;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[69334af] | 403 | /** Callback when new device is supposed to be controlled by this driver.
|
---|
| 404 | *
|
---|
| 405 | * This callback is a wrapper for USB specific version of @c add_device.
|
---|
| 406 | *
|
---|
| 407 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 408 | * @return Error code.
|
---|
| 409 | */
|
---|
[6105fc0] | 410 | int generic_add_device(ddf_dev_t *gen_dev)
|
---|
| 411 | {
|
---|
| 412 | assert(driver);
|
---|
| 413 | assert(driver->ops);
|
---|
| 414 | assert(driver->ops->add_device);
|
---|
| 415 |
|
---|
| 416 | int rc;
|
---|
| 417 |
|
---|
| 418 | usb_device_t *dev = malloc(sizeof(usb_device_t));
|
---|
[69334af] | 419 | if (dev == NULL) {
|
---|
| 420 | usb_log_error("Out of memory when adding device `%s'.\n",
|
---|
| 421 | gen_dev->name);
|
---|
| 422 | return ENOMEM;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[6105fc0] | 425 |
|
---|
| 426 | dev->ddf_dev = gen_dev;
|
---|
[69334af] | 427 | dev->ddf_dev->driver_data = dev;
|
---|
[6105fc0] | 428 | dev->driver_data = NULL;
|
---|
[e484f3b] | 429 | dev->descriptors.configuration = NULL;
|
---|
[6105fc0] | 430 |
|
---|
[09daa8b] | 431 | rc = initialize_pipes(dev);
|
---|
[69334af] | 432 | if (rc != EOK) {
|
---|
| 433 | free(dev);
|
---|
| 434 | return rc;
|
---|
[6105fc0] | 435 | }
|
---|
| 436 |
|
---|
[7526e3d9] | 437 | (void) initialize_alternate_interfaces(dev);
|
---|
| 438 |
|
---|
[6105fc0] | 439 | return driver->ops->add_device(dev);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[159b91f4] | 442 |
|
---|
[6105fc0] | 443 | /**
|
---|
| 444 | * @}
|
---|
| 445 | */
|
---|