hid-multitouch.c 44 KB

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