From 207657e383217bf0071678d075ce459aa585274a Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Thu, 21 May 2020 01:16:00 +0200
Subject: [PATCH] fix(usbcdc): Re-enable after lockup when attached again

When opening an ACM device Linux sends the ACM_SET_CONTROL_LINE_STATE
with DTR and CTS high. We can use this re-enable the ACM device on our
side after a lockup.
---
 epicardium/usb/cdcacm.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/epicardium/usb/cdcacm.c b/epicardium/usb/cdcacm.c
index ea046721a..a3e9f33db 100644
--- a/epicardium/usb/cdcacm.c
+++ b/epicardium/usb/cdcacm.c
@@ -22,6 +22,8 @@
 #include "FreeRTOS.h"
 #include "task.h"
 
+static volatile int lockup_disable = 0;
+
 static inline struct config_descriptor_cdcacm *
 descriptors(struct esb_config *self)
 {
@@ -36,15 +38,24 @@ static int cb_acm_read_ready(void)
 	return 0;
 }
 
+static int cb_acm_connected(void)
+{
+	lockup_disable = 0;
+	return 0;
+}
+
 int esb_cdc_init(struct esb_config *self)
 {
 	LOG_DEBUG("cdcacm", "init");
 	struct config_descriptor_cdcacm *dsc = descriptors(self);
 	usb_read_complete                    = 0;
+
+	int ret = acm_init(&dsc->comm_interface);
 	acm_register_callback(
 		ACM_CB_READ_READY,
 		cb_acm_read_ready); //SWYM: actually not needed
-	return acm_init(&dsc->comm_interface);
+	acm_register_callback(ACM_CB_CONNECTED, cb_acm_connected);
+	return ret;
 }
 
 int esb_cdc_configure(struct esb_config *self)
@@ -96,7 +107,6 @@ uint8_t cdcacm_read(void)
 
 void cdcacm_write(uint8_t *data, int len)
 {
-	static int lockup_disable = 0;
 	if (acm_present() && !lockup_disable) {
 		int ret = acm_write(data, len);
 		if (ret < 0) {
-- 
GitLab