GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support unconfigured devices · Raghuvardhan-Reddy78/libusb@f2ede98

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • TODO
  • libusb
    • core.c
    • descriptor.c
    • libusb.h
    • os
      • linux_usbfs.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎TODO‎

Copy file name to clipboardExpand all lines: TODO
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ endianness of control setup, issues when resubmitting transfers
66
serialization of handle_events
77
internal docs for OS porters
88
check which messages are sent during open, claim interface, close, release
9-
unconfigured devices
109

1110
1.0 API style/naming points to reconsider
1211
=========================================
Collapse file

‎libusb/core.c‎

Copy file name to clipboardExpand all lines: libusb/core.c
+11-6Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,16 @@ API_EXPORTED int libusb_get_max_packet_size(libusb_device *dev,
460460
unsigned char endpoint)
461461
{
462462
int iface_idx;
463-
struct libusb_config_descriptor *config =
464-
libusb_get_active_config_descriptor(dev);
465-
int r = LIBUSB_ERROR_NOT_FOUND;
466-
467-
if (!config) {
463+
struct libusb_config_descriptor *config;
464+
int r;
465+
466+
r = libusb_get_active_config_descriptor(dev, &config);
467+
if (r < 0) {
468468
usbi_err("could not retrieve active config descriptor");
469469
return LIBUSB_ERROR_OTHER;
470470
}
471471

472+
r = LIBUSB_ERROR_NOT_FOUND;
472473
for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
473474
const struct libusb_interface *iface = &config->interface[iface_idx];
474475
int altsetting_idx;
@@ -682,6 +683,10 @@ API_EXPORTED libusb_device *libusb_get_device(libusb_device_handle *dev_handle)
682683
* must release all claimed interfaces using libusb_release_interface() before
683684
* setting a new active configuration.
684685
*
686+
* A configuration value of -1 will put the device in unconfigured state.
687+
* The USB specifications state that a configuration value of 0 does this,
688+
* however buggy devices exist which actually have a configuration 0.
689+
*
685690
* You should always use this function rather than formulating your own
686691
* SET_CONFIGURATION control request. This is because the underlying operating
687692
* system needs to know when such changes happen.
@@ -690,7 +695,7 @@ API_EXPORTED libusb_device *libusb_get_device(libusb_device_handle *dev_handle)
690695
*
691696
* \param dev a device handle
692697
* \param configuration the bConfigurationValue of the configuration you
693-
* wish to activate
698+
* wish to activate, or -1 if you wish to put the device in unconfigured state
694699
* \returns 0 on success
695700
* \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
696701
* \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
Collapse file

‎libusb/descriptor.c‎

Copy file name to clipboardExpand all lines: libusb/descriptor.c
+59-44Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -447,53 +447,58 @@ API_EXPORTED int libusb_get_device_descriptor(libusb_device *dev,
447447
* sent to the device.
448448
*
449449
* \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
453456
* \see libusb_get_config_descriptor
454457
*/
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)
458460
{
459-
struct libusb_config_descriptor *config = malloc(sizeof(*config));
461+
struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
460462
unsigned char tmp[8];
461463
unsigned char *buf = NULL;
462464
int r;
463465

464466
usbi_dbg("");
465-
if (!config)
466-
return NULL;
467+
if (!_config)
468+
return LIBUSB_ERROR_NO_MEM;
467469

468470
r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp));
469471
if (r < 0)
470472
goto err;
471473

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;
475478
goto err;
479+
}
476480

477481
r = usbi_backend->get_active_config_descriptor(dev, buf,
478-
config->wTotalLength);
482+
_config->wTotalLength);
479483
if (r < 0)
480484
goto err;
481485

482-
r = parse_configuration(config, buf);
486+
r = parse_configuration(_config, buf);
483487
if (r < 0) {
484488
usbi_err("parse_configuration failed with error %d", r);
485489
goto err;
486490
} else if (r > 0) {
487491
usbi_warn("descriptor data still left");
488492
}
489493

490-
return config;
494+
*config = _config;
495+
return 0;
491496

492497
err:
493-
free(config);
498+
free(_config);
494499
if (buf)
495500
free(buf);
496-
return NULL;
501+
return r;
497502
}
498503

499504
/** \ingroup desc
@@ -503,58 +508,64 @@ struct libusb_config_descriptor *libusb_get_active_config_descriptor(
503508
*
504509
* \param dev a device
505510
* \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
509517
* \see libusb_get_active_config_descriptor()
510518
* \see libusb_get_config_descriptor_by_value()
511519
*/
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)
514522
{
515-
struct libusb_config_descriptor *config;
523+
struct libusb_config_descriptor *_config;
516524
unsigned char tmp[8];
517525
unsigned char *buf = NULL;
518526
int r;
519527

520528
usbi_dbg("index %d", config_index);
521529
if (config_index >= dev->num_configurations)
522-
return NULL;
530+
return LIBUSB_ERROR_NOT_FOUND;
523531

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;
527535

528536
r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
529537
sizeof(tmp));
530538
if (r < 0)
531539
goto err;
532540

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;
536545
goto err;
546+
}
537547

538548
r = usbi_backend->get_config_descriptor(dev, config_index, buf,
539-
config->wTotalLength);
549+
_config->wTotalLength);
540550
if (r < 0)
541551
goto err;
542552

543-
r = parse_configuration(config, buf);
553+
r = parse_configuration(_config, buf);
544554
if (r < 0) {
545555
usbi_err("parse_configuration failed with error %d", r);
546556
goto err;
547557
} else if (r > 0) {
548558
usbi_warn("descriptor data still left");
549559
}
550560

551-
return config;
561+
*config = _config;
562+
return 0;
552563

553564
err:
554-
free(config);
565+
free(_config);
555566
if (buf)
556567
free(buf);
557-
return NULL;
568+
return r;
558569
}
559570

560571
/* 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,
591602
* \param dev a device
592603
* \param bConfigurationValue the bConfigurationValue of the configuration you
593604
* 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
597611
* \see libusb_get_active_config_descriptor()
598612
* \see libusb_get_config_descriptor()
599613
*/
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)
603616
{
604617
int idx;
605618
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;
608623
else
609-
return libusb_get_config_descriptor(dev, idx);
624+
return libusb_get_config_descriptor(dev, idx, config);
610625
}
611626

612627
/** \ingroup desc
Collapse file

‎libusb/libusb.h‎

Copy file name to clipboardExpand all lines: libusb/libusb.h
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,12 @@ void libusb_unref_device(libusb_device *dev);
667667

668668
int libusb_get_device_descriptor(libusb_device *dev,
669669
struct libusb_device_descriptor *desc);
670-
struct libusb_config_descriptor *libusb_get_active_config_descriptor(
671-
libusb_device *dev);
672-
struct libusb_config_descriptor *libusb_get_config_descriptor(
673-
libusb_device *dev, uint8_t config_index);
674-
struct libusb_config_descriptor *libusb_get_config_descriptor_by_value(
675-
libusb_device *dev, uint8_t bConfigurationValue);
670+
int libusb_get_active_config_descriptor(libusb_device *dev,
671+
struct libusb_config_descriptor **config);
672+
int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
673+
struct libusb_config_descriptor **config);
674+
int libusb_get_config_descriptor_by_value(libusb_device *dev,
675+
uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
676676
void libusb_free_config_descriptor(struct libusb_config_descriptor *config);
677677
uint8_t libusb_get_bus_number(libusb_device *dev);
678678
uint8_t libusb_get_device_address(libusb_device *dev);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.