summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/export/config.h7
-rw-r--r--firmware/export/usb.h10
-rw-r--r--firmware/usb.c13
-rw-r--r--firmware/usbstack/usb_core.c33
4 files changed, 56 insertions, 7 deletions
diff --git a/firmware/export/config.h b/firmware/export/config.h
index b73f605d9d..e64ca4127f 100644
--- a/firmware/export/config.h
+++ b/firmware/export/config.h
@@ -871,6 +871,13 @@ Lyre prototype 1 */
871#endif 871#endif
872#endif /* HAVE_HEADPHONE_DETECTION */ 872#endif /* HAVE_HEADPHONE_DETECTION */
873 873
874#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
875/* USB charging support in the USB stack requires timeout objects */
876#ifndef INCLUDE_TIMEOUT_API
877#define INCLUDE_TIMEOUT_API
878#endif
879#endif /* HAVE_USB_CHARGING_ENABLE && HAVE_USBSTACK */
880
874#if defined(HAVE_USBSTACK) || (CONFIG_STORAGE & STORAGE_NAND) 881#if defined(HAVE_USBSTACK) || (CONFIG_STORAGE & STORAGE_NAND)
875#define STORAGE_GET_INFO 882#define STORAGE_GET_INFO
876#endif 883#endif
diff --git a/firmware/export/usb.h b/firmware/export/usb.h
index d544f5cab1..55b5f2cffb 100644
--- a/firmware/export/usb.h
+++ b/firmware/export/usb.h
@@ -52,6 +52,9 @@ enum {
52 USB_REQUEST_REBOOT, /* Event */ 52 USB_REQUEST_REBOOT, /* Event */
53#endif 53#endif
54 USB_QUIT, /* Event */ 54 USB_QUIT, /* Event */
55#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
56 USB_CHARGER_UPDATE, /* Event */
57#endif
55}; 58};
56 59
57#ifdef HAVE_USB_POWER 60#ifdef HAVE_USB_POWER
@@ -156,8 +159,11 @@ enum {
156 * or target-specific code on others 159 * or target-specific code on others
157 */ 160 */
158void usb_charging_enable(int state); 161void usb_charging_enable(int state);
159#endif 162#ifdef HAVE_USBSTACK
160#endif 163void usb_charger_update(void);
164#endif /* HAVE_USBSTACK */
165#endif /* HAVE_USB_CHARGING_ENABLE */
166#endif /* HAVE_USB_POWER */
161#ifdef HAVE_USBSTACK 167#ifdef HAVE_USBSTACK
162void usb_signal_transfer_completion( 168void usb_signal_transfer_completion(
163 struct usb_transfer_completion_event_data *event_data); 169 struct usb_transfer_completion_event_data *event_data);
diff --git a/firmware/usb.c b/firmware/usb.c
index c615e97d2b..31bf93e551 100644
--- a/firmware/usb.c
+++ b/firmware/usb.c
@@ -432,10 +432,23 @@ static void usb_thread(void)
432 try_reboot(); 432 try_reboot();
433 break; 433 break;
434#endif /* USB_FIREWIRE_HANDLING */ 434#endif /* USB_FIREWIRE_HANDLING */
435
436#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
437 case USB_CHARGER_UPDATE:
438 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
439 break;
440#endif
435 } 441 }
436 } 442 }
437} 443}
438 444
445#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
446void usb_charger_update(void)
447{
448 queue_post(&usb_queue, USB_CHARGER_UPDATE, 0);
449}
450#endif
451
439#ifdef USB_STATUS_BY_EVENT 452#ifdef USB_STATUS_BY_EVENT
440void usb_status_event(int current_status) 453void usb_status_event(int current_status)
441{ 454{
diff --git a/firmware/usbstack/usb_core.c b/firmware/usbstack/usb_core.c
index dfcce05064..d6c8c6e5cc 100644
--- a/firmware/usbstack/usb_core.c
+++ b/firmware/usbstack/usb_core.c
@@ -169,6 +169,16 @@ static enum { DEFAULT, ADDRESS, CONFIGURED } usb_state;
169#ifdef HAVE_USB_CHARGING_ENABLE 169#ifdef HAVE_USB_CHARGING_ENABLE
170static int usb_charging_mode = USB_CHARGING_DISABLE; 170static int usb_charging_mode = USB_CHARGING_DISABLE;
171static int usb_charging_current_requested = 500; 171static int usb_charging_current_requested = 500;
172static struct timeout usb_no_host_timeout;
173static bool usb_no_host = false;
174
175static int usb_no_host_callback(struct timeout *tmo)
176{
177 (void)tmo;
178 usb_no_host = true;
179 usb_charger_update();
180 return 0;
181}
172#endif 182#endif
173 183
174static int usb_core_num_interfaces; 184static int usb_core_num_interfaces;
@@ -365,6 +375,10 @@ void usb_core_init(void)
365 375
366 initialized = true; 376 initialized = true;
367 usb_state = DEFAULT; 377 usb_state = DEFAULT;
378#ifdef HAVE_USB_CHARGING_ENABLE
379 usb_no_host = false;
380 timeout_register(&usb_no_host_timeout, usb_no_host_callback, HZ*10, 0);
381#endif
368 logf("usb_core_init() finished"); 382 logf("usb_core_init() finished");
369} 383}
370 384
@@ -384,6 +398,7 @@ void usb_core_exit(void)
384 } 398 }
385 usb_state = DEFAULT; 399 usb_state = DEFAULT;
386#ifdef HAVE_USB_CHARGING_ENABLE 400#ifdef HAVE_USB_CHARGING_ENABLE
401 usb_no_host = false;
387 usb_charging_maxcurrent_change(usb_charging_maxcurrent()); 402 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
388#endif 403#endif
389 logf("usb_core_exit() finished"); 404 logf("usb_core_exit() finished");
@@ -800,6 +815,13 @@ static void request_handler_endpoint(struct usb_ctrlrequest* req)
800/* Handling USB requests starts here */ 815/* Handling USB requests starts here */
801static void usb_core_control_request_handler(struct usb_ctrlrequest* req) 816static void usb_core_control_request_handler(struct usb_ctrlrequest* req)
802{ 817{
818#ifdef HAVE_USB_CHARGING_ENABLE
819 timeout_cancel(&usb_no_host_timeout);
820 if(usb_no_host) {
821 usb_no_host = false;
822 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
823 }
824#endif
803 if(usb_state == DEFAULT) { 825 if(usb_state == DEFAULT) {
804 set_serial_descriptor(); 826 set_serial_descriptor();
805 usb_core_set_serial_function_id(); 827 usb_core_set_serial_function_id();
@@ -882,11 +904,12 @@ void usb_charging_enable(int state)
882 904
883int usb_charging_maxcurrent() 905int usb_charging_maxcurrent()
884{ 906{
885 if (!initialized 907 if (!initialized || usb_charging_mode == USB_CHARGING_DISABLE)
886 || usb_charging_mode == USB_CHARGING_DISABLE
887 || usb_state != CONFIGURED)
888 return 100; 908 return 100;
889 /* usb_state == CONFIGURED, charging enabled/forced */ 909 if (usb_state == CONFIGURED)
890 return usb_charging_current_requested; 910 return usb_charging_current_requested;
911 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
912 return 500;
913 return 100;
891} 914}
892#endif 915#endif