elo.c 9.1 KB

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