Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 38 | #define TSDEV_MINOR_BASE 128 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/random.h> |
| 52 | #include <linux/time.h> |
| 53 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #ifndef CONFIG_INPUT_TSDEV_SCREEN_X |
| 56 | #define CONFIG_INPUT_TSDEV_SCREEN_X 240 |
| 57 | #endif |
| 58 | #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y |
| 59 | #define CONFIG_INPUT_TSDEV_SCREEN_Y 320 |
| 60 | #endif |
| 61 | |
| 62 | /* This driver emulates both protocols of the old h3600_ts and h3600_tsraw |
| 63 | * devices. The first one must output X/Y data in 'cooked' format, e.g. |
| 64 | * filtered, dejittered and calibrated. Second device just outputs raw |
| 65 | * data received from the hardware. |
| 66 | * |
| 67 | * This driver doesn't support filtering and dejittering; it supports only |
| 68 | * calibration. Filtering and dejittering must be done in the low-level |
| 69 | * driver, if needed, because it may gain additional benefits from knowing |
| 70 | * the low-level details, the nature of noise and so on. |
| 71 | * |
| 72 | * The driver precomputes a calibration matrix given the initial xres and |
| 73 | * yres values (quite innacurate for most touchscreens) that will result |
| 74 | * in a more or less expected range of output values. The driver supports |
| 75 | * the TS_SET_CAL ioctl, which will replace the calibration matrix with a |
| 76 | * new one, supposedly generated from the values taken from the raw device. |
| 77 | */ |
| 78 | |
| 79 | MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>"); |
| 80 | MODULE_DESCRIPTION("Input driver to touchscreen converter"); |
| 81 | MODULE_LICENSE("GPL"); |
| 82 | |
| 83 | static int xres = CONFIG_INPUT_TSDEV_SCREEN_X; |
| 84 | module_param(xres, uint, 0); |
| 85 | MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)"); |
| 86 | |
| 87 | static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y; |
| 88 | module_param(yres, uint, 0); |
| 89 | MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)"); |
| 90 | |
| 91 | /* From Compaq's Touch Screen Specification version 0.2 (draft) */ |
| 92 | struct ts_event { |
| 93 | short pressure; |
| 94 | short x; |
| 95 | short y; |
| 96 | short millisecs; |
| 97 | }; |
| 98 | |
| 99 | struct ts_calibration { |
| 100 | int xscale; |
| 101 | int xtrans; |
| 102 | int yscale; |
| 103 | int ytrans; |
| 104 | int xyswap; |
| 105 | }; |
| 106 | |
| 107 | struct tsdev { |
| 108 | int exist; |
| 109 | int open; |
| 110 | int minor; |
| 111 | char name[8]; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 112 | struct input_handle handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | wait_queue_head_t wait; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 114 | struct list_head client_list; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 115 | struct device dev; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int x, y, pressure; |
| 118 | struct ts_calibration cal; |
| 119 | }; |
| 120 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 121 | struct tsdev_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static struct tsdev *tsdev_table[TSDEV_MINORS/2]; |
| 140 | |
| 141 | static int tsdev_fasync(int fd, struct file *file, int on) |
| 142 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 143 | struct tsdev_client *client = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | int retval; |
| 145 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 146 | retval = fasync_helper(fd, file, on, &client->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return retval < 0 ? retval : 0; |
| 148 | } |
| 149 | |
| 150 | static int tsdev_open(struct inode *inode, struct file *file) |
| 151 | { |
| 152 | int i = iminor(inode) - TSDEV_MINOR_BASE; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 153 | struct tsdev_client *client; |
| 154 | struct tsdev *tsdev; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 155 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Richard Purdie | ff141a0 | 2007-02-10 01:29:11 -0500 | [diff] [blame] | 157 | printk(KERN_WARNING "tsdev (compaq touchscreen emulation) is scheduled " |
| 158 | "for removal.\nSee Documentation/feature-removal-schedule.txt " |
| 159 | "for details.\n"); |
| 160 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 161 | if (i >= TSDEV_MINORS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return -ENODEV; |
| 163 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 164 | tsdev = tsdev_table[i & TSDEV_MINOR_MASK]; |
| 165 | if (!tsdev || !tsdev->exist) |
| 166 | return -ENODEV; |
| 167 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 168 | get_device(&tsdev->dev); |
| 169 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 170 | client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 171 | if (!client) { |
| 172 | error = -ENOMEM; |
| 173 | goto err_put_tsdev; |
| 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 176 | client->tsdev = tsdev; |
| 177 | client->raw = (i >= TSDEV_MINORS / 2) ? 1 : 0; |
| 178 | list_add_tail(&client->node, &tsdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 180 | if (!tsdev->open++ && tsdev->exist) { |
| 181 | error = input_open_device(&tsdev->handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 182 | if (error) |
| 183 | goto err_free_client; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 186 | file->private_data = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return 0; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 188 | |
| 189 | err_free_client: |
| 190 | list_del(&client->node); |
| 191 | kfree(client); |
| 192 | err_put_tsdev: |
| 193 | put_device(&tsdev->dev); |
| 194 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 197 | static void tsdev_free(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 199 | struct tsdev *tsdev = container_of(dev, struct tsdev, dev); |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | tsdev_table[tsdev->minor] = NULL; |
| 202 | kfree(tsdev); |
| 203 | } |
| 204 | |
| 205 | static int tsdev_release(struct inode *inode, struct file *file) |
| 206 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 207 | struct tsdev_client *client = file->private_data; |
| 208 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | tsdev_fasync(-1, file, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 212 | list_del(&client->node); |
| 213 | kfree(client); |
| 214 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 215 | if (!--tsdev->open && tsdev->exist) |
| 216 | input_close_device(&tsdev->handle); |
| 217 | |
| 218 | put_device(&tsdev->dev); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count, |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 224 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 226 | struct tsdev_client *client = file->private_data; |
| 227 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | int retval = 0; |
| 229 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 230 | if (client->head == client->tail && tsdev->exist && (file->f_flags & O_NONBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return -EAGAIN; |
| 232 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 233 | retval = wait_event_interruptible(tsdev->wait, |
| 234 | client->head != client->tail || !tsdev->exist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (retval) |
| 236 | return retval; |
| 237 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 238 | if (!tsdev->exist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return -ENODEV; |
| 240 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 241 | while (client->head != client->tail && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | retval + sizeof (struct ts_event) <= count) { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 243 | if (copy_to_user (buffer + retval, client->event + client->tail, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | sizeof (struct ts_event))) |
| 245 | return -EFAULT; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 246 | client->tail = (client->tail + 1) & (TSDEV_BUFFER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | retval += sizeof (struct ts_event); |
| 248 | } |
| 249 | |
| 250 | return retval; |
| 251 | } |
| 252 | |
| 253 | /* No kernel lock - fine */ |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 254 | static unsigned int tsdev_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 256 | struct tsdev_client *client = file->private_data; |
| 257 | struct tsdev *tsdev = client->tsdev; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 258 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 259 | poll_wait(file, &tsdev->wait, wait); |
| 260 | return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) | |
| 261 | (tsdev->exist ? 0 : (POLLHUP | POLLERR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int tsdev_ioctl(struct inode *inode, struct file *file, |
| 265 | unsigned int cmd, unsigned long arg) |
| 266 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 267 | struct tsdev_client *client = file->private_data; |
| 268 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | int retval = 0; |
| 270 | |
| 271 | switch (cmd) { |
| 272 | case TS_GET_CAL: |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 273 | if (copy_to_user((void __user *)arg, &tsdev->cal, |
| 274 | sizeof (struct ts_calibration))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | retval = -EFAULT; |
| 276 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | case TS_SET_CAL: |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 279 | if (copy_from_user(&tsdev->cal, (void __user *)arg, |
| 280 | sizeof (struct ts_calibration))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | retval = -EFAULT; |
| 282 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | default: |
| 285 | retval = -EINVAL; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | return retval; |
| 290 | } |
| 291 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 292 | static const struct file_operations tsdev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | .owner = THIS_MODULE, |
| 294 | .open = tsdev_open, |
| 295 | .release = tsdev_release, |
| 296 | .read = tsdev_read, |
| 297 | .poll = tsdev_poll, |
| 298 | .fasync = tsdev_fasync, |
| 299 | .ioctl = tsdev_ioctl, |
| 300 | }; |
| 301 | |
| 302 | static void tsdev_event(struct input_handle *handle, unsigned int type, |
| 303 | unsigned int code, int value) |
| 304 | { |
| 305 | struct tsdev *tsdev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 306 | struct tsdev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | struct timeval time; |
| 308 | |
| 309 | switch (type) { |
| 310 | case EV_ABS: |
| 311 | switch (code) { |
| 312 | case ABS_X: |
| 313 | tsdev->x = value; |
| 314 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | case ABS_Y: |
| 317 | tsdev->y = value; |
| 318 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | case ABS_PRESSURE: |
| 321 | if (value > handle->dev->absmax[ABS_PRESSURE]) |
| 322 | value = handle->dev->absmax[ABS_PRESSURE]; |
| 323 | value -= handle->dev->absmin[ABS_PRESSURE]; |
| 324 | if (value < 0) |
| 325 | value = 0; |
| 326 | tsdev->pressure = value; |
| 327 | break; |
| 328 | } |
| 329 | break; |
| 330 | |
| 331 | case EV_REL: |
| 332 | switch (code) { |
| 333 | case REL_X: |
| 334 | tsdev->x += value; |
| 335 | if (tsdev->x < 0) |
| 336 | tsdev->x = 0; |
| 337 | else if (tsdev->x > xres) |
| 338 | tsdev->x = xres; |
| 339 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | case REL_Y: |
| 342 | tsdev->y += value; |
| 343 | if (tsdev->y < 0) |
| 344 | tsdev->y = 0; |
| 345 | else if (tsdev->y > yres) |
| 346 | tsdev->y = yres; |
| 347 | break; |
| 348 | } |
| 349 | break; |
| 350 | |
| 351 | case EV_KEY: |
| 352 | if (code == BTN_TOUCH || code == BTN_MOUSE) { |
| 353 | switch (value) { |
| 354 | case 0: |
| 355 | tsdev->pressure = 0; |
| 356 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | case 1: |
| 359 | if (!tsdev->pressure) |
| 360 | tsdev->pressure = 1; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | if (type != EV_SYN || code != SYN_REPORT) |
| 368 | return; |
| 369 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 370 | list_for_each_entry(client, &tsdev->client_list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | int x, y, tmp; |
| 372 | |
| 373 | do_gettimeofday(&time); |
Andrew Morton | 65f88f8 | 2007-07-10 00:43:06 -0400 | [diff] [blame] | 374 | client->event[client->head].millisecs = time.tv_usec / 1000; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 375 | client->event[client->head].pressure = tsdev->pressure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | x = tsdev->x; |
| 378 | y = tsdev->y; |
| 379 | |
| 380 | /* Calibration */ |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 381 | if (!client->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans; |
| 383 | y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans; |
| 384 | if (tsdev->cal.xyswap) { |
| 385 | tmp = x; x = y; y = tmp; |
| 386 | } |
| 387 | } |
| 388 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 389 | client->event[client->head].x = x; |
| 390 | client->event[client->head].y = y; |
| 391 | client->head = (client->head + 1) & (TSDEV_BUFFER_SIZE - 1); |
| 392 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | wake_up_interruptible(&tsdev->wait); |
| 395 | } |
| 396 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 397 | static int tsdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 398 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
| 400 | struct tsdev *tsdev; |
| 401 | int minor, delta; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 402 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 404 | for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++); |
| 405 | if (minor >= TSDEV_MINORS / 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | printk(KERN_ERR |
| 407 | "tsdev: You have way too many touchscreens\n"); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 408 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 411 | tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL); |
| 412 | if (!tsdev) |
| 413 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 415 | INIT_LIST_HEAD(&tsdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | init_waitqueue_head(&tsdev->wait); |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | tsdev->exist = 1; |
| 419 | tsdev->minor = minor; |
| 420 | tsdev->handle.dev = dev; |
| 421 | tsdev->handle.name = tsdev->name; |
| 422 | tsdev->handle.handler = handler; |
| 423 | tsdev->handle.private = tsdev; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 424 | snprintf(tsdev->name, sizeof(tsdev->name), "ts%d", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | /* Precompute the rough calibration matrix */ |
| 427 | delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1; |
| 428 | if (delta == 0) |
| 429 | delta = 1; |
| 430 | tsdev->cal.xscale = (xres << 8) / delta; |
| 431 | tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8); |
| 432 | |
| 433 | delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1; |
| 434 | if (delta == 0) |
| 435 | delta = 1; |
| 436 | tsdev->cal.yscale = (yres << 8) / delta; |
| 437 | tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8); |
| 438 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 439 | snprintf(tsdev->dev.bus_id, sizeof(tsdev->dev.bus_id), |
| 440 | "ts%d", minor); |
| 441 | tsdev->dev.class = &input_class; |
| 442 | tsdev->dev.parent = &dev->dev; |
| 443 | tsdev->dev.devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor); |
| 444 | tsdev->dev.release = tsdev_free; |
| 445 | device_initialize(&tsdev->dev); |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | tsdev_table[minor] = tsdev; |
| 448 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 449 | error = device_add(&tsdev->dev); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 450 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 451 | goto err_free_tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 453 | error = input_register_handle(&tsdev->handle); |
| 454 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 455 | goto err_delete_tsdev; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 459 | err_delete_tsdev: |
| 460 | device_del(&tsdev->dev); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 461 | err_free_tsdev: |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 462 | put_device(&tsdev->dev); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 463 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void tsdev_disconnect(struct input_handle *handle) |
| 467 | { |
| 468 | struct tsdev *tsdev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 469 | struct tsdev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 471 | input_unregister_handle(handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 472 | device_del(&tsdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | tsdev->exist = 0; |
| 475 | |
| 476 | if (tsdev->open) { |
| 477 | input_close_device(handle); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 478 | list_for_each_entry(client, &tsdev->client_list, node) |
| 479 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
Dmitry Torokhov | 1dfa281 | 2007-06-03 23:29:36 -0400 | [diff] [blame] | 480 | wake_up_interruptible(&tsdev->wait); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | put_device(&tsdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 486 | static const struct input_device_id tsdev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 489 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 490 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, |
| 491 | .relbit = { BIT(REL_X) | BIT(REL_Y) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 492 | }, /* A mouse like device, at least one button, two relative axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | { |
| 495 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 496 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, |
| 497 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, |
| 498 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 499 | }, /* A tablet like device, at least touch detection, two absolute axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | { |
| 502 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 503 | .evbit = { BIT(EV_ABS) }, |
| 504 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 505 | }, /* A tablet like device with several gradations of pressure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 507 | {} /* Terminating entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | MODULE_DEVICE_TABLE(input, tsdev_ids); |
| 511 | |
| 512 | static struct input_handler tsdev_handler = { |
| 513 | .event = tsdev_event, |
| 514 | .connect = tsdev_connect, |
| 515 | .disconnect = tsdev_disconnect, |
| 516 | .fops = &tsdev_fops, |
| 517 | .minor = TSDEV_MINOR_BASE, |
| 518 | .name = "tsdev", |
| 519 | .id_table = tsdev_ids, |
| 520 | }; |
| 521 | |
| 522 | static int __init tsdev_init(void) |
| 523 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 524 | return input_register_handler(&tsdev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static void __exit tsdev_exit(void) |
| 528 | { |
| 529 | input_unregister_handler(&tsdev_handler); |
| 530 | } |
| 531 | |
| 532 | module_init(tsdev_init); |
| 533 | module_exit(tsdev_exit); |