tsdev.c 14 KB

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