2 * MicroTouch (3M) serial touchscreen driver
4 * Copyright (c) 2004 Vojtech Pavlik
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
14 * 2005/02/19 Dan Streetman <ddstreet@ieee.org>
15 * Copied elo.c and edited for MicroTouch protocol
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>
26 #define DRIVER_DESC "MicroTouch serial touchscreen driver"
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION(DRIVER_DESC);
30 MODULE_LICENSE("GPL");
33 * Definitions & global arrays.
36 #define MTOUCH_FORMAT_TABLET_STATUS_BIT 0x80
37 #define MTOUCH_FORMAT_TABLET_TOUCH_BIT 0x40
38 #define MTOUCH_FORMAT_TABLET_LENGTH 5
39 #define MTOUCH_RESPONSE_BEGIN_BYTE 0x01
40 #define MTOUCH_RESPONSE_END_BYTE 0x0d
42 /* todo: check specs for max length of all responses */
43 #define MTOUCH_MAX_LENGTH 16
45 #define MTOUCH_MIN_XC 0
46 #define MTOUCH_MAX_XC 0x3fff
47 #define MTOUCH_MIN_YC 0
48 #define MTOUCH_MAX_YC 0x3fff
50 #define MTOUCH_GET_XC(data) (((data[2])<<7) | data[1])
51 #define MTOUCH_GET_YC(data) (((data[4])<<7) | data[3])
52 #define MTOUCH_GET_TOUCHED(data) (MTOUCH_FORMAT_TABLET_TOUCH_BIT & data[0])
55 * Per-touchscreen data.
59 struct input_dev *dev;
62 unsigned char data[MTOUCH_MAX_LENGTH];
66 static void mtouch_process_format_tablet(struct mtouch *mtouch, struct pt_regs *regs)
68 struct input_dev *dev = mtouch->dev;
70 if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
71 input_regs(dev, regs);
72 input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
73 input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
74 input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
81 static void mtouch_process_response(struct mtouch *mtouch, struct pt_regs *regs)
83 if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
84 /* FIXME - process response */
86 } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
87 printk(KERN_ERR "mtouch.c: too many response bytes\n");
92 static irqreturn_t mtouch_interrupt(struct serio *serio,
93 unsigned char data, unsigned int flags, struct pt_regs *regs)
95 struct mtouch* mtouch = serio_get_drvdata(serio);
97 mtouch->data[mtouch->idx] = data;
99 if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
100 mtouch_process_format_tablet(mtouch, regs);
101 else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
102 mtouch_process_response(mtouch, regs);
104 printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
110 * mtouch_disconnect() is the opposite of mtouch_connect()
113 static void mtouch_disconnect(struct serio *serio)
115 struct mtouch* mtouch = serio_get_drvdata(serio);
117 input_get_device(mtouch->dev);
118 input_unregister_device(mtouch->dev);
120 serio_set_drvdata(serio, NULL);
121 input_put_device(mtouch->dev);
126 * mtouch_connect() is the routine that is called when someone adds a
127 * new serio device that supports MicroTouch (Format Tablet) protocol and registers it as
131 static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
133 struct mtouch *mtouch;
134 struct input_dev *input_dev;
137 mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
138 input_dev = input_allocate_device();
139 if (!mtouch || !input_dev) {
144 mtouch->serio = serio;
145 mtouch->dev = input_dev;
146 snprintf(mtouch->phys, sizeof(mtouch->phys), "%s/input0", serio->phys);
148 input_dev->private = mtouch;
149 input_dev->name = "MicroTouch Serial TouchScreen";
150 input_dev->phys = mtouch->phys;
151 input_dev->id.bustype = BUS_RS232;
152 input_dev->id.vendor = SERIO_MICROTOUCH;
153 input_dev->id.product = 0;
154 input_dev->id.version = 0x0100;
155 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
156 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
157 input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
158 input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
160 serio_set_drvdata(serio, mtouch);
162 err = serio_open(serio, drv);
166 input_register_device(mtouch->dev);
170 fail: serio_set_drvdata(serio, NULL);
171 input_free_device(input_dev);
177 * The serio driver structure.
180 static struct serio_device_id mtouch_serio_ids[] = {
183 .proto = SERIO_MICROTOUCH,
190 MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
192 static struct serio_driver mtouch_drv = {
196 .description = DRIVER_DESC,
197 .id_table = mtouch_serio_ids,
198 .interrupt = mtouch_interrupt,
199 .connect = mtouch_connect,
200 .disconnect = mtouch_disconnect,
204 * The functions for inserting/removing us as a module.
207 static int __init mtouch_init(void)
209 serio_register_driver(&mtouch_drv);
213 static void __exit mtouch_exit(void)
215 serio_unregister_driver(&mtouch_drv);
218 module_init(mtouch_init);
219 module_exit(mtouch_exit);