hid-3m-pct.c 7.4 KB

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