blob: 466da190ceeccc804303a205f0750dc6d71c3983 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#define DRIVER_DESC "Gunze AHL-51S touchscreen driver"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
45/*
46 * Definitions & global arrays.
47 */
48
49#define GUNZE_MAX_LENGTH 10
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Per-touchscreen data.
53 */
54
55struct gunze {
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -050056 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct serio *serio;
58 int idx;
59 unsigned char data[GUNZE_MAX_LENGTH];
60 char phys[32];
61};
62
63static void gunze_process_packet(struct gunze* gunze, struct pt_regs *regs)
64{
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -050065 struct input_dev *dev = gunze->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (gunze->idx != GUNZE_MAX_LENGTH || gunze->data[5] != ',' ||
68 (gunze->data[0] != 'T' && gunze->data[0] != 'R')) {
Dmitry Torokhova07461e2005-05-28 02:12:00 -050069 printk(KERN_WARNING "gunze.c: bad packet: >%.*s<\n", GUNZE_MAX_LENGTH, gunze->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return;
71 }
72
73 input_regs(dev, regs);
74 input_report_abs(dev, ABS_X, simple_strtoul(gunze->data + 1, NULL, 10));
75 input_report_abs(dev, ABS_Y, 1024 - simple_strtoul(gunze->data + 6, NULL, 10));
76 input_report_key(dev, BTN_TOUCH, gunze->data[0] == 'T');
77 input_sync(dev);
78}
79
80static irqreturn_t gunze_interrupt(struct serio *serio,
81 unsigned char data, unsigned int flags, struct pt_regs *regs)
82{
83 struct gunze* gunze = serio_get_drvdata(serio);
84
85 if (data == '\r') {
86 gunze_process_packet(gunze, regs);
87 gunze->idx = 0;
88 } else {
89 if (gunze->idx < GUNZE_MAX_LENGTH)
90 gunze->data[gunze->idx++] = data;
91 }
92 return IRQ_HANDLED;
93}
94
95/*
96 * gunze_disconnect() is the opposite of gunze_connect()
97 */
98
99static void gunze_disconnect(struct serio *serio)
100{
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500101 struct gunze *gunze = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500103 input_get_device(gunze->dev);
104 input_unregister_device(gunze->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 serio_close(serio);
106 serio_set_drvdata(serio, NULL);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500107 input_put_device(gunze->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kfree(gunze);
109}
110
111/*
112 * gunze_connect() is the routine that is called when someone adds a
113 * new serio device that supports Gunze protocol and registers it as
114 * an input device.
115 */
116
117static int gunze_connect(struct serio *serio, struct serio_driver *drv)
118{
119 struct gunze *gunze;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500120 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int err;
122
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500123 gunze = kzalloc(sizeof(struct gunze), GFP_KERNEL);
124 input_dev = input_allocate_device();
125 if (!gunze || !input_dev) {
126 err = -ENOMEM;
127 goto fail;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 gunze->serio = serio;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500131 gunze->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 sprintf(gunze->phys, "%s/input0", serio->phys);
133
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500134 input_dev->private = gunze;
135 input_dev->name = "Gunze AHL-51S TouchScreen";
136 input_dev->phys = gunze->phys;
137 input_dev->id.bustype = BUS_RS232;
138 input_dev->id.vendor = SERIO_GUNZE;
139 input_dev->id.product = 0x0051;
140 input_dev->id.version = 0x0100;
141 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
142 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
143 input_set_abs_params(input_dev, ABS_X, 24, 1000, 0, 0);
144 input_set_abs_params(input_dev, ABS_Y, 24, 1000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 serio_set_drvdata(serio, gunze);
147
148 err = serio_open(serio, drv);
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500149 if (err)
150 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500152 input_register_device(gunze->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
Dmitry Torokhoveca1ed12005-09-15 02:01:46 -0500154
155 fail: serio_set_drvdata(serio, NULL);
156 input_free_device(input_dev);
157 kfree(gunze);
158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
162 * The serio driver structure.
163 */
164
165static struct serio_device_id gunze_serio_ids[] = {
166 {
167 .type = SERIO_RS232,
168 .proto = SERIO_GUNZE,
169 .id = SERIO_ANY,
170 .extra = SERIO_ANY,
171 },
172 { 0 }
173};
174
175MODULE_DEVICE_TABLE(serio, gunze_serio_ids);
176
177static struct serio_driver gunze_drv = {
178 .driver = {
179 .name = "gunze",
180 },
181 .description = DRIVER_DESC,
182 .id_table = gunze_serio_ids,
183 .interrupt = gunze_interrupt,
184 .connect = gunze_connect,
185 .disconnect = gunze_disconnect,
186};
187
188/*
189 * The functions for inserting/removing us as a module.
190 */
191
192static int __init gunze_init(void)
193{
194 serio_register_driver(&gunze_drv);
195 return 0;
196}
197
198static void __exit gunze_exit(void)
199{
200 serio_unregister_driver(&gunze_drv);
201}
202
203module_init(gunze_init);
204module_exit(gunze_exit);