elo.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #define ELO10_LEAD_BYTE 'U'
  32. #define ELO10_TOUCH_PACKET 'T'
  33. /*
  34. * Per-touchscreen data.
  35. */
  36. struct elo {
  37. struct input_dev *dev;
  38. struct serio *serio;
  39. int id;
  40. int idx;
  41. unsigned char csum;
  42. unsigned char data[ELO_MAX_LENGTH];
  43. char phys[32];
  44. };
  45. static void elo_process_data_10(struct elo *elo, unsigned char data, struct pt_regs *regs)
  46. {
  47. struct input_dev *dev = elo->dev;
  48. elo->data[elo->idx] = data;
  49. switch (elo->idx++) {
  50. case 0:
  51. elo->csum = 0xaa;
  52. if (data != ELO10_LEAD_BYTE) {
  53. pr_debug("elo: unsynchronized data: 0x%02x\n", data);
  54. elo->idx = 0;
  55. }
  56. break;
  57. case 9:
  58. elo->idx = 0;
  59. if (data != elo->csum) {
  60. pr_debug("elo: bad checksum: 0x%02x, expected 0x%02x\n",
  61. data, elo->csum);
  62. break;
  63. }
  64. if (elo->data[1] != ELO10_TOUCH_PACKET) {
  65. pr_debug(elo: "unexpected packet: 0x%02x\n", elo->data[1]);
  66. break;
  67. }
  68. input_regs(dev, regs);
  69. input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]);
  70. input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]);
  71. input_report_abs(dev, ABS_PRESSURE, (elo->data[8] << 8) | elo->data[7]);
  72. input_report_key(dev, BTN_TOUCH, elo->data[8] || elo->data[7]);
  73. input_sync(dev);
  74. break;
  75. }
  76. elo->csum += data;
  77. }
  78. static void elo_process_data_6(struct elo *elo, unsigned char data, struct pt_regs *regs)
  79. {
  80. struct input_dev *dev = elo->dev;
  81. elo->data[elo->idx] = data;
  82. switch (elo->idx++) {
  83. case 0: if ((data & 0xc0) != 0xc0) elo->idx = 0; break;
  84. case 1: if ((data & 0xc0) != 0x80) elo->idx = 0; break;
  85. case 2: if ((data & 0xc0) != 0x40) elo->idx = 0; break;
  86. case 3:
  87. if (data & 0xc0) {
  88. elo->idx = 0;
  89. break;
  90. }
  91. input_regs(dev, regs);
  92. input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f));
  93. input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f));
  94. if (elo->id == 2) {
  95. input_report_key(dev, BTN_TOUCH, 1);
  96. input_sync(dev);
  97. elo->idx = 0;
  98. }
  99. break;
  100. case 4:
  101. if (data) {
  102. input_sync(dev);
  103. elo->idx = 0;
  104. }
  105. break;
  106. case 5:
  107. if ((data & 0xf0) == 0) {
  108. input_report_abs(dev, ABS_PRESSURE, elo->data[5]);
  109. input_report_key(dev, BTN_TOUCH, !!elo->data[5]);
  110. }
  111. input_sync(dev);
  112. elo->idx = 0;
  113. break;
  114. }
  115. }
  116. static void elo_process_data_3(struct elo *elo, unsigned char data, struct pt_regs *regs)
  117. {
  118. struct input_dev *dev = elo->dev;
  119. elo->data[elo->idx] = data;
  120. switch (elo->idx++) {
  121. case 0:
  122. if ((data & 0x7f) != 0x01)
  123. elo->idx = 0;
  124. break;
  125. case 2:
  126. input_regs(dev, regs);
  127. input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80));
  128. input_report_abs(dev, ABS_X, elo->data[1]);
  129. input_report_abs(dev, ABS_Y, elo->data[2]);
  130. input_sync(dev);
  131. elo->idx = 0;
  132. break;
  133. }
  134. }
  135. static irqreturn_t elo_interrupt(struct serio *serio,
  136. unsigned char data, unsigned int flags, struct pt_regs *regs)
  137. {
  138. struct elo *elo = serio_get_drvdata(serio);
  139. switch(elo->id) {
  140. case 0:
  141. elo_process_data_10(elo, data, regs);
  142. break;
  143. case 1:
  144. case 2:
  145. elo_process_data_6(elo, data, regs);
  146. break;
  147. case 3:
  148. elo_process_data_3(elo, data, regs);
  149. break;
  150. }
  151. return IRQ_HANDLED;
  152. }
  153. /*
  154. * elo_disconnect() is the opposite of elo_connect()
  155. */
  156. static void elo_disconnect(struct serio *serio)
  157. {
  158. struct elo *elo = serio_get_drvdata(serio);
  159. input_get_device(elo->dev);
  160. input_unregister_device(elo->dev);
  161. serio_close(serio);
  162. serio_set_drvdata(serio, NULL);
  163. input_put_device(elo->dev);
  164. kfree(elo);
  165. }
  166. /*
  167. * elo_connect() is the routine that is called when someone adds a
  168. * new serio device that supports Gunze protocol and registers it as
  169. * an input device.
  170. */
  171. static int elo_connect(struct serio *serio, struct serio_driver *drv)
  172. {
  173. struct elo *elo;
  174. struct input_dev *input_dev;
  175. int err;
  176. elo = kzalloc(sizeof(struct elo), GFP_KERNEL);
  177. input_dev = input_allocate_device();
  178. if (!elo || !input_dev) {
  179. err = -ENOMEM;
  180. goto fail1;
  181. }
  182. elo->serio = serio;
  183. elo->id = serio->id.id;
  184. elo->dev = input_dev;
  185. snprintf(elo->phys, sizeof(elo->phys), "%s/input0", serio->phys);
  186. input_dev->private = elo;
  187. input_dev->name = "Elo Serial TouchScreen";
  188. input_dev->phys = elo->phys;
  189. input_dev->id.bustype = BUS_RS232;
  190. input_dev->id.vendor = SERIO_ELO;
  191. input_dev->id.product = elo->id;
  192. input_dev->id.version = 0x0100;
  193. input_dev->cdev.dev = &serio->dev;
  194. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  195. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  196. switch (elo->id) {
  197. case 0: /* 10-byte protocol */
  198. input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0);
  199. input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0);
  200. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
  201. break;
  202. case 1: /* 6-byte protocol */
  203. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 15, 0, 0);
  204. case 2: /* 4-byte protocol */
  205. input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0);
  206. input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0);
  207. break;
  208. case 3: /* 3-byte protocol */
  209. input_set_abs_params(input_dev, ABS_X, 0, 255, 0, 0);
  210. input_set_abs_params(input_dev, ABS_Y, 0, 255, 0, 0);
  211. break;
  212. }
  213. serio_set_drvdata(serio, elo);
  214. err = serio_open(serio, drv);
  215. if (err)
  216. goto fail2;
  217. err = input_register_device(elo->dev);
  218. if (err)
  219. goto fail3;
  220. return 0;
  221. fail3: serio_close(serio);
  222. fail2: serio_set_drvdata(serio, NULL);
  223. fail1: input_free_device(input_dev);
  224. kfree(elo);
  225. return err;
  226. }
  227. /*
  228. * The serio driver structure.
  229. */
  230. static struct serio_device_id elo_serio_ids[] = {
  231. {
  232. .type = SERIO_RS232,
  233. .proto = SERIO_ELO,
  234. .id = SERIO_ANY,
  235. .extra = SERIO_ANY,
  236. },
  237. { 0 }
  238. };
  239. MODULE_DEVICE_TABLE(serio, elo_serio_ids);
  240. static struct serio_driver elo_drv = {
  241. .driver = {
  242. .name = "elo",
  243. },
  244. .description = DRIVER_DESC,
  245. .id_table = elo_serio_ids,
  246. .interrupt = elo_interrupt,
  247. .connect = elo_connect,
  248. .disconnect = elo_disconnect,
  249. };
  250. /*
  251. * The functions for inserting/removing us as a module.
  252. */
  253. static int __init elo_init(void)
  254. {
  255. serio_register_driver(&elo_drv);
  256. return 0;
  257. }
  258. static void __exit elo_exit(void)
  259. {
  260. serio_unregister_driver(&elo_drv);
  261. }
  262. module_init(elo_init);
  263. module_exit(elo_exit);