hid-multitouch.c 15 KB

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