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/smp_lock.h> |
| 52 | #include <linux/random.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* 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 | |
| 80 | MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>"); |
| 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 | /* From Compaq's Touch Screen Specification version 0.2 (draft) */ |
| 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; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 114 | struct list_head client_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | struct input_handle handle; |
| 116 | int x, y, pressure; |
| 117 | struct ts_calibration cal; |
| 118 | }; |
| 119 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 120 | struct tsdev_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static struct tsdev *tsdev_table[TSDEV_MINORS/2]; |
| 139 | |
| 140 | static int tsdev_fasync(int fd, struct file *file, int on) |
| 141 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 142 | struct tsdev_client *client = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | int retval; |
| 144 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 145 | retval = fasync_helper(fd, file, on, &client->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return retval < 0 ? retval : 0; |
| 147 | } |
| 148 | |
| 149 | static int tsdev_open(struct inode *inode, struct file *file) |
| 150 | { |
| 151 | int i = iminor(inode) - TSDEV_MINOR_BASE; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 152 | struct tsdev_client *client; |
| 153 | struct tsdev *tsdev; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 154 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Richard Purdie | ff141a0 | 2007-02-10 01:29:11 -0500 | [diff] [blame] | 156 | printk(KERN_WARNING "tsdev (compaq touchscreen emulation) is scheduled " |
| 157 | "for removal.\nSee Documentation/feature-removal-schedule.txt " |
| 158 | "for details.\n"); |
| 159 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 160 | if (i >= TSDEV_MINORS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return -ENODEV; |
| 162 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 163 | tsdev = tsdev_table[i & TSDEV_MINOR_MASK]; |
| 164 | if (!tsdev || !tsdev->exist) |
| 165 | return -ENODEV; |
| 166 | |
| 167 | client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL); |
| 168 | if (!client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 171 | client->tsdev = tsdev; |
| 172 | client->raw = (i >= TSDEV_MINORS / 2) ? 1 : 0; |
| 173 | list_add_tail(&client->node, &tsdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 175 | if (!tsdev->open++ && tsdev->exist) { |
| 176 | error = input_open_device(&tsdev->handle); |
| 177 | if (error) { |
| 178 | list_del(&client->node); |
| 179 | kfree(client); |
| 180 | return error; |
| 181 | } |
| 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 184 | file->private_data = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static void tsdev_free(struct tsdev *tsdev) |
| 189 | { |
| 190 | tsdev_table[tsdev->minor] = NULL; |
| 191 | kfree(tsdev); |
| 192 | } |
| 193 | |
| 194 | static int tsdev_release(struct inode *inode, struct file *file) |
| 195 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 196 | struct tsdev_client *client = file->private_data; |
| 197 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | tsdev_fasync(-1, file, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 201 | list_del(&client->node); |
| 202 | kfree(client); |
| 203 | |
| 204 | if (!--tsdev->open) { |
| 205 | if (tsdev->exist) |
| 206 | input_close_device(&tsdev->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | else |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 208 | tsdev_free(tsdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | 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] | 215 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 217 | struct tsdev_client *client = file->private_data; |
| 218 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | int retval = 0; |
| 220 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 221 | if (client->head == client->tail && tsdev->exist && (file->f_flags & O_NONBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return -EAGAIN; |
| 223 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 224 | retval = wait_event_interruptible(tsdev->wait, |
| 225 | client->head != client->tail || !tsdev->exist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (retval) |
| 227 | return retval; |
| 228 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 229 | if (!tsdev->exist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -ENODEV; |
| 231 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 232 | while (client->head != client->tail && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | retval + sizeof (struct ts_event) <= count) { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 234 | if (copy_to_user (buffer + retval, client->event + client->tail, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | sizeof (struct ts_event))) |
| 236 | return -EFAULT; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 237 | client->tail = (client->tail + 1) & (TSDEV_BUFFER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | retval += sizeof (struct ts_event); |
| 239 | } |
| 240 | |
| 241 | return retval; |
| 242 | } |
| 243 | |
| 244 | /* No kernel lock - fine */ |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 245 | static unsigned int tsdev_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 247 | struct tsdev_client *client = file->private_data; |
| 248 | struct tsdev *tsdev = client->tsdev; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 249 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 250 | poll_wait(file, &tsdev->wait, wait); |
| 251 | return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) | |
| 252 | (tsdev->exist ? 0 : (POLLHUP | POLLERR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static int tsdev_ioctl(struct inode *inode, struct file *file, |
| 256 | unsigned int cmd, unsigned long arg) |
| 257 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 258 | struct tsdev_client *client = file->private_data; |
| 259 | struct tsdev *tsdev = client->tsdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | int retval = 0; |
| 261 | |
| 262 | switch (cmd) { |
| 263 | case TS_GET_CAL: |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 264 | if (copy_to_user((void __user *)arg, &tsdev->cal, |
| 265 | sizeof (struct ts_calibration))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | retval = -EFAULT; |
| 267 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | case TS_SET_CAL: |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 270 | if (copy_from_user(&tsdev->cal, (void __user *)arg, |
| 271 | sizeof (struct ts_calibration))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | retval = -EFAULT; |
| 273 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | default: |
| 276 | retval = -EINVAL; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | return retval; |
| 281 | } |
| 282 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 283 | static const struct file_operations tsdev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | .owner = THIS_MODULE, |
| 285 | .open = tsdev_open, |
| 286 | .release = tsdev_release, |
| 287 | .read = tsdev_read, |
| 288 | .poll = tsdev_poll, |
| 289 | .fasync = tsdev_fasync, |
| 290 | .ioctl = tsdev_ioctl, |
| 291 | }; |
| 292 | |
| 293 | static void tsdev_event(struct input_handle *handle, unsigned int type, |
| 294 | unsigned int code, int value) |
| 295 | { |
| 296 | struct tsdev *tsdev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 297 | struct tsdev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | struct timeval time; |
| 299 | |
| 300 | switch (type) { |
| 301 | case EV_ABS: |
| 302 | switch (code) { |
| 303 | case ABS_X: |
| 304 | tsdev->x = value; |
| 305 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | case ABS_Y: |
| 308 | tsdev->y = value; |
| 309 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | case ABS_PRESSURE: |
| 312 | if (value > handle->dev->absmax[ABS_PRESSURE]) |
| 313 | value = handle->dev->absmax[ABS_PRESSURE]; |
| 314 | value -= handle->dev->absmin[ABS_PRESSURE]; |
| 315 | if (value < 0) |
| 316 | value = 0; |
| 317 | tsdev->pressure = value; |
| 318 | break; |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case EV_REL: |
| 323 | switch (code) { |
| 324 | case REL_X: |
| 325 | tsdev->x += value; |
| 326 | if (tsdev->x < 0) |
| 327 | tsdev->x = 0; |
| 328 | else if (tsdev->x > xres) |
| 329 | tsdev->x = xres; |
| 330 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | case REL_Y: |
| 333 | tsdev->y += value; |
| 334 | if (tsdev->y < 0) |
| 335 | tsdev->y = 0; |
| 336 | else if (tsdev->y > yres) |
| 337 | tsdev->y = yres; |
| 338 | break; |
| 339 | } |
| 340 | break; |
| 341 | |
| 342 | case EV_KEY: |
| 343 | if (code == BTN_TOUCH || code == BTN_MOUSE) { |
| 344 | switch (value) { |
| 345 | case 0: |
| 346 | tsdev->pressure = 0; |
| 347 | break; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | case 1: |
| 350 | if (!tsdev->pressure) |
| 351 | tsdev->pressure = 1; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (type != EV_SYN || code != SYN_REPORT) |
| 359 | return; |
| 360 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 361 | list_for_each_entry(client, &tsdev->client_list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | int x, y, tmp; |
| 363 | |
| 364 | do_gettimeofday(&time); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 365 | client->event[client->head].millisecs = time.tv_usec / 100; |
| 366 | client->event[client->head].pressure = tsdev->pressure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | x = tsdev->x; |
| 369 | y = tsdev->y; |
| 370 | |
| 371 | /* Calibration */ |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 372 | if (!client->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans; |
| 374 | y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans; |
| 375 | if (tsdev->cal.xyswap) { |
| 376 | tmp = x; x = y; y = tmp; |
| 377 | } |
| 378 | } |
| 379 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 380 | client->event[client->head].x = x; |
| 381 | client->event[client->head].y = y; |
| 382 | client->head = (client->head + 1) & (TSDEV_BUFFER_SIZE - 1); |
| 383 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | wake_up_interruptible(&tsdev->wait); |
| 386 | } |
| 387 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 388 | static int tsdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 389 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | struct tsdev *tsdev; |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 392 | struct class_device *cdev; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 393 | dev_t devt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int minor, delta; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 395 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 397 | for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++); |
| 398 | if (minor >= TSDEV_MINORS / 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | printk(KERN_ERR |
| 400 | "tsdev: You have way too many touchscreens\n"); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 401 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 404 | tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL); |
| 405 | if (!tsdev) |
| 406 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 408 | INIT_LIST_HEAD(&tsdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | init_waitqueue_head(&tsdev->wait); |
| 410 | |
| 411 | sprintf(tsdev->name, "ts%d", minor); |
| 412 | |
| 413 | tsdev->exist = 1; |
| 414 | tsdev->minor = minor; |
| 415 | tsdev->handle.dev = dev; |
| 416 | tsdev->handle.name = tsdev->name; |
| 417 | tsdev->handle.handler = handler; |
| 418 | tsdev->handle.private = tsdev; |
| 419 | |
| 420 | /* Precompute the rough calibration matrix */ |
| 421 | delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1; |
| 422 | if (delta == 0) |
| 423 | delta = 1; |
| 424 | tsdev->cal.xscale = (xres << 8) / delta; |
| 425 | tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8); |
| 426 | |
| 427 | delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1; |
| 428 | if (delta == 0) |
| 429 | delta = 1; |
| 430 | tsdev->cal.yscale = (yres << 8) / delta; |
| 431 | tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8); |
| 432 | |
| 433 | tsdev_table[minor] = tsdev; |
| 434 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 435 | devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor), |
| 436 | |
| 437 | cdev = class_device_create(&input_class, &dev->cdev, devt, |
| 438 | dev->cdev.dev, tsdev->name); |
| 439 | if (IS_ERR(cdev)) { |
| 440 | error = PTR_ERR(cdev); |
| 441 | goto err_free_tsdev; |
| 442 | } |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 443 | |
| 444 | /* temporary symlink to keep userspace happy */ |
Linus Torvalds | a3d5213 | 2007-05-04 18:13:17 -0700 | [diff] [blame] | 445 | error = sysfs_create_link(&input_class.subsys.kobj, |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 446 | &cdev->kobj, tsdev->name); |
| 447 | if (error) |
| 448 | goto err_cdev_destroy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 450 | error = input_register_handle(&tsdev->handle); |
| 451 | if (error) |
| 452 | goto err_remove_link; |
| 453 | |
| 454 | return 0; |
| 455 | |
| 456 | err_remove_link: |
Linus Torvalds | a3d5213 | 2007-05-04 18:13:17 -0700 | [diff] [blame] | 457 | sysfs_remove_link(&input_class.subsys.kobj, tsdev->name); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 458 | err_cdev_destroy: |
| 459 | class_device_destroy(&input_class, devt); |
| 460 | err_free_tsdev: |
| 461 | tsdev_table[minor] = NULL; |
| 462 | kfree(tsdev); |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 473 | sysfs_remove_link(&input_class.subsys.kobj, tsdev->name); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 474 | class_device_destroy(&input_class, |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame] | 475 | MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | tsdev->exist = 0; |
| 477 | |
| 478 | if (tsdev->open) { |
| 479 | input_close_device(handle); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 480 | list_for_each_entry(client, &tsdev->client_list, node) |
| 481 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
Dmitry Torokhov | 1dfa281 | 2007-06-03 23:29:36 -0400 | [diff] [blame^] | 482 | wake_up_interruptible(&tsdev->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } else |
| 484 | tsdev_free(tsdev); |
| 485 | } |
| 486 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 487 | static const struct input_device_id tsdev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 490 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 491 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, |
| 492 | .relbit = { BIT(REL_X) | BIT(REL_Y) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 493 | }, /* A mouse like device, at least one button, two relative axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | { |
| 496 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 497 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, |
| 498 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, |
| 499 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 500 | }, /* A tablet like device, at least touch detection, two absolute axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | { |
| 503 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 504 | .evbit = { BIT(EV_ABS) }, |
| 505 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 506 | }, /* A tablet like device with several gradations of pressure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 508 | {} /* Terminating entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | MODULE_DEVICE_TABLE(input, tsdev_ids); |
| 512 | |
| 513 | static struct input_handler tsdev_handler = { |
| 514 | .event = tsdev_event, |
| 515 | .connect = tsdev_connect, |
| 516 | .disconnect = tsdev_disconnect, |
| 517 | .fops = &tsdev_fops, |
| 518 | .minor = TSDEV_MINOR_BASE, |
| 519 | .name = "tsdev", |
| 520 | .id_table = tsdev_ids, |
| 521 | }; |
| 522 | |
| 523 | static int __init tsdev_init(void) |
| 524 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 525 | return input_register_handler(&tsdev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void __exit tsdev_exit(void) |
| 529 | { |
| 530 | input_unregister_handler(&tsdev_handler); |
| 531 | } |
| 532 | |
| 533 | module_init(tsdev_init); |
| 534 | module_exit(tsdev_exit); |