hid-multitouch.c 43 KB

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