diff options
Diffstat (limited to 'firmware/usbstack/usb_core.c')
-rw-r--r-- | firmware/usbstack/usb_core.c | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/firmware/usbstack/usb_core.c b/firmware/usbstack/usb_core.c index a2e2b5f063..dfcce05064 100644 --- a/firmware/usbstack/usb_core.c +++ b/firmware/usbstack/usb_core.c | |||
@@ -166,6 +166,11 @@ static int usb_address = 0; | |||
166 | static bool initialized = false; | 166 | static bool initialized = false; |
167 | static enum { DEFAULT, ADDRESS, CONFIGURED } usb_state; | 167 | static enum { DEFAULT, ADDRESS, CONFIGURED } usb_state; |
168 | 168 | ||
169 | #ifdef HAVE_USB_CHARGING_ENABLE | ||
170 | static int usb_charging_mode = USB_CHARGING_DISABLE; | ||
171 | static int usb_charging_current_requested = 500; | ||
172 | #endif | ||
173 | |||
169 | static int usb_core_num_interfaces; | 174 | static int usb_core_num_interfaces; |
170 | 175 | ||
171 | typedef void (*completion_handler_t)(int ep, int dir, int status, int length); | 176 | typedef void (*completion_handler_t)(int ep, int dir, int status, int length); |
@@ -378,6 +383,9 @@ void usb_core_exit(void) | |||
378 | initialized = false; | 383 | initialized = false; |
379 | } | 384 | } |
380 | usb_state = DEFAULT; | 385 | usb_state = DEFAULT; |
386 | #ifdef HAVE_USB_CHARGING_ENABLE | ||
387 | usb_charging_maxcurrent_change(usb_charging_maxcurrent()); | ||
388 | #endif | ||
381 | logf("usb_core_exit() finished"); | 389 | logf("usb_core_exit() finished"); |
382 | } | 390 | } |
383 | 391 | ||
@@ -552,6 +560,16 @@ static void request_handler_device_get_descriptor(struct usb_ctrlrequest* req) | |||
552 | config_descriptor.bDescriptorType = | 560 | config_descriptor.bDescriptorType = |
553 | USB_DT_OTHER_SPEED_CONFIG; | 561 | USB_DT_OTHER_SPEED_CONFIG; |
554 | } | 562 | } |
563 | #ifdef HAVE_USB_CHARGING_ENABLE | ||
564 | if (usb_charging_mode == USB_CHARGING_DISABLE) { | ||
565 | config_descriptor.bMaxPower = (100+1)/2; | ||
566 | usb_charging_current_requested = 100; | ||
567 | } | ||
568 | else { | ||
569 | config_descriptor.bMaxPower = (500+1)/2; | ||
570 | usb_charging_current_requested = 500; | ||
571 | } | ||
572 | #endif | ||
555 | size = sizeof(struct usb_config_descriptor); | 573 | size = sizeof(struct usb_config_descriptor); |
556 | 574 | ||
557 | for(i = 0; i < USB_NUM_DRIVERS; i++) | 575 | for(i = 0; i < USB_NUM_DRIVERS; i++) |
@@ -628,8 +646,10 @@ static void request_handler_device(struct usb_ctrlrequest* req) | |||
628 | } | 646 | } |
629 | else | 647 | else |
630 | usb_state = ADDRESS; | 648 | usb_state = ADDRESS; |
631 | |||
632 | usb_drv_send(EP_CONTROL, NULL, 0); | 649 | usb_drv_send(EP_CONTROL, NULL, 0); |
650 | #ifdef HAVE_USB_CHARGING_ENABLE | ||
651 | usb_charging_maxcurrent_change(usb_charging_maxcurrent()); | ||
652 | #endif | ||
633 | break; | 653 | break; |
634 | } | 654 | } |
635 | case USB_REQ_SET_ADDRESS: { | 655 | case USB_REQ_SET_ADDRESS: { |
@@ -809,6 +829,9 @@ void usb_core_bus_reset(void) | |||
809 | { | 829 | { |
810 | usb_address = 0; | 830 | usb_address = 0; |
811 | usb_state = DEFAULT; | 831 | usb_state = DEFAULT; |
832 | #ifdef HAVE_USB_CHARGING_ENABLE | ||
833 | usb_charging_maxcurrent_change(usb_charging_maxcurrent()); | ||
834 | #endif | ||
812 | } | 835 | } |
813 | 836 | ||
814 | /* called by usb_drv_transfer_completed() */ | 837 | /* called by usb_drv_transfer_completed() */ |
@@ -850,9 +873,20 @@ void usb_core_control_request(struct usb_ctrlrequest* req) | |||
850 | usb_signal_transfer_completion(completion_event); | 873 | usb_signal_transfer_completion(completion_event); |
851 | } | 874 | } |
852 | 875 | ||
853 | #ifdef HAVE_USB_POWER | 876 | #ifdef HAVE_USB_CHARGING_ENABLE |
854 | unsigned short usb_allowed_current() | 877 | void usb_charging_enable(int state) |
878 | { | ||
879 | usb_charging_mode = state; | ||
880 | usb_charging_maxcurrent_change(usb_charging_maxcurrent()); | ||
881 | } | ||
882 | |||
883 | int usb_charging_maxcurrent() | ||
855 | { | 884 | { |
856 | return (usb_state == CONFIGURED) ? MAX(USB_MAX_CURRENT, 100) : 100; | 885 | if (!initialized |
886 | || usb_charging_mode == USB_CHARGING_DISABLE | ||
887 | || usb_state != CONFIGURED) | ||
888 | return 100; | ||
889 | /* usb_state == CONFIGURED, charging enabled/forced */ | ||
890 | return usb_charging_current_requested; | ||
857 | } | 891 | } |
858 | #endif | 892 | #endif |