hid-multitouch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * HID driver for multitouch panels
  3. *
  4. * Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr>
  5. * Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  6. * Copyright (c) 2010-2011 Ecole Nationale de l'Aviation Civile, France
  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. #include "usbhid/usbhid.h"
  22. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  23. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  24. MODULE_DESCRIPTION("HID multitouch panels");
  25. MODULE_LICENSE("GPL");
  26. #include "hid-ids.h"
  27. /* quirks to control the device */
  28. #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
  29. #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
  30. #define MT_QUIRK_CYPRESS (1 << 2)
  31. #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
  32. #define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
  33. #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
  34. struct mt_slot {
  35. __s32 x, y, p, w, h;
  36. __s32 contactid; /* the device ContactID assigned to this slot */
  37. bool touch_state; /* is the touch valid? */
  38. bool seen_in_this_frame;/* has this slot been updated */
  39. };
  40. struct mt_device {
  41. struct mt_slot curdata; /* placeholder of incoming data */
  42. struct mt_class *mtclass; /* our mt device class */
  43. unsigned last_field_index; /* last field index of the report */
  44. unsigned last_slot_field; /* the last field of a slot */
  45. __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
  46. __u8 num_received; /* how many contacts we received */
  47. __u8 num_expected; /* expected last contact index */
  48. bool curvalid; /* is the current contact valid? */
  49. struct mt_slot slots[0]; /* first slot */
  50. };
  51. struct mt_class {
  52. __s32 name; /* MT_CLS */
  53. __s32 quirks;
  54. __s32 sn_move; /* Signal/noise ratio for move events */
  55. __s32 sn_pressure; /* Signal/noise ratio for pressure events */
  56. __u8 maxcontacts;
  57. };
  58. /* classes of device behavior */
  59. #define MT_CLS_DEFAULT 1
  60. #define MT_CLS_DUAL_INRANGE_CONTACTID 2
  61. #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
  62. #define MT_CLS_CYPRESS 4
  63. /*
  64. * these device-dependent functions determine what slot corresponds
  65. * to a valid contact that was just read.
  66. */
  67. static int cypress_compute_slot(struct mt_device *td)
  68. {
  69. if (td->curdata.contactid != 0 || td->num_received == 0)
  70. return td->curdata.contactid;
  71. else
  72. return -1;
  73. }
  74. static int find_slot_from_contactid(struct mt_device *td)
  75. {
  76. int i;
  77. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  78. if (td->slots[i].contactid == td->curdata.contactid &&
  79. td->slots[i].touch_state)
  80. return i;
  81. }
  82. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  83. if (!td->slots[i].seen_in_this_frame &&
  84. !td->slots[i].touch_state)
  85. return i;
  86. }
  87. /* should not occurs. If this happens that means
  88. * that the device sent more touches that it says
  89. * in the report descriptor. It is ignored then. */
  90. return -1;
  91. }
  92. struct mt_class mt_classes[] = {
  93. { .name = MT_CLS_DEFAULT,
  94. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
  95. .maxcontacts = 10 },
  96. { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
  97. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  98. MT_QUIRK_SLOT_IS_CONTACTID,
  99. .maxcontacts = 2 },
  100. { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  101. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  102. MT_QUIRK_SLOT_IS_CONTACTNUMBER,
  103. .maxcontacts = 2 },
  104. { .name = MT_CLS_CYPRESS,
  105. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
  106. MT_QUIRK_CYPRESS,
  107. .maxcontacts = 10 },
  108. { }
  109. };
  110. static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
  111. struct hid_field *field, struct hid_usage *usage)
  112. {
  113. if (usage->hid == HID_DG_INPUTMODE) {
  114. struct mt_device *td = hid_get_drvdata(hdev);
  115. td->inputmode = field->report->id;
  116. }
  117. }
  118. static void set_abs(struct input_dev *input, unsigned int code,
  119. struct hid_field *field, int snratio)
  120. {
  121. int fmin = field->logical_minimum;
  122. int fmax = field->logical_maximum;
  123. int fuzz = snratio ? (fmax - fmin) / snratio : 0;
  124. input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
  125. }
  126. static int mt_input_mapping(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. struct mt_device *td = hid_get_drvdata(hdev);
  131. struct mt_class *cls = td->mtclass;
  132. switch (usage->hid & HID_USAGE_PAGE) {
  133. case HID_UP_GENDESK:
  134. switch (usage->hid) {
  135. case HID_GD_X:
  136. hid_map_usage(hi, usage, bit, max,
  137. EV_ABS, ABS_MT_POSITION_X);
  138. set_abs(hi->input, ABS_MT_POSITION_X, field,
  139. cls->sn_move);
  140. /* touchscreen emulation */
  141. set_abs(hi->input, ABS_X, field, cls->sn_move);
  142. td->last_slot_field = usage->hid;
  143. return 1;
  144. case HID_GD_Y:
  145. hid_map_usage(hi, usage, bit, max,
  146. EV_ABS, ABS_MT_POSITION_Y);
  147. set_abs(hi->input, ABS_MT_POSITION_Y, field,
  148. cls->sn_move);
  149. /* touchscreen emulation */
  150. set_abs(hi->input, ABS_Y, field, cls->sn_move);
  151. td->last_slot_field = usage->hid;
  152. return 1;
  153. }
  154. return 0;
  155. case HID_UP_DIGITIZER:
  156. switch (usage->hid) {
  157. case HID_DG_INRANGE:
  158. td->last_slot_field = usage->hid;
  159. return 1;
  160. case HID_DG_CONFIDENCE:
  161. td->last_slot_field = usage->hid;
  162. return 1;
  163. case HID_DG_TIPSWITCH:
  164. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  165. input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
  166. td->last_slot_field = usage->hid;
  167. return 1;
  168. case HID_DG_CONTACTID:
  169. input_mt_init_slots(hi->input,
  170. td->mtclass->maxcontacts);
  171. td->last_slot_field = usage->hid;
  172. return 1;
  173. case HID_DG_WIDTH:
  174. hid_map_usage(hi, usage, bit, max,
  175. EV_ABS, ABS_MT_TOUCH_MAJOR);
  176. td->last_slot_field = usage->hid;
  177. return 1;
  178. case HID_DG_HEIGHT:
  179. hid_map_usage(hi, usage, bit, max,
  180. EV_ABS, ABS_MT_TOUCH_MINOR);
  181. field->logical_maximum = 1;
  182. field->logical_minimum = 0;
  183. set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
  184. td->last_slot_field = usage->hid;
  185. return 1;
  186. case HID_DG_TIPPRESSURE:
  187. hid_map_usage(hi, usage, bit, max,
  188. EV_ABS, ABS_MT_PRESSURE);
  189. set_abs(hi->input, ABS_MT_PRESSURE, field,
  190. cls->sn_pressure);
  191. /* touchscreen emulation */
  192. set_abs(hi->input, ABS_PRESSURE, field,
  193. cls->sn_pressure);
  194. td->last_slot_field = usage->hid;
  195. return 1;
  196. case HID_DG_CONTACTCOUNT:
  197. td->last_field_index = field->report->maxfield - 1;
  198. return 1;
  199. case HID_DG_CONTACTMAX:
  200. /* we don't set td->last_slot_field as contactcount and
  201. * contact max are global to the report */
  202. return -1;
  203. }
  204. /* let hid-input decide for the others */
  205. return 0;
  206. case 0xff000000:
  207. /* we do not want to map these: no input-oriented meaning */
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  213. struct hid_field *field, struct hid_usage *usage,
  214. unsigned long **bit, int *max)
  215. {
  216. if (usage->type == EV_KEY || usage->type == EV_ABS)
  217. set_bit(usage->type, hi->input->evbit);
  218. return -1;
  219. }
  220. static int mt_compute_slot(struct mt_device *td)
  221. {
  222. __s32 quirks = td->mtclass->quirks;
  223. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
  224. return td->curdata.contactid;
  225. if (quirks & MT_QUIRK_CYPRESS)
  226. return cypress_compute_slot(td);
  227. if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
  228. return td->num_received;
  229. return find_slot_from_contactid(td);
  230. }
  231. /*
  232. * this function is called when a whole contact has been processed,
  233. * so that it can assign it to a slot and store the data there
  234. */
  235. static void mt_complete_slot(struct mt_device *td)
  236. {
  237. td->curdata.seen_in_this_frame = true;
  238. if (td->curvalid) {
  239. int slotnum = mt_compute_slot(td);
  240. if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
  241. td->slots[slotnum] = td->curdata;
  242. }
  243. td->num_received++;
  244. }
  245. /*
  246. * this function is called when a whole packet has been received and processed,
  247. * so that it can decide what to send to the input layer.
  248. */
  249. static void mt_emit_event(struct mt_device *td, struct input_dev *input)
  250. {
  251. int i;
  252. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  253. struct mt_slot *s = &(td->slots[i]);
  254. if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
  255. !s->seen_in_this_frame) {
  256. s->touch_state = false;
  257. }
  258. input_mt_slot(input, i);
  259. input_mt_report_slot_state(input, MT_TOOL_FINGER,
  260. s->touch_state);
  261. if (s->touch_state) {
  262. input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
  263. input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
  264. input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
  265. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
  266. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
  267. }
  268. s->seen_in_this_frame = false;
  269. }
  270. input_mt_report_pointer_emulation(input, true);
  271. input_sync(input);
  272. td->num_received = 0;
  273. }
  274. static int mt_event(struct hid_device *hid, struct hid_field *field,
  275. struct hid_usage *usage, __s32 value)
  276. {
  277. struct mt_device *td = hid_get_drvdata(hid);
  278. __s32 quirks = td->mtclass->quirks;
  279. if (hid->claimed & HID_CLAIMED_INPUT) {
  280. switch (usage->hid) {
  281. case HID_DG_INRANGE:
  282. if (quirks & MT_QUIRK_VALID_IS_INRANGE)
  283. td->curvalid = value;
  284. break;
  285. case HID_DG_TIPSWITCH:
  286. if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  287. td->curvalid = value;
  288. td->curdata.touch_state = value;
  289. break;
  290. case HID_DG_CONFIDENCE:
  291. if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
  292. td->curvalid = value;
  293. break;
  294. case HID_DG_CONTACTID:
  295. td->curdata.contactid = value;
  296. break;
  297. case HID_DG_TIPPRESSURE:
  298. td->curdata.p = value;
  299. break;
  300. case HID_GD_X:
  301. td->curdata.x = value;
  302. break;
  303. case HID_GD_Y:
  304. td->curdata.y = value;
  305. break;
  306. case HID_DG_WIDTH:
  307. td->curdata.w = value;
  308. break;
  309. case HID_DG_HEIGHT:
  310. td->curdata.h = value;
  311. break;
  312. case HID_DG_CONTACTCOUNT:
  313. /*
  314. * Includes multi-packet support where subsequent
  315. * packets are sent with zero contactcount.
  316. */
  317. if (value)
  318. td->num_expected = value;
  319. break;
  320. default:
  321. /* fallback to the generic hidinput handling */
  322. return 0;
  323. }
  324. if (usage->hid == td->last_slot_field)
  325. mt_complete_slot(td);
  326. if (field->index == td->last_field_index
  327. && td->num_received >= td->num_expected)
  328. mt_emit_event(td, field->hidinput->input);
  329. }
  330. /* we have handled the hidinput part, now remains hiddev */
  331. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  332. hid->hiddev_hid_event(hid, field, usage, value);
  333. return 1;
  334. }
  335. static void mt_set_input_mode(struct hid_device *hdev)
  336. {
  337. struct mt_device *td = hid_get_drvdata(hdev);
  338. struct hid_report *r;
  339. struct hid_report_enum *re;
  340. if (td->inputmode < 0)
  341. return;
  342. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  343. r = re->report_id_hash[td->inputmode];
  344. if (r) {
  345. r->field[0]->value[0] = 0x02;
  346. usbhid_submit_report(hdev, r, USB_DIR_OUT);
  347. }
  348. }
  349. static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
  350. {
  351. int ret, i;
  352. struct mt_device *td;
  353. struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
  354. for (i = 0; mt_classes[i].name ; i++) {
  355. if (id->driver_data == mt_classes[i].name) {
  356. mtclass = &(mt_classes[i]);
  357. break;
  358. }
  359. }
  360. /* This allows the driver to correctly support devices
  361. * that emit events over several HID messages.
  362. */
  363. hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
  364. td = kzalloc(sizeof(struct mt_device) +
  365. mtclass->maxcontacts * sizeof(struct mt_slot),
  366. GFP_KERNEL);
  367. if (!td) {
  368. dev_err(&hdev->dev, "cannot allocate multitouch data\n");
  369. return -ENOMEM;
  370. }
  371. td->mtclass = mtclass;
  372. td->inputmode = -1;
  373. hid_set_drvdata(hdev, td);
  374. ret = hid_parse(hdev);
  375. if (ret != 0)
  376. goto fail;
  377. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  378. if (ret)
  379. goto fail;
  380. mt_set_input_mode(hdev);
  381. return 0;
  382. fail:
  383. kfree(td);
  384. return ret;
  385. }
  386. #ifdef CONFIG_PM
  387. static int mt_reset_resume(struct hid_device *hdev)
  388. {
  389. mt_set_input_mode(hdev);
  390. return 0;
  391. }
  392. #endif
  393. static void mt_remove(struct hid_device *hdev)
  394. {
  395. struct mt_device *td = hid_get_drvdata(hdev);
  396. hid_hw_stop(hdev);
  397. kfree(td);
  398. hid_set_drvdata(hdev, NULL);
  399. }
  400. static const struct hid_device_id mt_devices[] = {
  401. /* Cypress panel */
  402. { .driver_data = MT_CLS_CYPRESS,
  403. HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
  404. USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
  405. /* GeneralTouch panel */
  406. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  407. HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  408. USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
  409. /* PixCir-based panels */
  410. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  411. HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
  412. USB_DEVICE_ID_HANVON_MULTITOUCH) },
  413. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  414. HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
  415. USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
  416. { }
  417. };
  418. MODULE_DEVICE_TABLE(hid, mt_devices);
  419. static const struct hid_usage_id mt_grabbed_usages[] = {
  420. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  421. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  422. };
  423. static struct hid_driver mt_driver = {
  424. .name = "hid-multitouch",
  425. .id_table = mt_devices,
  426. .probe = mt_probe,
  427. .remove = mt_remove,
  428. .input_mapping = mt_input_mapping,
  429. .input_mapped = mt_input_mapped,
  430. .feature_mapping = mt_feature_mapping,
  431. .usage_table = mt_grabbed_usages,
  432. .event = mt_event,
  433. #ifdef CONFIG_PM
  434. .reset_resume = mt_reset_resume,
  435. #endif
  436. };
  437. static int __init mt_init(void)
  438. {
  439. return hid_register_driver(&mt_driver);
  440. }
  441. static void __exit mt_exit(void)
  442. {
  443. hid_unregister_driver(&mt_driver);
  444. }
  445. module_init(mt_init);
  446. module_exit(mt_exit);