hid-multitouch.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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;
  283. u32 size = hid_report_len(report);
  284. u8 *buf;
  285. /*
  286. * Do not fetch the feature report if the device has been explicitly
  287. * marked as non-capable.
  288. */
  289. if (td->initial_quirks & HID_QUIRK_NO_INIT_REPORTS)
  290. return;
  291. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  292. if (!buf)
  293. return;
  294. ret = hid_hw_raw_request(hdev, report->id, buf, size,
  295. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  296. if (ret < 0) {
  297. dev_warn(&hdev->dev, "failed to fetch feature %d\n",
  298. report->id);
  299. } else {
  300. ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
  301. size, 0);
  302. if (ret)
  303. dev_warn(&hdev->dev, "failed to report feature\n");
  304. }
  305. kfree(buf);
  306. }
  307. static void mt_feature_mapping(struct hid_device *hdev,
  308. struct hid_field *field, struct hid_usage *usage)
  309. {
  310. struct mt_device *td = hid_get_drvdata(hdev);
  311. switch (usage->hid) {
  312. case HID_DG_INPUTMODE:
  313. /* Ignore if value index is out of bounds. */
  314. if (usage->usage_index >= field->report_count) {
  315. dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
  316. break;
  317. }
  318. if (td->inputmode < 0) {
  319. td->inputmode = field->report->id;
  320. td->inputmode_index = usage->usage_index;
  321. } else {
  322. /*
  323. * Some elan panels wrongly declare 2 input mode
  324. * features, and silently ignore when we set the
  325. * value in the second field. Skip the second feature
  326. * and hope for the best.
  327. */
  328. dev_info(&hdev->dev,
  329. "Ignoring the extra HID_DG_INPUTMODE\n");
  330. }
  331. break;
  332. case HID_DG_CONTACTMAX:
  333. mt_get_feature(hdev, field->report);
  334. td->maxcontact_report_id = field->report->id;
  335. td->maxcontacts = field->value[0];
  336. if (!td->maxcontacts &&
  337. field->logical_maximum <= MT_MAX_MAXCONTACT)
  338. td->maxcontacts = field->logical_maximum;
  339. if (td->mtclass.maxcontacts)
  340. /* check if the maxcontacts is given by the class */
  341. td->maxcontacts = td->mtclass.maxcontacts;
  342. break;
  343. case HID_DG_BUTTONTYPE:
  344. if (usage->usage_index >= field->report_count) {
  345. dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
  346. break;
  347. }
  348. mt_get_feature(hdev, field->report);
  349. if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
  350. td->is_buttonpad = true;
  351. break;
  352. case 0xff0000c5:
  353. /* Retrieve the Win8 blob once to enable some devices */
  354. if (usage->usage_index == 0)
  355. mt_get_feature(hdev, field->report);
  356. break;
  357. }
  358. }
  359. static void set_abs(struct input_dev *input, unsigned int code,
  360. struct hid_field *field, int snratio)
  361. {
  362. int fmin = field->logical_minimum;
  363. int fmax = field->logical_maximum;
  364. int fuzz = snratio ? (fmax - fmin) / snratio : 0;
  365. input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
  366. input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
  367. }
  368. static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
  369. struct hid_input *hi)
  370. {
  371. struct mt_fields *f = td->fields;
  372. if (f->length >= HID_MAX_FIELDS)
  373. return;
  374. f->usages[f->length++] = usage->hid;
  375. }
  376. static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  377. struct hid_field *field, struct hid_usage *usage,
  378. unsigned long **bit, int *max)
  379. {
  380. struct mt_device *td = hid_get_drvdata(hdev);
  381. struct mt_class *cls = &td->mtclass;
  382. int code;
  383. struct hid_usage *prev_usage = NULL;
  384. if (field->application == HID_DG_TOUCHSCREEN)
  385. td->mt_flags |= INPUT_MT_DIRECT;
  386. /*
  387. * Model touchscreens providing buttons as touchpads.
  388. */
  389. if (field->application == HID_DG_TOUCHPAD ||
  390. (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
  391. td->mt_flags |= INPUT_MT_POINTER;
  392. td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
  393. }
  394. /* count the buttons on touchpads */
  395. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
  396. td->buttons_count++;
  397. if (usage->usage_index)
  398. prev_usage = &field->usage[usage->usage_index - 1];
  399. switch (usage->hid & HID_USAGE_PAGE) {
  400. case HID_UP_GENDESK:
  401. switch (usage->hid) {
  402. case HID_GD_X:
  403. if (prev_usage && (prev_usage->hid == usage->hid)) {
  404. hid_map_usage(hi, usage, bit, max,
  405. EV_ABS, ABS_MT_TOOL_X);
  406. set_abs(hi->input, ABS_MT_TOOL_X, field,
  407. cls->sn_move);
  408. } else {
  409. hid_map_usage(hi, usage, bit, max,
  410. EV_ABS, ABS_MT_POSITION_X);
  411. set_abs(hi->input, ABS_MT_POSITION_X, field,
  412. cls->sn_move);
  413. }
  414. mt_store_field(usage, td, hi);
  415. return 1;
  416. case HID_GD_Y:
  417. if (prev_usage && (prev_usage->hid == usage->hid)) {
  418. hid_map_usage(hi, usage, bit, max,
  419. EV_ABS, ABS_MT_TOOL_Y);
  420. set_abs(hi->input, ABS_MT_TOOL_Y, field,
  421. cls->sn_move);
  422. } else {
  423. hid_map_usage(hi, usage, bit, max,
  424. EV_ABS, ABS_MT_POSITION_Y);
  425. set_abs(hi->input, ABS_MT_POSITION_Y, field,
  426. cls->sn_move);
  427. }
  428. mt_store_field(usage, td, hi);
  429. return 1;
  430. }
  431. return 0;
  432. case HID_UP_DIGITIZER:
  433. switch (usage->hid) {
  434. case HID_DG_INRANGE:
  435. if (cls->quirks & MT_QUIRK_HOVERING) {
  436. hid_map_usage(hi, usage, bit, max,
  437. EV_ABS, ABS_MT_DISTANCE);
  438. input_set_abs_params(hi->input,
  439. ABS_MT_DISTANCE, 0, 1, 0, 0);
  440. }
  441. mt_store_field(usage, td, hi);
  442. return 1;
  443. case HID_DG_CONFIDENCE:
  444. if (cls->name == MT_CLS_WIN_8 &&
  445. field->application == HID_DG_TOUCHPAD)
  446. cls->quirks |= MT_QUIRK_CONFIDENCE;
  447. mt_store_field(usage, td, hi);
  448. return 1;
  449. case HID_DG_TIPSWITCH:
  450. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  451. input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
  452. mt_store_field(usage, td, hi);
  453. return 1;
  454. case HID_DG_CONTACTID:
  455. mt_store_field(usage, td, hi);
  456. td->touches_by_report++;
  457. td->mt_report_id = field->report->id;
  458. return 1;
  459. case HID_DG_WIDTH:
  460. hid_map_usage(hi, usage, bit, max,
  461. EV_ABS, ABS_MT_TOUCH_MAJOR);
  462. if (!(cls->quirks & MT_QUIRK_NO_AREA))
  463. set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
  464. cls->sn_width);
  465. mt_store_field(usage, td, hi);
  466. return 1;
  467. case HID_DG_HEIGHT:
  468. hid_map_usage(hi, usage, bit, max,
  469. EV_ABS, ABS_MT_TOUCH_MINOR);
  470. if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
  471. set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
  472. cls->sn_height);
  473. input_set_abs_params(hi->input,
  474. ABS_MT_ORIENTATION, 0, 1, 0, 0);
  475. }
  476. mt_store_field(usage, td, hi);
  477. return 1;
  478. case HID_DG_TIPPRESSURE:
  479. hid_map_usage(hi, usage, bit, max,
  480. EV_ABS, ABS_MT_PRESSURE);
  481. set_abs(hi->input, ABS_MT_PRESSURE, field,
  482. cls->sn_pressure);
  483. mt_store_field(usage, td, hi);
  484. return 1;
  485. case HID_DG_CONTACTCOUNT:
  486. /* Ignore if indexes are out of bounds. */
  487. if (field->index >= field->report->maxfield ||
  488. usage->usage_index >= field->report_count)
  489. return 1;
  490. td->cc_index = field->index;
  491. td->cc_value_index = usage->usage_index;
  492. return 1;
  493. case HID_DG_CONTACTMAX:
  494. /* we don't set td->last_slot_field as contactcount and
  495. * contact max are global to the report */
  496. return -1;
  497. case HID_DG_TOUCH:
  498. /* Legacy devices use TIPSWITCH and not TOUCH.
  499. * Let's just ignore this field. */
  500. return -1;
  501. }
  502. /* let hid-input decide for the others */
  503. return 0;
  504. case HID_UP_BUTTON:
  505. code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
  506. hid_map_usage(hi, usage, bit, max, EV_KEY, code);
  507. input_set_capability(hi->input, EV_KEY, code);
  508. return 1;
  509. case 0xff000000:
  510. /* we do not want to map these: no input-oriented meaning */
  511. return -1;
  512. }
  513. return 0;
  514. }
  515. static int mt_touch_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  516. struct hid_field *field, struct hid_usage *usage,
  517. unsigned long **bit, int *max)
  518. {
  519. if (usage->type == EV_KEY || usage->type == EV_ABS)
  520. set_bit(usage->type, hi->input->evbit);
  521. return -1;
  522. }
  523. static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
  524. {
  525. __s32 quirks = td->mtclass.quirks;
  526. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
  527. return td->curdata.contactid;
  528. if (quirks & MT_QUIRK_CYPRESS)
  529. return cypress_compute_slot(td);
  530. if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
  531. return td->num_received;
  532. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
  533. return td->curdata.contactid - 1;
  534. return input_mt_get_slot_by_key(input, td->curdata.contactid);
  535. }
  536. /*
  537. * this function is called when a whole contact has been processed,
  538. * so that it can assign it to a slot and store the data there
  539. */
  540. static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
  541. {
  542. if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
  543. td->num_received >= td->num_expected)
  544. return;
  545. if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
  546. int active;
  547. int slotnum = mt_compute_slot(td, input);
  548. struct mt_slot *s = &td->curdata;
  549. struct input_mt *mt = input->mt;
  550. if (slotnum < 0 || slotnum >= td->maxcontacts)
  551. return;
  552. if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
  553. struct input_mt_slot *slot = &mt->slots[slotnum];
  554. if (input_mt_is_active(slot) &&
  555. input_mt_is_used(mt, slot))
  556. return;
  557. }
  558. if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
  559. s->confidence_state = 1;
  560. active = (s->touch_state || s->inrange_state) &&
  561. s->confidence_state;
  562. input_mt_slot(input, slotnum);
  563. input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
  564. if (active) {
  565. /* this finger is in proximity of the sensor */
  566. int wide = (s->w > s->h);
  567. /* divided by two to match visual scale of touch */
  568. int major = max(s->w, s->h) >> 1;
  569. int minor = min(s->w, s->h) >> 1;
  570. input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
  571. input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
  572. input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
  573. input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
  574. input_event(input, EV_ABS, ABS_MT_DISTANCE,
  575. !s->touch_state);
  576. input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
  577. input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
  578. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
  579. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
  580. }
  581. }
  582. td->num_received++;
  583. }
  584. /*
  585. * this function is called when a whole packet has been received and processed,
  586. * so that it can decide what to send to the input layer.
  587. */
  588. static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
  589. {
  590. input_mt_sync_frame(input);
  591. input_sync(input);
  592. td->num_received = 0;
  593. }
  594. static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
  595. struct hid_usage *usage, __s32 value)
  596. {
  597. /* we will handle the hidinput part later, now remains hiddev */
  598. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  599. hid->hiddev_hid_event(hid, field, usage, value);
  600. return 1;
  601. }
  602. static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
  603. struct hid_usage *usage, __s32 value)
  604. {
  605. struct mt_device *td = hid_get_drvdata(hid);
  606. __s32 quirks = td->mtclass.quirks;
  607. struct input_dev *input = field->hidinput->input;
  608. if (hid->claimed & HID_CLAIMED_INPUT) {
  609. switch (usage->hid) {
  610. case HID_DG_INRANGE:
  611. if (quirks & MT_QUIRK_VALID_IS_INRANGE)
  612. td->curvalid = value;
  613. if (quirks & MT_QUIRK_HOVERING)
  614. td->curdata.inrange_state = value;
  615. break;
  616. case HID_DG_TIPSWITCH:
  617. if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  618. td->curvalid = value;
  619. td->curdata.touch_state = value;
  620. break;
  621. case HID_DG_CONFIDENCE:
  622. if (quirks & MT_QUIRK_CONFIDENCE)
  623. td->curdata.confidence_state = value;
  624. if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
  625. td->curvalid = value;
  626. break;
  627. case HID_DG_CONTACTID:
  628. td->curdata.contactid = value;
  629. break;
  630. case HID_DG_TIPPRESSURE:
  631. td->curdata.p = value;
  632. break;
  633. case HID_GD_X:
  634. if (usage->code == ABS_MT_TOOL_X)
  635. td->curdata.cx = value;
  636. else
  637. td->curdata.x = value;
  638. break;
  639. case HID_GD_Y:
  640. if (usage->code == ABS_MT_TOOL_Y)
  641. td->curdata.cy = value;
  642. else
  643. td->curdata.y = value;
  644. break;
  645. case HID_DG_WIDTH:
  646. td->curdata.w = value;
  647. break;
  648. case HID_DG_HEIGHT:
  649. td->curdata.h = value;
  650. break;
  651. case HID_DG_CONTACTCOUNT:
  652. break;
  653. case HID_DG_TOUCH:
  654. /* do nothing */
  655. break;
  656. default:
  657. if (usage->type)
  658. input_event(input, usage->type, usage->code,
  659. value);
  660. return;
  661. }
  662. if (usage->usage_index + 1 == field->report_count) {
  663. /* we only take into account the last report. */
  664. if (usage->hid == td->last_slot_field)
  665. mt_complete_slot(td, field->hidinput->input);
  666. }
  667. }
  668. }
  669. static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
  670. {
  671. struct mt_device *td = hid_get_drvdata(hid);
  672. struct hid_field *field;
  673. unsigned count;
  674. int r, n;
  675. /*
  676. * Includes multi-packet support where subsequent
  677. * packets are sent with zero contactcount.
  678. */
  679. if (td->cc_index >= 0) {
  680. struct hid_field *field = report->field[td->cc_index];
  681. int value = field->value[td->cc_value_index];
  682. if (value)
  683. td->num_expected = value;
  684. }
  685. for (r = 0; r < report->maxfield; r++) {
  686. field = report->field[r];
  687. count = field->report_count;
  688. if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
  689. continue;
  690. for (n = 0; n < count; n++)
  691. mt_process_mt_event(hid, field, &field->usage[n],
  692. field->value[n]);
  693. }
  694. if (td->num_received >= td->num_expected)
  695. mt_sync_frame(td, report->field[0]->hidinput->input);
  696. }
  697. static int mt_touch_input_configured(struct hid_device *hdev,
  698. struct hid_input *hi)
  699. {
  700. struct mt_device *td = hid_get_drvdata(hdev);
  701. struct mt_class *cls = &td->mtclass;
  702. struct input_dev *input = hi->input;
  703. int ret;
  704. if (!td->maxcontacts)
  705. td->maxcontacts = MT_DEFAULT_MAXCONTACT;
  706. mt_post_parse(td);
  707. if (td->serial_maybe)
  708. mt_post_parse_default_settings(td);
  709. if (cls->is_indirect)
  710. td->mt_flags |= INPUT_MT_POINTER;
  711. if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  712. td->mt_flags |= INPUT_MT_DROP_UNUSED;
  713. /* check for clickpads */
  714. if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
  715. td->is_buttonpad = true;
  716. if (td->is_buttonpad)
  717. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  718. ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
  719. if (ret)
  720. return ret;
  721. td->mt_flags = 0;
  722. return 0;
  723. }
  724. static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  725. struct hid_field *field, struct hid_usage *usage,
  726. unsigned long **bit, int *max)
  727. {
  728. struct mt_device *td = hid_get_drvdata(hdev);
  729. /*
  730. * If mtclass.export_all_inputs is not set, only map fields from
  731. * TouchScreen or TouchPad collections. We need to ignore fields
  732. * that belong to other collections such as Mouse that might have
  733. * the same GenericDesktop usages.
  734. */
  735. if (!td->mtclass.export_all_inputs &&
  736. field->application != HID_DG_TOUCHSCREEN &&
  737. field->application != HID_DG_PEN &&
  738. field->application != HID_DG_TOUCHPAD &&
  739. field->application != HID_GD_KEYBOARD &&
  740. field->application != HID_CP_CONSUMER_CONTROL)
  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. td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
  948. if (!td) {
  949. dev_err(&hdev->dev, "cannot allocate multitouch data\n");
  950. return -ENOMEM;
  951. }
  952. td->mtclass = *mtclass;
  953. td->inputmode = -1;
  954. td->maxcontact_report_id = -1;
  955. td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
  956. td->cc_index = -1;
  957. td->mt_report_id = -1;
  958. hid_set_drvdata(hdev, td);
  959. td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
  960. GFP_KERNEL);
  961. if (!td->fields) {
  962. dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
  963. return -ENOMEM;
  964. }
  965. if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
  966. td->serial_maybe = true;
  967. /*
  968. * Store the initial quirk state
  969. */
  970. td->initial_quirks = hdev->quirks;
  971. /* This allows the driver to correctly support devices
  972. * that emit events over several HID messages.
  973. */
  974. hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
  975. /*
  976. * This allows the driver to handle different input sensors
  977. * that emits events through different reports on the same HID
  978. * device.
  979. */
  980. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  981. hdev->quirks |= HID_QUIRK_NO_EMPTY_INPUT;
  982. /*
  983. * Some multitouch screens do not like to be polled for input
  984. * reports. Fortunately, the Win8 spec says that all touches
  985. * should be sent during each report, making the initialization
  986. * of input reports unnecessary. For Win7 devices, well, let's hope
  987. * they will still be happy (this is only be a problem if a touch
  988. * was already there while probing the device).
  989. *
  990. * In addition some touchpads do not behave well if we read
  991. * all feature reports from them. Instead we prevent
  992. * initial report fetching and then selectively fetch each
  993. * report we are interested in.
  994. */
  995. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  996. ret = hid_parse(hdev);
  997. if (ret != 0)
  998. return ret;
  999. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  1000. if (ret)
  1001. return ret;
  1002. ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
  1003. if (ret)
  1004. dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
  1005. hdev->name);
  1006. mt_set_maxcontacts(hdev);
  1007. mt_set_input_mode(hdev);
  1008. /* release .fields memory as it is not used anymore */
  1009. devm_kfree(&hdev->dev, td->fields);
  1010. td->fields = NULL;
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_PM
  1014. static void mt_release_contacts(struct hid_device *hid)
  1015. {
  1016. struct hid_input *hidinput;
  1017. list_for_each_entry(hidinput, &hid->inputs, list) {
  1018. struct input_dev *input_dev = hidinput->input;
  1019. struct input_mt *mt = input_dev->mt;
  1020. int i;
  1021. if (mt) {
  1022. for (i = 0; i < mt->num_slots; i++) {
  1023. input_mt_slot(input_dev, i);
  1024. input_mt_report_slot_state(input_dev,
  1025. MT_TOOL_FINGER,
  1026. false);
  1027. }
  1028. input_mt_sync_frame(input_dev);
  1029. input_sync(input_dev);
  1030. }
  1031. }
  1032. }
  1033. static int mt_reset_resume(struct hid_device *hdev)
  1034. {
  1035. mt_release_contacts(hdev);
  1036. mt_set_maxcontacts(hdev);
  1037. mt_set_input_mode(hdev);
  1038. return 0;
  1039. }
  1040. static int mt_resume(struct hid_device *hdev)
  1041. {
  1042. /* Some Elan legacy devices require SET_IDLE to be set on resume.
  1043. * It should be safe to send it to other devices too.
  1044. * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
  1045. hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
  1046. return 0;
  1047. }
  1048. #endif
  1049. static void mt_remove(struct hid_device *hdev)
  1050. {
  1051. struct mt_device *td = hid_get_drvdata(hdev);
  1052. sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
  1053. hid_hw_stop(hdev);
  1054. hdev->quirks = td->initial_quirks;
  1055. }
  1056. /*
  1057. * This list contains only:
  1058. * - VID/PID of products not working with the default multitouch handling
  1059. * - 2 generic rules.
  1060. * So there is no point in adding here any device with MT_CLS_DEFAULT.
  1061. */
  1062. static const struct hid_device_id mt_devices[] = {
  1063. /* 3M panels */
  1064. { .driver_data = MT_CLS_3M,
  1065. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1066. USB_DEVICE_ID_3M1968) },
  1067. { .driver_data = MT_CLS_3M,
  1068. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1069. USB_DEVICE_ID_3M2256) },
  1070. { .driver_data = MT_CLS_3M,
  1071. MT_USB_DEVICE(USB_VENDOR_ID_3M,
  1072. USB_DEVICE_ID_3M3266) },
  1073. /* Anton devices */
  1074. { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
  1075. MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
  1076. USB_DEVICE_ID_ANTON_TOUCH_PAD) },
  1077. /* Atmel panels */
  1078. { .driver_data = MT_CLS_SERIAL,
  1079. MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
  1080. USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
  1081. /* Baanto multitouch devices */
  1082. { .driver_data = MT_CLS_NSMU,
  1083. MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
  1084. USB_DEVICE_ID_BAANTO_MT_190W2) },
  1085. /* Cando panels */
  1086. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  1087. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1088. USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
  1089. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
  1090. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1091. USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
  1092. /* Chunghwa Telecom touch panels */
  1093. { .driver_data = MT_CLS_NSMU,
  1094. MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
  1095. USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
  1096. /* CJTouch panels */
  1097. { .driver_data = MT_CLS_NSMU,
  1098. MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
  1099. USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
  1100. { .driver_data = MT_CLS_NSMU,
  1101. MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
  1102. USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
  1103. /* CVTouch panels */
  1104. { .driver_data = MT_CLS_NSMU,
  1105. MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
  1106. USB_DEVICE_ID_CVTOUCH_SCREEN) },
  1107. /* eGalax devices (resistive) */
  1108. { .driver_data = MT_CLS_EGALAX,
  1109. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1110. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
  1111. { .driver_data = MT_CLS_EGALAX,
  1112. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1113. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
  1114. /* eGalax devices (capacitive) */
  1115. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1116. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1117. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
  1118. { .driver_data = MT_CLS_EGALAX,
  1119. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1120. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
  1121. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1122. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1123. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
  1124. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1125. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1126. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
  1127. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1128. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1129. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
  1130. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1131. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1132. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
  1133. { .driver_data = MT_CLS_EGALAX,
  1134. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1135. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
  1136. { .driver_data = MT_CLS_EGALAX,
  1137. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1138. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
  1139. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1140. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1141. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
  1142. { .driver_data = MT_CLS_EGALAX,
  1143. HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1144. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
  1145. { .driver_data = MT_CLS_EGALAX,
  1146. HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1147. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
  1148. { .driver_data = MT_CLS_EGALAX,
  1149. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1150. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
  1151. { .driver_data = MT_CLS_EGALAX,
  1152. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1153. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
  1154. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1155. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1156. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
  1157. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1158. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1159. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
  1160. { .driver_data = MT_CLS_EGALAX_SERIAL,
  1161. MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
  1162. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
  1163. /* Elitegroup panel */
  1164. { .driver_data = MT_CLS_SERIAL,
  1165. MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
  1166. USB_DEVICE_ID_ELITEGROUP_05D8) },
  1167. /* Flatfrog Panels */
  1168. { .driver_data = MT_CLS_FLATFROG,
  1169. MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
  1170. USB_DEVICE_ID_MULTITOUCH_3200) },
  1171. /* FocalTech Panels */
  1172. { .driver_data = MT_CLS_SERIAL,
  1173. MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
  1174. USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
  1175. /* GeneralTouch panel */
  1176. { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
  1177. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1178. USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
  1179. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1180. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1181. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
  1182. { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
  1183. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1184. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
  1185. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1186. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1187. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
  1188. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1189. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1190. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
  1191. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1192. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1193. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
  1194. { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
  1195. MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  1196. USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
  1197. /* Gametel game controller */
  1198. { .driver_data = MT_CLS_NSMU,
  1199. MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
  1200. USB_DEVICE_ID_GAMETEL_MT_MODE) },
  1201. /* GoodTouch panels */
  1202. { .driver_data = MT_CLS_NSMU,
  1203. MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
  1204. USB_DEVICE_ID_GOODTOUCH_000f) },
  1205. /* Hanvon panels */
  1206. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  1207. MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
  1208. USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
  1209. /* Ilitek dual touch panel */
  1210. { .driver_data = MT_CLS_NSMU,
  1211. MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
  1212. USB_DEVICE_ID_ILITEK_MULTITOUCH) },
  1213. /* MosArt panels */
  1214. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1215. MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
  1216. USB_DEVICE_ID_ASUS_T91MT)},
  1217. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1218. MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
  1219. USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
  1220. { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
  1221. MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
  1222. USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
  1223. /* Panasonic panels */
  1224. { .driver_data = MT_CLS_PANASONIC,
  1225. MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
  1226. USB_DEVICE_ID_PANABOARD_UBT780) },
  1227. { .driver_data = MT_CLS_PANASONIC,
  1228. MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
  1229. USB_DEVICE_ID_PANABOARD_UBT880) },
  1230. /* Novatek Panel */
  1231. { .driver_data = MT_CLS_NSMU,
  1232. MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
  1233. USB_DEVICE_ID_NOVATEK_PCT) },
  1234. /* Ntrig Panel */
  1235. { .driver_data = MT_CLS_NSMU,
  1236. HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
  1237. USB_VENDOR_ID_NTRIG, 0x1b05) },
  1238. /* PixArt optical touch screen */
  1239. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1240. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1241. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
  1242. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1243. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1244. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
  1245. { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
  1246. MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
  1247. USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
  1248. /* PixCir-based panels */
  1249. { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
  1250. MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
  1251. USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
  1252. /* Quanta-based panels */
  1253. { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
  1254. MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
  1255. USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
  1256. /* Stantum panels */
  1257. { .driver_data = MT_CLS_CONFIDENCE,
  1258. MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
  1259. USB_DEVICE_ID_MTP_STM)},
  1260. /* TopSeed panels */
  1261. { .driver_data = MT_CLS_TOPSEED,
  1262. MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
  1263. USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
  1264. /* Touch International panels */
  1265. { .driver_data = MT_CLS_NSMU,
  1266. MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
  1267. USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
  1268. /* Unitec panels */
  1269. { .driver_data = MT_CLS_NSMU,
  1270. MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
  1271. USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
  1272. { .driver_data = MT_CLS_NSMU,
  1273. MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
  1274. USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
  1275. /* VTL panels */
  1276. { .driver_data = MT_CLS_VTL,
  1277. MT_USB_DEVICE(USB_VENDOR_ID_VTL,
  1278. USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
  1279. /* Wistron panels */
  1280. { .driver_data = MT_CLS_NSMU,
  1281. MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
  1282. USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
  1283. /* XAT */
  1284. { .driver_data = MT_CLS_NSMU,
  1285. MT_USB_DEVICE(USB_VENDOR_ID_XAT,
  1286. USB_DEVICE_ID_XAT_CSR) },
  1287. /* Xiroku */
  1288. { .driver_data = MT_CLS_NSMU,
  1289. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1290. USB_DEVICE_ID_XIROKU_SPX) },
  1291. { .driver_data = MT_CLS_NSMU,
  1292. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1293. USB_DEVICE_ID_XIROKU_MPX) },
  1294. { .driver_data = MT_CLS_NSMU,
  1295. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1296. USB_DEVICE_ID_XIROKU_CSR) },
  1297. { .driver_data = MT_CLS_NSMU,
  1298. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1299. USB_DEVICE_ID_XIROKU_SPX1) },
  1300. { .driver_data = MT_CLS_NSMU,
  1301. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1302. USB_DEVICE_ID_XIROKU_MPX1) },
  1303. { .driver_data = MT_CLS_NSMU,
  1304. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1305. USB_DEVICE_ID_XIROKU_CSR1) },
  1306. { .driver_data = MT_CLS_NSMU,
  1307. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1308. USB_DEVICE_ID_XIROKU_SPX2) },
  1309. { .driver_data = MT_CLS_NSMU,
  1310. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1311. USB_DEVICE_ID_XIROKU_MPX2) },
  1312. { .driver_data = MT_CLS_NSMU,
  1313. MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
  1314. USB_DEVICE_ID_XIROKU_CSR2) },
  1315. /* Generic MT device */
  1316. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
  1317. /* Generic Win 8 certified MT device */
  1318. { .driver_data = MT_CLS_WIN_8,
  1319. HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
  1320. HID_ANY_ID, HID_ANY_ID) },
  1321. { }
  1322. };
  1323. MODULE_DEVICE_TABLE(hid, mt_devices);
  1324. static const struct hid_usage_id mt_grabbed_usages[] = {
  1325. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  1326. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  1327. };
  1328. static struct hid_driver mt_driver = {
  1329. .name = "hid-multitouch",
  1330. .id_table = mt_devices,
  1331. .probe = mt_probe,
  1332. .remove = mt_remove,
  1333. .input_mapping = mt_input_mapping,
  1334. .input_mapped = mt_input_mapped,
  1335. .input_configured = mt_input_configured,
  1336. .feature_mapping = mt_feature_mapping,
  1337. .usage_table = mt_grabbed_usages,
  1338. .event = mt_event,
  1339. .report = mt_report,
  1340. #ifdef CONFIG_PM
  1341. .reset_resume = mt_reset_resume,
  1342. .resume = mt_resume,
  1343. #endif
  1344. };
  1345. module_hid_driver(mt_driver);