penmount.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Penmount serial touchscreen driver
  4. *
  5. * Copyright (c) 2006 Rick Koch <[email protected]>
  6. * Copyright (c) 2011 John Sung <[email protected]>
  7. *
  8. * Based on ELO driver (drivers/input/touchscreen/elo.c)
  9. * Copyright (c) 2004 Vojtech Pavlik
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/serio.h>
  18. #define DRIVER_DESC "PenMount serial touchscreen driver"
  19. MODULE_AUTHOR("Rick Koch <[email protected]>");
  20. MODULE_AUTHOR("John Sung <[email protected]>");
  21. MODULE_DESCRIPTION(DRIVER_DESC);
  22. MODULE_LICENSE("GPL");
  23. /*
  24. * Definitions & global arrays.
  25. */
  26. #define PM_MAX_LENGTH 6
  27. #define PM_MAX_MTSLOT 16
  28. #define PM_3000_MTSLOT 2
  29. #define PM_6250_MTSLOT 12
  30. /*
  31. * Multi-touch slot
  32. */
  33. struct mt_slot {
  34. unsigned short x, y;
  35. bool active; /* is the touch valid? */
  36. };
  37. /*
  38. * Per-touchscreen data.
  39. */
  40. struct pm {
  41. struct input_dev *dev;
  42. struct serio *serio;
  43. int idx;
  44. unsigned char data[PM_MAX_LENGTH];
  45. char phys[32];
  46. unsigned char packetsize;
  47. unsigned char maxcontacts;
  48. struct mt_slot slots[PM_MAX_MTSLOT];
  49. void (*parse_packet)(struct pm *);
  50. };
  51. /*
  52. * pm_mtevent() sends mt events and also emulates pointer movement
  53. */
  54. static void pm_mtevent(struct pm *pm, struct input_dev *input)
  55. {
  56. int i;
  57. for (i = 0; i < pm->maxcontacts; ++i) {
  58. input_mt_slot(input, i);
  59. input_mt_report_slot_state(input, MT_TOOL_FINGER,
  60. pm->slots[i].active);
  61. if (pm->slots[i].active) {
  62. input_event(input, EV_ABS, ABS_MT_POSITION_X, pm->slots[i].x);
  63. input_event(input, EV_ABS, ABS_MT_POSITION_Y, pm->slots[i].y);
  64. }
  65. }
  66. input_mt_report_pointer_emulation(input, true);
  67. input_sync(input);
  68. }
  69. /*
  70. * pm_checkpacket() checks if data packet is valid
  71. */
  72. static bool pm_checkpacket(unsigned char *packet)
  73. {
  74. int total = 0;
  75. int i;
  76. for (i = 0; i < 5; i++)
  77. total += packet[i];
  78. return packet[5] == (unsigned char)~(total & 0xff);
  79. }
  80. static void pm_parse_9000(struct pm *pm)
  81. {
  82. struct input_dev *dev = pm->dev;
  83. if ((pm->data[0] & 0x80) && pm->packetsize == ++pm->idx) {
  84. input_report_abs(dev, ABS_X, pm->data[1] * 128 + pm->data[2]);
  85. input_report_abs(dev, ABS_Y, pm->data[3] * 128 + pm->data[4]);
  86. input_report_key(dev, BTN_TOUCH, !!(pm->data[0] & 0x40));
  87. input_sync(dev);
  88. pm->idx = 0;
  89. }
  90. }
  91. static void pm_parse_6000(struct pm *pm)
  92. {
  93. struct input_dev *dev = pm->dev;
  94. if ((pm->data[0] & 0xbf) == 0x30 && pm->packetsize == ++pm->idx) {
  95. if (pm_checkpacket(pm->data)) {
  96. input_report_abs(dev, ABS_X,
  97. pm->data[2] * 256 + pm->data[1]);
  98. input_report_abs(dev, ABS_Y,
  99. pm->data[4] * 256 + pm->data[3]);
  100. input_report_key(dev, BTN_TOUCH, pm->data[0] & 0x40);
  101. input_sync(dev);
  102. }
  103. pm->idx = 0;
  104. }
  105. }
  106. static void pm_parse_3000(struct pm *pm)
  107. {
  108. struct input_dev *dev = pm->dev;
  109. if ((pm->data[0] & 0xce) == 0x40 && pm->packetsize == ++pm->idx) {
  110. if (pm_checkpacket(pm->data)) {
  111. int slotnum = pm->data[0] & 0x0f;
  112. pm->slots[slotnum].active = pm->data[0] & 0x30;
  113. pm->slots[slotnum].x = pm->data[2] * 256 + pm->data[1];
  114. pm->slots[slotnum].y = pm->data[4] * 256 + pm->data[3];
  115. pm_mtevent(pm, dev);
  116. }
  117. pm->idx = 0;
  118. }
  119. }
  120. static void pm_parse_6250(struct pm *pm)
  121. {
  122. struct input_dev *dev = pm->dev;
  123. if ((pm->data[0] & 0xb0) == 0x30 && pm->packetsize == ++pm->idx) {
  124. if (pm_checkpacket(pm->data)) {
  125. int slotnum = pm->data[0] & 0x0f;
  126. pm->slots[slotnum].active = pm->data[0] & 0x40;
  127. pm->slots[slotnum].x = pm->data[2] * 256 + pm->data[1];
  128. pm->slots[slotnum].y = pm->data[4] * 256 + pm->data[3];
  129. pm_mtevent(pm, dev);
  130. }
  131. pm->idx = 0;
  132. }
  133. }
  134. static irqreturn_t pm_interrupt(struct serio *serio,
  135. unsigned char data, unsigned int flags)
  136. {
  137. struct pm *pm = serio_get_drvdata(serio);
  138. pm->data[pm->idx] = data;
  139. pm->parse_packet(pm);
  140. return IRQ_HANDLED;
  141. }
  142. /*
  143. * pm_disconnect() is the opposite of pm_connect()
  144. */
  145. static void pm_disconnect(struct serio *serio)
  146. {
  147. struct pm *pm = serio_get_drvdata(serio);
  148. serio_close(serio);
  149. input_unregister_device(pm->dev);
  150. kfree(pm);
  151. serio_set_drvdata(serio, NULL);
  152. }
  153. /*
  154. * pm_connect() is the routine that is called when someone adds a
  155. * new serio device that supports PenMount protocol and registers it as
  156. * an input device.
  157. */
  158. static int pm_connect(struct serio *serio, struct serio_driver *drv)
  159. {
  160. struct pm *pm;
  161. struct input_dev *input_dev;
  162. int max_x, max_y;
  163. int err;
  164. pm = kzalloc(sizeof(struct pm), GFP_KERNEL);
  165. input_dev = input_allocate_device();
  166. if (!pm || !input_dev) {
  167. err = -ENOMEM;
  168. goto fail1;
  169. }
  170. pm->serio = serio;
  171. pm->dev = input_dev;
  172. snprintf(pm->phys, sizeof(pm->phys), "%s/input0", serio->phys);
  173. pm->maxcontacts = 1;
  174. input_dev->name = "PenMount Serial TouchScreen";
  175. input_dev->phys = pm->phys;
  176. input_dev->id.bustype = BUS_RS232;
  177. input_dev->id.vendor = SERIO_PENMOUNT;
  178. input_dev->id.product = 0;
  179. input_dev->id.version = 0x0100;
  180. input_dev->dev.parent = &serio->dev;
  181. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  182. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  183. switch (serio->id.id) {
  184. default:
  185. case 0:
  186. pm->packetsize = 5;
  187. pm->parse_packet = pm_parse_9000;
  188. input_dev->id.product = 0x9000;
  189. max_x = max_y = 0x3ff;
  190. break;
  191. case 1:
  192. pm->packetsize = 6;
  193. pm->parse_packet = pm_parse_6000;
  194. input_dev->id.product = 0x6000;
  195. max_x = max_y = 0x3ff;
  196. break;
  197. case 2:
  198. pm->packetsize = 6;
  199. pm->parse_packet = pm_parse_3000;
  200. input_dev->id.product = 0x3000;
  201. max_x = max_y = 0x7ff;
  202. pm->maxcontacts = PM_3000_MTSLOT;
  203. break;
  204. case 3:
  205. pm->packetsize = 6;
  206. pm->parse_packet = pm_parse_6250;
  207. input_dev->id.product = 0x6250;
  208. max_x = max_y = 0x3ff;
  209. pm->maxcontacts = PM_6250_MTSLOT;
  210. break;
  211. }
  212. input_set_abs_params(pm->dev, ABS_X, 0, max_x, 0, 0);
  213. input_set_abs_params(pm->dev, ABS_Y, 0, max_y, 0, 0);
  214. if (pm->maxcontacts > 1) {
  215. input_mt_init_slots(pm->dev, pm->maxcontacts, 0);
  216. input_set_abs_params(pm->dev,
  217. ABS_MT_POSITION_X, 0, max_x, 0, 0);
  218. input_set_abs_params(pm->dev,
  219. ABS_MT_POSITION_Y, 0, max_y, 0, 0);
  220. }
  221. serio_set_drvdata(serio, pm);
  222. err = serio_open(serio, drv);
  223. if (err)
  224. goto fail2;
  225. err = input_register_device(pm->dev);
  226. if (err)
  227. goto fail3;
  228. return 0;
  229. fail3: serio_close(serio);
  230. fail2: serio_set_drvdata(serio, NULL);
  231. fail1: input_free_device(input_dev);
  232. kfree(pm);
  233. return err;
  234. }
  235. /*
  236. * The serio driver structure.
  237. */
  238. static const struct serio_device_id pm_serio_ids[] = {
  239. {
  240. .type = SERIO_RS232,
  241. .proto = SERIO_PENMOUNT,
  242. .id = SERIO_ANY,
  243. .extra = SERIO_ANY,
  244. },
  245. { 0 }
  246. };
  247. MODULE_DEVICE_TABLE(serio, pm_serio_ids);
  248. static struct serio_driver pm_drv = {
  249. .driver = {
  250. .name = "serio-penmount",
  251. },
  252. .description = DRIVER_DESC,
  253. .id_table = pm_serio_ids,
  254. .interrupt = pm_interrupt,
  255. .connect = pm_connect,
  256. .disconnect = pm_disconnect,
  257. };
  258. module_serio_driver(pm_drv);