2 * Penmount serial touchscreen driver
4 * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
5 * Copyright (c) 2011 John Sung <penmount.touch@gmail.com>
7 * Based on ELO driver (drivers/input/touchscreen/elo.c)
8 * Copyright (c) 2004 Vojtech Pavlik
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.
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>
22 #include <linux/input/mt.h>
23 #include <linux/serio.h>
24 #include <linux/init.h>
26 #define DRIVER_DESC "PenMount serial touchscreen driver"
28 MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>");
29 MODULE_AUTHOR("John Sung <penmount.touch@gmail.com>");
30 MODULE_DESCRIPTION(DRIVER_DESC);
31 MODULE_LICENSE("GPL");
34 * Definitions & global arrays.
37 #define PM_MAX_LENGTH 6
38 #define PM_MAX_MTSLOT 16
39 #define PM_3000_MTSLOT 2
40 #define PM_6250_MTSLOT 12
48 bool active; /* is the touch valid? */
52 * Per-touchscreen data.
56 struct input_dev *dev;
59 unsigned char data[PM_MAX_LENGTH];
61 unsigned char packetsize;
62 unsigned char maxcontacts;
63 struct mt_slot slots[PM_MAX_MTSLOT];
67 * pm_mtevent() sends mt events and also emulates pointer movement
70 static void pm_mtevent(struct pm *pm, struct input_dev *input)
74 for (i = 0; i < pm->maxcontacts; ++i) {
75 input_mt_slot(input, i);
76 input_mt_report_slot_state(input, MT_TOOL_FINGER,
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);
84 input_mt_report_pointer_emulation(input, true);
89 * pm_checkpacket() checks if data packet is valid
92 static bool pm_checkpacket(unsigned char *packet)
97 for (i = 0; i < 5; i++)
100 return packet[5] == (unsigned char)~(total & 0xff);
103 static irqreturn_t pm_interrupt(struct serio *serio,
104 unsigned char data, unsigned int flags)
106 struct pm *pm = serio_get_drvdata(serio);
107 struct input_dev *dev = pm->dev;
109 pm->data[pm->idx] = data;
111 switch (pm->dev->id.product) {
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));
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));
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];
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];
175 * pm_disconnect() is the opposite of pm_connect()
178 static void pm_disconnect(struct serio *serio)
180 struct pm *pm = serio_get_drvdata(serio);
182 input_get_device(pm->dev);
183 input_unregister_device(pm->dev);
185 serio_set_drvdata(serio, NULL);
186 input_put_device(pm->dev);
191 * pm_connect() is the routine that is called when someone adds a
192 * new serio device that supports PenMount protocol and registers it as
196 static int pm_connect(struct serio *serio, struct serio_driver *drv)
199 struct input_dev *input_dev;
203 pm = kzalloc(sizeof(struct pm), GFP_KERNEL);
204 input_dev = input_allocate_device();
205 if (!pm || !input_dev) {
212 snprintf(pm->phys, sizeof(pm->phys), "%s/input0", serio->phys);
215 input_dev->name = "PenMount Serial TouchScreen";
216 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;
221 input_dev->dev.parent = &serio->dev;
223 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
224 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
226 switch (serio->id.id) {
230 input_dev->id.product = 0x9000;
231 max_x = max_y = 0x3ff;
236 input_dev->id.product = 0x6000;
237 max_x = max_y = 0x3ff;
242 input_dev->id.product = 0x3000;
243 max_x = max_y = 0x7ff;
244 pm->maxcontacts = PM_3000_MTSLOT;
249 input_dev->id.product = 0x6250;
250 max_x = max_y = 0x3ff;
251 pm->maxcontacts = PM_6250_MTSLOT;
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);
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);
266 serio_set_drvdata(serio, pm);
268 err = serio_open(serio, drv);
272 err = input_register_device(pm->dev);
278 fail3: serio_close(serio);
279 fail2: serio_set_drvdata(serio, NULL);
280 fail1: input_free_device(input_dev);
286 * The serio driver structure.
289 static struct serio_device_id pm_serio_ids[] = {
292 .proto = SERIO_PENMOUNT,
299 MODULE_DEVICE_TABLE(serio, pm_serio_ids);
301 static struct serio_driver pm_drv = {
303 .name = "serio-penmount",
305 .description = DRIVER_DESC,
306 .id_table = pm_serio_ids,
307 .interrupt = pm_interrupt,
308 .connect = pm_connect,
309 .disconnect = pm_disconnect,
313 * The functions for inserting/removing us as a module.
316 static int __init pm_init(void)
318 return serio_register_driver(&pm_drv);
321 static void __exit pm_exit(void)
323 serio_unregister_driver(&pm_drv);
326 module_init(pm_init);
327 module_exit(pm_exit);