Skip to content

Commit f219050

Browse files
committed
Input: libps2 - do not discard non-ack bytes when controlling LEDs
Upon receiving a PS/2 command the device and controller are supposed to stop sending normal data (scancodes or movement packets) and instead immediately start delivering ACK/NAK and command response. Unfortunately often EC has an output buffer which may contain latched data by the time the EC receives a command from the host. The kernel used to ignore such data, but that may cause "stuck" keys if the data dropped happens to be a break code or a part of a break code. This occasionally happens, for example, on Chromebooks when the kernel tries to toggle CapsLock LED on a keyboard while user releases Alt+Search keyboard shortcut. Fix this by passing the first non-ACK byte to the normal handler for a handful of PS/2 commands that are expected to be used during normal device operation (as opposed to probe/configuration time). Reviewed-by: Raul E Rangel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent c4c7eac commit f219050

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

drivers/input/serio/libps2.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
#define PS2_CMD_SETSCALE11 0x00e6
2323
#define PS2_CMD_SETRES 0x10e8
24+
#define PS2_CMD_EX_SETLEDS 0x20eb
25+
#define PS2_CMD_SETLEDS 0x10ed
2426
#define PS2_CMD_GETID 0x02f2
27+
#define PS2_CMD_SETREP 0x10f3 /* Set repeat rate/set report rate */
2528
#define PS2_CMD_RESET_BAT 0x02ff
2629

2730
#define PS2_RET_BAT 0xaa
@@ -35,6 +38,7 @@
3538
#define PS2_FLAG_CMD1 BIT(2) /* Waiting for the first byte of command response */
3639
#define PS2_FLAG_WAITID BIT(3) /* Command executing is GET ID */
3740
#define PS2_FLAG_NAK BIT(4) /* Last transmission was NAKed */
41+
#define PS2_FLAG_PASS_NOACK BIT(5) /* Pass non-ACK byte to receive handler */
3842

3943
static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte,
4044
unsigned int timeout, unsigned int max_attempts)
@@ -281,9 +285,28 @@ int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
281285

282286
serio_pause_rx(ps2dev->serio);
283287

284-
/* Some mice do not ACK the "get ID" command, prepare to handle this. */
285-
ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
286288
ps2dev->cmdcnt = receive;
289+
290+
switch (command) {
291+
case PS2_CMD_GETID:
292+
/*
293+
* Some mice do not ACK the "get ID" command, prepare to
294+
* handle this.
295+
*/
296+
ps2dev->flags = PS2_FLAG_WAITID;
297+
break;
298+
299+
case PS2_CMD_SETLEDS:
300+
case PS2_CMD_EX_SETLEDS:
301+
case PS2_CMD_SETREP:
302+
ps2dev->flags = PS2_FLAG_PASS_NOACK;
303+
break;
304+
305+
default:
306+
ps2dev->flags = 0;
307+
break;
308+
}
309+
287310
if (receive) {
288311
/* Indicate that we expect response to the command. */
289312
ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
@@ -512,14 +535,19 @@ static void ps2_handle_ack(struct ps2dev *ps2dev, u8 data)
512535
* Do not signal errors if we get unexpected reply while
513536
* waiting for an ACK to the initial (first) command byte:
514537
* the device might not be quiesced yet and continue
515-
* delivering data.
538+
* delivering data. For certain commands (such as set leds and
539+
* set repeat rate) that can be used during normal device
540+
* operation, we even pass this data byte to the normal receive
541+
* handler.
516542
* Note that we reset PS2_FLAG_WAITID flag, so the workaround
517543
* for mice not acknowledging the Get ID command only triggers
518544
* on the 1st byte; if device spews data we really want to see
519545
* a real ACK from it.
520546
*/
521547
dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data);
522-
ps2dev->flags &= ~PS2_FLAG_WAITID;
548+
if (ps2dev->flags & PS2_FLAG_PASS_NOACK)
549+
ps2dev->receive_handler(ps2dev, data);
550+
ps2dev->flags &= ~(PS2_FLAG_WAITID | PS2_FLAG_PASS_NOACK);
523551
return;
524552
}
525553

0 commit comments

Comments
 (0)