Просмотр исходного кода

Various tidyups for USB descriptor code (#9005)

Ryan 5 лет назад
Родитель
Сommit
f209f91c7c

+ 4 - 4
tmk_core/protocol/chibios/usb_main.c

@@ -62,7 +62,7 @@ extern keymap_config_t keymap_config;
 
 
 uint8_t                keyboard_idle __attribute__((aligned(2)))     = 0;
 uint8_t                keyboard_idle __attribute__((aligned(2)))     = 0;
 uint8_t                keyboard_protocol __attribute__((aligned(2))) = 1;
 uint8_t                keyboard_protocol __attribute__((aligned(2))) = 1;
-uint8_t                keyboard_led_stats                            = 0;
+uint8_t                keyboard_led_state                            = 0;
 volatile uint16_t      keyboard_idle_count                           = 0;
 volatile uint16_t      keyboard_idle_count                           = 0;
 static virtual_timer_t keyboard_idle_timer;
 static virtual_timer_t keyboard_idle_timer;
 static void            keyboard_idle_timer_cb(void *arg);
 static void            keyboard_idle_timer_cb(void *arg);
@@ -386,10 +386,10 @@ static void    set_led_transfer_cb(USBDriver *usbp) {
     if (usbp->setup[6] == 2) { /* LSB(wLength) */
     if (usbp->setup[6] == 2) { /* LSB(wLength) */
         uint8_t report_id = set_report_buf[0];
         uint8_t report_id = set_report_buf[0];
         if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
         if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
-            keyboard_led_stats = set_report_buf[1];
+            keyboard_led_state = set_report_buf[1];
         }
         }
     } else {
     } else {
-        keyboard_led_stats = set_report_buf[0];
+        keyboard_led_state = set_report_buf[0];
     }
     }
 }
 }
 
 
@@ -610,7 +610,7 @@ static void keyboard_idle_timer_cb(void *arg) {
 }
 }
 
 
 /* LED status */
 /* LED status */
-uint8_t keyboard_leds(void) { return keyboard_led_stats; }
+uint8_t keyboard_leds(void) { return keyboard_led_state; }
 
 
 /* prepare and start sending a report IN
 /* prepare and start sending a report IN
  * not callable from ISR or locked state */
  * not callable from ISR or locked state */

+ 61 - 59
tmk_core/protocol/lufa/lufa.c

@@ -88,7 +88,7 @@ extern keymap_config_t keymap_config;
 uint8_t keyboard_idle = 0;
 uint8_t keyboard_idle = 0;
 /* 0: Boot Protocol, 1: Report Protocol(default) */
 /* 0: Boot Protocol, 1: Report Protocol(default) */
 uint8_t        keyboard_protocol  = 1;
 uint8_t        keyboard_protocol  = 1;
-static uint8_t keyboard_led_stats = 0;
+static uint8_t keyboard_led_state = 0;
 
 
 static report_keyboard_t keyboard_report_sent;
 static report_keyboard_t keyboard_report_sent;
 
 
@@ -103,30 +103,30 @@ host_driver_t  lufa_driver = {
 };
 };
 
 
 #ifdef VIRTSER_ENABLE
 #ifdef VIRTSER_ENABLE
+// clang-format off
+
 USB_ClassInfo_CDC_Device_t cdc_device = {
 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,
-                },
+    .Config = {
+        .ControlInterfaceNumber = CCI_INTERFACE,
+        .DataINEndpoint = {
+            .Address            = (CDC_IN_EPNUM | ENDPOINT_DIR_IN),
+            .Size               = CDC_EPSIZE,
+            .Banks              = 1
+        },
+        .DataOUTEndpoint = {
+            .Address            = (CDC_OUT_EPNUM | ENDPOINT_DIR_OUT),
+            .Size               = CDC_EPSIZE,
+            .Banks              = 1
         },
         },
+        .NotificationEndpoint = {
+            .Address            = (CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN),
+            .Size               = CDC_NOTIFICATION_EPSIZE,
+            .Banks              = 1
+        }
+    }
 };
 };
+
+// clang-format on
 #endif
 #endif
 
 
 #ifdef RAW_ENABLE
 #ifdef RAW_ENABLE
@@ -254,7 +254,7 @@ static void Console_Task(void) {
     // fill empty bank
     // fill empty bank
     while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);
     while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);
 
 
