Commit c4c7eac8 authored by Dmitry Torokhov's avatar Dmitry Torokhov

Input: libps2 - introduce common interrupt handler

Instead of exposing inner workings of libps2 to drivers such as atkbd and
psmouse, have them define pre-receive and receive callbacks, and provide a
common handler that can be used with underlying serio port.

While at this add kerneldoc to the module.

Link: https://lore.kernel.org/r/ZGK81cxqjr/KS1kA@google.comSigned-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent 7d674f91
...@@ -399,46 +399,60 @@ static unsigned int atkbd_compat_scancode(struct atkbd *atkbd, unsigned int code ...@@ -399,46 +399,60 @@ static unsigned int atkbd_compat_scancode(struct atkbd *atkbd, unsigned int code
} }
/* /*
* atkbd_interrupt(). Here takes place processing of data received from * Tries to handle frame or parity error by requesting the keyboard controller
* the keyboard into events. * to resend the last byte. This historically not done on x86 as controllers
* there typically do not implement this command.
*/ */
static bool __maybe_unused atkbd_handle_frame_error(struct ps2dev *ps2dev,
static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data, u8 data, unsigned int flags)
unsigned int flags)
{ {
struct atkbd *atkbd = atkbd_from_serio(serio); struct atkbd *atkbd = container_of(ps2dev, struct atkbd, ps2dev);
struct input_dev *dev = atkbd->dev; struct serio *serio = ps2dev->serio;
unsigned int code = data;
int scroll = 0, hscroll = 0, click = -1;
int value;
unsigned short keycode;
dev_dbg(&serio->dev, "Received %02x flags %02x\n", data, flags); if ((flags & (SERIO_FRAME | SERIO_PARITY)) &&
(~flags & SERIO_TIMEOUT) &&
#if !defined(__i386__) && !defined (__x86_64__) !atkbd->resend && atkbd->write) {
if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) {
dev_warn(&serio->dev, "Frame/parity error: %02x\n", flags); dev_warn(&serio->dev, "Frame/parity error: %02x\n", flags);
serio_write(serio, ATKBD_CMD_RESEND); serio_write(serio, ATKBD_CMD_RESEND);
atkbd->resend = true; atkbd->resend = true;
goto out; return true;
} }
if (!flags && data == ATKBD_RET_ACK) if (!flags && data == ATKBD_RET_ACK)
atkbd->resend = false; atkbd->resend = false;
return false;
}
static enum ps2_disposition atkbd_pre_receive_byte(struct ps2dev *ps2dev,
u8 data, unsigned int flags)
{
struct serio *serio = ps2dev->serio;
dev_dbg(&serio->dev, "Received %02x flags %02x\n", data, flags);
#if !defined(__i386__) && !defined (__x86_64__)
if (atkbd_handle_frame_error(ps2dev, data, flags))
return PS2_IGNORE;
#endif #endif
if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_ACK)) return PS2_PROCESS;
if (ps2_handle_ack(&atkbd->ps2dev, data)) }
goto out;
if (unlikely(atkbd->ps2dev.flags & PS2_FLAG_CMD)) static void atkbd_receive_byte(struct ps2dev *ps2dev, u8 data)
if (ps2_handle_response(&atkbd->ps2dev, data)) {
goto out; struct serio *serio = ps2dev->serio;
struct atkbd *atkbd = container_of(ps2dev, struct atkbd, ps2dev);
struct input_dev *dev = atkbd->dev;
unsigned int code = data;
int scroll = 0, hscroll = 0, click = -1;
int value;
unsigned short keycode;
pm_wakeup_event(&serio->dev, 0); pm_wakeup_event(&serio->dev, 0);
if (!atkbd->enabled) if (!atkbd->enabled)
goto out; return;
input_event(dev, EV_MSC, MSC_RAW, code); input_event(dev, EV_MSC, MSC_RAW, code);
...@@ -460,16 +474,16 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data, ...@@ -460,16 +474,16 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
case ATKBD_RET_BAT: case ATKBD_RET_BAT:
atkbd->enabled = false; atkbd->enabled = false;
serio_reconnect(atkbd->ps2dev.serio); serio_reconnect(atkbd->ps2dev.serio);
goto out; return;
case ATKBD_RET_EMUL0: case ATKBD_RET_EMUL0:
atkbd->emul = 1; atkbd->emul = 1;
goto out; return;
case ATKBD_RET_EMUL1: case ATKBD_RET_EMUL1:
atkbd->emul = 2; atkbd->emul = 2;
goto out; return;
case ATKBD_RET_RELEASE: case ATKBD_RET_RELEASE:
atkbd->release = true; atkbd->release = true;
goto out; return;
case ATKBD_RET_ACK: case ATKBD_RET_ACK:
case ATKBD_RET_NAK: case ATKBD_RET_NAK:
if (printk_ratelimit()) if (printk_ratelimit())
...@@ -477,18 +491,18 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data, ...@@ -477,18 +491,18 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
"Spurious %s on %s. " "Spurious %s on %s. "
"Some program might be trying to access hardware directly.\n", "Some program might be trying to access hardware directly.\n",
data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys); data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
goto out; return;
case ATKBD_RET_ERR: case ATKBD_RET_ERR:
atkbd->err_count++; atkbd->err_count++;
dev_dbg(&serio->dev, "Keyboard on %s reports too many keys pressed.\n", dev_dbg(&serio->dev, "Keyboard on %s reports too many keys pressed.\n",
serio->phys); serio->phys);
goto out; return;
} }
code = atkbd_compat_scancode(atkbd, code); code = atkbd_compat_scancode(atkbd, code);
if (atkbd->emul && --atkbd->emul) if (atkbd->emul && --atkbd->emul)
goto out; return;
keycode = atkbd->keycode[code]; keycode = atkbd->keycode[code];
...@@ -564,8 +578,6 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data, ...@@ -564,8 +578,6 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
} }
atkbd->release = false; atkbd->release = false;
out:
return IRQ_HANDLED;
} }
static int atkbd_set_repeat_rate(struct atkbd *atkbd) static int atkbd_set_repeat_rate(struct atkbd *atkbd)
...@@ -1229,7 +1241,8 @@ static int atkbd_connect(struct serio *serio, struct serio_driver *drv) ...@@ -1229,7 +1241,8 @@ static int atkbd_connect(struct serio *serio, struct serio_driver *drv)
goto fail1; goto fail1;
atkbd->dev = dev; atkbd->dev = dev;
ps2_init(&atkbd->ps2dev, serio); ps2_init(&atkbd->ps2dev, serio,
atkbd_pre_receive_byte, atkbd_receive_byte);
INIT_DELAYED_WORK(&atkbd->event_work, atkbd_event_work); INIT_DELAYED_WORK(&atkbd->event_work, atkbd_event_work);
mutex_init(&atkbd->mutex); mutex_init(&atkbd->mutex);
...@@ -1385,7 +1398,7 @@ static struct serio_driver atkbd_drv = { ...@@ -1385,7 +1398,7 @@ static struct serio_driver atkbd_drv = {
}, },
.description = DRIVER_DESC, .description = DRIVER_DESC,
.id_table = atkbd_serio_ids, .id_table = atkbd_serio_ids,
.interrupt = atkbd_interrupt, .interrupt = ps2_interrupt,
.connect = atkbd_connect, .connect = atkbd_connect,
.reconnect = atkbd_reconnect, .reconnect = atkbd_reconnect,
.disconnect = atkbd_disconnect, .disconnect = atkbd_disconnect,
......
...@@ -336,17 +336,14 @@ static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data) ...@@ -336,17 +336,14 @@ static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data)
} }
} }
/* static enum ps2_disposition psmouse_pre_receive_byte(struct ps2dev *ps2dev,
* psmouse_interrupt() handles incoming characters, either passing them u8 data,
* for normal processing or gathering them as command response. unsigned int flags)
*/
static irqreturn_t psmouse_interrupt(struct serio *serio,
u8 data, unsigned int flags)
{ {
struct psmouse *psmouse = psmouse_from_serio(serio); struct psmouse *psmouse = container_of(ps2dev, struct psmouse, ps2dev);
if (psmouse->state == PSMOUSE_IGNORE) if (psmouse->state == PSMOUSE_IGNORE)
goto out; return PS2_IGNORE;
if (unlikely((flags & SERIO_TIMEOUT) || if (unlikely((flags & SERIO_TIMEOUT) ||
((flags & SERIO_PARITY) && ((flags & SERIO_PARITY) &&
...@@ -357,27 +354,25 @@ static irqreturn_t psmouse_interrupt(struct serio *serio, ...@@ -357,27 +354,25 @@ static irqreturn_t psmouse_interrupt(struct serio *serio,
"bad data from KBC -%s%s\n", "bad data from KBC -%s%s\n",
flags & SERIO_TIMEOUT ? " timeout" : "", flags & SERIO_TIMEOUT ? " timeout" : "",
flags & SERIO_PARITY ? " bad parity" : ""); flags & SERIO_PARITY ? " bad parity" : "");
ps2_cmd_aborted(&psmouse->ps2dev); return PS2_ERROR;
goto out;
} }
if (flags & SERIO_OOB_DATA) { if (flags & SERIO_OOB_DATA) {
psmouse_handle_oob_data(psmouse, data); psmouse_handle_oob_data(psmouse, data);
goto out; return PS2_IGNORE;
} }
if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK)) return PS2_PROCESS;
if (ps2_handle_ack(&psmouse->ps2dev, data)) }
goto out;
if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD)) static void psmouse_receive_byte(struct ps2dev *ps2dev, u8 data)
if (ps2_handle_response(&psmouse->ps2dev, data)) {
goto out; struct psmouse *psmouse = container_of(ps2dev, struct psmouse, ps2dev);
pm_wakeup_event(&serio->dev, 0); pm_wakeup_event(&ps2dev->serio->dev, 0);
if (psmouse->state <= PSMOUSE_RESYNCING) if (psmouse->state <= PSMOUSE_RESYNCING)
goto out; return;
if (psmouse->state == PSMOUSE_ACTIVATED && if (psmouse->state == PSMOUSE_ACTIVATED &&
psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) { psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
...@@ -386,7 +381,7 @@ static irqreturn_t psmouse_interrupt(struct serio *serio, ...@@ -386,7 +381,7 @@ static irqreturn_t psmouse_interrupt(struct serio *serio,
psmouse->badbyte = psmouse->packet[0]; psmouse->badbyte = psmouse->packet[0];
__psmouse_set_state(psmouse, PSMOUSE_RESYNCING); __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
psmouse_queue_work(psmouse, &psmouse->resync_work, 0); psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
goto out; return;
} }
psmouse->packet[psmouse->pktcnt++] = data; psmouse->packet[psmouse->pktcnt++] = data;
...@@ -395,21 +390,21 @@ static irqreturn_t psmouse_interrupt(struct serio *serio, ...@@ -395,21 +390,21 @@ static irqreturn_t psmouse_interrupt(struct serio *serio,
if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) { if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
if (psmouse->pktcnt == 1) { if (psmouse->pktcnt == 1) {
psmouse->last = jiffies; psmouse->last = jiffies;
goto out; return;
} }
if (psmouse->packet[1] == PSMOUSE_RET_ID || if (psmouse->packet[1] == PSMOUSE_RET_ID ||
(psmouse->protocol->type == PSMOUSE_HGPK && (psmouse->protocol->type == PSMOUSE_HGPK &&
psmouse->packet[1] == PSMOUSE_RET_BAT)) { psmouse->packet[1] == PSMOUSE_RET_BAT)) {
__psmouse_set_state(psmouse, PSMOUSE_IGNORE); __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
serio_reconnect(serio); serio_reconnect(ps2dev->serio);
goto out; return;
} }
/* Not a new device, try processing first byte normally */ /* Not a new device, try processing first byte normally */
psmouse->pktcnt = 1; psmouse->pktcnt = 1;
if (psmouse_handle_byte(psmouse)) if (psmouse_handle_byte(psmouse))
goto out; return;
psmouse->packet[psmouse->pktcnt++] = data; psmouse->packet[psmouse->pktcnt++] = data;
} }
...@@ -424,14 +419,11 @@ static irqreturn_t psmouse_interrupt(struct serio *serio, ...@@ -424,14 +419,11 @@ static irqreturn_t psmouse_interrupt(struct serio *serio,
psmouse->badbyte = psmouse->packet[0]; psmouse->badbyte = psmouse->packet[0];
__psmouse_set_state(psmouse, PSMOUSE_RESYNCING); __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
psmouse_queue_work(psmouse, &psmouse->resync_work, 0); psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
goto out; return;
} }
psmouse->last = jiffies; psmouse->last = jiffies;
psmouse_handle_byte(psmouse); psmouse_handle_byte(psmouse);
out:
return IRQ_HANDLED;
} }
/* /*
...@@ -1604,7 +1596,8 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv) ...@@ -1604,7 +1596,8 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
if (!psmouse || !input_dev) if (!psmouse || !input_dev)
goto err_free; goto err_free;
ps2_init(&psmouse->ps2dev, serio); ps2_init(&psmouse->ps2dev, serio,
psmouse_pre_receive_byte, psmouse_receive_byte);
INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync); INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
psmouse->dev = input_dev; psmouse->dev = input_dev;
snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys); snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
...@@ -1786,7 +1779,7 @@ static struct serio_driver psmouse_drv = { ...@@ -1786,7 +1779,7 @@ static struct serio_driver psmouse_drv = {
}, },
.description = DRIVER_DESC, .description = DRIVER_DESC,
.id_table = psmouse_serio_ids, .id_table = psmouse_serio_ids,
.interrupt = psmouse_interrupt, .interrupt = ps2_interrupt,
.connect = psmouse_connect, .connect = psmouse_connect,
.reconnect = psmouse_reconnect, .reconnect = psmouse_reconnect,
.fast_reconnect = psmouse_fast_reconnect, .fast_reconnect = psmouse_fast_reconnect,
......
This diff is collapsed.
...@@ -8,43 +8,59 @@ ...@@ -8,43 +8,59 @@
*/ */
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/wait.h> #include <linux/wait.h>
#define PS2_CMD_SETSCALE11 0x00e6 struct ps2dev;
#define PS2_CMD_SETRES 0x10e8
#define PS2_CMD_GETID 0x02f2
#define PS2_CMD_RESET_BAT 0x02ff
#define PS2_RET_BAT 0xaa /**
#define PS2_RET_ID 0x00 * enum ps2_disposition - indicates how received byte should be handled
#define PS2_RET_ACK 0xfa * @PS2_PROCESS: pass to the main protocol handler, process normally
#define PS2_RET_NAK 0xfe * @PS2_IGNORE: skip the byte
#define PS2_RET_ERR 0xfc * @PS2_ERROR: do not process the byte, abort command in progress
*/
enum ps2_disposition {
PS2_PROCESS,
PS2_IGNORE,
PS2_ERROR,
};
#define PS2_FLAG_ACK BIT(0) /* Waiting for ACK/NAK */ typedef enum ps2_disposition (*ps2_pre_receive_handler_t)(struct ps2dev *, u8,
#define PS2_FLAG_CMD BIT(1) /* Waiting for a command to finish */ unsigned int);
#define PS2_FLAG_CMD1 BIT(2) /* Waiting for the first byte of command response */ typedef void (*ps2_receive_handler_t)(struct ps2dev *, u8);
#define PS2_FLAG_WAITID BIT(3) /* Command executing is GET ID */
#define PS2_FLAG_NAK BIT(4) /* Last transmission was NAKed */
/**
* struct ps2dev - represents a device using PS/2 protocol
* @serio: a serio port used by the PS/2 device
* @cmd_mutex: a mutex ensuring that only one command is executing at a time
* @wait: a waitqueue used to signal completion from the serio interrupt handler
* @flags: various internal flags indicating stages of PS/2 command execution
* @cmdbuf: buffer holding command response
* @cmdcnt: outstanding number of bytes of the command response
* @nak: a byte transmitted by the device when it refuses command
* @pre_receive_handler: checks communication errors and returns disposition
* (&enum ps2_disposition) of the received data byte
* @receive_handler: main handler of particular PS/2 protocol, such as keyboard
* or mouse protocol
*/
struct ps2dev { struct ps2dev {
struct serio *serio; struct serio *serio;
/* Ensures that only one command is executing at a time */
struct mutex cmd_mutex; struct mutex cmd_mutex;
/* Used to signal completion from interrupt handler */
wait_queue_head_t wait; wait_queue_head_t wait;
unsigned long flags; unsigned long flags;
u8 cmdbuf[8]; u8 cmdbuf[8];
u8 cmdcnt; u8 cmdcnt;
u8 nak; u8 nak;
ps2_pre_receive_handler_t pre_receive_handler;
ps2_receive_handler_t receive_handler;
}; };
void ps2_init(struct ps2dev *ps2dev, struct serio *serio); void ps2_init(struct ps2dev *ps2dev, struct serio *serio,
ps2_pre_receive_handler_t pre_receive_handler,
ps2_receive_handler_t receive_handler);
int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout); int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout);
void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout); void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout);
void ps2_begin_command(struct ps2dev *ps2dev); void ps2_begin_command(struct ps2dev *ps2dev);
...@@ -52,9 +68,8 @@ void ps2_end_command(struct ps2dev *ps2dev); ...@@ -52,9 +68,8 @@ void ps2_end_command(struct ps2dev *ps2dev);
int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command); int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command);
int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command); int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command);
int ps2_sliced_command(struct ps2dev *ps2dev, u8 command); int ps2_sliced_command(struct ps2dev *ps2dev, u8 command);
bool ps2_handle_ack(struct ps2dev *ps2dev, u8 data);
bool ps2_handle_response(struct ps2dev *ps2dev, u8 data);
void ps2_cmd_aborted(struct ps2dev *ps2dev);
bool ps2_is_keyboard_id(u8 id); bool ps2_is_keyboard_id(u8 id);
irqreturn_t ps2_interrupt(struct serio *serio, u8 data, unsigned int flags);
#endif /* _LIBPS2_H */ #endif /* _LIBPS2_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment