Skip to content

Commit 6118ca2

Browse files
solsbarrynashif
authored andcommitted
usb: make usb descriptor power options configurable
Add two new kconfig options USB_SELF_POWERED and USB_MAX_POWER. These can be set by the user to change the USB configuration descriptor. USB_MAX_POWER can be set to any value between 0 and 250, but practically should be 50 or 250. These values are half the ammount of mA that the device will tell the host that it needs. USB_SELF_POWERED sets the 7th bit in bmAttributes of the USB config descriptor. Should be set to y if the device has its own power source other than USB. Signed-off-by: Barry Solomon <[email protected]>
1 parent 86dc23a commit 6118ca2

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

include/usb/usb_common.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@
8686
#define BCDDEVICE_RELNUM (BCD(KERNEL_VERSION_MAJOR) << 8 | \
8787
BCD(KERNEL_VERSION_MINOR))
8888

89-
/* 100mA max power, per 2mA units */
90-
/* USB 1.1 spec indicates 100mA(max) per unit load, up to 5 loads */
91-
#define MAX_LOW_POWER 0x32
92-
#define MAX_HIGH_POWER 0xFA
93-
9489
/* Highest value of Frame Number in SOF packets. */
9590
#define USB_SOF_MAX 2047
9691

@@ -100,10 +95,13 @@
10095
* D5:Remote Wakeup -> 0,
10196
* D4...0:Reserved -> 0
10297
*/
103-
#define USB_CONFIGURATION_ATTRIBUTES_REMOTE_WAKEUP 0x20
104-
#define USB_CONFIGURATION_ATTRIBUTES 0xC0 \
105-
| (COND_CODE_1(CONFIG_USB_DEVICE_REMOTE_WAKEUP, \
106-
(USB_CONFIGURATION_ATTRIBUTES_REMOTE_WAKEUP), (0)))
98+
#define USB_CONFIGURATION_ATTRIBUTES_REMOTE_WAKEUP BIT(5)
99+
#define USB_CONFIGURATION_ATTRIBUTES_SELF_POWERED BIT(6)
100+
#define USB_CONFIGURATION_ATTRIBUTES BIT(7) \
101+
| ((COND_CODE_1(CONFIG_USB_SELF_POWERED, \
102+
(USB_CONFIGURATION_ATTRIBUTES_SELF_POWERED), (0))) \
103+
| (COND_CODE_1(CONFIG_USB_DEVICE_REMOTE_WAKEUP, \
104+
(USB_CONFIGURATION_ATTRIBUTES_REMOTE_WAKEUP), (0))))
107105

108106
/* Classes */
109107
#define COMMUNICATION_DEVICE_CLASS 0x02

subsys/usb/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ config USB_DEVICE_BOS
9090
config USB_DEVICE_OS_DESC
9191
bool "Enable MS OS Descriptors support"
9292

93+
config USB_SELF_POWERED
94+
bool "Set Self-powered characteristic"
95+
default y
96+
help
97+
Set Self-powered characteristic in bmAttributes to indicate
98+
self powered USB device.
99+
100+
config USB_MAX_POWER
101+
int "Set bMaxPower value"
102+
default 50
103+
range 0 250
104+
help
105+
Set bMaxPower value in the Standard Configuration Descriptor,
106+
the result is 2mA times the value provided.
107+
93108
menuconfig USB_VBUS_GPIO
94109
bool "Control VBUS via GPIO pin"
95110
depends on GPIO

subsys/usb/class/usb_dfu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ struct dev_dfu_mode_descriptor dfu_mode_desc = {
156156
.bConfigurationValue = 1,
157157
.iConfiguration = 0,
158158
.bmAttributes = USB_CONFIGURATION_ATTRIBUTES,
159-
.bMaxPower = MAX_LOW_POWER,
159+
.bMaxPower = CONFIG_USB_MAX_POWER,
160160
},
161161
.sec_dfu_cfg = {
162162
/* Interface descriptor */

subsys/usb/usb_descriptor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ USBD_DEVICE_DESCR_DEFINE(primary) struct common_descriptor common_desc = {
8989
.bConfigurationValue = 1,
9090
.iConfiguration = 0,
9191
.bmAttributes = USB_CONFIGURATION_ATTRIBUTES,
92-
.bMaxPower = MAX_LOW_POWER,
92+
.bMaxPower = CONFIG_USB_MAX_POWER,
9393
},
9494
};
9595

0 commit comments

Comments
 (0)