-    // flash senchar packet
+    // flush sendchar packet
     if (Endpoint_IsINReady()) {
     if (Endpoint_IsINReady()) {
         Endpoint_ClearIN();
         Endpoint_ClearIN();
     }
     }
@@ -370,45 +370,46 @@ void EVENT_USB_Device_StartOfFrame(void) {
 void EVENT_USB_Device_ConfigurationChanged(void) {
 void EVENT_USB_Device_ConfigurationChanged(void) {
     bool ConfigSuccess = true;
     bool ConfigSuccess = true;
 
 
-    /* Setup Keyboard HID Report Endpoints */
 #ifndef KEYBOARD_SHARED_EP
 #ifndef KEYBOARD_SHARED_EP
-    ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
+    /* Setup keyboard report endpoint */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((KEYBOARD_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1);
 #endif
 #endif
 
 
 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
-    /* Setup Mouse HID Report Endpoint */
-    ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
+    /* Setup mouse report endpoint */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((MOUSE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, MOUSE_EPSIZE, 1);
 #endif
 #endif
 
 
 #ifdef SHARED_EP_ENABLE
 #ifdef SHARED_EP_ENABLE
-    /* Setup Shared HID Report Endpoint */
-    ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE);
+    /* Setup shared report endpoint */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((SHARED_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, SHARED_EPSIZE, 1);
 #endif
 #endif
 
 
 #ifdef RAW_ENABLE
 #ifdef RAW_ENABLE
-    /* Setup Raw HID Report Endpoints */
-    ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
-    ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
+    /* Setup raw HID endpoints */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
 #endif
 #endif
 
 
 #ifdef CONSOLE_ENABLE
 #ifdef CONSOLE_ENABLE
-    /* Setup Console HID Report Endpoints */
-    ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
+    /* Setup console endpoint */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
 #    if 0
 #    if 0
-    ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
-                                     CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
 #    endif
 #    endif
 #endif
 #endif
 
 
 #ifdef MIDI_ENABLE
 #ifdef MIDI_ENABLE
-    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);
+    /* Setup MIDI stream endpoints */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
 #endif
 #endif
 
 
 #ifdef VIRTSER_ENABLE
 #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);
+    /* Setup virtual serial endpoints */
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1);
+    ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1);
 #endif
 #endif
 }
 }
 
 
@@ -472,10 +473,10 @@ void EVENT_USB_Device_ControlRequest(void) {
                             uint8_t report_id = Endpoint_Read_8();
                             uint8_t report_id = Endpoint_Read_8();
 
 
                             if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
                             if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
-                                keyboard_led_stats = Endpoint_Read_8();
+                                keyboard_led_state = Endpoint_Read_8();
                             }
                             }
                         } else {
                         } else {
-                            keyboard_led_stats = Endpoint_Read_8();
+                            keyboard_led_state = Endpoint_Read_8();
                         }
                         }
 
 
                         Endpoint_ClearOUT();
                         Endpoint_ClearOUT();
@@ -545,7 +546,7 @@ void EVENT_USB_Device_ControlRequest(void) {
  *
  *
  * FIXME: Needs doc
  * FIXME: Needs doc
  */
  */
-static uint8_t keyboard_leds(void) { return keyboard_led_stats; }
+static uint8_t keyboard_leds(void) { return keyboard_led_state; }
 
 
 /** \brief Send Keyboard
 /** \brief Send Keyboard
  *
  *
@@ -808,25 +809,26 @@ ERROR_EXIT:
  ******************************************************************************/
  ******************************************************************************/
 
 
 #ifdef MIDI_ENABLE
 #ifdef MIDI_ENABLE
+// clang-format off
+
 USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = {
 USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = {
-    .Config =
-        {
-            .StreamingInterfaceNumber = AS_INTERFACE,
-            .DataINEndpoint =
-                {
-                    .Address = MIDI_STREAM_IN_EPADDR,
-                    .Size    = MIDI_STREAM_EPSIZE,
-                    .Banks   = 1,
-                },
-            .DataOUTEndpoint =
-                {
-                    .Address = MIDI_STREAM_OUT_EPADDR,
-                    .Size    = MIDI_STREAM_EPSIZE,
-                    .Banks   = 1,
-                },
+    .Config = {
+        .StreamingInterfaceNumber = AS_INTERFACE,
+        .DataINEndpoint = {
+            .Address              = (MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN),
+            .Size                 = MIDI_STREAM_EPSIZE,
+            .Banks                = 1
         },
         },
+        .DataOUTEndpoint = {
+            .Address              = (MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_OUT),
+            .Size                 = MIDI_STREAM_EPSIZE,
+            .Banks                = 1
+        }
+    }
 };
 };
 
 
+// clang-format on
+
 void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); }
 void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); }
 
 
 bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); }
 bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); }

