Loading...
1/*
2 * Sahara TouchIT-213 serial touchscreen driver
3 *
4 * Copyright (c) 2007-2008 Claudio Nieder <private@claudio.ch>
5 *
6 * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
7 * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
8 * Copyright (c) 2004 Vojtech Pavlik
9 * and Dan Streetman <ddstreet@ieee.org>
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published
15 * by the Free Software Foundation.
16 */
17
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/input.h>
23#include <linux/serio.h>
24#include <linux/init.h>
25
26#define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
27
28MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
29MODULE_DESCRIPTION(DRIVER_DESC);
30MODULE_LICENSE("GPL");
31
32/*
33 * Definitions & global arrays.
34 */
35
36/*
37 * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
38 * of 5 byte each.
39 *
40 * +--------+ +--------+ +--------+ +--------+ +--------+
41 * |1000000p| |0xxxxxxx| |0xxxxxxx| |0yyyyyyy| |0yyyyyyy|
42 * +--------+ +--------+ +--------+ +--------+ +--------+
43 * MSB LSB MSB LSB
44 *
45 * The value of p is 1 as long as the screen is touched and 0 when
46 * reporting the location where touching stopped, e.g. where the pen was
47 * lifted from the screen.
48 *
49 * When holding the screen in landscape mode as the BIOS text output is
50 * presented, x is the horizontal axis with values growing from left to
51 * right and y is the vertical axis with values growing from top to
52 * bottom.
53 *
54 * When holding the screen in portrait mode with the Sahara logo in its
55 * correct position, x ist the vertical axis with values growing from
56 * top to bottom and y is the horizontal axis with values growing from
57 * right to left.
58 */
59
60#define T213_FORMAT_TOUCH_BIT 0x01
61#define T213_FORMAT_STATUS_BYTE 0x80
62#define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
63
64/*
65 * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
66 * and y values from 0x1d to 0x7e9, so the actual measurement is
67 * probably done with an 11 bit precision.
68 */
69#define T213_MIN_XC 0
70#define T213_MAX_XC 0x07ff
71#define T213_MIN_YC 0
72#define T213_MAX_YC 0x07ff
73
74/*
75 * Per-touchscreen data.
76 */
77
78struct touchit213 {
79 struct input_dev *dev;
80 struct serio *serio;
81 int idx;
82 unsigned char csum;
83 unsigned char data[5];
84 char phys[32];
85};
86
87static irqreturn_t touchit213_interrupt(struct serio *serio,
88 unsigned char data, unsigned int flags)
89{
90 struct touchit213 *touchit213 = serio_get_drvdata(serio);
91 struct input_dev *dev = touchit213->dev;
92
93 touchit213->data[touchit213->idx] = data;
94
95 switch (touchit213->idx++) {
96 case 0:
97 if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
98 T213_FORMAT_STATUS_BYTE) {
99 pr_debug("unsynchronized data: 0x%02x\n", data);
100 touchit213->idx = 0;
101 }
102 break;
103
104 case 4:
105 touchit213->idx = 0;
106 input_report_abs(dev, ABS_X,
107 (touchit213->data[1] << 7) | touchit213->data[2]);
108 input_report_abs(dev, ABS_Y,
109 (touchit213->data[3] << 7) | touchit213->data[4]);
110 input_report_key(dev, BTN_TOUCH,
111 touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
112 input_sync(dev);
113 break;
114 }
115
116 return IRQ_HANDLED;
117}
118
119/*
120 * touchit213_disconnect() is the opposite of touchit213_connect()
121 */
122
123static void touchit213_disconnect(struct serio *serio)
124{
125 struct touchit213 *touchit213 = serio_get_drvdata(serio);
126
127 input_get_device(touchit213->dev);
128 input_unregister_device(touchit213->dev);
129 serio_close(serio);
130 serio_set_drvdata(serio, NULL);
131 input_put_device(touchit213->dev);
132 kfree(touchit213);
133}
134
135/*
136 * touchit213_connect() is the routine that is called when someone adds a
137 * new serio device that supports the Touchright protocol and registers it as
138 * an input device.
139 */
140
141static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
142{
143 struct touchit213 *touchit213;
144 struct input_dev *input_dev;
145 int err;
146
147 touchit213 = kzalloc(sizeof(struct touchit213), GFP_KERNEL);
148 input_dev = input_allocate_device();
149 if (!touchit213 || !input_dev) {
150 err = -ENOMEM;
151 goto fail1;
152 }
153
154 touchit213->serio = serio;
155 touchit213->dev = input_dev;
156 snprintf(touchit213->phys, sizeof(touchit213->phys),
157 "%s/input0", serio->phys);
158
159 input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
160 input_dev->phys = touchit213->phys;
161 input_dev->id.bustype = BUS_RS232;
162 input_dev->id.vendor = SERIO_TOUCHIT213;
163 input_dev->id.product = 0;
164 input_dev->id.version = 0x0100;
165 input_dev->dev.parent = &serio->dev;
166 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
167 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
168 input_set_abs_params(touchit213->dev, ABS_X,
169 T213_MIN_XC, T213_MAX_XC, 0, 0);
170 input_set_abs_params(touchit213->dev, ABS_Y,
171 T213_MIN_YC, T213_MAX_YC, 0, 0);
172
173 serio_set_drvdata(serio, touchit213);
174
175 err = serio_open(serio, drv);
176 if (err)
177 goto fail2;
178
179 err = input_register_device(touchit213->dev);
180 if (err)
181 goto fail3;
182
183 return 0;
184
185 fail3: serio_close(serio);
186 fail2: serio_set_drvdata(serio, NULL);
187 fail1: input_free_device(input_dev);
188 kfree(touchit213);
189 return err;
190}
191
192/*
193 * The serio driver structure.
194 */
195
196static struct serio_device_id touchit213_serio_ids[] = {
197 {
198 .type = SERIO_RS232,
199 .proto = SERIO_TOUCHIT213,
200 .id = SERIO_ANY,
201 .extra = SERIO_ANY,
202 },
203 { 0 }
204};
205
206MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
207
208static struct serio_driver touchit213_drv = {
209 .driver = {
210 .name = "touchit213",
211 },
212 .description = DRIVER_DESC,
213 .id_table = touchit213_serio_ids,
214 .interrupt = touchit213_interrupt,
215 .connect = touchit213_connect,
216 .disconnect = touchit213_disconnect,
217};
218
219/*
220 * The functions for inserting/removing us as a module.
221 */
222
223static int __init touchit213_init(void)
224{
225 return serio_register_driver(&touchit213_drv);
226}
227
228static void __exit touchit213_exit(void)
229{
230 serio_unregister_driver(&touchit213_drv);
231}
232
233module_init(touchit213_init);
234module_exit(touchit213_exit);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Sahara TouchIT-213 serial touchscreen driver
4 *
5 * Copyright (c) 2007-2008 Claudio Nieder <private@claudio.ch>
6 *
7 * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
8 * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
9 * Copyright (c) 2004 Vojtech Pavlik
10 * and Dan Streetman <ddstreet@ieee.org>
11 */
12
13
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/input.h>
19#include <linux/serio.h>
20
21#define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
22
23MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
24MODULE_DESCRIPTION(DRIVER_DESC);
25MODULE_LICENSE("GPL");
26
27/*
28 * Definitions & global arrays.
29 */
30
31/*
32 * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
33 * of 5 byte each.
34 *
35 * +--------+ +--------+ +--------+ +--------+ +--------+
36 * |1000000p| |0xxxxxxx| |0xxxxxxx| |0yyyyyyy| |0yyyyyyy|
37 * +--------+ +--------+ +--------+ +--------+ +--------+
38 * MSB LSB MSB LSB
39 *
40 * The value of p is 1 as long as the screen is touched and 0 when
41 * reporting the location where touching stopped, e.g. where the pen was
42 * lifted from the screen.
43 *
44 * When holding the screen in landscape mode as the BIOS text output is
45 * presented, x is the horizontal axis with values growing from left to
46 * right and y is the vertical axis with values growing from top to
47 * bottom.
48 *
49 * When holding the screen in portrait mode with the Sahara logo in its
50 * correct position, x ist the vertical axis with values growing from
51 * top to bottom and y is the horizontal axis with values growing from
52 * right to left.
53 */
54
55#define T213_FORMAT_TOUCH_BIT 0x01
56#define T213_FORMAT_STATUS_BYTE 0x80
57#define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
58
59/*
60 * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
61 * and y values from 0x1d to 0x7e9, so the actual measurement is
62 * probably done with an 11 bit precision.
63 */
64#define T213_MIN_XC 0
65#define T213_MAX_XC 0x07ff
66#define T213_MIN_YC 0
67#define T213_MAX_YC 0x07ff
68
69/*
70 * Per-touchscreen data.
71 */
72
73struct touchit213 {
74 struct input_dev *dev;
75 struct serio *serio;
76 int idx;
77 unsigned char csum;
78 unsigned char data[5];
79 char phys[32];
80};
81
82static irqreturn_t touchit213_interrupt(struct serio *serio,
83 unsigned char data, unsigned int flags)
84{
85 struct touchit213 *touchit213 = serio_get_drvdata(serio);
86 struct input_dev *dev = touchit213->dev;
87
88 touchit213->data[touchit213->idx] = data;
89
90 switch (touchit213->idx++) {
91 case 0:
92 if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
93 T213_FORMAT_STATUS_BYTE) {
94 pr_debug("unsynchronized data: 0x%02x\n", data);
95 touchit213->idx = 0;
96 }
97 break;
98
99 case 4:
100 touchit213->idx = 0;
101 input_report_abs(dev, ABS_X,
102 (touchit213->data[1] << 7) | touchit213->data[2]);
103 input_report_abs(dev, ABS_Y,
104 (touchit213->data[3] << 7) | touchit213->data[4]);
105 input_report_key(dev, BTN_TOUCH,
106 touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
107 input_sync(dev);
108 break;
109 }
110
111 return IRQ_HANDLED;
112}
113
114/*
115 * touchit213_disconnect() is the opposite of touchit213_connect()
116 */
117
118static void touchit213_disconnect(struct serio *serio)
119{
120 struct touchit213 *touchit213 = serio_get_drvdata(serio);
121
122 input_get_device(touchit213->dev);
123 input_unregister_device(touchit213->dev);
124 serio_close(serio);
125 serio_set_drvdata(serio, NULL);
126 input_put_device(touchit213->dev);
127 kfree(touchit213);
128}
129
130/*
131 * touchit213_connect() is the routine that is called when someone adds a
132 * new serio device that supports the Touchright protocol and registers it as
133 * an input device.
134 */
135
136static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
137{
138 struct touchit213 *touchit213;
139 struct input_dev *input_dev;
140 int err;
141
142 touchit213 = kzalloc(sizeof(*touchit213), GFP_KERNEL);
143 input_dev = input_allocate_device();
144 if (!touchit213 || !input_dev) {
145 err = -ENOMEM;
146 goto fail1;
147 }
148
149 touchit213->serio = serio;
150 touchit213->dev = input_dev;
151 snprintf(touchit213->phys, sizeof(touchit213->phys),
152 "%s/input0", serio->phys);
153
154 input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
155 input_dev->phys = touchit213->phys;
156 input_dev->id.bustype = BUS_RS232;
157 input_dev->id.vendor = SERIO_TOUCHIT213;
158 input_dev->id.product = 0;
159 input_dev->id.version = 0x0100;
160 input_dev->dev.parent = &serio->dev;
161 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
162 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
163 input_set_abs_params(touchit213->dev, ABS_X,
164 T213_MIN_XC, T213_MAX_XC, 0, 0);
165 input_set_abs_params(touchit213->dev, ABS_Y,
166 T213_MIN_YC, T213_MAX_YC, 0, 0);
167
168 serio_set_drvdata(serio, touchit213);
169
170 err = serio_open(serio, drv);
171 if (err)
172 goto fail2;
173
174 err = input_register_device(touchit213->dev);
175 if (err)
176 goto fail3;
177
178 return 0;
179
180 fail3: serio_close(serio);
181 fail2: serio_set_drvdata(serio, NULL);
182 fail1: input_free_device(input_dev);
183 kfree(touchit213);
184 return err;
185}
186
187/*
188 * The serio driver structure.
189 */
190
191static const struct serio_device_id touchit213_serio_ids[] = {
192 {
193 .type = SERIO_RS232,
194 .proto = SERIO_TOUCHIT213,
195 .id = SERIO_ANY,
196 .extra = SERIO_ANY,
197 },
198 { 0 }
199};
200
201MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
202
203static struct serio_driver touchit213_drv = {
204 .driver = {
205 .name = "touchit213",
206 },
207 .description = DRIVER_DESC,
208 .id_table = touchit213_serio_ids,
209 .interrupt = touchit213_interrupt,
210 .connect = touchit213_connect,
211 .disconnect = touchit213_disconnect,
212};
213
214module_serio_driver(touchit213_drv);