[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>
This commit is contained in:
parent
80f8aae3ec
commit
3f9d464412
20 changed files with 165 additions and 130 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue