blob: 0300dca8591d9cdb076f0413eee62a6b56823fc8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040038#define TSDEV_MINOR_BASE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/smp_lock.h>
52#include <linux/random.h>
53#include <linux/time.h>
54#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#ifndef CONFIG_INPUT_TSDEV_SCREEN_X
57#define CONFIG_INPUT_TSDEV_SCREEN_X 240
58#endif
59#ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
60#define CONFIG_INPUT_TSDEV_SCREEN_Y 320
61#endif
62
63/* This driver emulates both protocols of the old h3600_ts and h3600_tsraw
64 * devices. The first one must output X/Y data in 'cooked' format, e.g.
65 * filtered, dejittered and calibrated. Second device just outputs raw
66 * data received from the hardware.
67 *
68 * This driver doesn't support filtering and dejittering; it supports only
69 * calibration. Filtering and dejittering must be done in the low-level
70 * driver, if needed, because it may gain additional benefits from knowing
71 * the low-level details, the nature of noise and so on.
72 *
73 * The driver precomputes a calibration matrix given the initial xres and
74 * yres values (quite innacurate for most touchscreens) that will result
75 * in a more or less expected range of output values. The driver supports
76 * the TS_SET_CAL ioctl, which will replace the calibration matrix with a
77 * new one, supposedly generated from the values taken from the raw device.
78 */
79
80MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
81MODULE_DESCRIPTION("Input driver to touchscreen converter");
82MODULE_LICENSE("GPL");
83
84static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
85module_param(xres, uint, 0);
86MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
87
88static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
89module_param(yres, uint, 0);
90MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
91
92/* From Compaq's Touch Screen Specification version 0.2 (draft) */
93struct ts_event {
94 short pressure;
95 short x;
96 short y;
97 short millisecs;
98};
99
100struct ts_calibration {
101 int xscale;
102 int xtrans;
103 int yscale;
104 int ytrans;
105 int xyswap;
106};
107
108struct tsdev {
109 int exist;
110 int open;
111 int minor;
112 char name[8];
113 wait_queue_head_t wait;
114 struct list_head list;
115 struct input_handle handle;
116 int x, y, pressure;
117 struct ts_calibration cal;
118};
119
120struct tsdev_list {
121 struct fasync_struct *fasync;
122 struct list_head node;
123 struct tsdev *tsdev;
124 int head, tail;
125 struct ts_event event[TSDEV_BUFFER_SIZE];
126 int raw;
127};
128
129/* The following ioctl codes are defined ONLY for backward compatibility.
130 * Don't use tsdev for new developement; use the tslib library instead.
131 * Touchscreen calibration is a fully userspace task.
132 */
133/* Use 'f' as magic number */
134#define IOC_H3600_TS_MAGIC 'f'
135#define TS_GET_CAL _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
136#define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct tsdev *tsdev_table[TSDEV_MINORS/2];
139
140static int tsdev_fasync(int fd, struct file *file, int on)
141{
142 struct tsdev_list *list = file->private_data;
143 int retval;
144
145 retval = fasync_helper(fd, file, on, &list->fasync);
146 return retval < 0 ? retval : 0;
147}
148
149static int tsdev_open(struct inode *inode, struct file *file)
150{
151 int i = iminor(inode) - TSDEV_MINOR_BASE;
152 struct tsdev_list *list;
153
Richard Purdieff141a02007-02-10 01:29:11 -0500154 printk(KERN_WARNING "tsdev (compaq touchscreen emulation) is scheduled "
155 "for removal.\nSee Documentation/feature-removal-schedule.txt "
156 "for details.\n");
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (i >= TSDEV_MINORS || !tsdev_table[i & TSDEV_MINOR_MASK])
159 return -ENODEV;
160
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500161 if (!(list = kzalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 list->raw = (i >= TSDEV_MINORS/2) ? 1 : 0;
165
166 i &= TSDEV_MINOR_MASK;
167 list->tsdev = tsdev_table[i];
168 list_add_tail(&list->node, &tsdev_table[i]->list);
169 file->private_data = list;
170
171 if (!list->tsdev->open++)
172 if (list->tsdev->exist)
173 input_open_device(&list->tsdev->handle);
174 return 0;
175}
176
177static void tsdev_free(struct tsdev *tsdev)
178{
179 tsdev_table[tsdev->minor] = NULL;
180 kfree(tsdev);
181}
182
183static int tsdev_release(struct inode *inode, struct file *file)
184{
185 struct tsdev_list *list = file->private_data;
186
187 tsdev_fasync(-1, file, 0);
188 list_del(&list->node);
189
190 if (!--list->tsdev->open) {
191 if (list->tsdev->exist)
192 input_close_device(&list->tsdev->handle);
193 else
194 tsdev_free(list->tsdev);
195 }
196 kfree(list);
197 return 0;
198}
199
200static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
201 loff_t * ppos)
202{
203 struct tsdev_list *list = file->private_data;
204 int retval = 0;
205
206 if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
207 return -EAGAIN;
208
209 retval = wait_event_interruptible(list->tsdev->wait,
210 list->head != list->tail || !list->tsdev->exist);
211
212 if (retval)
213 return retval;
214
215 if (!list->tsdev->exist)
216 return -ENODEV;
217
218 while (list->head != list->tail &&
219 retval + sizeof (struct ts_event) <= count) {
220 if (copy_to_user (buffer + retval, list->event + list->tail,
221 sizeof (struct ts_event)))
222 return -EFAULT;
223 list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
224 retval += sizeof (struct ts_event);
225 }
226
227 return retval;
228}
229
230/* No kernel lock - fine */
231static unsigned int tsdev_poll(struct file *file, poll_table * wait)
232{
233 struct tsdev_list *list = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 poll_wait(file, &list->tsdev->wait, wait);
236 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
237 (list->tsdev->exist ? 0 : (POLLHUP | POLLERR));
238}
239
240static int tsdev_ioctl(struct inode *inode, struct file *file,
241 unsigned int cmd, unsigned long arg)
242{
243 struct tsdev_list *list = file->private_data;
244 struct tsdev *tsdev = list->tsdev;
245 int retval = 0;
246
247 switch (cmd) {
248 case TS_GET_CAL:
249 if (copy_to_user ((void __user *)arg, &tsdev->cal,
250 sizeof (struct ts_calibration)))
251 retval = -EFAULT;
252 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 case TS_SET_CAL:
255 if (copy_from_user (&tsdev->cal, (void __user *)arg,
256 sizeof (struct ts_calibration)))
257 retval = -EFAULT;
258 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 default:
261 retval = -EINVAL;
262 break;
263 }
264
265 return retval;
266}
267
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400268static const struct file_operations tsdev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .owner = THIS_MODULE,
270 .open = tsdev_open,
271 .release = tsdev_release,
272 .read = tsdev_read,
273 .poll = tsdev_poll,
274 .fasync = tsdev_fasync,
275 .ioctl = tsdev_ioctl,
276};
277
278static void tsdev_event(struct input_handle *handle, unsigned int type,
279 unsigned int code, int value)
280{
281 struct tsdev *tsdev = handle->private;
282 struct tsdev_list *list;
283 struct timeval time;
284
285 switch (type) {
286 case EV_ABS:
287 switch (code) {
288 case ABS_X:
289 tsdev->x = value;
290 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case ABS_Y:
293 tsdev->y = value;
294 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 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;
304 }
305 break;
306
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;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case REL_Y:
318 tsdev->y += value;
319 if (tsdev->y < 0)
320 tsdev->y = 0;
321 else if (tsdev->y > yres)
322 tsdev->y = yres;
323 break;
324 }
325 break;
326
327 case EV_KEY:
328 if (code == BTN_TOUCH || code == BTN_MOUSE) {
329 switch (value) {
330 case 0:
331 tsdev->pressure = 0;
332 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 case 1:
335 if (!tsdev->pressure)
336 tsdev->pressure = 1;
337 break;
338 }
339 }
340 break;
341 }
342
343 if (type != EV_SYN || code != SYN_REPORT)
344 return;
345
346 list_for_each_entry(list, &tsdev->list, node) {
347 int x, y, tmp;
348
349 do_gettimeofday(&time);
350 list->event[list->head].millisecs = time.tv_usec / 100;
351 list->event[list->head].pressure = tsdev->pressure;
352
353 x = tsdev->x;
354 y = tsdev->y;
355
356 /* Calibration */
357 if (!list->raw) {
358 x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
359 y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
360 if (tsdev->cal.xyswap) {
361 tmp = x; x = y; y = tmp;
362 }
363 }
364
365 list->event[list->head].x = x;
366 list->event[list->head].y = y;
367 list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
368 kill_fasync(&list->fasync, SIGIO, POLL_IN);
369 }
370 wake_up_interruptible(&tsdev->wait);
371}
372
373static struct input_handle *tsdev_connect(struct input_handler *handler,
374 struct input_dev *dev,
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400375 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct tsdev *tsdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700378 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int minor, delta;
380
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400381 for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++);
382 if (minor >= TSDEV_MINORS / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 printk(KERN_ERR
384 "tsdev: You have way too many touchscreens\n");
385 return NULL;
386 }
387
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500388 if (!(tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 INIT_LIST_HEAD(&tsdev->list);
392 init_waitqueue_head(&tsdev->wait);
393
394 sprintf(tsdev->name, "ts%d", minor);
395
396 tsdev->exist = 1;
397 tsdev->minor = minor;
398 tsdev->handle.dev = dev;
399 tsdev->handle.name = tsdev->name;
400 tsdev->handle.handler = handler;
401 tsdev->handle.private = tsdev;
402
403 /* Precompute the rough calibration matrix */
404 delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
405 if (delta == 0)
406 delta = 1;
407 tsdev->cal.xscale = (xres << 8) / delta;
408 tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
409
410 delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
411 if (delta == 0)
412 delta = 1;
413 tsdev->cal.yscale = (yres << 8) / delta;
414 tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
415
416 tsdev_table[minor] = tsdev;
417
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700418 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800419 MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700420 dev->cdev.dev, tsdev->name);
421
422 /* temporary symlink to keep userspace happy */
423 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
424 tsdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return &tsdev->handle;
427}
428
429static void tsdev_disconnect(struct input_handle *handle)
430{
431 struct tsdev *tsdev = handle->private;
432 struct tsdev_list *list;
433
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700434 sysfs_remove_link(&input_class.subsys.kset.kobj, tsdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700435 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800436 MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 tsdev->exist = 0;
438
439 if (tsdev->open) {
440 input_close_device(handle);
441 wake_up_interruptible(&tsdev->wait);
442 list_for_each_entry(list, &tsdev->list, node)
443 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
444 } else
445 tsdev_free(tsdev);
446}
447
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400448static const struct input_device_id tsdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 {
450 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
451 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
452 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
453 .relbit = { BIT(REL_X) | BIT(REL_Y) },
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400454 }, /* A mouse like device, at least one button, two relative axes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 {
457 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
458 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
459 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
460 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400461 }, /* A tablet like device, at least touch detection, two absolute axes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 {
464 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
465 .evbit = { BIT(EV_ABS) },
466 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400467 }, /* A tablet like device with several gradations of pressure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400469 {} /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470};
471
472MODULE_DEVICE_TABLE(input, tsdev_ids);
473
474static struct input_handler tsdev_handler = {
475 .event = tsdev_event,
476 .connect = tsdev_connect,
477 .disconnect = tsdev_disconnect,
478 .fops = &tsdev_fops,
479 .minor = TSDEV_MINOR_BASE,
480 .name = "tsdev",
481 .id_table = tsdev_ids,
482};
483
484static int __init tsdev_init(void)
485{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400486 return input_register_handler(&tsdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void __exit tsdev_exit(void)
490{
491 input_unregister_handler(&tsdev_handler);
492}
493
494module_init(tsdev_init);
495module_exit(tsdev_exit);