hid-3m-pct.c 8.3 KB

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