elo.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Elo serial touchscreen driver
  3. *
  4. * Copyright (c) 2004 Vojtech Pavlik
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. /*
  12. * This driver can handle serial Elo touchscreens using either the Elo standard
  13. * 'E271-2210' 10-byte protocol, Elo legacy 'E281A-4002' 6-byte protocol, Elo
  14. * legacy 'E271-140' 4-byte protocol and Elo legacy 'E261-280' 3-byte protocol.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/input.h>
  21. #include <linux/serio.h>
  22. #include <linux/init.h>
  23. #define DRIVER_DESC "Elo serial touchscreen driver"
  24. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_LICENSE("GPL");
  27. /*
  28. * Definitions & global arrays.
  29. */
  30. #define ELO_MAX_LENGTH 10
  31. static char *elo_name = "Elo Serial TouchScreen";
  32. /*
  33. * Per-touchscreen data.
  34. */
  35. struct elo {
  36. struct input_dev dev;
  37. struct serio *serio;
  38. int id;
  39. int idx;
  40. unsigned char csum;
  41. unsigned char data[ELO_MAX_LENGTH];
  42. char phys[32];
  43. };
  44. static void elo_process_data_10(struct elo* elo, unsigned char data, struct pt_regs *regs)
  45. {
  46. struct input_dev *dev = &elo->dev;
  47. elo->csum += elo->data[elo->idx] = data;
  48. switch (elo->idx++) {
  49. case 0:
  50. if (data != 'U') {
  51. elo->idx = 0;
  52. elo->csum = 0;
  53. }
  54. break;
  55. case 1:
  56. if (data != 'T') {
  57. elo->idx = 0;
  58. elo->csum = 0;
  59. }
  60. break;
  61. case 9:
  62. if (elo->csum) {
  63. input_regs(dev, regs);
  64. input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]);
  65. input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]);
  66. input_report_abs(dev, ABS_PRESSURE, (elo->data[8] << 8) | elo->data[7]);
  67. input_report_key(dev, BTN_TOUCH, elo->data[2] & 3);
  68. input_sync(dev);
  69. }
  70. elo->idx = 0;
  71. elo->csum = 0;
  72. break;
  73. }
  74. }
  75. static void elo_process_data_6(struct elo* elo, unsigned char data, struct pt_regs *regs)
  76. {
  77. struct input_dev *dev = &elo->dev;
  78. elo->data[elo->idx] = data;
  79. switch (elo->idx++) {
  80. case 0: if ((data & 0xc0) != 0xc0) elo->idx = 0; break;
  81. case 1: if ((data & 0xc0) != 0x80) elo->idx = 0; break;
  82. case 2: if ((data & 0xc0) != 0x40) elo->idx = 0; break;
  83. case 3:
  84. if (data & 0xc0) {
  85. elo->idx = 0;
  86. break;
  87. }
  88. input_regs(dev, regs);
  89. input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f));
  90. input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f));
  91. if (elo->id == 2) {
  92. input_report_key(dev, BTN_TOUCH, 1);
  93. input_sync(dev);
  94. elo->idx = 0;
  95. }
  96. break;
  97. case 4:
  98. if (data) {
  99. input_sync(dev);
  100. elo->idx = 0;
  101. }
  102. break;
  103. case 5:
  104. if ((data & 0xf0) == 0) {
  105. input_report_abs(dev, ABS_PRESSURE, elo->data[5]);
  106. input_report_key(dev, BTN_TOUCH, elo->data[5]);
  107. }
  108. input_sync(dev);
  109. elo->idx = 0;
  110. break;
  111. }
  112. }
  113. static void elo_process_data_3(struct elo* elo, unsigned char data, struct pt_regs *regs)
  114. {
  115. struct input_dev *dev = &elo->dev;
  116. elo->data[elo->idx] = data;
  117. switch (elo->idx++) {
  118. case 0:
  119. if ((data & 0x7f) != 0x01)
  120. elo->idx = 0;
  121. break;
  122. case 2:
  123. input_regs(dev, regs);
  124. input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80));
  125. input_report_abs(dev, ABS_X, elo->data[1]);
  126. input_report_abs(dev, ABS_Y, elo->data[2]);
  127. input_sync(dev);
  128. elo->idx = 0;
  129. break;
  130. }
  131. }
  132. static irqreturn_t elo_interrupt(struct serio *serio,
  133. unsigned char data, unsigned int flags, struct pt_regs *regs)
  134. {
  135. struct elo* elo = serio_get_drvdata(serio);
  136. switch(elo->id) {
  137. case 0:
  138. elo_process_data_10(elo, data, regs);
  139. break;
  140. case 1:
  141. case 2:
  142. elo_process_data_6(elo, data, regs);
  143. break;
  144. case 3:
  145. elo_process_data_3(elo, data, regs);
  146. break;
  147. }
  148. return IRQ_HANDLED;
  149. }
  150. /*
  151. * elo_disconnect() is the opposite of elo_connect()
  152. */
  153. static void elo_disconnect(struct serio *serio)
  154. {
  155. struct elo* elo = serio_get_drvdata(serio);
  156. input_unregister_device(&elo->dev);
  157. serio_close(serio);
  158. serio_set_drvdata(serio, NULL);
  159. kfree(elo);
  160. }
  161. /*
  162. * elo_connect() is the routine that is called when someone adds a
  163. * new serio device that supports Gunze protocol and registers it as
  164. * an input device.
  165. */
  166. static int elo_connect(struct serio *serio, struct serio_driver *drv)
  167. {
  168. struct elo *elo;
  169. int err;
  170. if (!(elo = kmalloc(sizeof(struct elo), GFP_KERNEL)))
  171. return -ENOMEM;
  172. memset(elo, 0, sizeof(struct elo));
  173. init_input_dev(&elo->dev);
  174. elo->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  175. elo->dev.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  176. elo->id = serio->id.id;
  177. switch (elo->id) {
  178. case 0: /* 10-byte protocol */
  179. input_set_abs_params(&elo->dev, ABS_X, 96, 4000, 0, 0);
  180. input_set_abs_params(&elo->dev, ABS_Y, 96, 4000, 0, 0);
  181. input_set_abs_params(&elo->dev, ABS_PRESSURE, 0, 255, 0, 0);
  182. break;
  183. case 1: /* 6-byte protocol */
  184. input_set_abs_params(&elo->dev, ABS_PRESSURE, 0, 15, 0, 0);
  185. case 2: /* 4-byte protocol */
  186. input_set_abs_params(&elo->dev, ABS_X, 96, 4000, 0, 0);
  187. input_set_abs_params(&elo->dev, ABS_Y, 96, 4000, 0, 0);
  188. break;
  189. case 3: /* 3-byte protocol */
  190. input_set_abs_params(&elo->dev, ABS_X, 0, 255, 0, 0);
  191. input_set_abs_params(&elo->dev, ABS_Y, 0, 255, 0, 0);
  192. break;
  193. }
  194. elo->serio = serio;
  195. sprintf(elo->phys, "%s/input0", serio->phys);
  196. elo->dev.private = elo;
  197. elo->dev.name = elo_name;
  198. elo->dev.phys = elo->phys;
  199. elo->dev.id.bustype = BUS_RS232;
  200. elo->dev.id.vendor = SERIO_ELO;
  201. elo->dev.id.product = elo->id;
  202. elo->dev.id.version = 0x0100;
  203. serio_set_drvdata(serio, elo);
  204. err = serio_open(serio, drv);
  205. if (err) {
  206. serio_set_drvdata(serio, NULL);
  207. kfree(elo);
  208. return err;
  209. }
  210. input_register_device(&elo->dev);
  211. printk(KERN_INFO "input: %s on %s\n", elo_name, serio->phys);
  212. return 0;
  213. }
  214. /*
  215. * The serio driver structure.
  216. */
  217. static struct serio_device_id elo_serio_ids[] = {
  218. {
  219. .type = SERIO_RS232,
  220. .proto = SERIO_ELO,
  221. .id = SERIO_ANY,
  222. .extra = SERIO_ANY,
  223. },
  224. { 0 }
  225. };
  226. MODULE_DEVICE_TABLE(serio, elo_serio_ids);
  227. static struct serio_driver elo_drv = {
  228. .driver = {
  229. .name = "elo",
  230. },
  231. .description = DRIVER_DESC,
  232. .id_table = elo_serio_ids,
  233. .interrupt = elo_interrupt,
  234. .connect = elo_connect,
  235. .disconnect = elo_disconnect,
  236. };
  237. /*
  238. * The functions for inserting/removing us as a module.
  239. */
  240. static int __init elo_init(void)
  241. {
  242. serio_register_driver(&elo_drv);
  243. return 0;
  244. }
  245. static void __exit elo_exit(void)
  246. {
  247. serio_unregister_driver(&elo_drv);
  248. }
  249. module_init(elo_init);
  250. module_exit(elo_exit);