hid-3m-pct.c 7.4 KB

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