hid-3m-pct.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * HID driver for 3M PCT multitouch panels
  3. *
  4. * Copyright (c) 2009 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;
  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_CONTACTID:
  75. hid_map_usage(hi, usage, bit, max,
  76. EV_ABS, ABS_MT_TRACKING_ID);
  77. return 1;
  78. }
  79. /* let hid-input decide for the others */
  80. return 0;
  81. case 0xff000000:
  82. /* we do not want to map these: no input-oriented meaning */
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  88. struct hid_field *field, struct hid_usage *usage,
  89. unsigned long **bit, int *max)
  90. {
  91. if (usage->type == EV_KEY || usage->type == EV_ABS)
  92. clear_bit(usage->code, *bit);
  93. return 0;
  94. }
  95. /*
  96. * this function is called when a whole packet has been received and processed,
  97. * so that it can decide what to send to the input layer.
  98. */
  99. static void mmm_filter_event(struct mmm_data *md, struct input_dev *input)
  100. {
  101. struct mmm_finger *oldest = 0;
  102. bool pressed = false, released = false;
  103. int i;
  104. /*
  105. * we need to iterate on all fingers to decide if we have a press
  106. * or a release event in our touchscreen emulation.
  107. */
  108. for (i = 0; i < 10; ++i) {
  109. struct mmm_finger *f = &md->f[i];
  110. if (!f->valid) {
  111. /* this finger is just placeholder data, ignore */
  112. } else if (f->touch) {
  113. /* this finger is on the screen */
  114. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, i);
  115. input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x);
  116. input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y);
  117. input_mt_sync(input);
  118. /*
  119. * touchscreen emulation: maintain the age rank
  120. * of this finger, decide if we have a press
  121. */
  122. if (f->rank == 0) {
  123. f->rank = ++(md->num);
  124. if (f->rank == 1)
  125. pressed = true;
  126. }
  127. if (f->rank == 1)
  128. oldest = f;
  129. } else {
  130. /* this finger took off the screen */
  131. /* touchscreen emulation: maintain age rank of others */
  132. int j;
  133. for (j = 0; j < 10; ++j) {
  134. struct mmm_finger *g = &md->f[j];
  135. if (g->rank > f->rank) {
  136. g->rank--;
  137. if (g->rank == 1)
  138. oldest = g;
  139. }
  140. }
  141. f->rank = 0;
  142. --(md->num);
  143. if (md->num == 0)
  144. released = true;
  145. }
  146. f->valid = 0;
  147. }
  148. /* touchscreen emulation */
  149. if (oldest) {
  150. if (pressed)
  151. input_event(input, EV_KEY, BTN_TOUCH, 1);
  152. input_event(input, EV_ABS, ABS_X, oldest->x);
  153. input_event(input, EV_ABS, ABS_Y, oldest->y);
  154. } else if (released) {
  155. input_event(input, EV_KEY, BTN_TOUCH, 0);
  156. }
  157. }
  158. /*
  159. * this function is called upon all reports
  160. * so that we can accumulate contact point information,
  161. * and call input_mt_sync after each point.
  162. */
  163. static int mmm_event(struct hid_device *hid, struct hid_field *field,
  164. struct hid_usage *usage, __s32 value)
  165. {
  166. struct mmm_data *md = hid_get_drvdata(hid);
  167. /*
  168. * strangely, this function can be called before
  169. * field->hidinput is initialized!
  170. */
  171. if (hid->claimed & HID_CLAIMED_INPUT) {
  172. struct input_dev *input = field->hidinput->input;
  173. switch (usage->hid) {
  174. case HID_DG_TIPSWITCH:
  175. md->touch = value;
  176. break;
  177. case HID_DG_CONFIDENCE:
  178. md->valid = value;
  179. break;
  180. case HID_DG_CONTACTID:
  181. if (md->valid) {
  182. md->curid = value;
  183. md->f[value].touch = md->touch;
  184. md->f[value].valid = 1;
  185. }
  186. break;
  187. case HID_GD_X:
  188. if (md->valid)
  189. md->f[md->curid].x = value;
  190. break;
  191. case HID_GD_Y:
  192. if (md->valid)
  193. md->f[md->curid].y = value;
  194. break;
  195. case HID_DG_CONTACTCOUNT:
  196. mmm_filter_event(md, input);
  197. break;
  198. }
  199. }
  200. /* we have handled the hidinput part, now remains hiddev */
  201. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  202. hid->hiddev_hid_event(hid, field, usage, value);
  203. return 1;
  204. }
  205. static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  206. {
  207. int ret;
  208. struct mmm_data *md;
  209. md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL);
  210. if (!md) {
  211. dev_err(&hdev->dev, "cannot allocate 3M data\n");
  212. return -ENOMEM;
  213. }
  214. hid_set_drvdata(hdev, md);
  215. ret = hid_parse(hdev);
  216. if (!ret)
  217. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  218. if (ret)
  219. kfree(md);
  220. return ret;
  221. }
  222. static void mmm_remove(struct hid_device *hdev)
  223. {
  224. hid_hw_stop(hdev);
  225. kfree(hid_get_drvdata(hdev));
  226. hid_set_drvdata(hdev, NULL);
  227. }
  228. static const struct hid_device_id mmm_devices[] = {
  229. { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(hid, mmm_devices);
  233. static const struct hid_usage_id mmm_grabbed_usages[] = {
  234. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  235. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  236. };
  237. static struct hid_driver mmm_driver = {
  238. .name = "3m-pct",
  239. .id_table = mmm_devices,
  240. .probe = mmm_probe,
  241. .remove = mmm_remove,
  242. .input_mapping = mmm_input_mapping,
  243. .input_mapped = mmm_input_mapped,
  244. .usage_table = mmm_grabbed_usages,
  245. .event = mmm_event,
  246. };
  247. static int __init mmm_init(void)
  248. {
  249. return hid_register_driver(&mmm_driver);
  250. }
  251. static void __exit mmm_exit(void)
  252. {
  253. hid_unregister_driver(&mmm_driver);
  254. }
  255. module_init(mmm_init);
  256. module_exit(mmm_exit);
  257. MODULE_LICENSE("GPL");