18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------
38c2ecf20Sopenharmony_ci * touchkit_ps2.c  --  Driver for eGalax TouchKit PS/2 Touchscreens
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2005 by Stefan Lucke
68c2ecf20Sopenharmony_ci * Copyright (C) 2004 by Daniel Ritz
78c2ecf20Sopenharmony_ci * Copyright (C) by Todd E. Johnson (mtouchusb.c)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based upon touchkitusb.c
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Vendor documentation is available at:
128c2ecf20Sopenharmony_ci * http://home.eeti.com.tw/web20/drivers/Software%20Programming%20Guide_v2.0.pdf
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/input.h>
188c2ecf20Sopenharmony_ci#include <linux/serio.h>
198c2ecf20Sopenharmony_ci#include <linux/libps2.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "psmouse.h"
228c2ecf20Sopenharmony_ci#include "touchkit_ps2.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define TOUCHKIT_MAX_XC			0x07ff
258c2ecf20Sopenharmony_ci#define TOUCHKIT_MAX_YC			0x07ff
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define TOUCHKIT_CMD			0x0a
288c2ecf20Sopenharmony_ci#define TOUCHKIT_CMD_LENGTH		1
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define TOUCHKIT_CMD_ACTIVE		'A'
318c2ecf20Sopenharmony_ci#define TOUCHKIT_CMD_FIRMWARE_VERSION	'D'
328c2ecf20Sopenharmony_ci#define TOUCHKIT_CMD_CONTROLLER_TYPE	'E'
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define TOUCHKIT_SEND_PARMS(s, r, c)	((s) << 12 | (r) << 8 | (c))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define TOUCHKIT_GET_TOUCHED(packet)	(((packet)[0]) & 0x01)
378c2ecf20Sopenharmony_ci#define TOUCHKIT_GET_X(packet)		(((packet)[1] << 7) | (packet)[2])
388c2ecf20Sopenharmony_ci#define TOUCHKIT_GET_Y(packet)		(((packet)[3] << 7) | (packet)[4])
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic psmouse_ret_t touchkit_ps2_process_byte(struct psmouse *psmouse)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	unsigned char *packet = psmouse->packet;
438c2ecf20Sopenharmony_ci	struct input_dev *dev = psmouse->dev;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (psmouse->pktcnt != 5)
468c2ecf20Sopenharmony_ci		return PSMOUSE_GOOD_DATA;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	input_report_abs(dev, ABS_X, TOUCHKIT_GET_X(packet));
498c2ecf20Sopenharmony_ci	input_report_abs(dev, ABS_Y, TOUCHKIT_GET_Y(packet));
508c2ecf20Sopenharmony_ci	input_report_key(dev, BTN_TOUCH, TOUCHKIT_GET_TOUCHED(packet));
518c2ecf20Sopenharmony_ci	input_sync(dev);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return PSMOUSE_FULL_PACKET;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciint touchkit_ps2_detect(struct psmouse *psmouse, bool set_properties)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct input_dev *dev = psmouse->dev;
598c2ecf20Sopenharmony_ci	unsigned char param[3];
608c2ecf20Sopenharmony_ci	int command;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	param[0] = TOUCHKIT_CMD_LENGTH;
638c2ecf20Sopenharmony_ci	param[1] = TOUCHKIT_CMD_ACTIVE;
648c2ecf20Sopenharmony_ci	command = TOUCHKIT_SEND_PARMS(2, 3, TOUCHKIT_CMD);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (ps2_command(&psmouse->ps2dev, param, command))
678c2ecf20Sopenharmony_ci		return -ENODEV;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (param[0] != TOUCHKIT_CMD || param[1] != 0x01 ||
708c2ecf20Sopenharmony_ci	    param[2] != TOUCHKIT_CMD_ACTIVE)
718c2ecf20Sopenharmony_ci		return -ENODEV;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (set_properties) {
748c2ecf20Sopenharmony_ci		dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
758c2ecf20Sopenharmony_ci		dev->keybit[BIT_WORD(BTN_MOUSE)] = 0;
768c2ecf20Sopenharmony_ci		dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
778c2ecf20Sopenharmony_ci		input_set_abs_params(dev, ABS_X, 0, TOUCHKIT_MAX_XC, 0, 0);
788c2ecf20Sopenharmony_ci		input_set_abs_params(dev, ABS_Y, 0, TOUCHKIT_MAX_YC, 0, 0);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		psmouse->vendor = "eGalax";
818c2ecf20Sopenharmony_ci		psmouse->name = "Touchscreen";
828c2ecf20Sopenharmony_ci		psmouse->protocol_handler = touchkit_ps2_process_byte;
838c2ecf20Sopenharmony_ci		psmouse->pktsize = 5;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
88