elo.c 8.6 KB

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