xref: /linux/drivers/input/touchscreen/inexio.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * iNexio serial touchscreen driver
4  *
5  * Copyright (c) 2008 Richard Lemon
6  * Based on the mtouch driver (c) Vojtech Pavlik and Dan Streetman
7  */
8 
9 
10 /*
11  * 2008/06/19 Richard Lemon <richard@codelemon.com>
12  *   Copied mtouch.c and edited for iNexio protocol
13  */
14 
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/serio.h>
21 
22 #define DRIVER_DESC	"iNexio serial touchscreen driver"
23 
24 MODULE_AUTHOR("Richard Lemon <richard@codelemon.com>");
25 MODULE_DESCRIPTION(DRIVER_DESC);
26 MODULE_LICENSE("GPL");
27 
28 /*
29  * Definitions & global arrays.
30  */
31 
32 #define INEXIO_FORMAT_TOUCH_BIT 0x01
33 #define INEXIO_FORMAT_LENGTH 5
34 #define INEXIO_RESPONSE_BEGIN_BYTE 0x80
35 
36 /* todo: check specs for max length of all responses */
37 #define INEXIO_MAX_LENGTH 16
38 
39 #define INEXIO_MIN_XC 0
40 #define INEXIO_MAX_XC 0x3fff
41 #define INEXIO_MIN_YC 0
42 #define INEXIO_MAX_YC 0x3fff
43 
44 #define INEXIO_GET_XC(data) (((data[1])<<7) | data[2])
45 #define INEXIO_GET_YC(data) (((data[3])<<7) | data[4])
46 #define INEXIO_GET_TOUCHED(data) (INEXIO_FORMAT_TOUCH_BIT & data[0])
47 
48 /*
49  * Per-touchscreen data.
50  */
51 
52 struct inexio {
53 	struct input_dev *dev;
54 	struct serio *serio;
55 	int idx;
56 	unsigned char data[INEXIO_MAX_LENGTH];
57 	char phys[32];
58 };
59 
60 static void inexio_process_data(struct inexio *pinexio)
61 {
62 	struct input_dev *dev = pinexio->dev;
63 
64 	if (INEXIO_FORMAT_LENGTH == ++pinexio->idx) {
65 		input_report_abs(dev, ABS_X, INEXIO_GET_XC(pinexio->data));
66 		input_report_abs(dev, ABS_Y, INEXIO_GET_YC(pinexio->data));
67 		input_report_key(dev, BTN_TOUCH, INEXIO_GET_TOUCHED(pinexio->data));
68 		input_sync(dev);
69 
70 		pinexio->idx = 0;
71 	}
72 }
73 
74 static irqreturn_t inexio_interrupt(struct serio *serio,
75 		unsigned char data, unsigned int flags)
76 {
77 	struct inexio *pinexio = serio_get_drvdata(serio);
78 
79 	pinexio->data[pinexio->idx] = data;
80 
81 	if (INEXIO_RESPONSE_BEGIN_BYTE&pinexio->data[0])
82 		inexio_process_data(pinexio);
83 	else
84 		dev_dbg(&serio->dev,
85 			"unknown/unsynchronized data from device, byte %x\n",
86 			pinexio->data[0]);
87 
88 	return IRQ_HANDLED;
89 }
90 
91 /*
92  * inexio_disconnect() is the opposite of inexio_connect()
93  */
94 
95 static void inexio_disconnect(struct serio *serio)
96 {
97 	struct inexio *pinexio = serio_get_drvdata(serio);
98 
99 	input_get_device(pinexio->dev);
100 	input_unregister_device(pinexio->dev);
101 	serio_close(serio);
102 	serio_set_drvdata(serio, NULL);
103 	input_put_device(pinexio->dev);
104 	kfree(pinexio);
105 }
106 
107 /*
108  * inexio_connect() is the routine that is called when someone adds a
109  * new serio device that supports iNexio protocol and registers it as
110  * an input device. This is usually accomplished using inputattach.
111  */
112 
113 static int inexio_connect(struct serio *serio, struct serio_driver *drv)
114 {
115 	struct inexio *pinexio;
116 	struct input_dev *input_dev;
117 	int err;
118 
119 	pinexio = kzalloc_obj(*pinexio);
120 	input_dev = input_allocate_device();
121 	if (!pinexio || !input_dev) {
122 		err = -ENOMEM;
123 		goto fail1;
124 	}
125 
126 	pinexio->serio = serio;
127 	pinexio->dev = input_dev;
128 	scnprintf(pinexio->phys, sizeof(pinexio->phys), "%s/input0", serio->phys);
129 
130 	input_dev->name = "iNexio Serial TouchScreen";
131 	input_dev->phys = pinexio->phys;
132 	input_dev->id.bustype = BUS_RS232;
133 	input_dev->id.vendor = SERIO_INEXIO;
134 	input_dev->id.product = 0;
135 	input_dev->id.version = 0x0001;
136 	input_dev->dev.parent = &serio->dev;
137 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
138 	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
139 	input_set_abs_params(pinexio->dev, ABS_X, INEXIO_MIN_XC, INEXIO_MAX_XC, 0, 0);
140 	input_set_abs_params(pinexio->dev, ABS_Y, INEXIO_MIN_YC, INEXIO_MAX_YC, 0, 0);
141 
142 	serio_set_drvdata(serio, pinexio);
143 
144 	err = serio_open(serio, drv);
145 	if (err)
146 		goto fail2;
147 
148 	err = input_register_device(pinexio->dev);
149 	if (err)
150 		goto fail3;
151 
152 	return 0;
153 
154  fail3:	serio_close(serio);
155  fail2:	serio_set_drvdata(serio, NULL);
156  fail1:	input_free_device(input_dev);
157 	kfree(pinexio);
158 	return err;
159 }
160 
161 /*
162  * The serio driver structure.
163  */
164 
165 static const struct serio_device_id inexio_serio_ids[] = {
166 	{
167 		.type	= SERIO_RS232,
168 		.proto	= SERIO_INEXIO,
169 		.id	= SERIO_ANY,
170 		.extra	= SERIO_ANY,
171 	},
172 	{ 0 }
173 };
174 
175 MODULE_DEVICE_TABLE(serio, inexio_serio_ids);
176 
177 static struct serio_driver inexio_drv = {
178 	.driver		= {
179 		.name	= "inexio",
180 	},
181 	.description	= DRIVER_DESC,
182 	.id_table	= inexio_serio_ids,
183 	.interrupt	= inexio_interrupt,
184 	.connect	= inexio_connect,
185 	.disconnect	= inexio_disconnect,
186 };
187 
188 module_serio_driver(inexio_drv);
189