Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUFA: Consolidate report sending code #18629

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 43 additions & 118 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ 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_mouse, send_extra};

void send_report(uint8_t endpoint, void *report, size_t size) {
uint8_t timeout = 255;

if (USB_DeviceState != DEVICE_STATE_Configured) return;

Endpoint_SelectEndpoint(endpoint);

/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed()) {
_delay_us(40);
}
if (!Endpoint_IsReadWriteAllowed()) return;

Endpoint_Write_Stream_LE(report, size, NULL);
Endpoint_ClearIN();
}

#ifdef VIRTSER_ENABLE
// clang-format off

Expand Down Expand Up @@ -121,30 +138,8 @@ USB_ClassInfo_CDC_Device_t cdc_device = {
* FIXME: Needs doc
*/
void raw_hid_send(uint8_t *data, uint8_t length) {
// TODO: implement variable size packet
if (length != RAW_EPSIZE) {
return;
}

if (USB_DeviceState != DEVICE_STATE_Configured) {
return;
}

// TODO: decide if we allow calls to raw_hid_send() in the middle
// of other endpoint usage.
uint8_t ep = Endpoint_GetCurrentEndpoint();

Endpoint_SelectEndpoint(RAW_IN_EPNUM);

// Check to see if the host is ready to accept another packet
if (Endpoint_IsINReady()) {
// Write data
Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
// Finalize the stream transfer to send the last packet
Endpoint_ClearIN();
}

Endpoint_SelectEndpoint(ep);
if (length != RAW_EPSIZE) return;
send_report(RAW_IN_EPNUM, data, RAW_EPSIZE);
}

/** \brief Raw HID Receive
Expand Down Expand Up @@ -249,29 +244,6 @@ static void Console_Task(void) {
}
#endif

/*******************************************************************************
* Joystick
******************************************************************************/
void send_joystick(report_joystick_t *report) {
#ifdef JOYSTICK_ENABLE
uint8_t timeout = 255;

/* Select the Joystick Report Endpoint */
Endpoint_SelectEndpoint(JOYSTICK_IN_EPNUM);

/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;

/* Write Joystick Report Data */
Endpoint_Write_Stream_LE(report, sizeof(report_joystick_t), NULL);

/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
#endif
}

/*******************************************************************************
* USB Events
******************************************************************************/
Expand Down Expand Up @@ -587,32 +559,23 @@ static uint8_t keyboard_leds(void) {
* FIXME: Needs doc
*/
static void send_keyboard(report_keyboard_t *report) {
uint8_t timeout = 255;

/* Select the Keyboard Report Endpoint */
uint8_t ep = KEYBOARD_IN_EPNUM;
uint8_t size = KEYBOARD_REPORT_SIZE;
#ifdef NKRO_ENABLE
if (keyboard_protocol && keymap_config.nkro) {
ep = SHARED_IN_EPNUM;
size = sizeof(struct nkro_report);
}
#endif
Endpoint_SelectEndpoint(ep);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;

/* If we're in Boot Protocol, don't send any report ID or other funky fields */
if (!keyboard_protocol) {
Endpoint_Write_Stream_LE(&report->mods, 8, NULL);
send_report(ep, &report->mods, 8);
} else {
Endpoint_Write_Stream_LE(report, size, NULL);
}
#ifdef NKRO_ENABLE
if (keymap_config.nkro) {
ep = SHARED_IN_EPNUM;
size = sizeof(struct nkro_report);
}
#endif

/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
send_report(ep, report, size);
}

keyboard_report_sent = *report;
}
Expand All @@ -623,55 +586,35 @@ static void send_keyboard(report_keyboard_t *report) {
*/
static void send_mouse(report_mouse_t *report) {
#ifdef MOUSE_ENABLE
uint8_t timeout = 255;

/* Select the Mouse Report Endpoint */
Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);

/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;

/* Write Mouse Report Data */
Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);

/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
#endif
}

#if defined(EXTRAKEY_ENABLE) || defined(PROGRAMMABLE_BUTTON_ENABLE)
static void send_report(void *report, size_t size) {
uint8_t timeout = 255;

if (USB_DeviceState != DEVICE_STATE_Configured) return;

Endpoint_SelectEndpoint(SHARED_IN_EPNUM);

/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;

Endpoint_Write_Stream_LE(report, size, NULL);
Endpoint_ClearIN();
}
#endif

/** \brief Send Extra
*
* FIXME: Needs doc
*/
static void send_extra(report_extra_t *report) {
#ifdef EXTRAKEY_ENABLE
send_report(report, sizeof(report_extra_t));
send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t));
#endif
}

void send_joystick(report_joystick_t *report) {
#ifdef JOYSTICK_ENABLE
send_report(JOYSTICK_IN_EPNUM, report, sizeof(report_joystick_t));
#endif
}

void send_programmable_button(report_programmable_button_t *report) {
#ifdef PROGRAMMABLE_BUTTON_ENABLE
send_report(report, sizeof(report_programmable_button_t));
send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t));
#endif
}

void send_digitizer(report_digitizer_t *report) {
#ifdef DIGITIZER_ENABLE
send_report(DIGITIZER_IN_EPNUM, report, sizeof(report_digitizer_t));
#endif
}

Expand Down Expand Up @@ -844,24 +787,6 @@ void virtser_send(const uint8_t byte) {
}
#endif

void send_digitizer(report_digitizer_t *report) {
#ifdef DIGITIZER_ENABLE
uint8_t timeout = 255;

if (USB_DeviceState != DEVICE_STATE_Configured) return;

Endpoint_SelectEndpoint(DIGITIZER_IN_EPNUM);

/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;

Endpoint_Write_Stream_LE(report, sizeof(report_digitizer_t), NULL);
Endpoint_ClearIN();
#endif
}

/*******************************************************************************
* main
******************************************************************************/
Expand Down