tsdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. *
  33. * Should you need to contact me, the author, you can do so either by
  34. * e-mail - mail your message to <jsimmons@infradead.org>.
  35. */
  36. #define TSDEV_MINOR_BASE 128
  37. #define TSDEV_MINORS 32
  38. /* First 16 devices are h3600_ts compatible; second 16 are h3600_tsraw */
  39. #define TSDEV_MINOR_MASK 15
  40. #define TSDEV_BUFFER_SIZE 64
  41. #include <linux/slab.h>
  42. #include <linux/poll.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/init.h>
  46. #include <linux/input.h>
  47. #include <linux/major.h>
  48. #include <linux/config.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/random.h>
  51. #include <linux/time.h>
  52. #include <linux/device.h>
  53. #include <linux/devfs_fs_kernel.h>
  54. #ifndef CONFIG_INPUT_TSDEV_SCREEN_X
  55. #define CONFIG_INPUT_TSDEV_SCREEN_X 240
  56. #endif
  57. #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
  58. #define CONFIG_INPUT_TSDEV_SCREEN_Y 320
  59. #endif
  60. /* This driver emulates both protocols of the old h3600_ts and h3600_tsraw
  61. * devices. The first one must output X/Y data in 'cooked' format, e.g.
  62. * filtered, dejittered and calibrated. Second device just outputs raw
  63. * data received from the hardware.
  64. *
  65. * This driver doesn't support filtering and dejittering; it supports only
  66. * calibration. Filtering and dejittering must be done in the low-level
  67. * driver, if needed, because it may gain additional benefits from knowing
  68. * the low-level details, the nature of noise and so on.
  69. *
  70. * The driver precomputes a calibration matrix given the initial xres and
  71. * yres values (quite innacurate for most touchscreens) that will result
  72. * in a more or less expected range of output values. The driver supports
  73. * the TS_SET_CAL ioctl, which will replace the calibration matrix with a
  74. * new one, supposedly generated from the values taken from the raw device.
  75. */
  76. MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
  77. MODULE_DESCRIPTION("Input driver to touchscreen converter");
  78. MODULE_LICENSE("GPL");
  79. static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
  80. module_param(xres, uint, 0);
  81. MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
  82. static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
  83. module_param(yres, uint, 0);
  84. MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
  85. /* From Compaq's Touch Screen Specification version 0.2 (draft) */
  86. struct ts_event {
  87. short pressure;
  88. short x;
  89. short y;
  90. short millisecs;
  91. };
  92. struct ts_calibration {
  93. int xscale;
  94. int xtrans;
  95. int yscale;
  96. int ytrans;
  97. int xyswap;
  98. };
  99. struct tsdev {
  100. int exist;
  101. int open;
  102. int minor;
  103. char name[8];
  104. wait_queue_head_t wait;
  105. struct list_head list;
  106. struct input_handle handle;
  107. int x, y, pressure;
  108. struct ts_calibration cal;
  109. };
  110. struct tsdev_list {
  111. struct fasync_struct *fasync;
  112. struct list_head node;
  113. struct tsdev *tsdev;
  114. int head, tail;
  115. struct ts_event event[TSDEV_BUFFER_SIZE];
  116. int raw;
  117. };
  118. /* The following ioctl codes are defined ONLY for backward compatibility.
  119. * Don't use tsdev for new developement; use the tslib library instead.
  120. * Touchscreen calibration is a fully userspace task.
  121. */
  122. /* Use 'f' as magic number */
  123. #define IOC_H3600_TS_MAGIC 'f'
  124. #define TS_GET_CAL _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
  125. #define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
  126. static struct input_handler tsdev_handler;
  127. static struct tsdev *tsdev_table[TSDEV_MINORS/2];
  128. static int tsdev_fasync(int fd, struct file *file, int on)
  129. {
  130. struct tsdev_list *list = file->private_data;
  131. int retval;
  132. retval = fasync_helper(fd, file, on, &list->fasync);
  133. return retval < 0 ? retval : 0;
  134. }
  135. static int tsdev_open(struct inode *inode, struct file *file)
  136. {
  137. int i = iminor(inode) - TSDEV_MINOR_BASE;
  138. struct tsdev_list *list;
  139. if (i >= TSDEV_MINORS || !tsdev_table[i & TSDEV_MINOR_MASK])
  140. return -ENODEV;
  141. if (!(list = kmalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
  142. return -ENOMEM;
  143. memset(list, 0, sizeof(struct tsdev_list));
  144. list->raw = (i >= TSDEV_MINORS/2) ? 1 : 0;
  145. i &= TSDEV_MINOR_MASK;
  146. list->tsdev = tsdev_table[i];
  147. list_add_tail(&list->node, &tsdev_table[i]->list);
  148. file->private_data = list;
  149. if (!list->tsdev->open++)
  150. if (list->tsdev->exist)
  151. input_open_device(&list->tsdev->handle);
  152. return 0;
  153. }
  154. static void tsdev_free(struct tsdev *tsdev)
  155. {
  156. tsdev_table[tsdev->minor] = NULL;
  157. kfree(tsdev);
  158. }
  159. static int tsdev_release(struct inode *inode, struct file *file)
  160. {
  161. struct tsdev_list *list = file->private_data;
  162. tsdev_fasync(-1, file, 0);
  163. list_del(&list->node);
  164. if (!--list->tsdev->open) {
  165. if (list->tsdev->exist)
  166. input_close_device(&list->tsdev->handle);
  167. else
  168. tsdev_free(list->tsdev);
  169. }
  170. kfree(list);
  171. return 0;
  172. }
  173. static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
  174. loff_t * ppos)
  175. {
  176. struct tsdev_list *list = file->private_data;
  177. int retval = 0;
  178. if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
  179. return -EAGAIN;
  180. retval = wait_event_interruptible(list->tsdev->wait,
  181. list->head != list->tail || !list->tsdev->exist);
  182. if (retval)
  183. return retval;
  184. if (!list->tsdev->exist)
  185. return -ENODEV;
  186. while (list->head != list->tail &&
  187. retval + sizeof (struct ts_event) <= count) {
  188. if (copy_to_user (buffer + retval, list->event + list->tail,
  189. sizeof (struct ts_event)))
  190. return -EFAULT;
  191. list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
  192. retval += sizeof (struct ts_event);
  193. }
  194. return retval;
  195. }
  196. /* No kernel lock - fine */
  197. static unsigned int tsdev_poll(struct file *file, poll_table * wait)
  198. {
  199. struct tsdev_list *list = file->private_data;
  200. poll_wait(file, &list->tsdev->wait, wait);
  201. return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
  202. (list->tsdev->exist ? 0 : (POLLHUP | POLLERR));
  203. }
  204. static int tsdev_ioctl(struct inode *inode, struct file *file,
  205. unsigned int cmd, unsigned long arg)
  206. {
  207. struct tsdev_list *list = file->private_data;
  208. struct tsdev *tsdev = list->tsdev;
  209. int retval = 0;
  210. switch (cmd) {
  211. case TS_GET_CAL:
  212. if (copy_to_user ((void __user *)arg, &tsdev->cal,
  213. sizeof (struct ts_calibration)))
  214. retval = -EFAULT;
  215. break;
  216. case TS_SET_CAL:
  217. if (copy_from_user (&tsdev->cal, (void __user *)arg,
  218. sizeof (struct ts_calibration)))
  219. retval = -EFAULT;
  220. break;
  221. default:
  222. retval = -EINVAL;
  223. break;
  224. }
  225. return retval;
  226. }
  227. static struct file_operations tsdev_fops = {
  228. .owner = THIS_MODULE,
  229. .open = tsdev_open,
  230. .release = tsdev_release,
  231. .read = tsdev_read,
  232. .poll = tsdev_poll,
  233. .fasync = tsdev_fasync,
  234. .ioctl = tsdev_ioctl,
  235. };
  236. static void tsdev_event(struct input_handle *handle, unsigned int type,
  237. unsigned int code, int value)
  238. {
  239. struct tsdev *tsdev = handle->private;
  240. struct tsdev_list *list;
  241. struct timeval time;
  242. switch (type) {
  243. case EV_ABS:
  244. switch (code) {
  245. case ABS_X:
  246. tsdev->x = value;
  247. break;
  248. case ABS_Y:
  249. tsdev->y = value;
  250. break;
  251. case ABS_PRESSURE:
  252. if (value > handle->dev->absmax[ABS_PRESSURE])
  253. value = handle->dev->absmax[ABS_PRESSURE];
  254. value -= handle->dev->absmin[ABS_PRESSURE];
  255. if (value < 0)
  256. value = 0;
  257. tsdev->pressure = value;
  258. break;
  259. }
  260. break;
  261. case EV_REL:
  262. switch (code) {
  263. case REL_X:
  264. tsdev->x += value;
  265. if (tsdev->x < 0)
  266. tsdev->x = 0;
  267. else if (tsdev->x > xres)
  268. tsdev->x = xres;
  269. break;
  270. case REL_Y:
  271. tsdev->y += value;
  272. if (tsdev->y < 0)
  273. tsdev->y = 0;
  274. else if (tsdev->y > yres)
  275. tsdev->y = yres;
  276. break;
  277. }
  278. break;
  279. case EV_KEY:
  280. if (code == BTN_TOUCH || code == BTN_MOUSE) {
  281. switch (value) {
  282. case 0:
  283. tsdev->pressure = 0;
  284. break;
  285. case 1:
  286. if (!tsdev->pressure)
  287. tsdev->pressure = 1;
  288. break;
  289. }
  290. }
  291. break;
  292. }
  293. if (type != EV_SYN || code != SYN_REPORT)
  294. return;
  295. list_for_each_entry(list, &tsdev->list, node) {
  296. int x, y, tmp;
  297. do_gettimeofday(&time);
  298. list->event[list->head].millisecs = time.tv_usec / 100;
  299. list->event[list->head].pressure = tsdev->pressure;
  300. x = tsdev->x;
  301. y = tsdev->y;
  302. /* Calibration */
  303. if (!list->raw) {
  304. x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
  305. y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
  306. if (tsdev->cal.xyswap) {
  307. tmp = x; x = y; y = tmp;
  308. }
  309. }
  310. list->event[list->head].x = x;
  311. list->event[list->head].y = y;
  312. list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
  313. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  314. }
  315. wake_up_interruptible(&tsdev->wait);
  316. }
  317. static struct input_handle *tsdev_connect(struct input_handler *handler,
  318. struct input_dev *dev,
  319. struct input_device_id *id)
  320. {
  321. struct tsdev *tsdev;
  322. int minor, delta;
  323. for (minor = 0; minor < TSDEV_MINORS/2 && tsdev_table[minor];
  324. minor++);
  325. if (minor >= TSDEV_MINORS/2) {
  326. printk(KERN_ERR
  327. "tsdev: You have way too many touchscreens\n");
  328. return NULL;
  329. }
  330. if (!(tsdev = kmalloc(sizeof(struct tsdev), GFP_KERNEL)))
  331. return NULL;
  332. memset(tsdev, 0, sizeof(struct tsdev));
  333. INIT_LIST_HEAD(&tsdev->list);
  334. init_waitqueue_head(&tsdev->wait);
  335. sprintf(tsdev->name, "ts%d", minor);
  336. tsdev->exist = 1;
  337. tsdev->minor = minor;
  338. tsdev->handle.dev = dev;
  339. tsdev->handle.name = tsdev->name;
  340. tsdev->handle.handler = handler;
  341. tsdev->handle.private = tsdev;
  342. /* Precompute the rough calibration matrix */
  343. delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
  344. if (delta == 0)
  345. delta = 1;
  346. tsdev->cal.xscale = (xres << 8) / delta;
  347. tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
  348. delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
  349. if (delta == 0)
  350. delta = 1;
  351. tsdev->cal.yscale = (yres << 8) / delta;
  352. tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
  353. tsdev_table[minor] = tsdev;
  354. devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
  355. S_IFCHR|S_IRUGO|S_IWUSR, "input/ts%d", minor);
  356. devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor + TSDEV_MINORS/2),
  357. S_IFCHR|S_IRUGO|S_IWUSR, "input/tsraw%d", minor);
  358. class_device_create(input_class,
  359. MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
  360. dev->dev, "ts%d", minor);
  361. return &tsdev->handle;
  362. }
  363. static void tsdev_disconnect(struct input_handle *handle)
  364. {
  365. struct tsdev *tsdev = handle->private;
  366. struct tsdev_list *list;
  367. class_device_destroy(input_class,
  368. MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
  369. devfs_remove("input/ts%d", tsdev->minor);
  370. devfs_remove("input/tsraw%d", tsdev->minor);
  371. tsdev->exist = 0;
  372. if (tsdev->open) {
  373. input_close_device(handle);
  374. wake_up_interruptible(&tsdev->wait);
  375. list_for_each_entry(list, &tsdev->list, node)
  376. kill_fasync(&list->fasync, SIGIO, POLL_HUP);
  377. } else
  378. tsdev_free(tsdev);
  379. }
  380. static struct input_device_id tsdev_ids[] = {
  381. {
  382. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  383. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  384. .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
  385. .relbit = { BIT(REL_X) | BIT(REL_Y) },
  386. },/* A mouse like device, at least one button, two relative axes */
  387. {
  388. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  389. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  390. .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
  391. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  392. },/* A tablet like device, at least touch detection, two absolute axes */
  393. {
  394. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  395. .evbit = { BIT(EV_ABS) },
  396. .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
  397. },/* A tablet like device with several gradations of pressure */
  398. {},/* Terminating entry */
  399. };
  400. MODULE_DEVICE_TABLE(input, tsdev_ids);
  401. static struct input_handler tsdev_handler = {
  402. .event = tsdev_event,
  403. .connect = tsdev_connect,
  404. .disconnect = tsdev_disconnect,
  405. .fops = &tsdev_fops,
  406. .minor = TSDEV_MINOR_BASE,
  407. .name = "tsdev",
  408. .id_table = tsdev_ids,
  409. };
  410. static int __init tsdev_init(void)
  411. {
  412. input_register_handler(&tsdev_handler);
  413. printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
  414. return 0;
  415. }
  416. static void __exit tsdev_exit(void)
  417. {
  418. input_unregister_handler(&tsdev_handler);
  419. }
  420. module_init(tsdev_init);
  421. module_exit(tsdev_exit);