hid-multitouch.c 43 KB

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