| 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/interrupt.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/wait.h> |
| 15 | |
| 16 | struct ps2dev; |
| 17 | |
| 18 | /** |
| 19 | * enum ps2_disposition - indicates how received byte should be handled |
| 20 | * @PS2_PROCESS: pass to the main protocol handler, process normally |
| 21 | * @PS2_IGNORE: skip the byte |
| 22 | * @PS2_ERROR: do not process the byte, abort command in progress |
| 23 | */ |
| 24 | enum ps2_disposition { |
| 25 | PS2_PROCESS, |
| 26 | PS2_IGNORE, |
| 27 | PS2_ERROR, |
| 28 | }; |
| 29 | |
| 30 | typedef enum ps2_disposition (*ps2_pre_receive_handler_t)(struct ps2dev *, u8, |
| 31 | unsigned int); |
| 32 | typedef void (*ps2_receive_handler_t)(struct ps2dev *, u8); |
| 33 | |
| 34 | /** |
| 35 | * struct ps2dev - represents a device using PS/2 protocol |
| 36 | * @serio: a serio port used by the PS/2 device |
| 37 | * @cmd_mutex: a mutex ensuring that only one command is executing at a time |
| 38 | * @wait: a waitqueue used to signal completion from the serio interrupt handler |
| 39 | * @flags: various internal flags indicating stages of PS/2 command execution |
| 40 | * @cmdbuf: buffer holding command response |
| 41 | * @cmdcnt: outstanding number of bytes of the command response |
| 42 | * @nak: a byte transmitted by the device when it refuses command |
| 43 | * @pre_receive_handler: checks communication errors and returns disposition |
| 44 | * (&enum ps2_disposition) of the received data byte |
| 45 | * @receive_handler: main handler of particular PS/2 protocol, such as keyboard |
| 46 | * or mouse protocol |
| 47 | */ |
| 48 | struct ps2dev { |
| 49 | struct serio *serio; |
| 50 | struct mutex cmd_mutex; |
| 51 | wait_queue_head_t wait; |
| 52 | unsigned long flags; |
| 53 | u8 cmdbuf[8]; |
| 54 | u8 cmdcnt; |
| 55 | u8 nak; |
| 56 | |
| 57 | ps2_pre_receive_handler_t pre_receive_handler; |
| 58 | ps2_receive_handler_t receive_handler; |
| 59 | }; |
| 60 | |
| 61 | void ps2_init(struct ps2dev *ps2dev, struct serio *serio, |
| 62 | ps2_pre_receive_handler_t pre_receive_handler, |
| 63 | ps2_receive_handler_t receive_handler); |
| 64 | int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout); |
| 65 | void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout); |
| 66 | void ps2_begin_command(struct ps2dev *ps2dev); |
| 67 | void ps2_end_command(struct ps2dev *ps2dev); |
| 68 | int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command); |
| 69 | int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command); |
| 70 | int ps2_sliced_command(struct ps2dev *ps2dev, u8 command); |
| 71 | bool ps2_is_keyboard_id(u8 id); |
| 72 | |
| 73 | irqreturn_t ps2_interrupt(struct serio *serio, u8 data, unsigned int flags); |
| 74 | |
| 75 | #endif /* _LIBPS2_H */ |
| 76 | |