patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / touchscreen / gunze.c
1 /*
2  * $Id: gunze.c,v 1.12 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  */
6
7 /*
8  * Gunze AHL-51S touchscreen driver for Linux
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/input.h>
36 #include <linux/serio.h>
37 #include <linux/init.h>
38
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Gunze AHL-51S touchscreen driver");
41 MODULE_LICENSE("GPL");
42
43 /*
44  * Definitions & global arrays.
45  */
46
47 #define GUNZE_MAX_LENGTH        10
48
49 static char *gunze_name = "Gunze AHL-51S TouchScreen";
50
51 /*
52  * Per-touchscreen data.
53  */
54
55 struct gunze {
56         struct input_dev dev;
57         struct serio *serio;
58         int idx;
59         unsigned char data[GUNZE_MAX_LENGTH];
60         char phys[32];
61 };
62
63 static void gunze_process_packet(struct gunze* gunze, struct pt_regs *regs)
64 {
65         struct input_dev *dev = &gunze->dev;
66
67         if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
68                 (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
69                 gunze->data[10] = 0;
70                 printk(KERN_WARNING "gunze.c: bad packet: >%s<\n", gunze->data);
71                 return;
72         }
73
74         input_regs(dev, regs);
75         input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10) * 4);
76         input_report_abs(dev, ABS_Y, 3072 - simple_strtoul(gunze->data + 6, NULL, 10) * 3);
77         input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
78         input_sync(dev);
79 }
80
81 static irqreturn_t gunze_interrupt(struct serio *serio,
82                 unsigned char data, unsigned int flags, struct pt_regs *regs)
83 {
84         struct gunze* gunze = serio->private;
85
86         if (data == '\r') {
87                 gunze_process_packet(gunze, regs);
88                 gunze->idx = 0;
89         } else {
90                 if (gunze->idx < GUNZE_MAX_LENGTH)
91                         gunze->data[gunze->idx++] = data;
92         }
93         return IRQ_HANDLED;
94 }
95
96 /*
97  * gunze_disconnect() is the opposite of gunze_connect()
98  */
99
100 static void gunze_disconnect(struct serio *serio)
101 {
102         struct gunze* gunze = serio->private;
103         input_unregister_device(&gunze->dev);
104         serio_close(serio);
105         kfree(gunze);
106 }
107
108 /*
109  * gunze_connect() is the routine that is called when someone adds a
110  * new serio device. It looks whether it was registered as a Gunze touchscreen
111  * and if yes, registers it as an input device.
112  */
113
114 static void gunze_connect(struct serio *serio, struct serio_dev *dev)
115 {
116         struct gunze *gunze;
117
118         if (serio->type != (SERIO_RS232 | SERIO_GUNZE))
119                 return;
120
121         if (!(gunze = kmalloc(sizeof(struct gunze), GFP_KERNEL)))
122                 return;
123
124         memset(gunze, 0, sizeof(struct gunze));
125
126         init_input_dev(&gunze->dev);
127         gunze->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
128         gunze->dev.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
129         input_set_abs_params(&gunze->dev, ABS_X, 96, 4000, 0, 0);
130         input_set_abs_params(&gunze->dev, ABS_Y, 72, 3000, 0, 0);
131
132         gunze->serio = serio;
133         serio->private = gunze;
134
135         sprintf(gunze->phys, "%s/input0", serio->phys);
136
137         gunze->dev.private = gunze;
138         gunze->dev.name = gunze_name;
139         gunze->dev.phys = gunze->phys;
140         gunze->dev.id.bustype = BUS_RS232;
141         gunze->dev.id.vendor = SERIO_GUNZE;
142         gunze->dev.id.product = 0x0051;
143         gunze->dev.id.version = 0x0100;
144
145         if (serio_open(serio, dev)) {
146                 kfree(gunze);
147                 return;
148         }
149
150         input_register_device(&gunze->dev);
151
152         printk(KERN_INFO "input: %s on %s\n", gunze_name, serio->phys);
153 }
154
155 /*
156  * The serio device structure.
157  */
158
159 static struct serio_dev gunze_dev = {
160         .interrupt =    gunze_interrupt,
161         .connect =      gunze_connect,
162         .disconnect =   gunze_disconnect,
163 };
164
165 /*
166  * The functions for inserting/removing us as a module.
167  */
168
169 int __init gunze_init(void)
170 {
171         serio_register_device(&gunze_dev);
172         return 0;
173 }
174
175 void __exit gunze_exit(void)
176 {
177         serio_unregister_device(&gunze_dev);
178 }
179
180 module_init(gunze_init);
181 module_exit(gunze_exit);