elo.c 8.5 KB

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