1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ----------------------------------------------------------------------------
3  * touchkit_ps2.c  --  Driver for eGalax TouchKit PS/2 Touchscreens
4  *
5  * Copyright (C) 2005 by Stefan Lucke
6  * Copyright (C) 2004 by Daniel Ritz
7  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
8  *
9  * Based upon touchkitusb.c
10  *
11  * Vendor documentation is available at:
12  * http://home.eeti.com.tw/web20/drivers/Software%20Programming%20Guide_v2.0.pdf
13  */
14 
15 #include <linux/kernel.h>
16 
17 #include <linux/input.h>
18 #include <linux/serio.h>
19 #include <linux/libps2.h>
20 
21 #include "psmouse.h"
22 #include "touchkit_ps2.h"
23 
24 #define TOUCHKIT_MAX_XC			0x07ff
25 #define TOUCHKIT_MAX_YC			0x07ff
26 
27 #define TOUCHKIT_CMD			0x0a
28 #define TOUCHKIT_CMD_LENGTH		1
29 
30 #define TOUCHKIT_CMD_ACTIVE		'A'
31 #define TOUCHKIT_CMD_FIRMWARE_VERSION	'D'
32 #define TOUCHKIT_CMD_CONTROLLER_TYPE	'E'
33 
34 #define TOUCHKIT_SEND_PARMS(s, r, c)	((s) << 12 | (r) << 8 | (c))
35 
36 #define TOUCHKIT_GET_TOUCHED(packet)	(((packet)[0]) & 0x01)
37 #define TOUCHKIT_GET_X(packet)		(((packet)[1] << 7) | (packet)[2])
38 #define TOUCHKIT_GET_Y(packet)		(((packet)[3] << 7) | (packet)[4])
39 
40 static psmouse_ret_t touchkit_ps2_process_byte(struct psmouse *psmouse)
41 {
42 	unsigned char *packet = psmouse->packet;
43 	struct input_dev *dev = psmouse->dev;
44 
45 	if (psmouse->pktcnt != 5)
46 		return PSMOUSE_GOOD_DATA;
47 
48 	input_report_abs(dev, ABS_X, TOUCHKIT_GET_X(packet));
49 	input_report_abs(dev, ABS_Y, TOUCHKIT_GET_Y(packet));
50 	input_report_key(dev, BTN_TOUCH, TOUCHKIT_GET_TOUCHED(packet));
51 	input_sync(dev);
52 
53 	return PSMOUSE_FULL_PACKET;
54 }
55 
56 int touchkit_ps2_detect(struct psmouse *psmouse, bool set_properties)
57 {
58 	struct input_dev *dev = psmouse->dev;
59 	unsigned char param[3];
60 	int command;
61 
62 	param[0] = TOUCHKIT_CMD_LENGTH;
63 	param[1] = TOUCHKIT_CMD_ACTIVE;
64 	command = TOUCHKIT_SEND_PARMS(2, 3, TOUCHKIT_CMD);
65 
66 	if (ps2_command(&psmouse->ps2dev, param, command))
67 		return -ENODEV;
68 
69 	if (param[0] != TOUCHKIT_CMD || param[1] != 0x01 ||
70 	    param[2] != TOUCHKIT_CMD_ACTIVE)
71 		return -ENODEV;
72 
73 	if (set_properties) {
74 		dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
75 		dev->keybit[BIT_WORD(BTN_MOUSE)] = 0;
76 		dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
77 		input_set_abs_params(dev, ABS_X, 0, TOUCHKIT_MAX_XC, 0, 0);
78 		input_set_abs_params(dev, ABS_Y, 0, TOUCHKIT_MAX_YC, 0, 0);
79 
80 		psmouse->vendor = "eGalax";
81 		psmouse->name = "Touchscreen";
82 		psmouse->protocol_handler = touchkit_ps2_process_byte;
83 		psmouse->pktsize = 5;
84 	}
85 
86 	return 0;
87 }
88