Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / input / tsdev.c
1 /*
2  * $Id: tsdev.c,v 1.15 2002/04/10 16:50:19 jsimmons Exp $
3  *
4  *  Copyright (c) 2001 "Crazy" james Simmons
5  *
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.
12  *
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.
17  */
18
19 /*
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.
24  *
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.
29  *
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
33  *
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>.
36  */
37
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
43
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
57 #ifndef CONFIG_INPUT_TSDEV_SCREEN_X
58 #define CONFIG_INPUT_TSDEV_SCREEN_X     240
59 #endif
60 #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
61 #define CONFIG_INPUT_TSDEV_SCREEN_Y     320
62 #endif
63
64 /* This driver emulates both protocols of the old h3600_ts and h3600_tsraw
65  * devices. The first one must output X/Y data in 'cooked' format, e.g.
66  * filtered, dejittered and calibrated. Second device just outputs raw
67  * data received from the hardware.
68  *
69  * This driver doesn't support filtering and dejittering; it supports only
70  * calibration. Filtering and dejittering must be done in the low-level
71  * driver, if needed, because it may gain additional benefits from knowing
72  * the low-level details, the nature of noise and so on.
73  *
74  * The driver precomputes a calibration matrix given the initial xres and
75  * yres values (quite innacurate for most touchscreens) that will result
76  * in a more or less expected range of output values. The driver supports
77  * the TS_SET_CAL ioctl, which will replace the calibration matrix with a
78  * new one, supposedly generated from the values taken from the raw device.
79  */
80
81 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
82 MODULE_DESCRIPTION("Input driver to touchscreen converter");
83 MODULE_LICENSE("GPL");
84
85 static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
86 module_param(xres, uint, 0);
87 MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
88
89 static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
90 module_param(yres, uint, 0);
91 MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
92
93 /* From Compaq's Touch Screen Specification version 0.2 (draft) */
94 struct ts_event {
95         short pressure;
96         short x;
97         short y;
98         short millisecs;
99 };
100
101 struct ts_calibration {
102         int xscale;
103         int xtrans;
104         int yscale;
105         int ytrans;
106         int xyswap;
107 };
108
109 struct tsdev {
110         int exist;
111         int open;
112         int minor;
113         char name[8];
114         wait_queue_head_t wait;
115         struct list_head list;
116         struct input_handle handle;
117         int x, y, pressure;
118         struct ts_calibration cal;
119 };
120
121 struct tsdev_list {
122         struct fasync_struct *fasync;
123         struct list_head node;
124         struct tsdev *tsdev;
125         int head, tail;
126         struct ts_event event[TSDEV_BUFFER_SIZE];
127         int raw;
128 };
129
130 /* The following ioctl codes are defined ONLY for backward compatibility.
131  * Don't use tsdev for new developement; use the tslib library instead.
132  * Touchscreen calibration is a fully userspace task.
133  */
134 /* Use 'f' as magic number */
135 #define IOC_H3600_TS_MAGIC  'f'
136 #define TS_GET_CAL      _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
137 #define TS_SET_CAL      _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
138
139 static struct input_handler tsdev_handler;
140
141 static struct tsdev *tsdev_table[TSDEV_MINORS/2];
142
143 static int tsdev_fasync(int fd, struct file *file, int on)
144 {
145         struct tsdev_list *list = file->private_data;
146         int retval;
147
148         retval = fasync_helper(fd, file, on, &list->fasync);
149         return retval < 0 ? retval : 0;
150 }
151
152 static int tsdev_open(struct inode *inode, struct file *file)
153 {
154         int i = iminor(inode) - TSDEV_MINOR_BASE;
155         struct tsdev_list *list;
156
157         if (i >= TSDEV_MINORS || !tsdev_table[i & TSDEV_MINOR_MASK])
158                 return -ENODEV;
159
160         if (!(list = kzalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
161                 return -ENOMEM;
162
163         list->raw = (i >= TSDEV_MINORS/2) ? 1 : 0;
164
165         i &= TSDEV_MINOR_MASK;
166         list->tsdev = tsdev_table[i];
167         list_add_tail(&list->node, &tsdev_table[i]->list);
168         file->private_data = list;
169
170         if (!list->tsdev->open++)
171                 if (list->tsdev->exist)
172                         input_open_device(&list->tsdev->handle);
173         return 0;
174 }
175
176 static void tsdev_free(struct tsdev *tsdev)
177 {
178         tsdev_table[tsdev->minor] = NULL;
179         kfree(tsdev);
180 }
181
182 static int tsdev_release(struct inode *inode, struct file *file)
183 {
184         struct tsdev_list *list = file->private_data;
185
186         tsdev_fasync(-1, file, 0);
187         list_del(&list->node);
188
189         if (!--list->tsdev->open) {
190                 if (list->tsdev->exist)
191                         input_close_device(&list->tsdev->handle);
192                 else
193                         tsdev_free(list->tsdev);
194         }
195         kfree(list);
196         return 0;
197 }
198
199 static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
200                           loff_t * ppos)
201 {
202         struct tsdev_list *list = file->private_data;
203         int retval = 0;
204
205         if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
206                 return -EAGAIN;
207
208         retval = wait_event_interruptible(list->tsdev->wait,
209                         list->head != list->tail || !list->tsdev->exist);
210
211         if (retval)
212                 return retval;
213
214         if (!list->tsdev->exist)
215                 return -ENODEV;
216
217         while (list->head != list->tail &&
218                retval + sizeof (struct ts_event) <= count) {
219                 if (copy_to_user (buffer + retval, list->event + list->tail,
220                                   sizeof (struct ts_event)))
221                         return -EFAULT;
222                 list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
223                 retval += sizeof (struct ts_event);
224         }
225
226         return retval;
227 }
228
229 /* No kernel lock - fine */
230 static unsigned int tsdev_poll(struct file *file, poll_table * wait)
231 {
232         struct tsdev_list *list = file->private_data;
233
234         poll_wait(file, &list->tsdev->wait, wait);
235         return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
236                 (list->tsdev->exist ? 0 : (POLLHUP | POLLERR));
237 }
238
239 static int tsdev_ioctl(struct inode *inode, struct file *file,
240                        unsigned int cmd, unsigned long arg)
241 {
242         struct tsdev_list *list = file->private_data;
243         struct tsdev *tsdev = list->tsdev;
244         int retval = 0;
245
246         switch (cmd) {
247         case TS_GET_CAL:
248                 if (copy_to_user ((void __user *)arg, &tsdev->cal,
249                                   sizeof (struct ts_calibration)))
250                         retval = -EFAULT;
251                 break;
252
253         case TS_SET_CAL:
254                 if (copy_from_user (&tsdev->cal, (void __user *)arg,
255                                     sizeof (struct ts_calibration)))
256                         retval = -EFAULT;
257                 break;
258
259         default:
260                 retval = -EINVAL;
261                 break;
262         }
263
264         return retval;
265 }
266
267 static struct file_operations tsdev_fops = {
268         .owner =        THIS_MODULE,
269         .open =         tsdev_open,
270         .release =      tsdev_release,
271         .read =         tsdev_read,
272         .poll =         tsdev_poll,
273         .fasync =       tsdev_fasync,
274         .ioctl =        tsdev_ioctl,
275 };
276
277 static void tsdev_event(struct input_handle *handle, unsigned int type,
278                         unsigned int code, int value)
279 {
280         struct tsdev *tsdev = handle->private;
281         struct tsdev_list *list;
282         struct timeval time;
283
284         switch (type) {
285         case EV_ABS:
286                 switch (code) {
287                 case ABS_X:
288                         tsdev->x = value;
289                         break;
290
291                 case ABS_Y:
292                         tsdev->y = value;
293                         break;
294
295                 case ABS_PRESSURE:
296                         if (value > handle->dev->absmax[ABS_PRESSURE])
297                                 value = handle->dev->absmax[ABS_PRESSURE];
298                         value -= handle->dev->absmin[ABS_PRESSURE];
299                         if (value < 0)
300                                 value = 0;
301                         tsdev->pressure = value;
302                         break;
303                 }
304                 break;
305
306         case EV_REL:
307                 switch (code) {
308                 case REL_X:
309                         tsdev->x += value;
310                         if (tsdev->x < 0)
311                                 tsdev->x = 0;
312                         else if (tsdev->x > xres)
313                                 tsdev->x = xres;
314                         break;
315
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;
323                 }
324                 break;
325
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
333                         case 1:
334                                 if (!tsdev->pressure)
335                                         tsdev->pressure = 1;
336                                 break;
337                         }
338                 }
339                 break;
340         }
341
342         if (type != EV_SYN || code != SYN_REPORT)
343                 return;
344
345         list_for_each_entry(list, &tsdev->list, node) {
346                 int x, y, tmp;
347
348                 do_gettimeofday(&time);
349                 list->event[list->head].millisecs = time.tv_usec / 100;
350                 list->event[list->head].pressure = tsdev->pressure;
351
352                 x = tsdev->x;
353                 y = tsdev->y;
354
355                 /* Calibration */
356                 if (!list->raw) {
357                         x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
358                         y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
359                         if (tsdev->cal.xyswap) {
360                                 tmp = x; x = y; y = tmp;
361                         }
362                 }
363
364                 list->event[list->head].x = x;
365                 list->event[list->head].y = y;
366                 list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
367                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
368         }
369         wake_up_interruptible(&tsdev->wait);
370 }
371
372 static struct input_handle *tsdev_connect(struct input_handler *handler,
373                                           struct input_dev *dev,
374                                           struct input_device_id *id)
375 {
376         struct tsdev *tsdev;
377         struct class_device *cdev;
378         int minor, delta;
379
380         for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++);
381         if (minor >= TSDEV_MINORS / 2) {
382                 printk(KERN_ERR
383                        "tsdev: You have way too many touchscreens\n");
384                 return NULL;
385         }
386
387         if (!(tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL)))
388                 return NULL;
389
390         INIT_LIST_HEAD(&tsdev->list);
391         init_waitqueue_head(&tsdev->wait);
392
393         sprintf(tsdev->name, "ts%d", minor);
394
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;
401
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);
408
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);
414
415         tsdev_table[minor] = tsdev;
416
417         cdev = class_device_create(&input_class, &dev->cdev,
418                         MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
419                         dev->cdev.dev, tsdev->name);
420
421         /* temporary symlink to keep userspace happy */
422         sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
423                           tsdev->name);
424
425         return &tsdev->handle;
426 }
427
428 static void tsdev_disconnect(struct input_handle *handle)
429 {
430         struct tsdev *tsdev = handle->private;
431         struct tsdev_list *list;
432
433         sysfs_remove_link(&input_class.subsys.kset.kobj, tsdev->name);
434         class_device_destroy(&input_class,
435                         MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
436         tsdev->exist = 0;
437
438         if (tsdev->open) {
439                 input_close_device(handle);
440                 wake_up_interruptible(&tsdev->wait);
441                 list_for_each_entry(list, &tsdev->list, node)
442                         kill_fasync(&list->fasync, SIGIO, POLL_HUP);
443         } else
444                 tsdev_free(tsdev);
445 }
446
447 static struct input_device_id tsdev_ids[] = {
448         {
449               .flags    = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
450               .evbit    = { BIT(EV_KEY) | BIT(EV_REL) },
451               .keybit   = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
452               .relbit   = { BIT(REL_X) | BIT(REL_Y) },
453         }, /* A mouse like device, at least one button, two relative axes */
454
455         {
456               .flags    = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
457               .evbit    = { BIT(EV_KEY) | BIT(EV_ABS) },
458               .keybit   = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
459               .absbit   = { BIT(ABS_X) | BIT(ABS_Y) },
460         }, /* A tablet like device, at least touch detection, two absolute axes */
461
462         {
463               .flags    = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
464               .evbit    = { BIT(EV_ABS) },
465               .absbit   = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
466         }, /* A tablet like device with several gradations of pressure */
467
468         {} /* Terminating entry */
469 };
470
471 MODULE_DEVICE_TABLE(input, tsdev_ids);
472
473 static struct input_handler tsdev_handler = {
474         .event =        tsdev_event,
475         .connect =      tsdev_connect,
476         .disconnect =   tsdev_disconnect,
477         .fops =         &tsdev_fops,
478         .minor =        TSDEV_MINOR_BASE,
479         .name =         "tsdev",
480         .id_table =     tsdev_ids,
481 };
482
483 static int __init tsdev_init(void)
484 {
485         input_register_handler(&tsdev_handler);
486         printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
487         return 0;
488 }
489
490 static void __exit tsdev_exit(void)
491 {
492         input_unregister_handler(&tsdev_handler);
493 }
494
495 module_init(tsdev_init);
496 module_exit(tsdev_exit);