From 80d10bef073e3d32149aa4a137d8016ac999dffc Mon Sep 17 00:00:00 2001
From: Jason Green <jason@jasg.org>
Date: Mon, 1 Aug 2016 05:02:52 +0000
Subject: [PATCH] Added USB Virtual Serial support

---
 Makefile                            |   4 +
 tmk_core/common/virtser.h           |  10 +++
 tmk_core/protocol/lufa.mk           |   4 +
 tmk_core/protocol/lufa/descriptor.c | 112 +++++++++++++++++++++++++++-
 tmk_core/protocol/lufa/descriptor.h |  43 ++++++++++-
 tmk_core/protocol/lufa/lufa.c       | 107 ++++++++++++++++++++++++++
 6 files changed, 276 insertions(+), 4 deletions(-)
 create mode 100644 tmk_core/common/virtser.h

diff --git a/Makefile b/Makefile
index 693edc9f06..331badcf84 100644
--- a/Makefile
+++ b/Makefile
@@ -190,6 +190,10 @@ ifeq ($(strip $(MIDI_ENABLE)), yes)
 	SRC += $(QUANTUM_DIR)/process_keycode/process_midi.c
 endif
 
+ifeq ($(strip $(VIRTSER_ENABLE)), yes)
+    OPT_DEFS += -DVIRTSER_ENABLE
+endif
+
 ifeq ($(strip $(AUDIO_ENABLE)), yes)
     OPT_DEFS += -DAUDIO_ENABLE
 	SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
diff --git a/tmk_core/common/virtser.h b/tmk_core/common/virtser.h
new file mode 100644
index 0000000000..74891b6ae0
--- /dev/null
+++ b/tmk_core/common/virtser.h
@@ -0,0 +1,10 @@
+#ifndef _VIRTSER_H_
+#define _VIRTSER_H_
+
+/* Define this function in your code to process incoming bytes */
+void virtser_recv(const uint8_t ch);
+
+/* Call this to send a character over the Virtual Serial Device */
+void virtser_send(const uint8_t byte);
+
+#endif
diff --git a/tmk_core/protocol/lufa.mk b/tmk_core/protocol/lufa.mk
index 0eeace44ec..5b1e3d19d0 100644
--- a/tmk_core/protocol/lufa.mk
+++ b/tmk_core/protocol/lufa.mk
@@ -26,6 +26,10 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
 	$(TMK_DIR)/protocol/serial_uart.c
 endif
 
+ifeq ($(strip $(VIRTSER_ENABLE)), yes)
+	LUFA_SRC += $(LUFA_ROOT_PATH)/Drivers/USB/Class/Device/CDCClassDevice.c
+endif
+
 SRC += $(LUFA_SRC)
 
 # Search Path
diff --git a/tmk_core/protocol/lufa/descriptor.c b/tmk_core/protocol/lufa/descriptor.c
index 539a58d66b..6f2407f580 100644
--- a/tmk_core/protocol/lufa/descriptor.c
+++ b/tmk_core/protocol/lufa/descriptor.c
@@ -231,9 +231,15 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
     .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
 
     .USBSpecification       = VERSION_BCD(1,1,0),
+#if VIRTSER_ENABLE
+    .Class                  = USB_CSCP_IADDeviceClass,
+    .SubClass               = USB_CSCP_IADDeviceSubclass,
+    .Protocol               = USB_CSCP_IADDeviceProtocol,
+#else
     .Class                  = USB_CSCP_NoDeviceClass,
     .SubClass               = USB_CSCP_NoDeviceSubclass,
     .Protocol               = USB_CSCP_NoDeviceProtocol,
+#endif
 
     .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
 
@@ -643,8 +649,112 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 
             .TotalEmbeddedJacks       = 0x01,
             .AssociatedJackID         = {0x03}
-        }
+        },
 #endif
