penmount.c 7.1 KB

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