hid-3m-pct.c 8.3 KB

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