tsdev.c 13 KB

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