@@ -447,53 +447,58 @@ API_EXPORTED int libusb_get_device_descriptor(libusb_device *dev, |
447 | 447 | * sent to the device. |
448 | 448 | * |
449 | 449 | * \param dev a device |
450 | | - * \returns the USB configuration descriptor which must be freed with |
451 | | - * libusb_free_config_descriptor() when done |
452 | | - * \returns NULL on error |
| 450 | + * \param config output location for the USB configuration descriptor. Only |
| 451 | + * valid if 0 was returned. Must be freed with libusb_free_config_descriptor() |
| 452 | + * after use. |
| 453 | + * \returns 0 on success |
| 454 | + * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state |
| 455 | + * \returns another LIBUSB_ERROR code on error |
453 | 456 | * \see libusb_get_config_descriptor |
454 | 457 | */ |
455 | | -API_EXPORTED |
456 | | -struct libusb_config_descriptor *libusb_get_active_config_descriptor( |
457 | | - libusb_device *dev) |
| 458 | +API_EXPORTED int libusb_get_active_config_descriptor(libusb_device *dev, |
| 459 | + struct libusb_config_descriptor **config) |
458 | 460 | { |
459 | | - struct libusb_config_descriptor *config = malloc(sizeof(*config)); |
| 461 | + struct libusb_config_descriptor *_config = malloc(sizeof(*_config)); |
460 | 462 | unsigned char tmp[8]; |
461 | 463 | unsigned char *buf = NULL; |
462 | 464 | int r; |
463 | 465 |
|
464 | 466 | usbi_dbg(""); |
465 | | - if (!config) |
466 | | - return NULL; |
| 467 | + if (!_config) |
| 468 | + return LIBUSB_ERROR_NO_MEM; |
467 | 469 |
|
468 | 470 | r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp)); |
469 | 471 | if (r < 0) |
470 | 472 | goto err; |
471 | 473 |
|
472 | | - usbi_parse_descriptor(tmp, "bbw", config); |
473 | | - buf = malloc(config->wTotalLength); |
474 | | - if (!buf) |
| 474 | + usbi_parse_descriptor(tmp, "bbw", _config); |
| 475 | + buf = malloc(_config->wTotalLength); |
| 476 | + if (!buf) { |
| 477 | + r = LIBUSB_ERROR_NO_MEM; |
475 | 478 | goto err; |
| 479 | + } |
476 | 480 |
|
477 | 481 | r = usbi_backend->get_active_config_descriptor(dev, buf, |
478 | | - config->wTotalLength); |
| 482 | + _config->wTotalLength); |
479 | 483 | if (r < 0) |
480 | 484 | goto err; |
481 | 485 |
|
482 | | - r = parse_configuration(config, buf); |
| 486 | + r = parse_configuration(_config, buf); |
483 | 487 | if (r < 0) { |
484 | 488 | usbi_err("parse_configuration failed with error %d", r); |
485 | 489 | goto err; |
486 | 490 | } else if (r > 0) { |
487 | 491 | usbi_warn("descriptor data still left"); |
488 | 492 | } |
489 | 493 |
|
490 | | - return config; |
| 494 | + *config = _config; |
| 495 | + return 0; |
491 | 496 |
|
492 | 497 | err: |
493 | | - free(config); |
| 498 | + free(_config); |
494 | 499 | if (buf) |
495 | 500 | free(buf); |
496 | | - return NULL; |
| 501 | + return r; |
497 | 502 | } |
498 | 503 |
|
499 | 504 | /** \ingroup desc |
@@ -503,58 +508,64 @@ struct libusb_config_descriptor *libusb_get_active_config_descriptor( |
503 | 508 | * |
504 | 509 | * \param dev a device |
505 | 510 | * \param config_index the index of the configuration you wish to retrieve |
506 | | - * \returns the USB configuration descriptor which must be freed with |
507 | | - * libusb_free_config_descriptor() when done |
508 | | - * \returns NULL on error |
| 511 | + * \param config output location for the USB configuration descriptor. Only |
| 512 | + * valid if 0 was returned. Must be freed with libusb_free_config_descriptor() |
| 513 | + * after use. |
| 514 | + * \returns 0 on success |
| 515 | + * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist |
| 516 | + * \returns another LIBUSB_ERROR code on error |
509 | 517 | * \see libusb_get_active_config_descriptor() |
510 | 518 | * \see libusb_get_config_descriptor_by_value() |
511 | 519 | */ |
512 | | -API_EXPORTED struct libusb_config_descriptor *libusb_get_config_descriptor( |
513 | | - libusb_device *dev, uint8_t config_index) |
| 520 | +API_EXPORTED int libusb_get_config_descriptor(libusb_device *dev, |
| 521 | + uint8_t config_index, struct libusb_config_descriptor **config) |
514 | 522 | { |
515 | | - struct libusb_config_descriptor *config; |
| 523 | + struct libusb_config_descriptor *_config; |
516 | 524 | unsigned char tmp[8]; |
517 | 525 | unsigned char *buf = NULL; |
518 | 526 | int r; |
519 | 527 |
|
520 | 528 | usbi_dbg("index %d", config_index); |
521 | 529 | if (config_index >= dev->num_configurations) |
522 | | - return NULL; |
| 530 | + return LIBUSB_ERROR_NOT_FOUND; |
523 | 531 |
|
524 | | - config = malloc(sizeof(*config)); |
525 | | - if (!config) |
526 | | - return NULL; |
| 532 | + _config = malloc(sizeof(*_config)); |
| 533 | + if (!_config) |
| 534 | + return LIBUSB_ERROR_NO_MEM; |
527 | 535 |
|
528 | 536 | r = usbi_backend->get_config_descriptor(dev, config_index, tmp, |
529 | 537 | sizeof(tmp)); |
530 | 538 | if (r < 0) |
531 | 539 | goto err; |
532 | 540 |
|
533 | | - usbi_parse_descriptor(tmp, "bbw", config); |
534 | | - buf = malloc(config->wTotalLength); |
535 | | - if (!buf) |
| 541 | + usbi_parse_descriptor(tmp, "bbw", _config); |
| 542 | + buf = malloc(_config->wTotalLength); |
| 543 | + if (!buf) { |
| 544 | + r = LIBUSB_ERROR_NO_MEM; |
536 | 545 | goto err; |
| 546 | + } |
537 | 547 |
|
538 | 548 | r = usbi_backend->get_config_descriptor(dev, config_index, buf, |
539 | | - config->wTotalLength); |
| 549 | + _config->wTotalLength); |
540 | 550 | if (r < 0) |
541 | 551 | goto err; |
542 | 552 |
|
543 | | - r = parse_configuration(config, buf); |
| 553 | + r = parse_configuration(_config, buf); |
544 | 554 | if (r < 0) { |
545 | 555 | usbi_err("parse_configuration failed with error %d", r); |
546 | 556 | goto err; |
547 | 557 | } else if (r > 0) { |
548 | 558 | usbi_warn("descriptor data still left"); |
549 | 559 | } |
550 | 560 |
|
551 | | - return config; |
| 561 | + *config = _config; |
| 562 | + return 0; |
552 | 563 |
|
553 | 564 | err: |
554 | | - free(config); |
| 565 | + free(_config); |
555 | 566 | if (buf) |
556 | 567 | free(buf); |
557 | | - return NULL; |
| 568 | + return r; |
558 | 569 | } |
559 | 570 |
|
560 | 571 | /* iterate through all configurations, returning the index of the configuration |
@@ -591,22 +602,26 @@ int usbi_get_config_index_by_value(struct libusb_device *dev, |
591 | 602 | * \param dev a device |
592 | 603 | * \param bConfigurationValue the bConfigurationValue of the configuration you |
593 | 604 | * wish to retrieve |
594 | | - * \returns the USB configuration descriptor which must be freed with |
595 | | - * libusb_free_config_descriptor() when done |
596 | | - * \returns NULL on error |
| 605 | + * \param config output location for the USB configuration descriptor. Only |
| 606 | + * valid if 0 was returned. Must be freed with libusb_free_config_descriptor() |
| 607 | + * after use. |
| 608 | + * \returns 0 on success |
| 609 | + * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist |
| 610 | + * \returns another LIBUSB_ERROR code on error |
597 | 611 | * \see libusb_get_active_config_descriptor() |
598 | 612 | * \see libusb_get_config_descriptor() |
599 | 613 | */ |
600 | | -API_EXPORTED |
601 | | -struct libusb_config_descriptor *libusb_get_config_descriptor_by_value( |
602 | | - libusb_device *dev, uint8_t bConfigurationValue) |
| 614 | +API_EXPORTED int libusb_get_config_descriptor_by_value(libusb_device *dev, |
| 615 | + uint8_t bConfigurationValue, struct libusb_config_descriptor **config) |
603 | 616 | { |
604 | 617 | int idx; |
605 | 618 | int r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx); |
606 | | - if (r < 0 || idx == -1) |
607 | | - return NULL; |
| 619 | + if (r < 0) |
| 620 | + return r; |
| 621 | + else if (idx == -1) |
| 622 | + return LIBUSB_ERROR_NOT_FOUND; |
608 | 623 | else |
609 | | - return libusb_get_config_descriptor(dev, idx); |
| 624 | + return libusb_get_config_descriptor(dev, idx, config); |
610 | 625 | } |
611 | 626 |
|
612 | 627 | /** \ingroup desc |
|