penmount.c 6.9 KB

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