elo.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include <linux/ctype.h>
  24. #define DRIVER_DESC "Elo serial touchscreen driver"
  25. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  26. MODULE_DESCRIPTION(DRIVER_DESC);
  27. MODULE_LICENSE("GPL");
  28. /*
  29. * Definitions & global arrays.
  30. */
  31. #define ELO_MAX_LENGTH 10
  32. #define ELO10_PACKET_LEN 8
  33. #define ELO10_TOUCH 0x03
  34. #define ELO10_PRESSURE 0x80
  35. #define ELO10_LEAD_BYTE 'U'
  36. #define ELO10_ID_CMD 'i'
  37. #define ELO10_TOUCH_PACKET 'T'
  38. #define ELO10_ACK_PACKET 'A'
  39. #define ELI10_ID_PACKET 'I'
  40. /*
  41. * Per-touchscreen data.
  42. */
  43. struct elo {
  44. struct input_dev *dev;
  45. struct serio *serio;
  46. struct mutex cmd_mutex;
  47. struct completion cmd_done;
  48. int id;
  49. int idx;
  50. unsigned char expected_packet;
  51. unsigned char csum;
  52. unsigned char data[ELO_MAX_LENGTH];
  53. unsigned char response[ELO10_PACKET_LEN];
  54. char phys[32];
  55. };
  56. static void elo_process_data_10(struct elo *elo, unsigned char data)
  57. {
  58. struct input_dev *dev = elo->dev;
  59. elo->data[elo->idx] = data;
  60. switch (elo->idx++) {
  61. case 0:
  62. elo->csum = 0xaa;
  63. if (data != ELO10_LEAD_BYTE) {
  64. pr_debug("elo: unsynchronized data: 0x%02x\n", data);
  65. elo->idx = 0;
  66. }
  67. break;
  68. case 9:
  69. elo->idx = 0;
  70. if (data != elo->csum) {
  71. pr_debug("elo: bad checksum: 0x%02x, expected 0x%02x\n",
  72. data, elo->csum);
  73. break;
  74. }
  75. if (elo->data[1] != elo->expected_packet) {
  76. if (elo->data[1] != ELO10_TOUCH_PACKET)
  77. pr_debug("elo: unexpected packet: 0x%02x\n",
  78. elo->data[1]);
  79. break;
  80. }
  81. if (likely(elo->data[1] == ELO10_TOUCH_PACKET)) {
  82. input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]);
  83. input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]);
  84. if (elo->data[2] & ELO10_PRESSURE)
  85. input_report_abs(dev, ABS_PRESSURE,
  86. (elo->data[8] << 8) | elo->data[7]);
  87. input_report_key(dev, BTN_TOUCH, elo->data[2] & ELO10_TOUCH);
  88. input_sync(dev);
  89. } else if (elo->data[1] == ELO10_ACK_PACKET) {
  90. if (elo->data[2] == '0')
  91. elo->expected_packet = ELO10_TOUCH_PACKET;
  92. complete(&elo->cmd_done);
  93. } else {
  94. memcpy(elo->response, &elo->data[1], ELO10_PACKET_LEN);
  95. elo->expected_packet = ELO10_ACK_PACKET;
  96. }
  97. break;
  98. }
  99. elo->csum += data;
  100. }
  101. static void elo_process_data_6(struct elo *elo, unsigned char data)
  102. {
  103. struct input_dev *dev = elo->dev;
  104. elo->data[elo->idx] = data;
  105. switch (elo->idx++) {
  106. case 0: if ((data & 0xc0) != 0xc0) elo->idx = 0; break;
  107. case 1: if ((data & 0xc0) != 0x80) elo->idx = 0; break;
  108. case 2: if ((data & 0xc0) != 0x40) elo->idx = 0; break;
  109. case 3:
  110. if (data & 0xc0) {
  111. elo->idx = 0;
  112. break;
  113. }
  114. input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f));
  115. input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f));
  116. if (elo->id == 2) {
  117. input_report_key(dev, BTN_TOUCH, 1);
  118. input_sync(dev);
  119. elo->idx = 0;
  120. }
  121. break;
  122. case 4:
  123. if (data) {
  124. input_sync(dev);
  125. elo->idx = 0;
  126. }
  127. break;
  128. case 5:
  129. if ((data & 0xf0) == 0) {
  130. input_report_abs(dev, ABS_PRESSURE, elo->data[5]);
  131. input_report_key(dev, BTN_TOUCH, !!elo->data[5]);
  132. }
  133. input_sync(dev);
  134. elo->idx = 0;
  135. break;
  136. }
  137. }
  138. static void elo_process_data_3(struct elo *elo, unsigned char data)
  139. {
  140. struct input_dev *dev = elo->dev;
  141. elo->data[elo->idx] = data;
  142. switch (elo->idx++) {
  143. case 0:
  144. if ((data & 0x7f) != 0x01)
  145. elo->idx = 0;
  146. break;
  147. case 2:
  148. input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80));
  149. input_report_abs(dev, ABS_X, elo->data[1]);
  150. input_report_abs(dev, ABS_Y, elo->data[2]);
  151. input_sync(dev);
  152. elo->idx = 0;
  153. break;
  154. }
  155. }
  156. static irqreturn_t elo_interrupt(struct serio *serio,
  157. unsigned char data, unsigned int flags)
  158. {
  159. struct elo *elo = serio_get_drvdata(serio);
  160. switch(elo->id) {
  161. case 0:
  162. elo_process_data_10(elo, data);
  163. break;
  164. case 1:
  165. case 2:
  166. elo_process_data_6(elo, data);
  167. break;
  168. case 3:
  169. elo_process_data_3(elo, data);
  170. break;
  171. }
  172. return IRQ_HANDLED;
  173. }
  174. static int elo_command_10(struct elo *elo, unsigned char *packet)
  175. {
  176. int rc = -1;
  177. int i;
  178. unsigned char csum = 0xaa + ELO10_LEAD_BYTE;
  179. mutex_lock(&elo->cmd_mutex);
  180. serio_pause_rx(elo->serio);
  181. elo->expected_packet = toupper(packet[0]);
  182. init_completion(&elo->cmd_done);
  183. serio_continue_rx(elo->serio);
  184. if (serio_write(elo->serio, ELO10_LEAD_BYTE))
  185. goto out;
  186. for (i = 0; i < ELO10_PACKET_LEN; i++) {
  187. csum += packet[i];
  188. if (serio_write(elo->serio, packet[i]))
  189. goto out;
  190. }
  191. if (serio_write(elo->serio, csum))
  192. goto out;
  193. wait_for_completion_timeout(&elo->cmd_done, HZ);
  194. if (elo->expected_packet == ELO10_TOUCH_PACKET) {
  195. /* We are back in reporting mode, the command was ACKed */
  196. memcpy(packet, elo->response, ELO10_PACKET_LEN);
  197. rc = 0;
  198. }
  199. out:
  200. mutex_unlock(&elo->cmd_mutex);
  201. return rc;
  202. }
  203. static int elo_setup_10(struct elo *elo)
  204. {
  205. static const char *elo_types[] = { "Accu", "Dura", "Intelli", "Carroll" };
  206. struct input_dev *dev = elo->dev;
  207. unsigned char packet[ELO10_PACKET_LEN] = { ELO10_ID_CMD };
  208. if (elo_command_10(elo, packet))
  209. return -1;
  210. dev->id.version = (packet[5] << 8) | packet[4];
  211. input_set_abs_params(dev, ABS_X, 96, 4000, 0, 0);
  212. input_set_abs_params(dev, ABS_Y, 96, 4000, 0, 0);
  213. if (packet[3] & ELO10_PRESSURE)
  214. input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
  215. printk(KERN_INFO "elo: %sTouch touchscreen, fw: %02x.%02x, "
  216. "features: %x02x, controller: 0x%02x\n",
  217. elo_types[(packet[1] -'0') & 0x03],
  218. packet[5], packet[4], packet[3], packet[7]);
  219. return 0;
  220. }
  221. /*
  222. * elo_disconnect() is the opposite of elo_connect()
  223. */
  224. static void elo_disconnect(struct serio *serio)
  225. {
  226. struct elo *elo = serio_get_drvdata(serio);
  227. input_get_device(elo->dev);
  228. input_unregister_device(elo->dev);
  229. serio_close(serio);
  230. serio_set_drvdata(serio, NULL);
  231. input_put_device(elo->dev);
  232. kfree(elo);
  233. }
  234. /*
  235. * elo_connect() is the routine that is called when someone adds a
  236. * new serio device that supports Gunze protocol and registers it as
  237. * an input device.
  238. */
  239. static int elo_connect(struct serio *serio, struct serio_driver *drv)
  240. {
  241. struct elo *elo;
  242. struct input_dev *input_dev;
  243. int err;
  244. elo = kzalloc(sizeof(struct elo), GFP_KERNEL);
  245. input_dev = input_allocate_device();
  246. if (!elo || !input_dev) {
  247. err = -ENOMEM;
  248. goto fail1;
  249. }
  250. elo->serio = serio;
  251. elo->id = serio->id.id;
  252. elo->dev = input_dev;
  253. elo->expected_packet = ELO10_TOUCH_PACKET;
  254. mutex_init(&elo->cmd_mutex);
  255. init_completion(&elo->cmd_done);
  256. snprintf(elo->phys, sizeof(elo->phys), "%s/input0", serio->phys);
  257. input_dev->name = "Elo Serial TouchScreen";
  258. input_dev->phys = elo->phys;
  259. input_dev->id.bustype = BUS_RS232;
  260. input_dev->id.vendor = SERIO_ELO;
  261. input_dev->id.product = elo->id;
  262. input_dev->id.version = 0x0100;
  263. input_dev->dev.parent = &serio->dev;
  264. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  265. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  266. serio_set_drvdata(serio, elo);
  267. err = serio_open(serio, drv);
  268. if (err)
  269. goto fail2;
  270. switch (elo->id) {
  271. case 0: /* 10-byte protocol */
  272. if (elo_setup_10(elo))
  273. goto fail3;
  274. break;
  275. case 1: /* 6-byte protocol */
  276. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 15, 0, 0);
  277. case 2: /* 4-byte protocol */
  278. input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0);
  279. input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0);
  280. break;
  281. case 3: /* 3-byte protocol */
  282. input_set_abs_params(input_dev, ABS_X, 0, 255, 0, 0);
  283. input_set_abs_params(input_dev, ABS_Y, 0, 255, 0, 0);
  284. break;
  285. }
  286. err = input_register_device(elo->dev);
  287. if (err)
  288. goto fail3;
  289. return 0;
  290. fail3: serio_close(serio);
  291. fail2: serio_set_drvdata(serio, NULL);
  292. fail1: input_free_device(input_dev);
  293. kfree(elo);
  294. return err;
  295. }
  296. /*
  297. * The serio driver structure.
  298. */
  299. static struct serio_device_id elo_serio_ids[] = {
  300. {
  301. .type = SERIO_RS232,
  302. .proto = SERIO_ELO,
  303. .id = SERIO_ANY,
  304. .extra = SERIO_ANY,
  305. },
  306. { 0 }
  307. };
  308. MODULE_DEVICE_TABLE(serio, elo_serio_ids);
  309. static struct serio_driver elo_drv = {
  310. .driver = {
  311. .name = "elo",
  312. },
  313. .description = DRIVER_DESC,
  314. .id_table = elo_serio_ids,
  315. .interrupt = elo_interrupt,
  316. .connect = elo_connect,
  317. .disconnect = elo_disconnect,
  318. };
  319. /*
  320. * The functions for inserting/removing us as a module.
  321. */
  322. static int __init elo_init(void)
  323. {
  324. return serio_register_driver(&elo_drv);
  325. }
  326. static void __exit elo_exit(void)
  327. {
  328. serio_unregister_driver(&elo_drv);
  329. }
  330. module_init(elo_init);
  331. module_exit(elo_exit);