+
+#ifdef VIRTSER_ENABLE
+    .CDC_Interface_Association =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
+
+                    .FirstInterfaceIndex    = CCI_INTERFACE,
+                    .TotalInterfaces        = 2,
+
+                    .Class                  = CDC_CSCP_CDCClass,
+                    .SubClass               = CDC_CSCP_ACMSubclass,
+                    .Protocol               = CDC_CSCP_ATCommandProtocol,
+
+                    .IADStrIndex            = NO_DESCRIPTOR,
+            },
+
+    .CDC_CCI_Interface =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
+
+                    .InterfaceNumber        = CCI_INTERFACE,
+                    .AlternateSetting       = 0,
+
+                    .TotalEndpoints         = 1,
+
+                    .Class                  = CDC_CSCP_CDCClass,
+                    .SubClass               = CDC_CSCP_ACMSubclass,
+                    .Protocol               = CDC_CSCP_ATCommandProtocol,
+
+                    .InterfaceStrIndex      = NO_DESCRIPTOR
+            },
+
+    .CDC_Functional_Header =
+            {
+                    .Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
+                    .Subtype                = 0x00,
+
+                    .CDCSpecification       = VERSION_BCD(1,1,0),
+            },
+
+    .CDC_Functional_ACM =
+            {
+                    .Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
+                    .Subtype                = 0x02,
+
+                    .Capabilities           = 0x02,
+            },
+
+    .CDC_Functional_Union =
+            {
+                    .Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
+                    .Subtype                = 0x06,
+
+                    .MasterInterfaceNumber  = CCI_INTERFACE,
+                    .SlaveInterfaceNumber   = CDI_INTERFACE,
+            },
+
+    .CDC_NotificationEndpoint =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+                    .EndpointAddress        = CDC_NOTIFICATION_EPADDR,
+                    .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+                    .EndpointSize           = CDC_NOTIFICATION_EPSIZE,
+                    .PollingIntervalMS      = 0xFF
+            },
+
+    .CDC_DCI_Interface =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
+
+                    .InterfaceNumber        = CDI_INTERFACE,
+                    .AlternateSetting       = 0,
+
+                    .TotalEndpoints         = 2,
+
+                    .Class                  = CDC_CSCP_CDCDataClass,
+                    .SubClass               = CDC_CSCP_NoDataSubclass,
+                    .Protocol               = CDC_CSCP_NoDataProtocol,
+
+                    .InterfaceStrIndex      = NO_DESCRIPTOR
+            },
+
+    .CDC_DataOutEndpoint =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+                    .EndpointAddress        = CDC_OUT_EPADDR,
+                    .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+                    .EndpointSize           = CDC_EPSIZE,
+                    .PollingIntervalMS      = 0x05
+            },
+
+    .CDC_DataInEndpoint =
+            {
+                    .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+                    .EndpointAddress        = CDC_IN_EPADDR,
+                    .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+                    .EndpointSize           = CDC_EPSIZE,
+                    .PollingIntervalMS      = 0x05
+            },
+#endif
+
 };
 
 
diff --git a/tmk_core/protocol/lufa/descriptor.h b/tmk_core/protocol/lufa/descriptor.h
index 4fd81a0e87..316650a7b1 100644
--- a/tmk_core/protocol/lufa/descriptor.h
+++ b/tmk_core/protocol/lufa/descriptor.h
@@ -104,6 +104,21 @@ typedef struct
       USB_MIDI_Descriptor_Jack_Endpoint_t       MIDI_Out_Jack_Endpoint_SPC;
 #endif
 
+#ifdef VIRTSER_ENABLE
+        USB_Descriptor_Interface_Association_t   CDC_Interface_Association;
+
+	// CDC Control Interface
+	USB_Descriptor_Interface_t               CDC_CCI_Interface;
+	USB_CDC_Descriptor_FunctionalHeader_t    CDC_Functional_Header;
+	USB_CDC_Descriptor_FunctionalACM_t       CDC_Functional_ACM;
+	USB_CDC_Descriptor_FunctionalUnion_t     CDC_Functional_Union;
+	USB_Descriptor_Endpoint_t                CDC_NotificationEndpoint;
+
+	// CDC Data Interface
+	USB_Descriptor_Interface_t               CDC_DCI_Interface;
+	USB_Descriptor_Endpoint_t                CDC_DataOutEndpoint;
+	USB_Descriptor_Endpoint_t                CDC_DataInEndpoint;
+#endif
 } USB_Descriptor_Configuration_t;
 
 
@@ -141,8 +156,15 @@ typedef struct
 #   define AS_INTERFACE           NKRO_INTERFACE
 #endif
 
+#ifdef VIRTSER_ENABLE
+#   define CCI_INTERFACE         (AS_INTERFACE + 1)
+#   define CDI_INTERFACE         (AS_INTERFACE + 2)
+#else
+#   define CDI_INTERFACE         AS_INTERFACE
+#endif
+
 /* nubmer of interfaces */
-#define TOTAL_INTERFACES            AS_INTERFACE + 1
+#define TOTAL_INTERFACES            (CDI_INTERFACE + 1)
 
 
 // Endopoint number and size
@@ -180,11 +202,24 @@ typedef struct
 #   define MIDI_STREAM_OUT_EPNUM    (NKRO_IN_EPNUM + 2)
 #   define MIDI_STREAM_IN_EPADDR    (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM)
 #   define MIDI_STREAM_OUT_EPADDR   (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM)
+#else
+#   define MIDI_STREAM_OUT_EPNUM     NKRO_IN_EPNUM
+#endif
+
+#ifdef VIRTSER_ENABLE
+#   define CDC_NOTIFICATION_EPNUM   (MIDI_STREAM_OUT_EPNUM + 1)
+#   define CDC_IN_EPNUM		    (MIDI_STREAM_OUT_EPNUM + 2)
+#   define CDC_OUT_EPNUM		    (MIDI_STREAM_OUT_EPNUM + 3)
+#   define CDC_NOTIFICATION_EPADDR        (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM)
+#   define CDC_IN_EPADDR                  (ENDPOINT_DIR_IN | CDC_IN_EPNUM)
+#   define CDC_OUT_EPADDR                  (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM)
+#else
+#   define CDC_OUT_EPNUM	MIDI_STREAM_OUT_EPNUM
 #endif
 
 
