hid-3m-pct.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * HID driver for 3M PCT multitouch panels
  3. *
  4. * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
  5. *
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/usb.h>
  18. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  19. MODULE_DESCRIPTION("3M PCT multitouch panels");
  20. MODULE_LICENSE("GPL");
  21. #include "hid-ids.h"
  22. #define MAX_SLOTS 60
  23. #define MAX_TRKID 59
  24. struct mmm_finger {
  25. __s32 x, y, w, h;
  26. __u8 rank;
  27. bool touch, valid;
  28. };
  29. struct mmm_data {
  30. struct mmm_finger f[MAX_SLOTS];
  31. __u8 curid, num;
  32. __u8 nexp, nreal;
  33. bool touch, valid;
  34. };
  35. static int mmm_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  36. struct hid_field *field, struct hid_usage *usage,
  37. unsigned long **bit, int *max)
  38. {
  39. switch (usage->hid & HID_USAGE_PAGE) {
  40. case HID_UP_BUTTON:
  41. return -1;
  42. case HID_UP_GENDESK:
  43. switch (usage->hid) {
  44. case HID_GD_X:
  45. hid_map_usage(hi, usage, bit, max,
  46. EV_ABS, ABS_MT_POSITION_X);
  47. /* touchscreen emulation */
  48. input_set_abs_params(hi->input, ABS_X,
  49. field->logical_minimum,
  50. field->logical_maximum, 0, 0);
  51. return 1;
  52. case HID_GD_Y:
  53. hid_map_usage(hi, usage, bit, max,
  54. EV_ABS, ABS_MT_POSITION_Y);
  55. /* touchscreen emulation */
  56. input_set_abs_params(hi->input, ABS_Y,
  57. field->logical_minimum,
  58. field->logical_maximum, 0, 0);
  59. return 1;
  60. }
  61. return 0;
  62. case HID_UP_DIGITIZER:
  63. switch (usage->hid) {
  64. /* we do not want to map these: no input-oriented meaning */
  65. case 0x14:
  66. case 0x23:
  67. case HID_DG_INPUTMODE:
  68. case HID_DG_DEVICEINDEX:
  69. case HID_DG_CONTACTCOUNT:
  70. case HID_DG_CONTACTMAX:
  71. case HID_DG_INRANGE:
  72. case HID_DG_CONFIDENCE:
  73. return -1;
  74. case HID_DG_TIPSWITCH:
  75. /* touchscreen emulation */
  76. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  77. return 1;
  78. case HID_DG_WIDTH:
  79. hid_map_usage(hi, usage, bit, max,
  80. EV_ABS, ABS_MT_TOUCH_MAJOR);
  81. return 1;
  82. case HID_DG_HEIGHT:
  83. hid_map_usage(hi, usage, bit, max,
  84. EV_ABS, ABS_MT_TOUCH_MINOR);
  85. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  86. 0, 1, 0, 0);
  87. return 1;
  88. case HID_DG_CONTACTID:
  89. field->logical_maximum = MAX_TRKID;
  90. hid_map_usage(hi, usage, bit, max,
  91. EV_ABS, ABS_MT_TRACKING_ID);
  92. return 1;
  93. }
  94. /* let hid-input decide for the others */
  95. return 0;
  96. case 0xff000000:
  97. /* we do not want to map these: no input-oriented meaning */
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  103. struct hid_field *field, struct hid_usage *usage,
  104. unsigned long **bit, int *max)
  105. {
  106. if (usage->type == EV_KEY || usage->type == EV_ABS)
  107. clear_bit(usage->code, *bit);
  108. return 0;
  109. }
  110. /*
  111. * this function is called when a whole packet has been received and processed,
  112. * so that it can decide what to send to the input layer.
  113. */
  114. static void mmm_filter_event(struct mmm_data *md, struct input_dev *input)
  115. {
  116. struct mmm_finger *oldest = 0;
  117. bool pressed = false, released = false;
  118. int i;
  119. /*
  120. * we need to iterate on all fingers to decide if we have a press
  121. * or a release event in our touchscreen emulation.
  122. */
  123. for (i = 0; i < MAX_SLOTS; ++i) {
  124. struct mmm_finger *f = &md->f[i];
  125. if (!f->valid) {
  126. /* this finger is just placeholder data, ignore */
  127. } else if (f->touch) {
  128. /* this finger is on the screen */
  129. int wide = (f->w > f->h);
  130. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, i);
  131. input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x);
  132. input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y);
  133. input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
  134. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR,
  135. wide ? f->w : f->h);
  136. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR,
  137. wide ? f->h : f->w);
  138. input_mt_sync(input);
  139. /*
  140. * touchscreen emulation: maintain the age rank
  141. * of this finger, decide if we have a press
  142. */
  143. if (f->rank == 0) {
  144. f->rank = ++(md->num);
  145. if (f->rank == 1)
  146. pressed = true;
  147. }
  148. if (f->rank == 1)
  149. oldest = f;
  150. } else {
  151. /* this finger took off the screen */
  152. /* touchscreen emulation: maintain age rank of others */
  153. int j;
  154. for (j = 0; j < 10; ++j) {
  155. struct mmm_finger *g = &md->f[j];
  156. if (g->rank > f->rank) {
  157. g->rank--;
  158. if (g->rank == 1)
  159. oldest = g;
  160. }
  161. }
  162. f->rank = 0;
  163. --(md->num);
  164. if (md->num == 0)
  165. released = true;
  166. }
  167. f->valid = 0;
  168. }
  169. /* touchscreen emulation */
  170. if (oldest) {
  171. if (pressed)
  172. input_event(input, EV_KEY, BTN_TOUCH, 1);
  173. input_event(input, EV_ABS, ABS_X, oldest->x);
  174. input_event(input, EV_ABS, ABS_Y, oldest->y);
  175. } else if (released) {
  176. input_event(input, EV_KEY, BTN_TOUCH, 0);
  177. }
  178. input_sync(input);
  179. }
  180. /*
  181. * this function is called upon all reports
  182. * so that we can accumulate contact point information,
  183. * and call input_mt_sync after each point.
  184. */
  185. static int mmm_event(struct hid_device *hid, struct hid_field *field,
  186. struct hid_usage *usage, __s32 value)
  187. {
  188. struct mmm_data *md = hid_get_drvdata(hid);
  189. /*
  190. * strangely, this function can be called before
  191. * field->hidinput is initialized!
  192. */
  193. if (hid->claimed & HID_CLAIMED_INPUT) {
  194. struct input_dev *input = field->hidinput->input;
  195. switch (usage->hid) {
  196. case HID_DG_TIPSWITCH:
  197. md->touch = value;
  198. break;
  199. case HID_DG_CONFIDENCE:
  200. md->valid = value;
  201. break;
  202. case HID_DG_WIDTH:
  203. if (md->valid)
  204. md->f[md->curid].w = value;
  205. break;
  206. case HID_DG_HEIGHT:
  207. if (md->valid)
  208. md->f[md->curid].h = value;
  209. break;
  210. case HID_DG_CONTACTID:
  211. value = clamp_val(value, 0, MAX_SLOTS - 1);
  212. if (md->valid) {
  213. md->curid = value;
  214. md->f[value].touch = md->touch;
  215. md->f[value].valid = 1;
  216. md->nreal++;
  217. }
  218. break;
  219. case HID_GD_X:
  220. if (md->valid)
  221. md->f[md->curid].x = value;
  222. break;
  223. case HID_GD_Y:
  224. if (md->valid)
  225. md->f[md->curid].y = value;
  226. break;
  227. case HID_DG_CONTACTCOUNT:
  228. if (value)
  229. md->nexp = value;
  230. if (md->nreal >= md->nexp) {
  231. mmm_filter_event(md, input);
  232. md->nreal = 0;
  233. }
  234. break;
  235. }
  236. }
  237. /* we have handled the hidinput part, now remains hiddev */
  238. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  239. hid->hiddev_hid_event(hid, field, usage, value);
  240. return 1;
  241. }
  242. static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  243. {
  244. int ret;
  245. struct mmm_data *md;
  246. hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
  247. md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL);
  248. if (!md) {
  249. dev_err(&hdev->dev, "cannot allocate 3M data\n");
  250. return -ENOMEM;
  251. }
  252. hid_set_drvdata(hdev, md);
  253. ret = hid_parse(hdev);
  254. if (!ret)
  255. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  256. if (ret)
  257. kfree(md);
  258. return ret;
  259. }
  260. static void mmm_remove(struct hid_device *hdev)
  261. {
  262. hid_hw_stop(hdev);
  263. kfree(hid_get_drvdata(hdev));
  264. hid_set_drvdata(hdev, NULL);
  265. }
  266. static const struct hid_device_id mmm_devices[] = {
  267. { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
  268. { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) },
  269. { }
  270. };
  271. MODULE_DEVICE_TABLE(hid, mmm_devices);
  272. static const struct hid_usage_id mmm_grabbed_usages[] = {
  273. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  274. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  275. };
  276. static struct hid_driver mmm_driver = {
  277. .name = "3m-pct",
  278. .id_table = mmm_devices,
  279. .probe = mmm_probe,
  280. .remove = mmm_remove,
  281. .input_mapping = mmm_input_mapping,
  282. .input_mapped = mmm_input_mapped,
  283. .usage_table = mmm_grabbed_usages,
  284. .event = mmm_event,
  285. };
  286. static int __init mmm_init(void)
  287. {
  288. return hid_register_driver(&mmm_driver);
  289. }
  290. static void __exit mmm_exit(void)
  291. {
  292. hid_unregister_driver(&mmm_driver);
  293. }
  294. module_init(mmm_init);
  295. module_exit(mmm_exit);