hid-3m-pct.c 6.7 KB

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