Skip to content

Commit 697b6cd

Browse files
Rich Barlowvpelletier
authored andcommitted
Correct scaling of max power for Super Speed
The 'MaxPower' field of the Configuration descriptor is in units of 2mA for everything pre-dating Super Speed and in units of 8mA for Super Speed. Also, it is actually a current (measured in Amps), not a power (measured in Watts); so the previous docstring was wrong/misleading when it talked about mW (milliwatts).
1 parent 17b34a8 commit 697b6cd

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

usb1/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ def getTransfer(self, iso_packets=0):
16571657
return result
16581658

16591659
class USBConfiguration(object):
1660-
def __init__(self, context, config):
1660+
def __init__(self, context, config, device_speed):
16611661
"""
16621662
You should not instanciate this class directly.
16631663
Call USBDevice methods to get instances of this class.
@@ -1666,6 +1666,7 @@ def __init__(self, context, config):
16661666
raise TypeError('Unexpected descriptor type.')
16671667
self.__config = config
16681668
self.__context = context
1669+
self.__device_speed = device_speed
16691670

16701671
def getNumInterfaces(self):
16711672
return self.__config.bNumInterfaces
@@ -1683,11 +1684,13 @@ def getAttributes(self):
16831684

16841685
def getMaxPower(self):
16851686
"""
1686-
Returns device's power consumption in mW.
1687-
Beware of unit: USB descriptor uses 2mW increments, this method
1688-
converts it to mW units.
1687+
Returns device's power consumption in mA.
1688+
1689+
USB descriptor is expressed in units of 2 mA when the device is operating in high-speed mode
1690+
and in units of 8 mA when the device is operating in super-speed mode. This function scales
1691+
the descriptor value appropriately.
16891692
"""
1690-
return self.__config.MaxPower * 2
1693+
return self.__config.MaxPower * (8 if self.__device_speed == SPEED_SUPER else 2)
16911694

16921695
def getExtra(self):
16931696
"""
@@ -1945,7 +1948,7 @@ def __len__(self):
19451948

19461949
def __getitem__(self, index):
19471950
return USBConfiguration(
1948-
self.__context, self.__configuration_descriptor_list[index])
1951+
self.__context, self.__configuration_descriptor_list[index], self.getDeviceSpeed())
19491952

19501953
def __key(self):
19511954
return (
@@ -1970,7 +1973,7 @@ def __eq__(self, other):
19701973
def iterConfigurations(self):
19711974
context = self.__context
19721975
for config in self.__configuration_descriptor_list:
1973-
yield USBConfiguration(context, config)
1976+
yield USBConfiguration(context, config, self.getDeviceSpeed())
19741977

19751978
# BBB
19761979
iterConfiguations = iterConfigurations

0 commit comments

Comments
 (0)