+ 0 - 4
tmk_core/protocol/lufa/lufa.h

@@ -69,8 +69,4 @@ extern host_driver_t lufa_driver;
 #    define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0))
 #    define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0))
 #endif
 #endif
 
 
-#define ENDPOINT_BANK_SINGLE 1
-#define ENDPOINT_BANK_DOUBLE 2
-#define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank)
-
 #endif
 #endif

+ 5 - 5
tmk_core/protocol/usb_descriptor.c

@@ -673,7 +673,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
                 .Size           = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
                 .Size           = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
                 .Type           = DTYPE_Endpoint
                 .Type           = DTYPE_Endpoint
             },
             },
-            .EndpointAddress    = MIDI_STREAM_OUT_EPADDR,
+            .EndpointAddress    = (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM),
             .Attributes         = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
             .Attributes         = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
             .EndpointSize       = MIDI_STREAM_EPSIZE,
             .EndpointSize       = MIDI_STREAM_EPSIZE,
             .PollingIntervalMS  = 0x05
             .PollingIntervalMS  = 0x05
@@ -696,7 +696,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
                 .Size           = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
                 .Size           = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t),
                 .Type           = DTYPE_Endpoint
                 .Type           = DTYPE_Endpoint
             },
             },
-            .EndpointAddress    = MIDI_STREAM_IN_EPADDR,
+            .EndpointAddress    = (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM),
             .Attributes         = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
             .Attributes         = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
             .EndpointSize       = MIDI_STREAM_EPSIZE,
             .EndpointSize       = MIDI_STREAM_EPSIZE,
             .PollingIntervalMS  = 0x05
             .PollingIntervalMS  = 0x05
@@ -774,7 +774,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Type               = DTYPE_Endpoint
             .Type               = DTYPE_Endpoint
         },
         },
-        .EndpointAddress        = CDC_NOTIFICATION_EPADDR,
+        .EndpointAddress        = (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM),
         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .EndpointSize           = CDC_NOTIFICATION_EPSIZE,
         .EndpointSize           = CDC_NOTIFICATION_EPSIZE,
         .PollingIntervalMS      = 0xFF
         .PollingIntervalMS      = 0xFF
@@ -797,7 +797,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Type               = DTYPE_Endpoint
             .Type               = DTYPE_Endpoint
         },
         },
-        .EndpointAddress        = CDC_OUT_EPADDR,
+        .EndpointAddress        = (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM),
         .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .EndpointSize           = CDC_EPSIZE,
         .EndpointSize           = CDC_EPSIZE,
         .PollingIntervalMS      = 0x05
         .PollingIntervalMS      = 0x05
@@ -807,7 +807,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = {
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Size               = sizeof(USB_Descriptor_Endpoint_t),
             .Type               = DTYPE_Endpoint
             .Type               = DTYPE_Endpoint
         },
         },
-        .EndpointAddress        = CDC_IN_EPADDR,
+        .EndpointAddress        = (ENDPOINT_DIR_IN | CDC_IN_EPNUM),
         .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
         .EndpointSize           = CDC_EPSIZE,
         .EndpointSize           = CDC_EPSIZE,
         .PollingIntervalMS      = 0x05
         .PollingIntervalMS      = 0x05

+ 0 - 5
tmk_core/protocol/usb_descriptor.h

