The device has a battery, but it would be nice to be bus-powered when available. A computer will typically supply 500 mA, for instance, while a smartphone in OTG mode is only required to supply 8 mA.
Is it possible to do something like this?
- request 500 mA
- if accepted: power from computer and charge battery from computer
- else: request 200 mA
- if accepted: power from computer
- else: claim self-powered, power from battery
Therefore it is possible to have two configurations, one for when the device is bus powered and another when it is mains powered. ... Once all the configurations have been examined by the host, the host will send a SetConfiguration command with a non zero value which matches the bConfigurationValue of one of the configurations. This is used to select the desired configuration.
Is it enough to specify multiple configurations in the descriptors, one for each current requirement? Does the OS automatically try them in order and move on to the next if one fails?
Multi-config USB devices and Windows seems to say that's how it works:
During enumeration, USBCCGP will first attempt to select the configuration whose descriptor is found at the specified “original” index. If the attempt fails, normally due to the configuration requiring more than 100mA while the device’s upstream hub has only bus power, then USBCCGP attempts to select the configuration found at the specified “alternate” index instead.
“Microsoft drivers invariably work with just the first configuration of a device. The Microsoft support for composite devices won’t engage if the device has multiple configurations. Consequently, multiconfiguration devices seem to be rare in practice, and Microsoft discourages people from designing new ones.”
Microsoft drivers will only ask for the first configuration - and if that asks for too much current, so be it.
it is not common to have more than one configuration. Windows standard drivers will always select the first configuration so there is not a lot of point.
If multiple configurations don't work in practice, can the device detect that it's been rejected, simulate a disconnect and reconnect with a different set of descriptors?
(This is a question about the USB enumeration/communication part, not the hardware implementation of how to connect the battery, etc.)