| 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 <[email protected]>
|
| 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 <[email protected]>.
|
| 36 |
|
| 37 |
|
| 38 | #define TSDEV_MINOR_BASE 128
|
| 39 | #define TSDEV_MINORS 32
|
| 40 |
|
| 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/smp_lock.h>
|
| 52 | #include <linux/random.h>
|
| 53 | #include <linux/time.h>
|
| 54 | #include <linux/device.h>
|
| 55 |
|
| 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 | |
| 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 |
|
| 80 | MODULE_AUTHOR("James Simmons <[email protected]>");
|
| 81 | MODULE_DESCRIPTION("Input driver to touchscreen converter");
|
| 82 | MODULE_LICENSE("GPL");
|
| 83 |
|
| 84 | static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
|
| 85 | module_param(xres, uint, 0);
|
| 86 | MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
|
| 87 |
|
| 88 | static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
|
| 89 | module_param(yres, uint, 0);
|
| 90 | MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
|
| 91 |
|
| 92 |
|
| 93 | struct ts_event {
|
| 94 | short pressure;
|
| 95 | short x;
|
| 96 | short y;
|
| 97 | short millisecs;
|
| 98 | };
|
| 99 |
|
| 100 | struct ts_calibration {
|
| 101 | int xscale;
|
| 102 | int xtrans;
|
| 103 | int yscale;
|
| 104 | int ytrans;
|
| 105 | int xyswap;
|
| 106 | };
|
| 107 |
|
| 108 | struct 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 |
|
| 120 | struct 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 | |
| 130 | * Don't use tsdev for new developement; use the tslib library instead.
|
| 131 | * Touchscreen calibration is a fully userspace task.
|
| 132 |
|
| 133 |
|
| 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 |
|
| 138 | static struct input_handler tsdev_handler;
|
| 139 |
|
| 140 | static struct tsdev *tsdev_table[TSDEV_MINORS/2];
|
| 141 |
|
| 142 | static int tsdev_fasync(int fd, struct file *file, int on)
|
| 143 | {
|
| 144 | struct tsdev_list *list = file->private_data;
|
| 145 | int retval;
|
| 146 |
|
| 147 | retval = fasync_helper(fd, file, on, &list->fasync);
|
| 148 | return retval < 0 ? retval : 0;
|
| 149 | }
|
| 150 |
|
| 151 | static int tsdev_open(struct inode *inode, struct file *file)
|
| 152 | {
|
| 153 | int i = iminor(inode) - TSDEV_MINOR_BASE;
|
| 154 | struct tsdev_list *list;
|
| 155 |
|
| 156 | if (i >= TSDEV_MINORS || !tsdev_table[i & TSDEV_MINOR_MASK])
|
| 157 | return -ENODEV;
|
| 158 |
|
| 159 | if (!(list = kzalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
|
| 160 | return -ENOMEM;
|
| 161 |
|
| 162 | list->raw = (i >= TSDEV_MINORS/2) ? 1 : 0;
|
| 163 |
|
| 164 | i &= TSDEV_MINOR_MASK;
|
| 165 | list->tsdev = tsdev_table[i];
|
| 166 | list_add_tail(&list->node, &tsdev_table[i]->list);
|
| 167 | file->private_data = list;
|
| 168 |
|
| 169 | if (!list->tsdev->open++)
|
| 170 | if (list->tsdev->exist)
|
| 171 | input_open_device(&list->tsdev->handle);
|
| 172 | return 0;
|
| 173 | }
|
| 174 |
|
| 175 | static void tsdev_free(struct tsdev *tsdev)
|
| 176 | {
|
| 177 | tsdev_table[tsdev->minor] = NULL;
|
| 178 | kfree(tsdev);
|
| 179 | }
|
| 180 |
|
| 181 | static int tsdev_release(struct inode *inode, struct file *file)
|
| 182 | {
|
| 183 | struct tsdev_list *list = file->private_data;
|
| 184 |
|
| 185 | tsdev_fasync(-1, file, 0);
|
| 186 | list_del(&list->node);
|
| 187 |
|
| 188 | if (!--list->tsdev->open) {
|
| 189 | if (list->tsdev->exist)
|
| 190 | input_close_device(&list->tsdev->handle);
|
| 191 | else
|
| 192 | tsdev_free(list->tsdev);
|
| 193 | }
|
| 194 | kfree(list);
|
| 195 | return 0;
|
| 196 | }
|
| 197 |
|
| 198 | static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
|
| 199 | loff_t * ppos)
|
| 200 | {
|
| 201 | struct tsdev_list *list = file->private_data;
|
| 202 | int retval = 0;
|
| 203 |
|
| 204 | if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
|
| 205 | return -EAGAIN;
|
| 206 |
|
| 207 | retval = wait_event_interruptible(list->tsdev->wait,
|
| 208 | list->head != list->tail || !list->tsdev->exist);
|
| 209 |
|
| 210 | if (retval)
|
| 211 | return retval;
|
| 212 |
|
| 213 | if (!list->tsdev->exist)
|
| 214 | return -ENODEV;
|
| 215 |
|
| 216 | while (list->head != list->tail &&
|
| 217 | retval + sizeof (struct ts_event) <= count) {
|
| 218 | if (copy_to_user (buffer + retval, list->event + list->tail,
|
| 219 | sizeof (struct ts_event)))
|
| 220 | return -EFAULT;
|
| 221 | list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
|
| 222 | retval += sizeof (struct ts_event);
|
| 223 | }
|
| 224 |
|
| 225 | return retval;
|
| 226 | }
|
| 227 |
|
| 228 |
|
| 229 | static unsigned int tsdev_poll(struct file *file, poll_table * wait)
|
| 230 | {
|
| 231 | struct tsdev_list *list = file->private_data;
|
| 232 |
|
| 233 | poll_wait(file, &list->tsdev->wait, wait);
|
| 234 | return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
|
| 235 | (list->tsdev->exist ? 0 : (POLLHUP | POLLERR));
|
| 236 | }
|
| 237 |
|
| 238 | static int tsdev_ioctl(struct inode *inode, struct file *file,
|
| 239 | unsigned int cmd, unsigned long arg)
|
| 240 | {
|
| 241 | struct tsdev_list *list = file->private_data;
|
| 242 | struct tsdev *tsdev = list->tsdev;
|
| 243 | int retval = 0;
|
| 244 |
|
| 245 | switch (cmd) {
|
| 246 | case TS_GET_CAL:
|
| 247 | if (copy_to_user ((void __user *)arg, &tsdev->cal,
|
| 248 | sizeof (struct ts_calibration)))
|
| 249 | retval = -EFAULT;
|
| 250 | break;
|
| 251 |
|
| 252 | case TS_SET_CAL:
|
| 253 | if (copy_from_user (&tsdev->cal, (void __user *)arg,
|
| 254 | sizeof (struct ts_calibration)))
|
| 255 | retval = -EFAULT;
|
| 256 | break;
|
| 257 |
|
| 258 | default:
|
| 259 | retval = -EINVAL;
|
| 260 | break;
|
| 261 | }
|
| 262 |
|
| 263 | return retval;
|
| 264 | }
|
| 265 |
|
| 266 | static struct file_operations tsdev_fops = {
|
| 267 | .owner = THIS_MODULE,
|
| 268 | .open = tsdev_open,
|
| 269 | .release = tsdev_release,
|
| 270 | .read = tsdev_read,
|
| 271 | .poll = tsdev_poll,
|
| 272 | .fasync = tsdev_fasync,
|
| 273 | .ioctl = tsdev_ioctl,
|
| 274 | };
|
| 275 |
|
| 276 | static void tsdev_event(struct input_handle *handle, unsigned int type,
|
| 277 | unsigned int code, int value)
|
| 278 | {
|
| 279 | struct tsdev *tsdev = handle->private;
|
| 280 | struct tsdev_list *list;
|
| 281 | struct timeval time;
|
| 282 |
|
| 283 | switch (type) {
|
| 284 | case EV_ABS:
|
| 285 | switch (code) {
|
| 286 | case ABS_X:
|
| 287 | tsdev->x = value;
|
| 288 | break;
|
| 289 |
|
| 290 | case ABS_Y:
|
| 291 | tsdev->y = value;
|
| 292 | break;
|
| 293 |
|
| 294 | case ABS_PRESSURE:
|
| 295 | if (value > handle->dev->absmax[ABS_PRESSURE])
|
| 296 | value = handle->dev->absmax[ABS_PRESSURE];
|
| 297 | value -= handle->dev->absmin[ABS_PRESSURE];
|
| 298 | if (value < 0)
|
| 299 | value = 0;
|
| 300 | tsdev->pressure = value;
|
| 301 | break;
|
| 302 | }
|
| 303 | break;
|
| 304 |
|
| 305 | case EV_REL:
|
| 306 | switch (code) {
|
| 307 | case REL_X:
|
| 308 | tsdev->x += value;
|
| 309 | if (tsdev->x < 0)
|
| 310 | tsdev->x = 0;
|
| 311 | else if (tsdev->x > xres)
|
| 312 | tsdev->x = xres;
|
| 313 | break;
|
| 314 |
|
| 315 | case REL_Y:
|
| 316 | tsdev->y += value;
|
| 317 | if (tsdev->y < 0)
|
| 318 | tsdev->y = 0;
|
| 319 | else if (tsdev->y > yres)
|
| 320 | tsdev->y = yres;
|
| 321 | break;
|
| 322 | }
|
| 323 | break;
|
| 324 |
|
| 325 | case EV_KEY:
|
| 326 | if (code == BTN_TOUCH || code == BTN_MOUSE) {
|
| 327 | switch (value) {
|
| 328 | case 0:
|
| 329 | tsdev->pressure = 0;
|
| 330 | break;
|
| 331 |
|
| 332 | case 1:
|
| 333 | if (!tsdev->pressure)
|
| 334 | tsdev->pressure = 1;
|
| 335 | break;
|
| 336 | }
|
| 337 | }
|
| 338 | break;
|
| 339 | }
|
| 340 |
|
| 341 | if (type != EV_SYN || code != SYN_REPORT)
|
| 342 | return;
|
| 343 |
|
| 344 | list_for_each_entry(list, &tsdev->list, node) {
|
| 345 | int x, y, tmp;
|
| 346 |
|
| 347 | do_gettimeofday(&time);
|
| 348 | list->event[list->head].millisecs = time.tv_usec / 100;
|
| 349 | list->event[list->head].pressure = tsdev->pressure;
|
| 350 |
|
| 351 | x = tsdev->x;
|
| 352 | y = tsdev->y;
|
| 353 |
|
| 354 |
|
| 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;
|
| 360 | }
|
| 361 | }
|
| 362 |
|
| 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);
|
| 367 | }
|
| 368 | wake_up_interruptible(&tsdev->wait);
|
| 369 | }
|
| 370 |
|
| 371 | static struct input_handle *tsdev_connect(struct input_handler *handler,
|
| 372 | struct input_dev *dev,
|
| 373 | struct input_device_id *id)
|
| 374 | {
|
| 375 | struct tsdev *tsdev;
|
| 376 | struct class_device *cdev;
|
| 377 | int minor, delta;
|
| 378 |
|
| 379 | for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++);
|
| 380 | if (minor >= TSDEV_MINORS / 2) {
|
| 381 | printk(KERN_ERR
|
| 382 | "tsdev: You have way too many touchscreens\n");
|
| 383 | return NULL;
|
| 384 | }
|
| 385 |
|
| 386 | if (!(tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL)))
|
| 387 | return NULL;
|
| 388 |
|
| 389 | INIT_LIST_HEAD(&tsdev->list);
|
| 390 | init_waitqueue_head(&tsdev->wait);
|
| 391 |
|
| 392 | sprintf(tsdev->name, "ts%d", minor);
|
| 393 |
|
| 394 | tsdev->exist = 1;
|
| 395 | tsdev->minor = minor;
|
| 396 | tsdev->handle.dev = dev;
|
| 397 | tsdev->handle.name = tsdev->name;
|
| 398 | tsdev->handle.handler = handler;
|
| 399 | tsdev->handle.private = tsdev;
|
| 400 |
|
| 401 |
|
| 402 | delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
|
| 403 | if (delta == 0)
|
| 404 | delta = 1;
|
| 405 | tsdev->cal.xscale = (xres << 8) / delta;
|
| 406 | tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
|
| 407 |
|
| 408 | delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
|
| 409 | if (delta == 0)
|
| 410 | delta = 1;
|
| 411 | tsdev->cal.yscale = (yres << 8) / delta;
|
| 412 | tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
|
| 413 |
|
| 414 | tsdev_table[minor] = tsdev;
|
| 415 |
|
| 416 | cdev = class_device_create(&input_class, &dev->cdev,
|
| 417 | MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
|
| 418 | dev->cdev.dev, tsdev->name);
|
| 419 |
|
| 420 |
|
| 421 | sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
|
| 422 | tsdev->name);
|
| 423 |
|
| 424 | return &tsdev->handle;
|
| 425 | }
|
| 426 |
|
| 427 | static void tsdev_disconnect(struct input_handle *handle)
|
| 428 | {
|
| 429 | struct tsdev *tsdev = handle->private;
|
| 430 | struct tsdev_list *list;
|
| 431 |
|
| 432 | sysfs_remove_link(&input_class.subsys.kset.kobj, tsdev->name);
|
| 433 | class_device_destroy(&input_class,
|
| 434 | MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
|
| 435 | tsdev->exist = 0;
|
| 436 |
|
| 437 | if (tsdev->open) {
|
| 438 | input_close_device(handle);
|
| 439 | wake_up_interruptible(&tsdev->wait);
|
| 440 | list_for_each_entry(list, &tsdev->list, node)
|
| 441 | kill_fasync(&list->fasync, SIGIO, POLL_HUP);
|
| 442 | } else
|
| 443 | tsdev_free(tsdev);
|
| 444 | }
|
| 445 |
|
| 446 | static struct input_device_id tsdev_ids[] = {
|
| 447 | {
|
| 448 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
|
| 449 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
|
| 450 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
|
| 451 | .relbit = { BIT(REL_X) | BIT(REL_Y) },
|
| 452 | },
|
| 453 |
|
| 454 | {
|
| 455 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
|
| 456 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
|
| 457 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
|
| 458 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
|
| 459 | },
|
| 460 |
|
| 461 | {
|
| 462 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
|
| 463 | .evbit = { BIT(EV_ABS) },
|
| 464 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
|
| 465 | },
|
| 466 |
|
| 467 | {}
|
| 468 | };
|
| 469 |
|
| 470 | MODULE_DEVICE_TABLE(input, tsdev_ids);
|
| 471 |
|
| 472 | static struct input_handler tsdev_handler = {
|
| 473 | .event = tsdev_event,
|
| 474 | .connect = tsdev_connect,
|
| 475 | .disconnect = tsdev_disconnect,
|
| 476 | .fops = &tsdev_fops,
|
| 477 | .minor = TSDEV_MINOR_BASE,
|
| 478 | .name = "tsdev",
|
| 479 | .id_table = tsdev_ids,
|
| 480 | };
|
| 481 |
|
| 482 | static int __init tsdev_init(void)
|
| 483 | {
|
| 484 | input_register_handler(&tsdev_handler);
|
| 485 | printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
|
| 486 | return 0;
|
| 487 | }
|
| 488 |
|
| 489 | static void __exit tsdev_exit(void)
|
| 490 | {
|
| 491 | input_unregister_handler(&tsdev_handler);
|
| 492 | }
|
| 493 |
|
| 494 | module_init(tsdev_init);
|
| 495 | module_exit(tsdev_exit);
|