-#if defined(__AVR_ATmega32U2__) && MIDI_STREAM_OUT_EPADDR > 4
-# error "Endpoints are not available enough to support all functions. Remove some in Makefile.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI)"
+#if defined(__AVR_ATmega32U2__) && CDC_OUT_EPNUM > 4
+# error "Endpoints are not available enough to support all functions. Remove some in Makefile.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI, SERIAL)"
 #endif
 
 #define KEYBOARD_EPSIZE             8
@@ -193,6 +228,8 @@ typedef struct
 #define CONSOLE_EPSIZE              32
 #define NKRO_EPSIZE                 16
 #define MIDI_STREAM_EPSIZE          64
+#define CDC_NOTIFICATION_EPSIZE     8
+#define CDC_EPSIZE                  16
 
 
 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index 9ca55dbc9d..af73f34d7b 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -60,6 +60,10 @@
     #include "bluetooth.h"
 #endif
 
+#ifdef VIRTSER_ENABLE
+    #include "virtser.h"
+#endif
+
 uint8_t keyboard_idle = 0;
 /* 0: Boot Protocol, 1: Report Protocol(default) */
 uint8_t keyboard_protocol = 1;
@@ -127,6 +131,34 @@ USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
 #define SYS_COMMON_3 0x30
 #endif
 
+#ifdef VIRTSER_ENABLE
+USB_ClassInfo_CDC_Device_t cdc_device =
+{
+  .Config =
+  {
+    .ControlInterfaceNumber = CCI_INTERFACE,
+    .DataINEndpoint         =
+    {
+      .Address		= CDC_IN_EPADDR,
+      .Size		= CDC_EPSIZE,
+      .Banks		= 1,
+    },
+    .DataOUTEndpoint	    =
+    {
+      .Address		= CDC_OUT_EPADDR,
+      .Size		= CDC_EPSIZE,
+      .Banks		= 1,
+    },
+    .NotificationEndpoint   =
+    {
+      .Address		= CDC_NOTIFICATION_EPADDR,
+      .Size		= CDC_NOTIFICATION_EPSIZE,
+      .Banks		= 1,
+    },
+  },
+};
+#endif
+
 
 /*******************************************************************************
  * Console
@@ -311,6 +343,12 @@ void EVENT_USB_Device_ConfigurationChanged(void)
     ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
     ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
 #endif
+
+#ifdef VIRTSER_ENABLE
+    ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
+#endif
 }
 
 /*
@@ -432,10 +470,15 @@ void EVENT_USB_Device_ControlRequest(void)
 
             break;
     }
+
+#ifdef VIRTSER_ENABLE
+    CDC_Device_ProcessControlRequest(&cdc_device);
+#endif
 }
 
 /*******************************************************************************
  * Host driver
+p
  ******************************************************************************/
 static uint8_t keyboard_leds(void)
 {
@@ -827,6 +870,61 @@ void MIDI_Task(void)
 
 #endif
 
+/*******************************************************************************
+ * VIRTUAL SERIAL
+ ******************************************************************************/
+
+#ifdef VIRTSER_ENABLE
+void virtser_init(void)
+{
+  cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
+  CDC_Device_SendControlLineStateChange(&cdc_device);
+}
+
+__attribute__ ((weak))
+void virtser_recv(uint8_t c)
+{
+  // Ignore by default
+}
+
+void virtser_task(void)
+{
+  uint16_t count = CDC_Device_BytesReceived(&cdc_device);
+  uint8_t ch;
+  if (count)
+  {
+    ch = CDC_Device_ReceiveByte(&cdc_device);
+    virtser_recv(ch);
+  }
+}
+void virtser_send(const uint8_t byte)
+{
+  uint8_t timeout = 255;
+  uint8_t ep = Endpoint_GetCurrentEndpoint();
+
+  if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
+  {
+    /* IN packet */
+    Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
+
+    if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
+        Endpoint_SelectEndpoint(ep);
+        return;
+    }
+
+    while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
+
+    Endpoint_Write_8(byte);
+    CDC_Device_Flush(&cdc_device);
+
+    if (Endpoint_IsINReady()) {
+      Endpoint_ClearIN();
+    }
+
+    Endpoint_SelectEndpoint(ep);
+  }
+}
+#endif
 
 /*******************************************************************************
  * main
@@ -918,6 +1016,10 @@ int main(void)
     sleep_led_init();
 #endif
 
+#ifdef VIRTSER_ENABLE
+    virtser_init();
+#endif
+
     print("Keyboard start.\n");
     while (1) {
         #ifndef BLUETOOTH_ENABLE
@@ -936,6 +1038,11 @@ int main(void)
 #endif
         keyboard_task();
 
+#ifdef VIRTSER_ENABLE
+        virtser_task();
+        CDC_Device_USBTask(&cdc_device);
+#endif
+
 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
         USB_USBTask();
 #endif