elo.c 8.9 KB

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