hid-multitouch.c 13 KB

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