blob: 9077228418b7028242caa03cc99292ee8f2c62cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MicroTouch (3M) serial touchscreen driver
3 *
4 * Copyright (c) 2004 Vojtech Pavlik
5 */
6
7/*
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.
11 */
12
13/*
14 * 2005/02/19 Dan Streetman <ddstreet@ieee.org>
15 * Copied elo.c and edited for MicroTouch protocol
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 "MicroTouch serial touchscreen driver"
27
28MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29MODULE_DESCRIPTION(DRIVER_DESC);
30MODULE_LICENSE("GPL");
31
32/*
33 * Definitions & global arrays.
34 */
35
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
41
42/* todo: check specs for max length of all responses */
43#define MTOUCH_MAX_LENGTH 16
44
45#define MTOUCH_MIN_XC 0
46#define MTOUCH_MAX_XC 0x3fff
47#define MTOUCH_MIN_YC 0
48#define MTOUCH_MAX_YC 0x3fff
49
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])
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Per-touchscreen data.
56 */
57
58struct mtouch {
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -050059 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct serio *serio;
61 int idx;
62 unsigned char data[MTOUCH_MAX_LENGTH];
63 char phys[32];
64};
65
David Howells7d12e782006-10-05 14:55:46 +010066static void mtouch_process_format_tablet(struct mtouch *mtouch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -050068 struct input_dev *dev = mtouch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (MTOUCH_FORMAT_TABLET_LENGTH == ++mtouch->idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 input_report_abs(dev, ABS_X, MTOUCH_GET_XC(mtouch->data));
72 input_report_abs(dev, ABS_Y, MTOUCH_MAX_YC - MTOUCH_GET_YC(mtouch->data));
73 input_report_key(dev, BTN_TOUCH, MTOUCH_GET_TOUCHED(mtouch->data));
74 input_sync(dev);
75
76 mtouch->idx = 0;
77 }
78}
79
David Howells7d12e782006-10-05 14:55:46 +010080static void mtouch_process_response(struct mtouch *mtouch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 if (MTOUCH_RESPONSE_END_BYTE == mtouch->data[mtouch->idx++]) {
83 /* FIXME - process response */
84 mtouch->idx = 0;
85 } else if (MTOUCH_MAX_LENGTH == mtouch->idx) {
86 printk(KERN_ERR "mtouch.c: too many response bytes\n");
87 mtouch->idx = 0;
88 }
89}
90
91static irqreturn_t mtouch_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +010092 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct mtouch* mtouch = serio_get_drvdata(serio);
95
96 mtouch->data[mtouch->idx] = data;
97
98 if (MTOUCH_FORMAT_TABLET_STATUS_BIT & mtouch->data[0])
David Howells7d12e782006-10-05 14:55:46 +010099 mtouch_process_format_tablet(mtouch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else if (MTOUCH_RESPONSE_BEGIN_BYTE == mtouch->data[0])
David Howells7d12e782006-10-05 14:55:46 +0100101 mtouch_process_response(mtouch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 else
103 printk(KERN_DEBUG "mtouch.c: unknown/unsynchronized data from device, byte %x\n",mtouch->data[0]);
104
105 return IRQ_HANDLED;
106}
107
108/*
109 * mtouch_disconnect() is the opposite of mtouch_connect()
110 */
111
112static void mtouch_disconnect(struct serio *serio)
113{
114 struct mtouch* mtouch = serio_get_drvdata(serio);
115
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500116 input_get_device(mtouch->dev);
117 input_unregister_device(mtouch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 serio_close(serio);
119 serio_set_drvdata(serio, NULL);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500120 input_put_device(mtouch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 kfree(mtouch);
122}
123
124/*
125 * mtouch_connect() is the routine that is called when someone adds a
126 * new serio device that supports MicroTouch (Format Tablet) protocol and registers it as
127 * an input device.
128 */
129
130static int mtouch_connect(struct serio *serio, struct serio_driver *drv)
131{
132 struct mtouch *mtouch;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500133 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int err;
135
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500136 mtouch = kzalloc(sizeof(struct mtouch), GFP_KERNEL);
137 input_dev = input_allocate_device();
138 if (!mtouch || !input_dev) {
139 err = -ENOMEM;
Dmitry Torokhov52c1f572006-11-05 22:40:03 -0500140 goto fail1;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 mtouch->serio = serio;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500144 mtouch->dev = input_dev;
Dmitry Torokhova21466c2006-06-26 01:46:04 -0400145 snprintf(mtouch->phys, sizeof(mtouch->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500147 input_dev->name = "MicroTouch Serial TouchScreen";
148 input_dev->phys = mtouch->phys;
149 input_dev->id.bustype = BUS_RS232;
150 input_dev->id.vendor = SERIO_MICROTOUCH;
151 input_dev->id.product = 0;
152 input_dev->id.version = 0x0100;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400153 input_dev->dev.parent = &serio->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700154 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
155 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500156 input_set_abs_params(mtouch->dev, ABS_X, MTOUCH_MIN_XC, MTOUCH_MAX_XC, 0, 0);
157 input_set_abs_params(mtouch->dev, ABS_Y, MTOUCH_MIN_YC, MTOUCH_MAX_YC, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 serio_set_drvdata(serio, mtouch);
160
161 err = serio_open(serio, drv);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500162 if (err)
Dmitry Torokhov52c1f572006-11-05 22:40:03 -0500163 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Dmitry Torokhov52c1f572006-11-05 22:40:03 -0500165 err = input_register_device(mtouch->dev);
166 if (err)
167 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return 0;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500170
Dmitry Torokhov52c1f572006-11-05 22:40:03 -0500171 fail3: serio_close(serio);
172 fail2: serio_set_drvdata(serio, NULL);
173 fail1: input_free_device(input_dev);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500174 kfree(mtouch);
175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/*
179 * The serio driver structure.
180 */
181
182static struct serio_device_id mtouch_serio_ids[] = {
183 {
184 .type = SERIO_RS232,
185 .proto = SERIO_MICROTOUCH,
186 .id = SERIO_ANY,
187 .extra = SERIO_ANY,
188 },
189 { 0 }
190};
191
192MODULE_DEVICE_TABLE(serio, mtouch_serio_ids);
193
194static struct serio_driver mtouch_drv = {
195 .driver = {
196 .name = "mtouch",
197 },
198 .description = DRIVER_DESC,
199 .id_table = mtouch_serio_ids,
200 .interrupt = mtouch_interrupt,
201 .connect = mtouch_connect,
202 .disconnect = mtouch_disconnect,
203};
204
205/*
206 * The functions for inserting/removing us as a module.
207 */
208
209static int __init mtouch_init(void)
210{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500211 return serio_register_driver(&mtouch_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void __exit mtouch_exit(void)
215{
216 serio_unregister_driver(&mtouch_drv);
217}
218
219module_init(mtouch_init);
220module_exit(mtouch_exit);