blob: a047850e38b108fc8e5c30647566cc7d8ada3299 [file] [log] [blame]
Rick Kochee479992006-08-05 00:32:18 -04001/*
2 * Penmount serial touchscreen driver
3 *
4 * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
John Sung21ae5082011-09-09 13:33:12 -07005 * Copyright (c) 2011 John Sung <penmount.touch@gmail.com>
Rick Kochee479992006-08-05 00:32:18 -04006 *
7 * Based on ELO driver (drivers/input/touchscreen/elo.c)
8 * Copyright (c) 2004 Vojtech Pavlik
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 */
16
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/input.h>
John Sung90aba7d2011-09-09 13:33:12 -070022#include <linux/input/mt.h>
Rick Kochee479992006-08-05 00:32:18 -040023#include <linux/serio.h>
24#include <linux/init.h>
25
John Sung21ae5082011-09-09 13:33:12 -070026#define DRIVER_DESC "PenMount serial touchscreen driver"
Rick Kochee479992006-08-05 00:32:18 -040027
28MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>");
John Sung21ae5082011-09-09 13:33:12 -070029MODULE_AUTHOR("John Sung <penmount.touch@gmail.com>");
Rick Kochee479992006-08-05 00:32:18 -040030MODULE_DESCRIPTION(DRIVER_DESC);
31MODULE_LICENSE("GPL");
32
33/*
34 * Definitions & global arrays.
35 */
36
John Sungc42e2e42011-09-09 13:33:12 -070037#define PM_MAX_LENGTH 6
John Sung90aba7d2011-09-09 13:33:12 -070038#define PM_MAX_MTSLOT 16
39#define PM_3000_MTSLOT 2
John Sungbd8f6d22011-09-09 13:33:12 -070040#define PM_6250_MTSLOT 12
John Sung90aba7d2011-09-09 13:33:12 -070041
42/*
43 * Multi-touch slot
44 */
45
46struct mt_slot {
47 unsigned short x, y;
48 bool active; /* is the touch valid? */
49};
Rick Kochee479992006-08-05 00:32:18 -040050
51/*
52 * Per-touchscreen data.
53 */
54
55struct pm {
56 struct input_dev *dev;
57 struct serio *serio;
58 int idx;
59 unsigned char data[PM_MAX_LENGTH];
60 char phys[32];
John Sungc42e2e42011-09-09 13:33:12 -070061 unsigned char packetsize;
John Sung90aba7d2011-09-09 13:33:12 -070062 unsigned char maxcontacts;
63 struct mt_slot slots[PM_MAX_MTSLOT];
Rick Kochee479992006-08-05 00:32:18 -040064};
65
John Sungc42e2e42011-09-09 13:33:12 -070066/*
John Sung90aba7d2011-09-09 13:33:12 -070067 * pm_mtevent() sends mt events and also emulates pointer movement
68 */
69
70static void pm_mtevent(struct pm *pm, struct input_dev *input)
71{
72 int i;
73
74 for (i = 0; i < pm->maxcontacts; ++i) {
75 input_mt_slot(input, i);
76 input_mt_report_slot_state(input, MT_TOOL_FINGER,
77 pm->slots[i].active);
78 if (pm->slots[i].active) {
79 input_event(input, EV_ABS, ABS_MT_POSITION_X, pm->slots[i].x);
80 input_event(input, EV_ABS, ABS_MT_POSITION_Y, pm->slots[i].y);
81 }
82 }
83
84 input_mt_report_pointer_emulation(input, true);
85 input_sync(input);
86}
87
88/*
John Sungc42e2e42011-09-09 13:33:12 -070089 * pm_checkpacket() checks if data packet is valid
90 */
91
92static bool pm_checkpacket(unsigned char *packet)
93{
94 int total = 0;
95 int i;
96
97 for (i = 0; i < 5; i++)
98 total += packet[i];
99
100 return packet[5] == (unsigned char)~(total & 0xff);
101}
102
Rick Kochee479992006-08-05 00:32:18 -0400103static irqreturn_t pm_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100104 unsigned char data, unsigned int flags)
Rick Kochee479992006-08-05 00:32:18 -0400105{
106 struct pm *pm = serio_get_drvdata(serio);
107 struct input_dev *dev = pm->dev;
108
109 pm->data[pm->idx] = data;
110
John Sungc42e2e42011-09-09 13:33:12 -0700111 switch (pm->dev->id.product) {
112 case 0x9000:
113 if (pm->data[0] & 0x80) {
114 if (pm->packetsize == ++pm->idx) {
115 input_report_abs(dev, ABS_X, pm->data[1] * 128 + pm->data[2]);
116 input_report_abs(dev, ABS_Y, pm->data[3] * 128 + pm->data[4]);
117 input_report_key(dev, BTN_TOUCH, !!(pm->data[0] & 0x40));
118 input_sync(dev);
119 pm->idx = 0;
120 }
Rick Kochee479992006-08-05 00:32:18 -0400121 }
John Sungc42e2e42011-09-09 13:33:12 -0700122 break;
123
124 case 0x6000:
125 if ((pm->data[0] & 0xbf) == 0x30) {
126 if (pm->packetsize == ++pm->idx) {
127 if (pm_checkpacket(pm->data)) {
128 input_report_abs(dev, ABS_X,
129 pm->data[2] * 256 + pm->data[1]);
130 input_report_abs(dev, ABS_Y,
131 pm->data[4] * 256 + pm->data[3]);
132 input_report_key(dev, BTN_TOUCH, !!(pm->data[0] & 0x40));
133 input_sync(dev);
134 }
135 pm->idx = 0;
136 }
137 }
138 break;
John Sung90aba7d2011-09-09 13:33:12 -0700139
140 case 0x3000:
141 if ((pm->data[0] & 0xce) == 0x40) {
142 if (pm->packetsize == ++pm->idx) {
143 if (pm_checkpacket(pm->data)) {
144 int slotnum = pm->data[0] & 0x0f;
145 pm->slots[slotnum].active = pm->data[0] & 0x30;
146 pm->slots[slotnum].x = pm->data[2] * 256 + pm->data[1];
147 pm->slots[slotnum].y = pm->data[4] * 256 + pm->data[3];
148 pm_mtevent(pm, dev);
149 }
150 pm->idx = 0;
151 }
152 }
153 break;
John Sungbd8f6d22011-09-09 13:33:12 -0700154
155 case 0x6250:
156 if ((pm->data[0] & 0xb0) == 0x30) {
157 if (pm->packetsize == ++pm->idx) {
158 if (pm_checkpacket(pm->data)) {
159 int slotnum = pm->data[0] & 0x0f;
160 pm->slots[slotnum].active = pm->data[0] & 0x40;
161 pm->slots[slotnum].x = pm->data[2] * 256 + pm->data[1];
162 pm->slots[slotnum].y = pm->data[4] * 256 + pm->data[3];
163 pm_mtevent(pm, dev);
164 }
165 pm->idx = 0;
166 }
167 }
168 break;
Rick Kochee479992006-08-05 00:32:18 -0400169 }
170
171 return IRQ_HANDLED;
172}
173
174/*
175 * pm_disconnect() is the opposite of pm_connect()
176 */
177
178static void pm_disconnect(struct serio *serio)
179{
180 struct pm *pm = serio_get_drvdata(serio);
181
182 input_get_device(pm->dev);
183 input_unregister_device(pm->dev);
184 serio_close(serio);
185 serio_set_drvdata(serio, NULL);
186 input_put_device(pm->dev);
187 kfree(pm);
188}
189
190/*
191 * pm_connect() is the routine that is called when someone adds a
John Sung21ae5082011-09-09 13:33:12 -0700192 * new serio device that supports PenMount protocol and registers it as
Rick Kochee479992006-08-05 00:32:18 -0400193 * an input device.
194 */
195
196static int pm_connect(struct serio *serio, struct serio_driver *drv)
197{
198 struct pm *pm;
199 struct input_dev *input_dev;
John Sung90aba7d2011-09-09 13:33:12 -0700200 int max_x, max_y;
Rick Kochee479992006-08-05 00:32:18 -0400201 int err;
202
203 pm = kzalloc(sizeof(struct pm), GFP_KERNEL);
204 input_dev = input_allocate_device();
205 if (!pm || !input_dev) {
206 err = -ENOMEM;
207 goto fail1;
208 }
209
210 pm->serio = serio;
211 pm->dev = input_dev;
212 snprintf(pm->phys, sizeof(pm->phys), "%s/input0", serio->phys);
John Sung90aba7d2011-09-09 13:33:12 -0700213 pm->maxcontacts = 1;
Rick Kochee479992006-08-05 00:32:18 -0400214
John Sung21ae5082011-09-09 13:33:12 -0700215 input_dev->name = "PenMount Serial TouchScreen";
Rick Kochee479992006-08-05 00:32:18 -0400216 input_dev->phys = pm->phys;
217 input_dev->id.bustype = BUS_RS232;
218 input_dev->id.vendor = SERIO_PENMOUNT;
219 input_dev->id.product = 0;
220 input_dev->id.version = 0x0100;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400221 input_dev->dev.parent = &serio->dev;
Rick Kochee479992006-08-05 00:32:18 -0400222
John Sung90aba7d2011-09-09 13:33:12 -0700223 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
224 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Rick Kochee479992006-08-05 00:32:18 -0400225
John Sungc42e2e42011-09-09 13:33:12 -0700226 switch (serio->id.id) {
227 default:
228 case 0:
229 pm->packetsize = 5;
230 input_dev->id.product = 0x9000;
John Sung90aba7d2011-09-09 13:33:12 -0700231 max_x = max_y = 0x3ff;
John Sungc42e2e42011-09-09 13:33:12 -0700232 break;
233
234 case 1:
235 pm->packetsize = 6;
236 input_dev->id.product = 0x6000;
John Sung90aba7d2011-09-09 13:33:12 -0700237 max_x = max_y = 0x3ff;
John Sungc42e2e42011-09-09 13:33:12 -0700238 break;
John Sung90aba7d2011-09-09 13:33:12 -0700239
240 case 2:
241 pm->packetsize = 6;
242 input_dev->id.product = 0x3000;
243 max_x = max_y = 0x7ff;
244 pm->maxcontacts = PM_3000_MTSLOT;
245 break;
John Sungbd8f6d22011-09-09 13:33:12 -0700246
247 case 3:
248 pm->packetsize = 6;
249 input_dev->id.product = 0x6250;
250 max_x = max_y = 0x3ff;
251 pm->maxcontacts = PM_6250_MTSLOT;
252 break;
John Sung90aba7d2011-09-09 13:33:12 -0700253 }
254
255 input_set_abs_params(pm->dev, ABS_X, 0, max_x, 0, 0);
256 input_set_abs_params(pm->dev, ABS_Y, 0, max_y, 0, 0);
257
258 if (pm->maxcontacts > 1) {
259 input_mt_init_slots(pm->dev, pm->maxcontacts);
260 input_set_abs_params(pm->dev,
261 ABS_MT_POSITION_X, 0, max_x, 0, 0);
262 input_set_abs_params(pm->dev,
263 ABS_MT_POSITION_Y, 0, max_y, 0, 0);
John Sungc42e2e42011-09-09 13:33:12 -0700264 }
265
Rick Kochee479992006-08-05 00:32:18 -0400266 serio_set_drvdata(serio, pm);
267
268 err = serio_open(serio, drv);
269 if (err)
270 goto fail2;
271
272 err = input_register_device(pm->dev);
273 if (err)
274 goto fail3;
275
276 return 0;
277
278 fail3: serio_close(serio);
279 fail2: serio_set_drvdata(serio, NULL);
280 fail1: input_free_device(input_dev);
281 kfree(pm);
282 return err;
283}
284
285/*
286 * The serio driver structure.
287 */
288
289static struct serio_device_id pm_serio_ids[] = {
290 {
291 .type = SERIO_RS232,
292 .proto = SERIO_PENMOUNT,
293 .id = SERIO_ANY,
294 .extra = SERIO_ANY,
295 },
296 { 0 }
297};
298
299MODULE_DEVICE_TABLE(serio, pm_serio_ids);
300
301static struct serio_driver pm_drv = {
302 .driver = {
John Sung21ae5082011-09-09 13:33:12 -0700303 .name = "serio-penmount",
Rick Kochee479992006-08-05 00:32:18 -0400304 },
305 .description = DRIVER_DESC,
306 .id_table = pm_serio_ids,
307 .interrupt = pm_interrupt,
308 .connect = pm_connect,
309 .disconnect = pm_disconnect,
310};
311
312/*
313 * The functions for inserting/removing us as a module.
314 */
315
316static int __init pm_init(void)
317{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500318 return serio_register_driver(&pm_drv);
Rick Kochee479992006-08-05 00:32:18 -0400319}
320
321static void __exit pm_exit(void)
322{
323 serio_unregister_driver(&pm_drv);
324}
325
326module_init(pm_init);
327module_exit(pm_exit);