MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / tsdev.c
blob18c37d8ac5d6f23cdebeebc74a2cf1fbd559c59c
1 /*
2 * $Id: tsdev.c,v 1.15 2002/04/10 16:50:19 jsimmons Exp $
4 * Copyright (c) 2001 "Crazy" james Simmons
6 * Compaq touchscreen protocol driver. The protocol emulated by this driver
7 * is obsolete; for new programs use the tslib library which can read directly
8 * from evdev and perform dejittering, variance filtering and calibration -
9 * all in user space, not at kernel level. The meaning of this driver is
10 * to allow usage of newer input drivers with old applications that use the
11 * old /dev/h3600_ts and /dev/h3600_tsraw devices.
13 * 09-Apr-2004: Andrew Zabolotny <zap@homelink.ru>
14 * Fixed to actually work, not just output random numbers.
15 * Added support for both h3600_ts and h3600_tsraw protocol
16 * emulation.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Should you need to contact me, the author, you can do so either by
35 * e-mail - mail your message to <jsimmons@infradead.org>.
38 #define TSDEV_MINOR_BASE 128
39 #define TSDEV_MINORS 32
40 /* First 16 devices are h3600_ts compatible; second 16 are h3600_tsraw */
41 #define TSDEV_MINOR_MASK 15
42 #define TSDEV_BUFFER_SIZE 64
44 #include <linux/slab.h>
45 #include <linux/poll.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/init.h>
49 #include <linux/input.h>
50 #include <linux/major.h>
51 #include <linux/config.h>
52 #include <linux/smp_lock.h>
53 #include <linux/random.h>
54 #include <linux/time.h>
55 #include <linux/device.h>
56 #include <linux/devfs_fs_kernel.h>
58 #ifndef CONFIG_INPUT_TSDEV_SCREEN_X
59 #define CONFIG_INPUT_TSDEV_SCREEN_X 240
60 #endif
61 #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
62 #define CONFIG_INPUT_TSDEV_SCREEN_Y 320
63 #endif
65 /* This driver emulates both protocols of the old h3600_ts and h3600_tsraw
66 * devices. The first one must output X/Y data in 'cooked' format, e.g.
67 * filtered, dejittered and calibrated. Second device just outputs raw
68 * data received from the hardware.
70 * This driver doesn't support filtering and dejittering; it supports only
71 * calibration. Filtering and dejittering must be done in the low-level
72 * driver, if needed, because it may gain additional benefits from knowing
73 * the low-level details, the nature of noise and so on.
75 * The driver precomputes a calibration matrix given the initial xres and
76 * yres values (quite innacurate for most touchscreens) that will result
77 * in a more or less expected range of output values. The driver supports
78 * the TS_SET_CAL ioctl, which will replace the calibration matrix with a
79 * new one, supposedly generated from the values taken from the raw device.
82 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
83 MODULE_DESCRIPTION("Input driver to touchscreen converter");
84 MODULE_LICENSE("GPL");
86 static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
87 module_param(xres, uint, 0);
88 MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
90 static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
91 module_param(yres, uint, 0);
92 MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
94 /* From Compaq's Touch Screen Specification version 0.2 (draft) */
95 struct ts_event {
96 short pressure;
97 short x;
98 short y;
99 short millisecs;
102 struct ts_calibration {
103 int xscale;
104 int xtrans;
105 int yscale;
106 int ytrans;
107 int xyswap;
110 struct tsdev {
111 int exist;
112 int open;
113 int minor;
114 char name[8];
115 wait_queue_head_t wait;
116 struct list_head list;
117 struct input_handle handle;
118 int x, y, pressure;
119 struct ts_calibration cal;
122 struct tsdev_list {
123 struct fasync_struct *fasync;
124 struct list_head node;
125 struct tsdev *tsdev;
126 int head, tail;
127 struct ts_event event[TSDEV_BUFFER_SIZE];
128 int raw;
131 /* The following ioctl codes are defined ONLY for backward compatibility.
132 * Don't use tsdev for new developement; use the tslib library instead.
133 * Touchscreen calibration is a fully userspace task.
135 /* Use 'f' as magic number */
136 #define IOC_H3600_TS_MAGIC 'f'
137 #define TS_GET_CAL _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
138 #define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
140 static struct input_handler tsdev_handler;
142 static struct tsdev *tsdev_table[TSDEV_MINORS/2];
144 static int tsdev_fasync(int fd, struct file *file, int on)
146 struct tsdev_list *list = file->private_data;
147 int retval;
149 retval = fasync_helper(fd, file, on, &list->fasync);
150 return retval < 0 ? retval : 0;
153 static int tsdev_open(struct inode *inode, struct file *file)
155 int i = iminor(inode) - TSDEV_MINOR_BASE;
156 struct tsdev_list *list;
158 if (i >= TSDEV_MINORS || !tsdev_table[i & TSDEV_MINOR_MASK])
159 return -ENODEV;
161 if (!(list = kmalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
162 return -ENOMEM;
163 memset(list, 0, sizeof(struct tsdev_list));
165 list->raw = (i >= TSDEV_MINORS/2) ? 1 : 0;
167 i &= TSDEV_MINOR_MASK;
168 list->tsdev = tsdev_table[i];
169 list_add_tail(&list->node, &tsdev_table[i]->list);
170 file->private_data = list;
172 if (!list->tsdev->open++)
173 if (list->tsdev->exist)
174 input_open_device(&list->tsdev->handle);
175 return 0;
178 static void tsdev_free(struct tsdev *tsdev)
180 devfs_remove("input/ts%d", tsdev->minor);
181 class_simple_device_remove(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
182 tsdev_table[tsdev->minor] = NULL;
183 kfree(tsdev);
186 static int tsdev_release(struct inode *inode, struct file *file)
188 struct tsdev_list *list = file->private_data;
190 tsdev_fasync(-1, file, 0);
191 list_del(&list->node);
193 if (!--list->tsdev->open) {
194 if (list->tsdev->exist)
195 input_close_device(&list->tsdev->handle);
196 else
197 tsdev_free(list->tsdev);
199 kfree(list);
200 return 0;
203 static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
204 loff_t * ppos)
206 struct tsdev_list *list = file->private_data;
207 int retval = 0;
209 if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
210 return -EAGAIN;
212 retval = wait_event_interruptible(list->tsdev->wait,
213 list->head != list->tail || !list->tsdev->exist);
215 if (retval)
216 return retval;
218 if (!list->tsdev->exist)
219 return -ENODEV;
221 while (list->head != list->tail &&
222 retval + sizeof (struct ts_event) <= count) {
223 if (copy_to_user (buffer + retval, list->event + list->tail,
224 sizeof (struct ts_event)))
225 return -EFAULT;
226 list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
227 retval += sizeof (struct ts_event);
230 return retval;
233 /* No kernel lock - fine */
234 static unsigned int tsdev_poll(struct file *file, poll_table * wait)
236 struct tsdev_list *list = file->private_data;
238 poll_wait(file, &list->tsdev->wait, wait);
239 if (list->head != list->tail)
240 return POLLIN | POLLRDNORM;
241 return 0;
244 static int tsdev_ioctl(struct inode *inode, struct file *file,
245 unsigned int cmd, unsigned long arg)
247 struct tsdev_list *list = file->private_data;
248 struct tsdev *tsdev = list->tsdev;
249 int retval = 0;
251 switch (cmd) {
252 case TS_GET_CAL:
253 if (copy_to_user ((void __user *)arg, &tsdev->cal,
254 sizeof (struct ts_calibration)))
255 retval = -EFAULT;
256 break;
257 case TS_SET_CAL:
258 if (copy_from_user (&tsdev->cal, (void __user *)arg,
259 sizeof (struct ts_calibration)))
260 retval = -EFAULT;
261 break;
262 default:
263 retval = -EINVAL;
264 break;
267 return retval;
270 struct file_operations tsdev_fops = {
271 .owner = THIS_MODULE,
272 .open = tsdev_open,
273 .release = tsdev_release,
274 .read = tsdev_read,
275 .poll = tsdev_poll,
276 .fasync = tsdev_fasync,
277 .ioctl = tsdev_ioctl,
280 static void tsdev_event(struct input_handle *handle, unsigned int type,
281 unsigned int code, int value)
283 struct tsdev *tsdev = handle->private;
284 struct tsdev_list *list;
285 struct timeval time;
287 switch (type) {
288 case EV_ABS:
289 switch (code) {
290 case ABS_X:
291 tsdev->x = value;
292 break;
293 case ABS_Y:
294 tsdev->y = value;
295 break;
296 case ABS_PRESSURE:
297 if (value > handle->dev->absmax[ABS_PRESSURE])
298 value = handle->dev->absmax[ABS_PRESSURE];
299 value -= handle->dev->absmin[ABS_PRESSURE];
300 if (value < 0)
301 value = 0;
302 tsdev->pressure = value;
303 break;
305 break;
307 case EV_REL:
308 switch (code) {
309 case REL_X:
310 tsdev->x += value;
311 if (tsdev->x < 0)
312 tsdev->x = 0;
313 else if (tsdev->x > xres)
314 tsdev->x = xres;
315 break;
316 case REL_Y:
317 tsdev->y += value;
318 if (tsdev->y < 0)
319 tsdev->y = 0;
320 else if (tsdev->y > yres)
321 tsdev->y = yres;
322 break;
324 break;
326 case EV_KEY:
327 if (code == BTN_TOUCH || code == BTN_MOUSE) {
328 switch (value) {
329 case 0:
330 tsdev->pressure = 0;
331 break;
332 case 1:
333 if (!tsdev->pressure)
334 tsdev->pressure = 1;
335 break;
338 break;
341 if (type != EV_SYN || code != SYN_REPORT)
342 return;
344 list_for_each_entry(list, &tsdev->list, node) {
345 int x, y, tmp;
347 do_gettimeofday(&time);
348 list->event[list->head].millisecs = time.tv_usec / 100;
349 list->event[list->head].pressure = tsdev->pressure;
351 x = tsdev->x;
352 y = tsdev->y;
354 /* Calibration */
355 if (!list->raw) {
356 x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
357 y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
358 if (tsdev->cal.xyswap) {
359 tmp = x; x = y; y = tmp;
363 list->event[list->head].x = x;
364 list->event[list->head].y = y;
365 list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
366 kill_fasync(&list->fasync, SIGIO, POLL_IN);
368 wake_up_interruptible(&tsdev->wait);
371 static struct input_handle *tsdev_connect(struct input_handler *handler,
372 struct input_dev *dev,
373 struct input_device_id *id)
375 struct tsdev *tsdev;
376 int minor, delta;
378 for (minor = 0; minor < TSDEV_MINORS/2 && tsdev_table[minor];
379 minor++);
380 if (minor >= TSDEV_MINORS/2) {
381 printk(KERN_ERR
382 "tsdev: You have way too many touchscreens\n");
383 return NULL;
386 if (!(tsdev = kmalloc(sizeof(struct tsdev), GFP_KERNEL)))
387 return NULL;
388 memset(tsdev, 0, sizeof(struct tsdev));
390 INIT_LIST_HEAD(&tsdev->list);
391 init_waitqueue_head(&tsdev->wait);
393 sprintf(tsdev->name, "ts%d", minor);
395 tsdev->exist = 1;
396 tsdev->minor = minor;
397 tsdev->handle.dev = dev;
398 tsdev->handle.name = tsdev->name;
399 tsdev->handle.handler = handler;
400 tsdev->handle.private = tsdev;
402 /* Precompute the rough calibration matrix */
403 delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
404 if (delta == 0)
405 delta = 1;
406 tsdev->cal.xscale = (xres << 8) / delta;
407 tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
409 delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
410 if (delta == 0)
411 delta = 1;
412 tsdev->cal.yscale = (yres << 8) / delta;
413 tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
415 tsdev_table[minor] = tsdev;
417 devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
418 S_IFCHR|S_IRUGO|S_IWUSR, "input/ts%d", minor);
419 devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor + TSDEV_MINORS/2),
420 S_IFCHR|S_IRUGO|S_IWUSR, "input/tsraw%d", minor);
421 class_simple_device_add(input_class,
422 MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
423 dev->dev, "ts%d", minor);
425 return &tsdev->handle;
428 static void tsdev_disconnect(struct input_handle *handle)
430 struct tsdev *tsdev = handle->private;
432 tsdev->exist = 0;
434 if (tsdev->open) {
435 input_close_device(handle);
436 wake_up_interruptible(&tsdev->wait);
437 } else
438 tsdev_free(tsdev);
439 devfs_remove("input/tsraw%d", tsdev->minor);
442 static struct input_device_id tsdev_ids[] = {
444 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
445 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
446 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
447 .relbit = { BIT(REL_X) | BIT(REL_Y) },
448 },/* A mouse like device, at least one button, two relative axes */
451 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
452 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
453 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
454 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
455 },/* A tablet like device, at least touch detection, two absolute axes */
458 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
459 .evbit = { BIT(EV_ABS) },
460 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
461 },/* A tablet like device with several gradations of pressure */
463 {},/* Terminating entry */
466 MODULE_DEVICE_TABLE(input, tsdev_ids);
468 static struct input_handler tsdev_handler = {
469 .event = tsdev_event,
470 .connect = tsdev_connect,
471 .disconnect = tsdev_disconnect,
472 .fops = &tsdev_fops,
473 .minor = TSDEV_MINOR_BASE,
474 .name = "tsdev",
475 .id_table = tsdev_ids,
478 static int __init tsdev_init(void)
480 input_register_handler(&tsdev_handler);
481 printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
482 return 0;
485 static void __exit tsdev_exit(void)
487 input_unregister_handler(&tsdev_handler);
490 module_init(tsdev_init);
491 module_exit(tsdev_exit);