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

Support PS/2 mouse 9-bit output with MOUSE_EXTENDED_REPORT #20734

Merged
merged 2 commits into from
May 20, 2023
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
27 changes: 19 additions & 8 deletions drivers/ps2/ps2_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ void ps2_mouse_task(void) {
rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
if (rcv == PS2_ACK) {
mouse_report.buttons = ps2_host_recv_response();
mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
mouse_report.x = ps2_host_recv_response();
mouse_report.y = ps2_host_recv_response();
# ifdef PS2_MOUSE_ENABLE_SCROLLING
mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK);
# endif
} else {
if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
}
#else
if (pbuf_has_data()) {
mouse_report.buttons = ps2_host_recv_response();
mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
mouse_report.x = ps2_host_recv_response();
mouse_report.y = ps2_host_recv_response();
# ifdef PS2_MOUSE_ENABLE_SCROLLING
mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK);
# endif
} else {
if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
Expand Down Expand Up @@ -168,15 +168,26 @@ void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
#define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW))
#define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW))
static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
#ifndef MOUSE_EXTENDED_REPORT
// PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
// bit: 8 7 ... 0
// sign \8-bit/
//
// Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
//
// This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
mouse_report->x *= PS2_MOUSE_X_MULTIPLIER;
mouse_report->y *= PS2_MOUSE_Y_MULTIPLIER;
mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
#else
// Sign extend if negative, otherwise leave positive 8-bits as-is
mouse_report->x = X_IS_NEG ? (mouse_report->x | ~0xFF) : mouse_report->x;
mouse_report->y = Y_IS_NEG ? (mouse_report->y | ~0xFF) : mouse_report->y;
mouse_report->x *= PS2_MOUSE_X_MULTIPLIER;
mouse_report->y *= PS2_MOUSE_Y_MULTIPLIER;
#endif
mouse_report->v *= PS2_MOUSE_V_MULTIPLIER;

#ifdef PS2_MOUSE_INVERT_BUTTONS
// swap left & right buttons
Expand All @@ -197,8 +208,8 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report)
#endif

#ifdef PS2_MOUSE_ROTATE
int8_t x = mouse_report->x;
int8_t y = mouse_report->y;
mouse_xy_report_t x = mouse_report->x;
mouse_xy_report_t y = mouse_report->y;
# if PS2_MOUSE_ROTATE == 90
mouse_report->x = y;
mouse_report->y = -x;
Expand Down