@@ -212,17 +212,12 @@ enum usb_endpoints {
 #ifdef MIDI_ENABLE
 #ifdef MIDI_ENABLE
     MIDI_STREAM_IN_EPNUM  = NEXT_EPNUM,
     MIDI_STREAM_IN_EPNUM  = NEXT_EPNUM,
     MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
     MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
-#    define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM)
-#    define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM)
 #endif
 #endif
 
 
 #ifdef VIRTSER_ENABLE
 #ifdef VIRTSER_ENABLE
     CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
     CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
     CDC_IN_EPNUM           = NEXT_EPNUM,
     CDC_IN_EPNUM           = NEXT_EPNUM,
     CDC_OUT_EPNUM          = NEXT_EPNUM,
     CDC_OUT_EPNUM          = NEXT_EPNUM,
-#    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)
 #endif
 #endif
 };
 };
 
 

+ 10 - 14
tmk_core/protocol/vusb/vusb.c

@@ -65,7 +65,7 @@ enum usb_interfaces {
 #    error There are not enough available interfaces to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Raw HID, Console
 #    error There are not enough available interfaces to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Raw HID, Console
 #endif
 #endif
 
 
-static uint8_t vusb_keyboard_leds = 0;
+static uint8_t keyboard_led_state = 0;
 static uint8_t vusb_idle_rate     = 0;
 static uint8_t vusb_idle_rate     = 0;
 
 
 /* Keyboard report send buffer */
 /* Keyboard report send buffer */
@@ -74,13 +74,7 @@ static report_keyboard_t kbuf[KBUF_SIZE];
 static uint8_t           kbuf_head = 0;
 static uint8_t           kbuf_head = 0;
 static uint8_t           kbuf_tail = 0;
 static uint8_t           kbuf_tail = 0;
 
 
-typedef struct {
-    uint8_t modifier;
-    uint8_t reserved;
-    uint8_t keycode[6];
-} keyboard_report_t;
-
-static keyboard_report_t keyboard_report;  // sent to PC
+static report_keyboard_t keyboard_report_sent;
 
 
 #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
 #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
 
 
@@ -218,7 +212,7 @@ static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_sy
 
 
 host_driver_t *vusb_driver(void) { return &driver; }
 host_driver_t *vusb_driver(void) { return &driver; }
 
 
-static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; }
+static uint8_t keyboard_leds(void) { return keyboard_led_state; }
 
 
 static void send_keyboard(report_keyboard_t *report) {
 static void send_keyboard(report_keyboard_t *report) {
     uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
     uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
@@ -232,6 +226,7 @@ static void send_keyboard(report_keyboard_t *report) {
     // NOTE: send key strokes of Macro
     // NOTE: send key strokes of Macro
     usbPoll();
     usbPoll();
     vusb_transfer_keyboard();
     vusb_transfer_keyboard();
+    keyboard_report_sent = *report;
 }
 }
 
 
 typedef struct {
 typedef struct {
@@ -289,9 +284,10 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
     if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
     if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
         if (rq->bRequest == USBRQ_HID_GET_REPORT) {
         if (rq->bRequest == USBRQ_HID_GET_REPORT) {
             debug("GET_REPORT:");
             debug("GET_REPORT:");
-            /* we only have one report type, so don't look at wValue */
-            usbMsgPtr = (usbMsgPtr_t)&keyboard_report;
-            return sizeof(keyboard_report);
+            if (rq->wIndex.word == KEYBOARD_INTERFACE) {
+                usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
+                return sizeof(keyboard_report_sent);
+            }
         } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
         } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
             debug("GET_IDLE: ");
             debug("GET_IDLE: ");
             // debug_hex(vusb_idle_rate);
             // debug_hex(vusb_idle_rate);
@@ -304,7 +300,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
         } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
         } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
             debug("SET_REPORT: ");
             debug("SET_REPORT: ");
             // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
             // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
-            if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
+            if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
                 debug("SET_LED: ");
                 debug("SET_LED: ");
                 last_req.kind = SET_LED;
                 last_req.kind = SET_LED;
                 last_req.len  = rq->wLength.word;
                 last_req.len  = rq->wLength.word;
@@ -330,7 +326,7 @@ uchar usbFunctionWrite(uchar *data, uchar len) {
             debug("SET_LED: ");
             debug("SET_LED: ");
             debug_hex(data[0]);
             debug_hex(data[0]);
             debug("\n");
             debug("\n");
-            vusb_keyboard_leds = data[0];
+            keyboard_led_state = data[0];
             last_req.len       = 0;
             last_req.len       = 0;
             return 1;
             return 1;
             break;
             break;