hid-3m-pct.c 7.4 KB

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