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. struct input_handle handle;
  102. wait_queue_head_t wait;
  103. struct list_head client_list;
  104. struct device dev;
  105. int x, y, pressure;
  106. struct ts_calibration cal;
  107. };
  108. struct tsdev_client {
  109. struct fasync_struct *fasync;
  110. struct list_head node;
  111. struct tsdev *tsdev;
  112. int head, tail;
  113. struct ts_event event[TSDEV_BUFFER_SIZE];
  114. int raw;
  115. };
  116. /* The following ioctl codes are defined ONLY for backward compatibility.
  117. * Don't use tsdev for new developement; use the tslib library instead.
  118. * Touchscreen calibration is a fully userspace task.
  119. */
  120. /* Use 'f' as magic number */
  121. #define IOC_H3600_TS_MAGIC 'f'
  122. #define TS_GET_CAL _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
  123. #define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
  124. static struct tsdev *tsdev_table[TSDEV_MINORS/2];
  125. static int tsdev_fasync(int fd, struct file *file, int on)
  126. {
  127. struct tsdev_client *client = file->private_data;
  128. int retval;
  129. retval = fasync_helper(fd, file, on, &client->fasync);
  130. return retval < 0 ? retval : 0;
  131. }
  132. static int tsdev_open(struct inode *inode, struct file *file)
  133. {
  134. int i = iminor(inode) - TSDEV_MINOR_BASE;
  135. struct tsdev_client *client;
  136. struct tsdev *tsdev;
  137. int error;
  138. printk(KERN_WARNING "tsdev (compaq touchscreen emulation) is scheduled "
  139. "for removal.\nSee Documentation/feature-removal-schedule.txt "
  140. "for details.\n");
  141. if (i >= TSDEV_MINORS)
  142. return -ENODEV;
  143. tsdev = tsdev_table[i & TSDEV_MINOR_MASK];
  144. if (!tsdev || !tsdev->exist)
  145. return -ENODEV;
  146. get_device(&tsdev->dev);
  147. client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL);
  148. if (!client) {
  149. error = -ENOMEM;
  150. goto err_put_tsdev;
  151. }
  152. client->tsdev = tsdev;
  153. client->raw = (i >= TSDEV_MINORS / 2) ? 1 : 0;
  154. list_add_tail(&client->node, &tsdev->client_list);
  155. if (!tsdev->open++ && tsdev->exist) {
  156. error = input_open_device(&tsdev->handle);
  157. if (error)
  158. goto err_free_client;
  159. }
  160. file->private_data = client;
  161. return 0;
  162. err_free_client:
  163. list_del(&client->node);
  164. kfree(client);
  165. err_put_tsdev:
  166. put_device(&tsdev->dev);
  167. return error;
  168. }
  169. static void tsdev_free(struct device *dev)
  170. {
  171. struct tsdev *tsdev = container_of(dev, struct tsdev, dev);
  172. tsdev_table[tsdev->minor] = NULL;
  173. kfree(tsdev);
  174. }
  175. static int tsdev_release(struct inode *inode, struct file *file)
  176. {
  177. struct tsdev_client *client = file->private_data;
  178. struct tsdev *tsdev = client->tsdev;
  179. tsdev_fasync(-1, file, 0);
  180. list_del(&client->node);
  181. kfree(client);
  182. if (!--tsdev->open && tsdev->exist)
  183. input_close_device(&tsdev->handle);
  184. put_device(&tsdev->dev);
  185. return 0;
  186. }
  187. static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
  188. loff_t *ppos)
  189. {
  190. struct tsdev_client *client = file->private_data;
  191. struct tsdev *tsdev = client->tsdev;
  192. int retval = 0;
  193. if (client->head == client->tail && tsdev->exist && (file->f_flags & O_NONBLOCK))
  194. return -EAGAIN;
  195. retval = wait_event_interruptible(tsdev->wait,
  196. client->head != client->tail || !tsdev->exist);
  197. if (retval)
  198. return retval;
  199. if (!tsdev->exist)
  200. return -ENODEV;
  201. while (client->head != client->tail &&
  202. retval + sizeof (struct ts_event) <= count) {
  203. if (copy_to_user (buffer + retval, client->event + client->tail,
  204. sizeof (struct ts_event)))
  205. return -EFAULT;
  206. client->tail = (client->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
  207. retval += sizeof (struct ts_event);
  208. }
  209. return retval;
  210. }
  211. /* No kernel lock - fine */
  212. static unsigned int tsdev_poll(struct file *file, poll_table *wait)
  213. {
  214. struct tsdev_client *client = file->private_data;
  215. struct tsdev *tsdev = client->tsdev;
  216. poll_wait(file, &tsdev->wait, wait);
  217. return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) |
  218. (tsdev->exist ? 0 : (POLLHUP | POLLERR));
  219. }
  220. static int tsdev_ioctl(struct inode *inode, struct file *file,
  221. unsigned int cmd, unsigned long arg)
  222. {
  223. struct tsdev_client *client = file->private_data;
  224. struct tsdev *tsdev = client->tsdev;
  225. int retval = 0;
  226. switch (cmd) {
  227. case TS_GET_CAL:
  228. if (copy_to_user((void __user *)arg, &tsdev->cal,
  229. sizeof (struct ts_calibration)))
  230. retval = -EFAULT;
  231. break;
  232. case TS_SET_CAL:
  233. if (copy_from_user(&tsdev->cal, (void __user *)arg,
  234. sizeof (struct ts_calibration)))
  235. retval = -EFAULT;
  236. break;
  237. default:
  238. retval = -EINVAL;
  239. break;
  240. }
  241. return retval;
  242. }
  243. static const struct file_operations tsdev_fops = {
  244. .owner = THIS_MODULE,
  245. .open = tsdev_open,
  246. .release = tsdev_release,
  247. .read = tsdev_read,
  248. .poll = tsdev_poll,
  249. .fasync = tsdev_fasync,
  250. .ioctl = tsdev_ioctl,
  251. };
  252. static void tsdev_event(struct input_handle *handle, unsigned int type,
  253. unsigned int code, int value)
  254. {
  255. struct tsdev *tsdev = handle->private;
  256. struct tsdev_client *client;
  257. struct timeval time;
  258. switch (type) {
  259. case EV_ABS:
  260. switch (code) {
  261. case ABS_X:
  262. tsdev->x = value;
  263. break;
  264. case ABS_Y:
  265. tsdev->y = value;
  266. break;
  267. case ABS_PRESSURE:
  268. if (value > handle->dev->absmax[ABS_PRESSURE])
  269. value = handle->dev->absmax[ABS_PRESSURE];
  270. value -= handle->dev->absmin[ABS_PRESSURE];
  271. if (value < 0)
  272. value = 0;
  273. tsdev->pressure = value;
  274. break;
  275. }
  276. break;
  277. case EV_REL:
  278. switch (code) {
  279. case REL_X:
  280. tsdev->x += value;
  281. if (tsdev->x < 0)
  282. tsdev->x = 0;
  283. else if (tsdev->x > xres)
  284. tsdev->x = xres;
  285. break;
  286. case REL_Y:
  287. tsdev->y += value;
  288. if (tsdev->y < 0)
  289. tsdev->y = 0;
  290. else if (tsdev->y > yres)
  291. tsdev->y = yres;
  292. break;
  293. }
  294. break;
  295. case EV_KEY:
  296. if (code == BTN_TOUCH || code == BTN_MOUSE) {
  297. switch (value) {
  298. case 0:
  299. tsdev->pressure = 0;
  300. break;
  301. case 1:
  302. if (!tsdev->pressure)
  303. tsdev->pressure = 1;
  304. break;
  305. }
  306. }
  307. break;
  308. }
  309. if (type != EV_SYN || code != SYN_REPORT)
  310. return;
  311. list_for_each_entry(client, &tsdev->client_list, node) {
  312. int x, y, tmp;
  313. do_gettimeofday(&time);
  314. client->event[client->head].millisecs = time.tv_usec / 1000;
  315. client->event[client->head].pressure = tsdev->pressure;
  316. x = tsdev->x;
  317. y = tsdev->y;
  318. /* Calibration */
  319. if (!client->raw) {
  320. x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
  321. y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
  322. if (tsdev->cal.xyswap) {
  323. tmp = x; x = y; y = tmp;
  324. }
  325. }
  326. client->event[client->head].x = x;
  327. client->event[client->head].y = y;
  328. client->head = (client->head + 1) & (TSDEV_BUFFER_SIZE - 1);
  329. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  330. }
  331. wake_up_interruptible(&tsdev->wait);
  332. }
  333. static int tsdev_connect(struct input_handler *handler, struct input_dev *dev,
  334. const struct input_device_id *id)
  335. {
  336. struct tsdev *tsdev;
  337. int minor, delta;
  338. int error;
  339. for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++);
  340. if (minor >= TSDEV_MINORS / 2) {
  341. printk(KERN_ERR
  342. "tsdev: You have way too many touchscreens\n");
  343. return -ENFILE;
  344. }
  345. tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL);
  346. if (!tsdev)
  347. return -ENOMEM;
  348. INIT_LIST_HEAD(&tsdev->client_list);
  349. init_waitqueue_head(&tsdev->wait);
  350. tsdev->exist = 1;
  351. tsdev->minor = minor;
  352. tsdev->handle.dev = dev;
  353. tsdev->handle.name = tsdev->name;
  354. tsdev->handle.handler = handler;
  355. tsdev->handle.private = tsdev;
  356. snprintf(tsdev->name, sizeof(tsdev->name), "ts%d", minor);
  357. /* Precompute the rough calibration matrix */
  358. delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
  359. if (delta == 0)
  360. delta = 1;
  361. tsdev->cal.xscale = (xres << 8) / delta;
  362. tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
  363. delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
  364. if (delta == 0)
  365. delta = 1;
  366. tsdev->cal.yscale = (yres << 8) / delta;
  367. tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
  368. snprintf(tsdev->dev.bus_id, sizeof(tsdev->dev.bus_id),
  369. "ts%d", minor);
  370. tsdev->dev.class = &input_class;
  371. tsdev->dev.parent = &dev->dev;
  372. tsdev->dev.devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor);
  373. tsdev->dev.release = tsdev_free;
  374. device_initialize(&tsdev->dev);
  375. tsdev_table[minor] = tsdev;
  376. error = device_add(&tsdev->dev);
  377. if (error)
  378. goto err_free_tsdev;
  379. error = input_register_handle(&tsdev->handle);
  380. if (error)
  381. goto err_delete_tsdev;
  382. return 0;
  383. err_delete_tsdev:
  384. device_del(&tsdev->dev);
  385. err_free_tsdev:
  386. put_device(&tsdev->dev);
  387. return error;
  388. }
  389. static void tsdev_disconnect(struct input_handle *handle)
  390. {
  391. struct tsdev *tsdev = handle->private;
  392. struct tsdev_client *client;
  393. input_unregister_handle(handle);
  394. device_del(&tsdev->dev);
  395. tsdev->exist = 0;
  396. if (tsdev->open) {
  397. input_close_device(handle);
  398. list_for_each_entry(client, &tsdev->client_list, node)
  399. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  400. wake_up_interruptible(&tsdev->wait);
  401. }
  402. put_device(&tsdev->dev);
  403. }
  404. static const struct input_device_id tsdev_ids[] = {
  405. {
  406. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  407. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  408. .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
  409. .relbit = { BIT(REL_X) | BIT(REL_Y) },
  410. }, /* A mouse like device, at least one button, two relative axes */
  411. {
  412. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  413. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  414. .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
  415. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  416. }, /* A tablet like device, at least touch detection, two absolute axes */
  417. {
  418. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  419. .evbit = { BIT(EV_ABS) },
  420. .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) },
  421. }, /* A tablet like device with several gradations of pressure */
  422. {} /* Terminating entry */
  423. };
  424. MODULE_DEVICE_TABLE(input, tsdev_ids);
  425. static struct input_handler tsdev_handler = {
  426. .event = tsdev_event,
  427. .connect = tsdev_connect,
  428. .disconnect = tsdev_disconnect,
  429. .fops = &tsdev_fops,
  430. .minor = TSDEV_MINOR_BASE,
  431. .name = "tsdev",
  432. .id_table = tsdev_ids,
  433. };
  434. static int __init tsdev_init(void)
  435. {
  436. return input_register_handler(&tsdev_handler);
  437. }
  438. static void __exit tsdev_exit(void)
  439. {
  440. input_unregister_handler(&tsdev_handler);
  441. }
  442. module_init(tsdev_init);
  443. module_exit(tsdev_exit);