aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa
diff options
context:
space:
mode:
authorStefan Kerkmann <karlk90@pm.me>2024-10-18 09:57:08 +0200
committerGitHub <noreply@github.com>2024-10-18 09:57:08 +0200
commit3f9d4644126483bbd937f2be75a8878a1c986630 (patch)
tree0c91e51941b048b87c816e05f36e8a6b50753b9b /tmk_core/protocol/lufa
parent80f8aae3ec733cfbd6957a70f5fe436e5b92e725 (diff)
[Core] `usb_device_state`: consolidate usb state handling across implementations (#24258)
* usb_device_state: add idle_rate, led and protocol Previously all usb drivers and platform implementations (expect for our oddball atsam) tracked the same two global variables: - keyboard_protocol: to indicate if we are in report or boot protocol - keyboard_idle: for the idle_rate of the keyboard endpoint And a local variable that was exposed trough some indirection: - keyboard_led_state: for the currently set indicator leds (caps lock etc.) These have all been moved into the usb_device_state struct wich is accessible by getters and setters. This reduces code duplication and centralizes the state management across platforms and drivers. Signed-off-by: Stefan Kerkmann <karlk90@pm.me> * usb_device_state: reset protocol on reset The usb hid specification section 7.2.6 states: When initialized, all devices default to report protocol. However the host should not make any assumptions about the device’s state and should set the desired protocol whenever initializing a device. Thus on reset we should always do exactly that. Signed-off-by: Stefan Kerkmann <karlk90@pm.me> * keyboards: fix oversize warnings Signed-off-by: Stefan Kerkmann <karlk90@pm.me> --------- Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
Diffstat (limited to 'tmk_core/protocol/lufa')
-rw-r--r--tmk_core/protocol/lufa/lufa.c38
1 files changed, 13 insertions, 25 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index b0c9758d2f..81da035f0c 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -72,20 +72,14 @@
# define USB_WAIT_FOR_ENUMERATION
#endif
-uint8_t keyboard_idle = 0;
-/* 0: Boot Protocol, 1: Report Protocol(default) */
-uint8_t keyboard_protocol = 1;
-static uint8_t keyboard_led_state = 0;
-
static report_keyboard_t keyboard_report_sent;
/* Host driver */
-static uint8_t keyboard_leds(void);
-static void send_keyboard(report_keyboard_t *report);
-static void send_nkro(report_nkro_t *report);
-static void send_mouse(report_mouse_t *report);
-static void send_extra(report_extra_t *report);
-host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_nkro, send_mouse, send_extra};
+static void send_keyboard(report_keyboard_t *report);
+static void send_nkro(report_nkro_t *report);
+static void send_mouse(report_mouse_t *report);
+static void send_extra(report_extra_t *report);
+host_driver_t lufa_driver = {.keyboard_leds = usb_device_state_get_leds, .send_keyboard = send_keyboard, .send_nkro = send_nkro, .send_mouse = send_mouse, .send_extra = send_extra};
void send_report(uint8_t endpoint, void *report, size_t size) {
uint8_t timeout = 255;
@@ -271,6 +265,7 @@ void EVENT_USB_Device_Disconnect(void) {
void EVENT_USB_Device_Reset(void) {
print("[R]");
usb_device_state_set_reset();
+ usb_device_state_set_protocol(USB_PROTOCOL_REPORT);
}
/** \brief Event USB Device Connect
@@ -453,10 +448,10 @@ void EVENT_USB_Device_ControlRequest(void) {
uint8_t report_id = Endpoint_Read_8();
if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
- keyboard_led_state = Endpoint_Read_8();
+ usb_device_state_set_leds(Endpoint_Read_8());
}
} else {
- keyboard_led_state = Endpoint_Read_8();
+ usb_device_state_set_leds(Endpoint_Read_8());
}
Endpoint_ClearOUT();
@@ -473,7 +468,7 @@ void EVENT_USB_Device_ControlRequest(void) {
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()))
;
- Endpoint_Write_8(keyboard_protocol);
+ Endpoint_Write_8(usb_device_state_get_protocol());
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
@@ -486,7 +481,7 @@ void EVENT_USB_Device_ControlRequest(void) {
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
- keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
+ usb_device_state_set_protocol(USB_ControlRequest.wValue & 0xFF);
clear_keyboard();
}
}
@@ -497,7 +492,7 @@ void EVENT_USB_Device_ControlRequest(void) {
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
- keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
+ usb_device_state_set_idle_rate(USB_ControlRequest.wValue >> 8);
}
break;
@@ -506,7 +501,7 @@ void EVENT_USB_Device_ControlRequest(void) {
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()))
;
- Endpoint_Write_8(keyboard_idle);
+ Endpoint_Write_8(usb_device_state_get_idle_rate());
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
@@ -522,13 +517,6 @@ void EVENT_USB_Device_ControlRequest(void) {
/*******************************************************************************
* Host driver
******************************************************************************/
-/** \brief Keyboard LEDs
- *
- * FIXME: Needs doc
- */
-static uint8_t keyboard_leds(void) {
- return keyboard_led_state;
-}
/** \brief Send Keyboard
*
@@ -536,7 +524,7 @@ static uint8_t keyboard_leds(void) {
*/
static void send_keyboard(report_keyboard_t *report) {
/* If we're in Boot Protocol, don't send any report ID or other funky fields */
- if (!keyboard_protocol) {
+ if (usb_device_state_get_protocol() == USB_PROTOCOL_BOOT) {
send_report(KEYBOARD_IN_EPNUM, &report->mods, 8);
} else {
send_report(KEYBOARD_IN_EPNUM, report, KEYBOARD_REPORT_SIZE);