2 * $Id: tsdev.c,v 1.15 2002/04/10 16:50:19 jsimmons Exp $
4 * Copyright (c) 2001 "Crazy" james Simmons
6 * Input driver to Touchscreen device driver module.
8 * Sponsored by Transvirtual Technology
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.
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.
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
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <jsimmons@transvirtual.com>.
30 #define TSDEV_MINOR_BASE 128
31 #define TSDEV_MINORS 32
32 #define TSDEV_BUFFER_SIZE 64
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/major.h>
41 #include <linux/config.h>
42 #include <linux/smp_lock.h>
43 #include <linux/random.h>
44 #include <linux/time.h>
45 #include <linux/device.h>
46 #include <linux/devfs_fs_kernel.h>
48 #ifndef CONFIG_INPUT_TSDEV_SCREEN_X
49 #define CONFIG_INPUT_TSDEV_SCREEN_X 240
51 #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
52 #define CONFIG_INPUT_TSDEV_SCREEN_Y 320
55 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
56 MODULE_DESCRIPTION("Input driver to touchscreen converter");
57 MODULE_LICENSE("GPL");
59 static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
60 module_param(xres, uint, 0);
61 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
63 static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
64 module_param(yres, uint, 0);
65 MODULE_PARM_DESC(yres, "Vertical screen resolution");
72 wait_queue_head_t wait;
73 struct list_head list;
74 struct input_handle handle;
77 /* From Compaq's Touch Screen Specification version 0.2 (draft) */
86 struct fasync_struct *fasync;
87 struct list_head node;
90 int oldx, oldy, pendown;
91 TS_EVENT event[TSDEV_BUFFER_SIZE];
94 static struct input_handler tsdev_handler;
96 static struct tsdev *tsdev_table[TSDEV_MINORS];
98 static int tsdev_fasync(int fd, struct file *file, int on)
100 struct tsdev_list *list = file->private_data;
103 retval = fasync_helper(fd, file, on, &list->fasync);
104 return retval < 0 ? retval : 0;
107 static int tsdev_open(struct inode *inode, struct file *file)
109 int i = iminor(inode) - TSDEV_MINOR_BASE;
110 struct tsdev_list *list;
112 if (i >= TSDEV_MINORS || !tsdev_table[i])
115 if (!(list = kmalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
117 memset(list, 0, sizeof(struct tsdev_list));
119 list->tsdev = tsdev_table[i];
120 list_add_tail(&list->node, &tsdev_table[i]->list);
121 file->private_data = list;
123 if (!list->tsdev->open++)
124 if (list->tsdev->exist)
125 input_open_device(&list->tsdev->handle);
129 static void tsdev_free(struct tsdev *tsdev)
131 devfs_remove("input/ts%d", tsdev->minor);
132 class_simple_device_remove(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
133 tsdev_table[tsdev->minor] = NULL;
137 static int tsdev_release(struct inode *inode, struct file *file)
139 struct tsdev_list *list = file->private_data;
141 tsdev_fasync(-1, file, 0);
142 list_del(&list->node);
144 if (!--list->tsdev->open) {
145 if (list->tsdev->exist)
146 input_close_device(&list->tsdev->handle);
148 tsdev_free(list->tsdev);
154 static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
157 struct tsdev_list *list = file->private_data;
160 if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
163 retval = wait_event_interruptible(list->tsdev->wait,
164 (list->head != list->tail) && list->tsdev->exist);
169 if (!list->tsdev->exist)
172 while (list->head != list->tail && retval + sizeof(TS_EVENT) <= count) {
173 if (copy_to_user (buffer + retval, list->event + list->tail, sizeof(TS_EVENT)))
175 list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
176 retval += sizeof(TS_EVENT);
182 /* No kernel lock - fine */
183 static unsigned int tsdev_poll(struct file *file, poll_table * wait)
185 struct tsdev_list *list = file->private_data;
187 poll_wait(file, &list->tsdev->wait, wait);
188 if (list->head != list->tail)
189 return POLLIN | POLLRDNORM;
193 static int tsdev_ioctl(struct inode *inode, struct file *file,
194 unsigned int cmd, unsigned long arg)
197 struct tsdev_list *list = file->private_data;
198 struct tsdev *evdev = list->tsdev;
199 struct input_dev *dev = tsdev->handle.dev;
214 struct file_operations tsdev_fops = {
215 .owner = THIS_MODULE,
217 .release = tsdev_release,
220 .fasync = tsdev_fasync,
221 .ioctl = tsdev_ioctl,
224 static void tsdev_event(struct input_handle *handle, unsigned int type,
225 unsigned int code, int value)
227 struct tsdev *tsdev = handle->private;
228 struct tsdev_list *list;
232 list_for_each_entry(list, &tsdev->list, node) {
239 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
241 list->oldx = ((value - handle->dev->absmin[ABS_X]) * xres / size);
243 list->oldx = ((value - handle->dev->absmin[ABS_X]));
248 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
250 list->oldy = ((value - handle->dev->absmin[ABS_Y]) * yres / size);
252 list->oldy = ((value - handle->dev->absmin[ABS_Y]));
255 list->pendown = ((value > handle->dev-> absmin[ABS_PRESSURE])) ?
256 value - handle->dev->absmin[ABS_PRESSURE] : 0;
269 else if (list->oldx > xres)
278 else if (list->oldy > xres)
285 if (code == BTN_TOUCH || code == BTN_MOUSE) {
301 do_gettimeofday(&time);
302 list->event[list->head].millisecs = time.tv_usec / 100;
303 list->event[list->head].pressure = list->pendown;
304 list->event[list->head].x = list->oldx;
305 list->event[list->head].y = list->oldy;
306 list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
307 kill_fasync(&list->fasync, SIGIO, POLL_IN);
309 wake_up_interruptible(&tsdev->wait);
312 static struct input_handle *tsdev_connect(struct input_handler *handler,
313 struct input_dev *dev,
314 struct input_device_id *id)
319 for (minor = 0; minor < TSDEV_MINORS && tsdev_table[minor];
321 if (minor == TSDEV_MINORS) {
323 "tsdev: You have way too many touchscreens\n");
327 if (!(tsdev = kmalloc(sizeof(struct tsdev), GFP_KERNEL)))
329 memset(tsdev, 0, sizeof(struct tsdev));
331 INIT_LIST_HEAD(&tsdev->list);
332 init_waitqueue_head(&tsdev->wait);
334 sprintf(tsdev->name, "ts%d", minor);
337 tsdev->minor = minor;
338 tsdev->handle.dev = dev;
339 tsdev->handle.name = tsdev->name;
340 tsdev->handle.handler = handler;
341 tsdev->handle.private = tsdev;
343 tsdev_table[minor] = tsdev;
345 devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
346 S_IFCHR|S_IRUGO|S_IWUSR, "input/ts%d", minor);
347 class_simple_device_add(input_class,
348 MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
349 dev->dev, "ts%d", minor);
351 return &tsdev->handle;
354 static void tsdev_disconnect(struct input_handle *handle)
356 struct tsdev *tsdev = handle->private;
361 input_close_device(handle);
362 wake_up_interruptible(&tsdev->wait);
367 static struct input_device_id tsdev_ids[] = {
369 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
370 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
371 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
372 .relbit = { BIT(REL_X) | BIT(REL_Y) },
373 },/* A mouse like device, at least one button, two relative axes */
376 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
377 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
378 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
379 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
380 },/* A tablet like device, at least touch detection, two absolute axes */
382 {},/* Terminating entry */
385 MODULE_DEVICE_TABLE(input, tsdev_ids);
387 static struct input_handler tsdev_handler = {
388 .event = tsdev_event,
389 .connect = tsdev_connect,
390 .disconnect = tsdev_disconnect,
392 .minor = TSDEV_MINOR_BASE,
394 .id_table = tsdev_ids,
397 static int __init tsdev_init(void)
399 input_register_handler(&tsdev_handler);
400 printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
404 static void __exit tsdev_exit(void)
406 input_unregister_handler(&tsdev_handler);
409 module_init(tsdev_init);
410 module_exit(tsdev_exit);