xref: /linux-6.15/include/linux/libps2.h (revision fc522f3b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LIBPS2_H
3 #define _LIBPS2_H
4 
5 /*
6  * Copyright (C) 1999-2002 Vojtech Pavlik
7  * Copyright (C) 2004 Dmitry Torokhov
8  */
9 
10 #include <linux/bitops.h>
11 #include <linux/mutex.h>
12 #include <linux/types.h>
13 #include <linux/wait.h>
14 
15 #define PS2_CMD_SETSCALE11	0x00e6
16 #define PS2_CMD_SETRES		0x10e8
17 #define PS2_CMD_GETID		0x02f2
18 #define PS2_CMD_RESET_BAT	0x02ff
19 
20 #define PS2_RET_BAT		0xaa
21 #define PS2_RET_ID		0x00
22 #define PS2_RET_ACK		0xfa
23 #define PS2_RET_NAK		0xfe
24 #define PS2_RET_ERR		0xfc
25 
26 #define PS2_FLAG_ACK		BIT(0)	/* Waiting for ACK/NAK */
27 #define PS2_FLAG_CMD		BIT(1)	/* Waiting for a command to finish */
28 #define PS2_FLAG_CMD1		BIT(2)	/* Waiting for the first byte of command response */
29 #define PS2_FLAG_WAITID		BIT(3)	/* Command executing is GET ID */
30 #define PS2_FLAG_NAK		BIT(4)	/* Last transmission was NAKed */
31 
32 struct ps2dev {
33 	struct serio *serio;
34 
35 	/* Ensures that only one command is executing at a time */
36 	struct mutex cmd_mutex;
37 
38 	/* Used to signal completion from interrupt handler */
39 	wait_queue_head_t wait;
40 
41 	unsigned long flags;
42 	u8 cmdbuf[8];
43 	u8 cmdcnt;
44 	u8 nak;
45 };
46 
47 void ps2_init(struct ps2dev *ps2dev, struct serio *serio);
48 int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout);
49 void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout);
50 void ps2_begin_command(struct ps2dev *ps2dev);
51 void ps2_end_command(struct ps2dev *ps2dev);
52 int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command);
53 int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command);
54 int ps2_sliced_command(struct ps2dev *ps2dev, u8 command);
55 bool ps2_handle_ack(struct ps2dev *ps2dev, u8 data);
56 bool ps2_handle_response(struct ps2dev *ps2dev, u8 data);
57 void ps2_cmd_aborted(struct ps2dev *ps2dev);
58 bool ps2_is_keyboard_id(u8 id);
59 
60 #endif /* _LIBPS2_H */
61