hid-multitouch.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. /*
  2. * HID driver for multitouch panels
  3. *
  4. * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
  5. * Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  6. * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
  7. * Copyright (c) 2012-2013 Red Hat, Inc
  8. *
  9. * This code is partly based on hid-egalax.c:
  10. *
  11. * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
  12. * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
  13. * Copyright (c) 2010 Canonical, Ltd.
  14. *
  15. * This code is partly based on hid-3m-pct.c:
  16. *
  17. * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
  18. * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
  19. * Copyright (c) 2010 Canonical, Ltd.
  20. *
  21. */
  22. /*
  23. * This program is free software; you can redistribute it and/or modify it
  24. * under the terms of the GNU General Public License as published by the Free
  25. * Software Foundation; either version 2 of the License, or (at your option)
  26. * any later version.
  27. */
  28. /*
  29. * This driver is regularly tested thanks to the tool hid-test[1].
  30. * This tool relies on hid-replay[2] and a database of hid devices[3].
  31. * Please run these regression tests before patching this module so that
  32. * your patch won't break existing known devices.
  33. *
  34. * [1] https://github.com/bentiss/hid-test
  35. * [2] https://github.com/bentiss/hid-replay
  36. * [3] https://github.com/bentiss/hid-devices
  37. */
  38. #include <linux/device.h>
  39. #include <linux/hid.h>
  40. #include <linux/module.h>
  41. #include <linux/slab.h>
  42. #include <linux/input/mt.h>
  43. #include <linux/string.h>
  44. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  45. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  46. MODULE_DESCRIPTION("HID multitouch panels");
  47. MODULE_LICENSE("GPL");
  48. #include "hid-ids.h"
  49. /* quirks to control the device */
  50. #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
  51. #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
  52. #define MT_QUIRK_CYPRESS (1 << 2)
  53. #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
  54. #define MT_QUIRK_ALWAYS_VALID (1 << 4)
  55. #define MT_QUIRK_VALID_IS_INRANGE (1 << 5)
  56. #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 6)
  57. #define MT_QUIRK_CONFIDENCE (1 << 7)
  58. #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE (1 << 8)
  59. #define MT_QUIRK_NO_AREA (1 << 9)
  60. #define MT_QUIRK_IGNORE_DUPLICATES (1 << 10)
  61. #define MT_QUIRK_HOVERING (1 << 11)
  62. #define MT_QUIRK_CONTACT_CNT_ACCURATE (1 << 12)
  63. #define MT_QUIRK_FORCE_GET_FEATURE (1 << 13)
  64. #define MT_INPUTMODE_TOUCHSCREEN 0x02
  65. #define MT_INPUTMODE_TOUCHPAD 0x03
  66. #define MT_BUTTONTYPE_CLICKPAD 0
  67. struct mt_slot {
  68. __s32 x, y, cx, cy, p, w, h;
  69. __s32 contactid; /* the device ContactID assigned to this slot */
  70. bool touch_state; /* is the touch valid? */
  71. bool inrange_state; /* is the finger in proximity of the sensor? */
  72. bool confidence_state; /* is the touch made by a finger? */
  73. };
  74. struct mt_class {
  75. __s32 name; /* MT_CLS */
  76. __s32 quirks;
  77. __s32 sn_move; /* Signal/noise ratio for move events */
  78. __s32 sn_width; /* Signal/noise ratio for width events */
  79. __s32 sn_height; /* Signal/noise ratio for height events */
  80. __s32 sn_pressure; /* Signal/noise ratio for pressure events */
  81. __u8 maxcontacts;
  82. bool is_indirect; /* true for touchpads */
  83. bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
  84. };
  85. struct mt_fields {
  86. unsigned usages[HID_MAX_FIELDS];
  87. unsigned int length;
  88. };
  89. struct mt_device {
  90. struct mt_slot curdata; /* placeholder of incoming data */
  91. struct mt_class mtclass; /* our mt device class */
  92. struct mt_fields *fields; /* temporary placeholder for storing the
  93. multitouch fields */
  94. int cc_index; /* contact count field index in the report */
  95. int cc_value_index; /* contact count value index in the field */
  96. unsigned last_slot_field; /* the last field of a slot */
  97. unsigned mt_report_id; /* the report ID of the multitouch device */
  98. unsigned long initial_quirks; /* initial quirks state */
  99. __s16 inputmode; /* InputMode HID feature, -1 if non-existent */
  100. __s16 inputmode_index; /* InputMode HID feature index in the report */
  101. __s16 maxcontact_report_id; /* Maximum Contact Number HID feature,
  102. -1 if non-existent */
  103. __u8 inputmode_value; /* InputMode HID feature value */
  104. __u8 num_received; /* how many contacts we received */
  105. __u8 num_expected; /* expected last contact index */
  106. __u8 maxcontacts;
  107. __u8 touches_by_report; /* how many touches are present in one report:
  108. * 1 means we should use a serial protocol
  109. * > 1 means hybrid (multitouch) protocol */
  110. __u8 buttons_count; /* number of physical buttons per touchpad */
  111. bool is_buttonpad; /* is this device a button pad? */
  112. bool serial_maybe; /* need to check for serial protocol */
  113. bool curvalid; /* is the current contact valid? */
  114. unsigned mt_flags; /* flags to pass to input-mt */
  115. };
  116. static void mt_post_parse_default_settings(struct mt_device *td);
  117. static void mt_post_parse(struct mt_device *td);
  118. /* classes of device behavior */
  119. #define MT_CLS_DEFAULT 0x0001
  120. #define MT_CLS_SERIAL 0x0002
  121. #define MT_CLS_CONFIDENCE 0x0003
  122. #define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
  123. #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
  124. #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
  125. #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
  126. /* reserved 0x0008 */
  127. #define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
  128. #define MT_CLS_NSMU 0x000a
  129. /* reserved 0x0010 */
  130. /* reserved 0x0011 */
  131. #define MT_CLS_WIN_8 0x0012
  132. #define MT_CLS_EXPORT_ALL_INPUTS 0x0013
  133. /* vendor specific classes */
  134. #define MT_CLS_3M 0x0101
  135. /* reserved 0x0102 */
  136. #define MT_CLS_EGALAX 0x0103
  137. #define MT_CLS_EGALAX_SERIAL 0x0104
  138. #define MT_CLS_TOPSEED 0x0105
  139. #define MT_CLS_PANASONIC 0x0106
  140. #define MT_CLS_FLATFROG 0x0107
  141. #define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
  142. #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
  143. #define MT_CLS_VTL 0x0110
  144. #define MT_DEFAULT_MAXCONTACT 10
  145. #define MT_MAX_MAXCONTACT 250
  146. #define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
  147. #define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
  148. /*
  149. * these device-dependent functions determine what slot corresponds
  150. * to a valid contact that was just read.
  151. */
  152. static int cypress_compute_slot(struct mt_device *td)
  153. {
  154. if (td->curdata.contactid != 0 || td->num_received == 0)
  155. return td->curdata.contactid;
  156. else
  157. return -1;
  158. }
  159. static struct mt_class mt_classes[] = {
  160. { .name = MT_CLS_DEFAULT,
  161. .quirks = MT_QUIRK_ALWAYS_VALID |
  162. MT_QUIRK_CONTACT_CNT_ACCURATE },
  163. { .name = MT_CLS_NSMU,
  164. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
  165. { .name = MT_CLS_SERIAL,
  166. .quirks = MT_QUIRK_ALWAYS_VALID},
  167. { .name = MT_CLS_CONFIDENCE,
  168. .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
  169. { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
  170. .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
  171. MT_QUIRK_SLOT_IS_CONTACTID },
  172. { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
  173. .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
  174. MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
  175. { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
  176. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  177. MT_QUIRK_SLOT_IS_CONTACTID,
  178. .maxcontacts = 2 },
  179. { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  180. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  181. MT_QUIRK_SLOT_IS_CONTACTNUMBER,
  182. .maxcontacts = 2 },
  183. { .name = MT_CLS_INRANGE_CONTACTNUMBER,
  184. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  185. MT_QUIRK_SLOT_IS_CONTACTNUMBER },
  186. { .name = MT_CLS_WIN_8,
  187. .quirks = MT_QUIRK_ALWAYS_VALID |
  188. MT_QUIRK_IGNORE_DUPLICATES |
  189. MT_QUIRK_HOVERING |
  190. MT_QUIRK_CONTACT_CNT_ACCURATE },
  191. { .name = MT_CLS_EXPORT_ALL_INPUTS,
  192. .quirks = MT_QUIRK_ALWAYS_VALID |
  193. MT_QUIRK_CONTACT_CNT_ACCURATE,
  194. .export_all_inputs = true },
  195. /*
  196. * vendor specific classes
  197. */
  198. { .name = MT_CLS_3M,
  199. .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
  200. MT_QUIRK_SLOT_IS_CONTACTID,
  201. .sn_move = 2048,
  202. .sn_width = 128,
  203. .sn_height = 128,
  204. .maxcontacts = 60,
  205. },
  206. { .name = MT_CLS_EGALAX,
  207. .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
  208. MT_QUIRK_VALID_IS_INRANGE,
  209. .sn_move = 4096,
  210. .sn_pressure = 32,
  211. },
  212. { .name = MT_CLS_EGALAX_SERIAL,
  213. .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
  214. MT_QUIRK_ALWAYS_VALID,
  215. .sn_move = 4096,
  216. .sn_pressure = 32,
  217. },
  218. { .name = MT_CLS_TOPSEED,
  219. .quirks = MT_QUIRK_ALWAYS_VALID,
  220. .is_indirect = true,
  221. .maxcontacts = 2,
  222. },
  223. { .name = MT_CLS_PANASONIC,
  224. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
  225. .maxcontacts = 4 },
  226. { .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
  227. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
  228. MT_QUIRK_VALID_IS_INRANGE |
  229. MT_QUIRK_SLOT_IS_CONTACTID,
  230. .maxcontacts = 2
  231. },
  232. { .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  233. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
  234. MT_QUIRK_SLOT_IS_CONTACTID
  235. },
  236. { .name = MT_CLS_FLATFROG,
  237. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
  238. MT_QUIRK_NO_AREA,
  239. .sn_move = 2048,
  240. .maxcontacts = 40,
  241. },
  242. { .name = MT_CLS_VTL,
  243. .quirks = MT_QUIRK_ALWAYS_VALID |
  244. MT_QUIRK_CONTACT_CNT_ACCURATE |
  245. MT_QUIRK_FORCE_GET_FEATURE,
  246. },
  247. { }
  248. };
  249. static ssize_t mt_show_quirks(struct device *dev,
  250. struct device_attribute *attr,
  251. char *buf)
  252. {
  253. struct hid_device *hdev = to_hid_device(dev);
  254. struct mt_device *td = hid_get_drvdata(hdev);
  255. return sprintf(buf, "%u\n", td->mtclass.quirks);
  256. }
  257. static ssize_t mt_set_quirks(struct device *dev,
  258. struct device_attribute *attr,
  259. const char *buf, size_t count)
  260. {
  261. struct hid_device *hdev = to_hid_device(dev);
  262. struct mt_device *td = hid_get_drvdata(hdev);
  263. unsigned long val;
  264. if (kstrtoul(buf, 0, &val))
  265. return -EINVAL;
  266. td->mtclass.quirks = val;
  267. if (td->cc_index < 0)
  268. td->mtclass.quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
  269. return count;
  270. }
  271. static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
  272. static struct attribute *sysfs_attrs[] = {
  273. &dev_attr_quirks.attr,
  274. NULL
  275. };
  276. static struct attribute_group mt_attribute_group = {
  277. .attrs = sysfs_attrs
  278. };
  279. static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
  280. {
  281. struct mt_device *td = hid_get_drvdata(hdev);
  282. int ret, size = hid_report_len(report);
  283. u8 *buf;
  284. /*
  285. * Do not fetch the feature report if the device has been explicitly
  286. * marked as non-capable.
  287. */
  288. if (td->initial_quirks & HID_QUIRK_NO_INIT_REPORTS)
  289. return;
  290. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  291. if (!buf)
  292. return;
  293. ret = hid_hw_raw_request(hdev, report->id, buf, size,
  294. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  295. if (ret < 0) {
  296. dev_warn(&hdev->dev, "failed to fetch feature %d\n",
  297. report->id);
  298. } else {
  299. ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
  300. size, 0);
  301. if (ret)
  302. dev_warn(&hdev->dev, "failed to report feature\n");
  303. }
  304. kfree(buf);
  305. }
  306. static void mt_feature_mapping(struct hid_device *hdev,
  307. struct hid_field *field, struct hid_usage *usage)
  308. {
  309. struct mt_device *td = hid_get_drvdata(hdev);
  310. switch (usage->hid) {
  311. case HID_DG_INPUTMODE:
  312. /* Ignore if value index is out of bounds. */
  313. if (usage->usage_index >= field->report_count) {
  314. dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
  315. break;
  316. }
  317. if (td->inputmode < 0) {
  318. td->inputmode = field->report->id;
  319. td->inputmode_index = usage->usage_index;
  320. } else {
  321. /*
  322. * Some elan panels wrongly declare 2 input mode
  323. * features, and silently ignore when we set the
  324. * value in the second field. Skip the second feature
  325. * and hope for the best.
  326. */
  327. dev_info(&hdev->dev,
  328. "Ignoring the extra HID_DG_INPUTMODE\n");
  329. }
  330. break;
  331. case HID_DG_CONTACTMAX:
  332. mt_get_feature(hdev, field->report);
  333. td->maxcontact_report_id = field->report->id;
  334. td->maxcontacts = field->value[0];
  335. if (!td->maxcontacts &&
  336. field->logical_maximum <= MT_MAX_MAXCONTACT)
  337. td->maxcontacts = field->logical_maximum;
  338. if (td->mtclass.maxcontacts)
  339. /* check if the maxcontacts is given by the class */
  340. td->maxcontacts = td->mtclass.maxcontacts;
  341. break;
  342. case HID_DG_BUTTONTYPE:
  343. if (usage->usage_index >= field->report_count) {
  344. dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
  345. break;
  346. }
  347. mt_get_feature(hdev, field->report);
  348. if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
  349. td->is_buttonpad = true;
  350. break;
  351. case 0xff0000c5:
  352. /* Retrieve the Win8 blob once to enable some devices */
  353. if (usage->usage_index == 0)
  354. mt_get_feature(hdev, field->report);
  355. break;
  356. }
  357. }
  358. static void set_abs(struct input_dev *input, unsigned int code,
  359. struct hid_field *field, int snratio)
  360. {
  361. int fmin = field->logical_minimum;
  362. int fmax = field->logical_maximum;
  363. int fuzz = snratio ? (fmax - fmin) / snratio : 0;
  364. input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
  365. input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
  366. }
  367. static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
  368. struct hid_input *hi)
  369. {
  370. struct mt_fields *f = td->fields;
  371. if (f->length >= HID_MAX_FIELDS)
  372. return;
  373. f->usages[f->length++] = usage->hid;
  374. }
  375. static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  376. struct hid_field *field, struct hid_usage *usage,
  377. unsigned long **bit, int *max)
  378. {
  379. struct mt_device *td = hid_get_drvdata(hdev);
  380. struct mt_class *cls = &td->mtclass;
  381. int code;
  382. struct hid_usage *prev_usage = NULL;
  383. if (field->application == HID_DG_TOUCHSCREEN)
  384. td->mt_flags |= INPUT_MT_DIRECT;
  385. /*
  386. * Model touchscreens providing buttons as touchpads.
  387. */
  388. if (field->application == HID_DG_TOUCHPAD ||
  389. (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  390. td->mt_flags |= INPUT_MT_POINTER;
  391. td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
  392. }
  393. /* count the buttons on touchpads */
  394. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
  395. td->buttons_count++;
  396. if (usage->usage_index)
  397. prev_usage = &field->usage[usage->usage_index - 1];
  398. switch (usage->hid & HID_USAGE_PAGE) {
  399. case HID_UP_GENDESK:
  400. switch (usage->hid) {
  401. case HID_GD_X:
  402. if (prev_usage && (prev_usage->hid == usage->hid)) {
  403. hid_map_usage(hi, usage, bit, max,
  404. EV_ABS, ABS_MT_TOOL_X);
  405. set_abs(hi->input, ABS_MT_TOOL_X, field,
  406. cls->sn_move);
  407. } else {
  408. hid_map_usage(hi, usage, bit, max,
  409. EV_ABS, ABS_MT_POSITION_X);
  410. set_abs(hi->input, ABS_MT_POSITION_X, field,
  411. cls->sn_move);
  412. }
  413. mt_store_field(usage, td, hi);
  414. return 1;
  415. case HID_GD_Y:
  416. if (prev_usage && (prev_usage->hid == usage->hid)) {
  417. hid_map_usage(hi, usage, bit, max,
  418. EV_ABS, ABS_MT_TOOL_Y);
  419. set_abs(hi->input, ABS_MT_TOOL_Y, field,
  420. cls->sn_move);
  421. } else {
  422. hid_map_usage(hi, usage, bit, max,
  423. EV_ABS, ABS_MT_POSITION_Y);
  424. set_abs(hi->input, ABS_MT_POSITION_Y, field,
  425. cls->sn_move);
  426. }
  427. mt_store_field(usage, td, hi);
  428. return 1;
  429. }
  430. return 0;
  431. case HID_UP_DIGITIZER:
  432. switch (usage->hid) {
  433. case HID_DG_INRANGE:
  434. if (cls->quirks & MT_QUIRK_HOVERING) {
  435. hid_map_usage(hi, usage, bit, max,
  436. EV_ABS, ABS_MT_DISTANCE);
  437. input_set_abs_params(hi->input,
  438. ABS_MT_DISTANCE, 0, 1, 0, 0);
  439. }
  440. mt_store_field(usage, td, hi);
  441. return 1;
  442. case HID_DG_CONFIDENCE:
  443. if (cls->name == MT_CLS_WIN_8 &&
  444. field->application == HID_DG_TOUCHPAD)
  445. cls->quirks |= MT_QUIRK_CONFIDENCE;
  446. mt_store_field(usage, td, hi);
  447. return 1;
  448. case HID_DG_TIPSWITCH:
  449. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  450. input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
  451. mt_store_field(usage, td, hi);
  452. return 1;
  453. case HID_DG_CONTACTID:
  454. mt_store_field(usage, td, hi);
  455. td->touches_by_report++;
  456. td->mt_report_id = field->report->id;
  457. return 1;
  458. case HID_DG_WIDTH:
  459. hid_map_usage(hi, usage, bit, max,
  460. EV_ABS, ABS_MT_TOUCH_MAJOR);
  461. if (!(cls->quirks & MT_QUIRK_NO_AREA))
  462. set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
  463. cls->sn_width);
  464. mt_store_field(usage, td, hi);
  465. return 1;
  466. case HID_DG_HEIGHT:
  467. hid_map_usage(hi, usage, bit, max,
  468. EV_ABS, ABS_MT_TOUCH_MINOR);
  469. if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
  470. set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
  471. cls->sn_height);
  472. input_set_abs_params(hi->input,
  473. ABS_MT_ORIENTATION, 0, 1, 0, 0);
  474. }
  475. mt_store_field(usage, td, hi);
  476. return 1;
  477. case HID_DG_TIPPRESSURE:
  478. hid_map_usage(hi, usage, bit, max,
  479. EV_ABS, ABS_MT_PRESSURE);
  480. set_abs(hi->input, ABS_MT_PRESSURE, field,
  481. cls->sn_pressure);
  482. mt_store_field(usage, td, hi);
  483. return 1;
  484. case HID_DG_CONTACTCOUNT:
  485. /* Ignore if indexes are out of bounds. */
  486. if (field->index >= field->report->maxfield ||
  487. usage->usage_index >= field->report_count)
  488. return 1;
  489. td->cc_index = field->index;
  490. td->cc_value_index = usage->usage_index;
  491. return 1;
  492. case HID_DG_CONTACTMAX:
  493. /* we don't set td->last_slot_field as contactcount and
  494. * contact max are global to the report */
  495. return -1;
  496. case HID_DG_TOUCH:
  497. /* Legacy devices use TIPSWITCH and not TOUCH.
  498. * Let's just ignore this field. */
  499. return -1;
  500. }
  501. /* let hid-input decide for the others */
  502. return 0;
  503. case HID_UP_BUTTON:
  504. code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
  505. hid_map_usage(hi, usage, bit, max, EV_KEY, code);
  506. input_set_capability(hi->input, EV_KEY, code);
  507. return 1;
  508. case 0xff000000:
  509. /* we do not want to map these: no input-oriented meaning */
  510. return -1;
  511. }
  512. return 0;
  513. }
  514. static int mt_touch_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  515. struct hid_field *field, struct hid_usage *usage,
  516. unsigned long **bit, int *max)
  517. {
  518. if (usage->type == EV_KEY || usage->type == EV_ABS)
  519. set_bit(usage->type, hi->input->evbit);
  520. return -1;
  521. }
  522. static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
  523. {
  524. __s32 quirks = td->mtclass.quirks;
  525. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
  526. return td->curdata.contactid;
  527. if (quirks & MT_QUIRK_CYPRESS)
  528. return cypress_compute_slot(td);
  529. if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
  530. return td->num_received;
  531. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
  532. return td->curdata.contactid - 1;
  533. return input_mt_get_slot_by_key(input, td->curdata.contactid);
  534. }
  535. /*
  536. * this function is called when a whole contact has been processed,
  537. * so that it can assign it to a slot and store the data there
  538. */
  539. static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
  540. {
  541. if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
  542. td->num_received >= td->num_expected)
  543. return;
  544. if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
  545. int active;
  546. int slotnum = mt_compute_slot(td, input);
  547. struct mt_slot *s = &td->curdata;
  548. struct input_mt *mt = input->mt;
  549. if (slotnum < 0 || slotnum >= td->maxcontacts)
  550. return;
  551. if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
  552. struct input_mt_slot *slot = &mt->slots[slotnum];
  553. if (input_mt_is_active(slot) &&
  554. input_mt_is_used(mt, slot))
  555. return;
  556. }
  557. if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
  558. s->confidence_state = 1;
  559. active = (s->touch_state || s->inrange_state) &&
  560. s->confidence_state;
  561. input_mt_slot(input, slotnum);
  562. input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
  563. if (active) {
  564. /* this finger is in proximity of the sensor */
  565. int wide = (s->w > s->h);
  566. /* divided by two to match visual scale of touch */
  567. int major = max(s->w, s->h) >> 1;
  568. int minor = min(s->w, s->h) >> 1;
  569. input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
  570. input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
  571. input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
  572. input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
  573. input_event(input, EV_ABS, ABS_MT_DISTANCE,
  574. !s->touch_state);
  575. input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
  576. input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
  577. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
  578. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
  579. }
  580. }
  581. td->num_received++;
  582. }
  583. /*
  584. * this function is called when a whole packet has been received and processed,
  585. * so that it can decide what to send to the input layer.
  586. */
  587. static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
  588. {
  589. input_mt_sync_frame(input);
  590. input_sync(input);
  591. td->num_received = 0;
  592. }
  593. static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
  594. struct hid_usage *usage, __s32 value)
  595. {
  596. /* we will handle the hidinput part later, now remains hiddev */
  597. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  598. hid->hiddev_hid_event(hid, field, usage, value);
  599. return 1;
  600. }
  601. static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
  602. struct hid_usage *usage, __s32 value)
  603. {
  604. struct mt_device *td = hid_get_drvdata(hid);
  605. __s32 quirks = td->mtclass.quirks;
  606. struct input_dev *input = field->hidinput->input;
  607. if (hid->claimed & HID_CLAIMED_INPUT) {
  608. switch (usage->hid) {
  609. case HID_DG_INRANGE:
  610. if (quirks & MT_QUIRK_VALID_IS_INRANGE)
  611. td->curvalid = value;
  612. if (quirks & MT_QUIRK_HOVERING)
  613. td->curdata.inrange_state = value;
  614. break;
  615. case HID_DG_TIPSWITCH:
  616. if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  617. td->curvalid = value;
  618. td->curdata.touch_state = value;
  619. break;
  620. case HID_DG_CONFIDENCE:
  621. if (quirks & MT_QUIRK_CONFIDENCE)
  622. td->curdata.confidence_state = value;
  623. if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
  624. td->curvalid = value;
  625. break;
  626. case HID_DG_CONTACTID:
  627. td->curdata.contactid = value;
  628. break;
  629. case HID_DG_TIPPRESSURE:
  630. td->curdata.p = value;
  631. break;
  632. case HID_GD_X:
  633. if (usage->code == ABS_MT_TOOL_X)
  634. td->curdata.cx = value;
  635. else
  636. td->curdata.x = value;
  637. break;
  638. case HID_GD_Y:
  639. if (usage->code == ABS_MT_TOOL_Y)
  640. td->curdata.cy = value;
  641. else
  642. td->curdata.y = value;
  643. break;
  644. case HID_DG_WIDTH:
  645. td->curdata.w = value;
  646. break;
  647. case HID_DG_HEIGHT:
  648. td->curdata.h = value;
  649. break;
  650. case HID_DG_CONTACTCOUNT:
  651. break;
  652. case HID_DG_TOUCH:
  653. /* do nothing */
  654. break;
  655. default:
  656. if (usage->type)
  657. input_event(input, usage->type, usage->code,
  658. value);
  659. return;
  660. }
  661. if (usage->usage_index + 1 == field->report_count) {
  662. /* we only take into account the last report. */
  663. if (usage->hid == td->last_slot_field)
  664. mt_complete_slot(td, field->hidinput->input);
  665. }
  666. }
  667. }
  668. static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
  669. {
  670. struct mt_device *td = hid_get_drvdata(hid);
  671. struct hid_field *field;
  672. unsigned count;
  673. int r, n;
  674. /*
  675. * Includes multi-packet support where subsequent
  676. * packets are sent with zero contactcount.
  677. */
  678. if (td->cc_index >= 0) {
  679. struct hid_field *field = report->field[td->cc_index];
  680. int value = field->value[td->cc_value_index];
  681. if (value)
  682. td->num_expected = value;
  683. }
  684. for (r = 0; r < report->maxfield; r++) {
  685. field = report->field[r];
  686. count = field->report_count;
  687. if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
  688. continue;
  689. for (n = 0; n < count; n++)
  690. mt_process_mt_event(hid, field, &field->usage[n],
  691. field->value[n]);
  692. }
  693. if (td->num_received >= td->num_expected)
  694. mt_sync_frame(td, report->field[0]->hidinput->input);
  695. }
  696. static int mt_touch_input_configured(struct hid_device *hdev,
  697. struct hid_input *hi)
  698. {
  699. struct mt_device *td = hid_get_drvdata(hdev);
  700. struct mt_class *cls = &td->mtclass;
  701. struct input_dev *input = hi->input;
  702. int ret;
  703. if (!td->maxcontacts)
  704. td->maxcontacts = MT_DEFAULT_MAXCONTACT;
  705. mt_post_parse(td);
  706. if (td->serial_maybe)
  707. mt_post_parse_default_settings(td);
  708. if (cls->is_indirect)
  709. td->mt_flags |= INPUT_MT_POINTER;
  710. if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  711. td->mt_flags |= INPUT_MT_DROP_UNUSED;
  712. /* check for clickpads */
  713. if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
  714. td->is_buttonpad = true;
  715. if (td->is_buttonpad)
  716. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  717. ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
  718. if (ret)
  719. return ret;
  720. td->mt_flags = 0;
  721. return 0;
  722. }
  723. static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  724. struct hid_field *field, struct hid_usage *usage,
  725. unsigned long **bit, int *max)
  726. {
  727. struct mt_device *td = hid_get_drvdata(hdev);
  728. /*
  729. * If mtclass.export_all_inputs is not set, only map fields from
  730. * TouchScreen or TouchPad collections. We need to ignore fields
  731. * that belong to other collections such as Mouse that might have
  732. * the same GenericDesktop usages.
  733. */
  734. if (!td->mtclass.export_all_inputs &&
  735. field->application != HID_DG_TOUCHSCREEN &&
  736. field->application != HID_DG_PEN &&
  737. field->application != HID_DG_TOUCHPAD &&
  738. field->application != HID_GD_KEYBOARD &&
  739. field->application != HID_CP_CONSUMER_CONTROL)
  740. return -1;
  741. /*
  742. * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
  743. * for the stylus.
  744. * The check for mt_report_id ensures we don't process
  745. * HID_DG_CONTACTCOUNT from the pen report as it is outside the physical
  746. * collection, but within the report ID.
  747. */
  748. if (field->physical == HID_DG_STYLUS)
  749. return 0;
  750. else if ((field->physical == 0) &&
  751. (field->report->id != td->mt_report_id) &&
  752. (td->mt_report_id != -1))
  753. return 0;
  754. if (field->application == HID_DG_TOUCHSCREEN ||
  755. field->application == HID_DG_TOUCHPAD)
  756. return mt_touch_input_mapping(hdev, hi, field, usage, bit, max);
  757. /* let hid-core decide for the others */
  758. return 0;
  759. }
  760. static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  761. struct hid_field *field, struct hid_usage *usage,
  762. unsigned long **bit, int *max)
  763. {
  764. /*
  765. * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
  766. * for the stylus.
  767. */
  768. if (field->physical == HID_DG_STYLUS)
  769. return 0;
  770. if (field->application == HID_DG_TOUCHSCREEN ||
  771. field->application == HID_DG_TOUCHPAD)
  772. return mt_touch_input_mapped(hdev, hi, field, usage, bit, max);
  773. /* let hid-core decide for the others */
  774. return 0;
  775. }
  776. static int mt_event(struct hid_device *hid, struct hid_field *field,
  777. struct hid_usage *usage, __s32 value)
  778. {
  779. struct mt_device *td = hid_get_drvdata(hid);
  780. if (field->report->id == td->mt_report_id)
  781. return mt_touch_event(hid, field, usage, value);
  782. return 0;
  783. }
  784. static void mt_report(struct hid_device *hid, struct hid_report *report)
  785. {
  786. struct mt_device *td = hid_get_drvdata(hid);
  787. struct hid_field *field = report->field[0];
  788. if (!(hid->claimed & HID_CLAIMED_INPUT))
  789. return;
  790. if (report->id == td->mt_report_id)
  791. return mt_touch_report(hid, report);
  792. if (field && field->hidinput && field->hidinput->input)
  793. input_sync(field->hidinput->input);
  794. }
  795. static void mt_set_input_mode(struct hid_device *hdev)
  796. {
  797. struct mt_device *td = hid_get_drvdata(hdev);
  798. struct hid_report *r;
  799. struct hid_report_enum *re;
  800. struct mt_class *cls = &td->mtclass;
  801. char *buf;
  802. int report_len;
  803. if (td->inputmode < 0)
  804. return;
  805. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  806. r = re->report_id_hash[td->inputmode];
  807. if (r) {
  808. if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
  809. report_len = hid_report_len(r);
  810. buf = hid_alloc_report_buf(r, GFP_KERNEL);
  811. if (!buf) {
  812. hid_err(hdev, "failed to allocate buffer for report\n");
  813. return;
  814. }
  815. hid_hw_raw_request(hdev, r->id, buf, report_len,
  816. HID_FEATURE_REPORT,
  817. HID_REQ_GET_REPORT);
  818. kfree(buf);
  819. }
  820. r->field[0]->value[td->inputmode_index] = td->inputmode_value;
  821. hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
  822. }
  823. }
  824. static void mt_set_maxcontacts(struct hid_device *hdev)
  825. {
  826. struct mt_device *td = hid_get_drvdata(hdev);
  827. struct hid_report *r;
  828. struct hid_report_enum *re;
  829. int fieldmax, max;
  830. if (td->maxcontact_report_id < 0)
  831. return;
  832. if (!td->mtclass.maxcontacts)
  833. return;
  834. re = &hdev->report_enum[HID_FEATURE_REPORT];
  835. r = re->report_id_hash[td->maxcontact_report_id];
  836. if (r) {
  837. max = td->mtclass.maxcontacts;
  838. fieldmax = r->field[0]->logical_maximum;
  839. max = min(fieldmax, max);
  840. if (r->field[0]->value[0] != max) {
  841. r->field[0]->value[0] = max;
  842. hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
  843. }
  844. }
  845. }
  846. static void mt_post_parse_default_settings(struct mt_device *td)
  847. {
  848. __s32 quirks = td->mtclass.quirks;
  849. /* unknown serial device needs special quirks */
  850. if (td->touches_by_report == 1) {
  851. quirks |= MT_QUIRK_ALWAYS_VALID;
  852. quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
  853. quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
  854. quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
  855. quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
  856. }
  857. td->mtclass.quirks = quirks;
  858. }
  859. static void mt_post_parse(struct mt_device *td)
  860. {
  861. struct mt_fields *f = td->fields;
  862. struct mt_class *cls = &td->mtclass;
  863. if (td->touches_by_report > 0) {
  864. int field_count_per_touch = f->length / td->touches_by_report;
  865. td->last_slot_field = f->usages[field_count_per_touch - 1];
  866. }
  867. if (td->cc_index < 0)
  868. cls->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
  869. }
  870. static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
  871. {
  872. struct mt_device *td = hid_get_drvdata(hdev);
  873. char *name;
  874. const char *suffix = NULL;
  875. struct hid_field *field = hi->report->field[0];
  876. int ret;
  877. if (hi->report->id == td->mt_report_id) {
  878. ret = mt_touch_input_configured(hdev, hi);
  879. if (ret)
  880. return ret;
  881. }
  882. /*
  883. * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
  884. * for the stylus. Check this first, and then rely on the application
  885. * field.
  886. */
  887. if (hi->report->field[0]->physical == HID_DG_STYLUS) {
  888. suffix = "Pen";
  889. /* force BTN_STYLUS to allow tablet matching in udev */
  890. __set_bit(BTN_STYLUS, hi->input->keybit);
  891. } else {
  892. switch (field->application) {
  893. case HID_GD_KEYBOARD:
  894. suffix = "Keyboard";
  895. break;
  896. case HID_GD_KEYPAD:
  897. suffix = "Keypad";
  898. break;
  899. case HID_GD_MOUSE:
  900. suffix = "Mouse";
  901. break;
  902. case HID_DG_STYLUS:
  903. suffix = "Pen";
  904. /* force BTN_STYLUS to allow tablet matching in udev */
  905. __set_bit(BTN_STYLUS, hi->input->keybit);
  906. break;
  907. case HID_DG_TOUCHSCREEN:
  908. /* we do not set suffix = "Touchscreen" */
  909. break;
  910. case HID_DG_TOUCHPAD:
  911. suffix = "Touchpad";
  912. break;
  913. case HID_GD_SYSTEM_CONTROL:
  914. suffix = "System Control";
  915. break;
  916. case HID_CP_CONSUMER_CONTROL:
  917. suffix = "Consumer Control";
  918. break;
  919. default:
  920. suffix = "UNKNOWN";
  921. break;
  922. }
  923. }
  924. if (suffix) {
  925. name = devm_kzalloc(&hi->input->dev,
  926. strlen(hdev->name) + strlen(suffix) + 2,
  927. GFP_KERNEL);
  928. if (name) {
  929. sprintf(name, "%s %s", hdev->name, suffix);
  930. hi->input->name = name;
  931. }
  932. }
  933. return 0;
  934. }
  935. static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
  936. {
  937. int ret, i;
  938. struct mt_device *td;
  939. struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
  940. for (i = 0; mt_classes[i].name ; i++) {
  941. if (id->driver_data == mt_classes[i].name) {
  942. mtclass = &(mt_classes[i]);
  943. break;
  944. }
  945. }
  946. td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
  947. if (!td) {
  948. dev_err(&hdev->dev, "cannot allocate multitouch data\n");
  949. return -ENOMEM;
  950. }
  951. td->mtclass = *mtclass;
  952. td->inputmode = -1;
  953. td->maxcontact_report_id = -1;
  954. td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
  955. td->cc_index = -1;
  956. td->mt_report_id = -1;
  957. hid_set_drvdata(hdev, td);
  958. td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
  959. GFP_KERNEL);
  960. if (!td->fields) {
  961. dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
  962. return -ENOMEM;
  963. }
  964. if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
  965. td->serial_maybe = true;
  966. /*
  967. * Store the initial quirk state
  968. */
  969. td->initial_quirks = hdev->quirks;
  970. /* This allows the driver to correctly support devices
  971. * that emit events over several HID messages.
  972. */
  973. hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
  974. /*
  975. * This allows the driver to handle different input sensors
  976. * that emits events through different reports on the same HID
  977. * device.
  978. */
  979. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  980. hdev->quirks |= HID_QUIRK_NO_EMPTY_INPUT;
  981. /*
  982. * Some multitouch screens do not like to be polled for input
  983. * reports. Fortunately, the Win8 spec says that all touches
  984. * should be sent during each report, making the initialization
  985. * of input reports unnecessary. For Win7 devices, well, let's hope
  986. * they will still be happy (this is only be a problem if a touch
  987. * was already there while probing the device).
  988. *
  989. * In addition some touchpads do not behave well if we read
  990. * all feature reports from them. Instead we prevent
  991. * initial report fetching and then selectively fetch each
  992. * report we are interested in.
  993. */
  994. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  995. ret = hid_parse(hdev);
  996. if (ret != 0)
  997. return ret;
  998. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  999. if (ret)
  1000. return ret;
  1001. ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
  1002. if (ret)
  1003. dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
  1004. hdev->name);
  1005. mt_set_maxcontacts(hdev);
  1006. mt_set_input_mode(hdev);
  1007. /* release .fields memory as it is not used anymore */
  1008. devm_kfree(&hdev->dev, td->fields);
  1009. td->fields = NULL;
  1010. return 0;
  1011. }
  1012. #ifdef CONFIG_PM
  1013. static void mt_release_contacts(struct hid_device *hid)
  1014. {
  1015. struct hid_input *hidinput;
  1016. list_for_each_entry(hidinput, &hid->inputs, list) {
  1017. struct input_dev *input_dev = hidinput->input;
  1018. struct input_mt *mt = input_dev->mt;
  1019. int i;
  1020. if (mt) {
  1021. for (i = 0; i < mt->num_slots; i++) {
  1022. input_mt_slot(input_dev, i);
  1023. input_mt_report_slot_state(input_dev,
  1024. MT_TOOL_FINGER,
  1025. false);
  1026. }
  1027. input_mt_sync_frame(input_dev);
  1028. input_sync(input_dev);
  1029. }
  1030. }
  1031. }
  1032. static int mt_reset_resume(struct hid_device *hdev)
  1033. {
  1034. mt_release_contacts(hdev);
  1035. mt_set_maxcontacts(hdev);
  1036. mt_set_input_mode(hdev);
  1037. return 0;
  1038. }
  1039. static int mt_resume(struct hid_device *hdev)
  1040. {
  1041. /* Some Elan legacy devices require SET_IDLE to be set on resume.
  1042. * It should be safe to send it to other devices too.
  1043. * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
  1044. hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
  1045. return 0;
  1046. }
  1047. #endif
  1048. static void mt_remove(struct hid_device *hdev)
  1049. {
  1050. struct mt_device *td = hid_get_drvdata(hdev);
  1051. sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
  1052. hid_hw_stop(hdev);
  1053. hdev->quirks = td->initial_quirks;
  1054. }
  1055. /*
  1056. * This list contains only:
  1057. * - VID/PID of products not working with the default multitouch handling
  1058. * - 2 generic rules.
  1059. * So there is no point in adding here any device with MT_CLS_DEFAULT.
  1060. */
  1061. static const struct hid_device_id mt_devices[] = {
  1062. /* 3M panels */
  1063. { .driver_data = MT_CLS_3M,
  1064. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1065. USB_DEVICE_ID_3M1968) },
  1066. { .driver_data = MT_CLS_3M,
  1067. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1068. USB_DEVICE_ID_3M2256) },
  1069. { .driver_data = MT_CLS_3M,
  1070. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1071. USB_DEVICE_ID_3M3266) },
  1072. /* Anton devices */
  1073. { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
  1074. MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
  1075. USB_DEVICE_ID_ANTON_TOUCH_PAD) },
  1076. /* Atmel panels */
  1077. { .driver_data = MT_CLS_SERIAL,
  1078. MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
  1079. USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
  1080. /* Baanto multitouch devices */
  1081. { .driver_data = MT_CLS_NSMU,
  1082. MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
  1083. USB_DEVICE_ID_BAANTO_MT_190W2) },
  1084. /* Cando panels */
  1085. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  1086. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1087. USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
  1088. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  1089. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1090. USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
  1091. /* Chunghwa Telecom touch panels */
  1092. { .driver_data = MT_CLS_NSMU,
  1093. MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
  1094. USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
  1095. /* CJTouch panels */
  1096. { .driver_data = MT_CLS_NSMU,
  1097. MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
  1098. USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
  1099. { .driver_data = MT_CLS_NSMU,
  1100. MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
  1101. USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
  1102. /* CVTouch panels */
  1103. { .driver_data = MT_CLS_NSMU,
  1104. MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
  1105. USB_DEVICE_ID_CVTOUCH_SCREEN) },
  1106. /* eGalax devices (resistive) */
  1107. { .driver_data = MT_CLS_EGALAX,
  1108. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1109. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
  1110. { .driver_data = MT_CLS_EGALAX,
  1111. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1112. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
  1113. /* eGalax devices (capacitive) */
  1114. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1115. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1116. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
  1117. { .driver_data = MT_CLS_EGALAX,
  1118. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1119. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
  1120. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1121. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1122. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
  1123. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1124. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1125. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
  1126. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1127. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1128. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
  1129. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1130. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1131. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
  1132. { .driver_data = MT_CLS_EGALAX,
  1133. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1134. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
  1135. { .driver_data = MT_CLS_EGALAX,
  1136. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1137. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
  1138. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1139. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1140. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
  1141. { .driver_data = MT_CLS_EGALAX,
  1142. HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1143. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
  1144. { .driver_data = MT_CLS_EGALAX,
  1145. HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1146. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
  1147. { .driver_data = MT_CLS_EGALAX,
  1148. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1149. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
  1150. { .driver_data = MT_CLS_EGALAX,
  1151. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1152. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
  1153. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1154. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1155. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
  1156. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1157. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1158. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
  1159. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1160. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1161. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
  1162. /* Elitegroup panel */
  1163. { .driver_data = MT_CLS_SERIAL,
  1164. MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
  1165. USB_DEVICE_ID_ELITEGROUP_05D8) },
  1166. /* Flatfrog Panels */
  1167. { .driver_data = MT_CLS_FLATFROG,
  1168. MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
  1169. USB_DEVICE_ID_MULTITOUCH_3200) },
  1170. /* FocalTech Panels */
  1171. { .driver_data = MT_CLS_SERIAL,
  1172. MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
  1173. USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
  1174. /* GeneralTouch panel */
  1175. { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
  1176. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1177. USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
  1178. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1179. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1180. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
  1181. { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
  1182. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1183. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
  1184. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1185. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1186. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
  1187. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1188. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1189. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
  1190. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1191. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1192. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
  1193. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1194. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1195. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
  1196. /* Gametel game controller */
  1197. { .driver_data = MT_CLS_NSMU,
  1198. MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
  1199. USB_DEVICE_ID_GAMETEL_MT_MODE) },
  1200. /* GoodTouch panels */
  1201. { .driver_data = MT_CLS_NSMU,
  1202. MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
  1203. USB_DEVICE_ID_GOODTOUCH_000f) },
  1204. /* Hanvon panels */
  1205. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  1206. MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
  1207. USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
  1208. /* Ilitek dual touch panel */
  1209. { .driver_data = MT_CLS_NSMU,
  1210. MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
  1211. USB_DEVICE_ID_ILITEK_MULTITOUCH) },
  1212. /* MosArt panels */
  1213. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1214. MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
  1215. USB_DEVICE_ID_ASUS_T91MT)},
  1216. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1217. MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
  1218. USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
  1219. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1220. MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
  1221. USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
  1222. /* Panasonic panels */
  1223. { .driver_data = MT_CLS_PANASONIC,
  1224. MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
  1225. USB_DEVICE_ID_PANABOARD_UBT780) },
  1226. { .driver_data = MT_CLS_PANASONIC,
  1227. MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
  1228. USB_DEVICE_ID_PANABOARD_UBT880) },
  1229. /* Novatek Panel */
  1230. { .driver_data = MT_CLS_NSMU,
  1231. MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
  1232. USB_DEVICE_ID_NOVATEK_PCT) },
  1233. /* Ntrig Panel */
  1234. { .driver_data = MT_CLS_NSMU,
  1235. HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
  1236. USB_VENDOR_ID_NTRIG, 0x1b05) },
  1237. /* PixArt optical touch screen */
  1238. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1239. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1240. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
  1241. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1242. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1243. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
  1244. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1245. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1246. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
  1247. /* PixCir-based panels */
  1248. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  1249. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1250. USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
  1251. /* Quanta-based panels */
  1252. { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
  1253. MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
  1254. USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
  1255. /* Stantum panels */
  1256. { .driver_data = MT_CLS_CONFIDENCE,
  1257. MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
  1258. USB_DEVICE_ID_MTP_STM)},
  1259. /* TopSeed panels */
  1260. { .driver_data = MT_CLS_TOPSEED,
  1261. MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
  1262. USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
  1263. /* Touch International panels */
  1264. { .driver_data = MT_CLS_NSMU,
  1265. MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
  1266. USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
  1267. /* Unitec panels */
  1268. { .driver_data = MT_CLS_NSMU,
  1269. MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
  1270. USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
  1271. { .driver_data = MT_CLS_NSMU,
  1272. MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
  1273. USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
  1274. /* VTL panels */
  1275. { .driver_data = MT_CLS_VTL,
  1276. MT_USB_DEVICE(USB_VENDOR_ID_VTL,
  1277. USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
  1278. /* Wistron panels */
  1279. { .driver_data = MT_CLS_NSMU,
  1280. MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
  1281. USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
  1282. /* XAT */
  1283. { .driver_data = MT_CLS_NSMU,
  1284. MT_USB_DEVICE(USB_VENDOR_ID_XAT,
  1285. USB_DEVICE_ID_XAT_CSR) },
  1286. /* Xiroku */
  1287. { .driver_data = MT_CLS_NSMU,
  1288. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1289. USB_DEVICE_ID_XIROKU_SPX) },
  1290. { .driver_data = MT_CLS_NSMU,
  1291. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1292. USB_DEVICE_ID_XIROKU_MPX) },
  1293. { .driver_data = MT_CLS_NSMU,
  1294. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1295. USB_DEVICE_ID_XIROKU_CSR) },
  1296. { .driver_data = MT_CLS_NSMU,
  1297. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1298. USB_DEVICE_ID_XIROKU_SPX1) },
  1299. { .driver_data = MT_CLS_NSMU,
  1300. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1301. USB_DEVICE_ID_XIROKU_MPX1) },
  1302. { .driver_data = MT_CLS_NSMU,
  1303. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1304. USB_DEVICE_ID_XIROKU_CSR1) },
  1305. { .driver_data = MT_CLS_NSMU,
  1306. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1307. USB_DEVICE_ID_XIROKU_SPX2) },
  1308. { .driver_data = MT_CLS_NSMU,
  1309. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1310. USB_DEVICE_ID_XIROKU_MPX2) },
  1311. { .driver_data = MT_CLS_NSMU,
  1312. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1313. USB_DEVICE_ID_XIROKU_CSR2) },
  1314. /* Generic MT device */
  1315. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
  1316. /* Generic Win 8 certified MT device */
  1317. { .driver_data = MT_CLS_WIN_8,
  1318. HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
  1319. HID_ANY_ID, HID_ANY_ID) },
  1320. { }
  1321. };
  1322. MODULE_DEVICE_TABLE(hid, mt_devices);
  1323. static const struct hid_usage_id mt_grabbed_usages[] = {
  1324. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  1325. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  1326. };
  1327. static struct hid_driver mt_driver = {
  1328. .name = "hid-multitouch",
  1329. .id_table = mt_devices,
  1330. .probe = mt_probe,
  1331. .remove = mt_remove,
  1332. .input_mapping = mt_input_mapping,
  1333. .input_mapped = mt_input_mapped,
  1334. .input_configured = mt_input_configured,
  1335. .feature_mapping = mt_feature_mapping,
  1336. .usage_table = mt_grabbed_usages,
  1337. .event = mt_event,
  1338. .report = mt_report,
  1339. #ifdef CONFIG_PM
  1340. .reset_resume = mt_reset_resume,
  1341. .resume = mt_resume,
  1342. #endif
  1343. };
  1344. module_hid_driver(mt_driver);