hid-input.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2000-2001 Vojtech Pavlik
  4. * Copyright (c) 2006-2010 Jiri Kosina
  5. *
  6. * HID to Linux Input mapping
  7. */
  8. /*
  9. *
  10. * Should you need to contact me, the author, you can do so either by
  11. * e-mail - mail your message to <[email protected]>, or by paper mail:
  12. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/hid.h>
  18. #include <linux/hid-debug.h>
  19. #include "hid-ids.h"
  20. #define unk KEY_UNKNOWN
  21. static const unsigned char hid_keyboard[256] = {
  22. 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
  23. 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
  24. 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
  25. 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
  26. 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
  27. 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
  28. 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
  29. 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
  30. 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
  31. 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
  32. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  33. unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
  34. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  35. unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
  36. 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
  37. 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
  38. };
  39. static const struct {
  40. __s32 x;
  41. __s32 y;
  42. } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  43. struct usage_priority {
  44. __u32 usage; /* the HID usage associated */
  45. bool global; /* we assume all usages to be slotted,
  46. * unless global
  47. */
  48. unsigned int slot_overwrite; /* for globals: allows to set the usage
  49. * before or after the slots
  50. */
  51. };
  52. /*
  53. * hid-input will convert this list into priorities:
  54. * the first element will have the highest priority
  55. * (the length of the following array) and the last
  56. * element the lowest (1).
  57. *
  58. * hid-input will then shift the priority by 8 bits to leave some space
  59. * in case drivers want to interleave other fields.
  60. *
  61. * To accommodate slotted devices, the slot priority is
  62. * defined in the next 8 bits (defined by 0xff - slot).
  63. *
  64. * If drivers want to add fields before those, hid-input will
  65. * leave out the first 8 bits of the priority value.
  66. *
  67. * This still leaves us 65535 individual priority values.
  68. */
  69. static const struct usage_priority hidinput_usages_priorities[] = {
  70. { /* Eraser (eraser touching) must always come before tipswitch */
  71. .usage = HID_DG_ERASER,
  72. },
  73. { /* Invert must always come before In Range */
  74. .usage = HID_DG_INVERT,
  75. },
  76. { /* Is the tip of the tool touching? */
  77. .usage = HID_DG_TIPSWITCH,
  78. },
  79. { /* Tip Pressure might emulate tip switch */
  80. .usage = HID_DG_TIPPRESSURE,
  81. },
  82. { /* In Range needs to come after the other tool states */
  83. .usage = HID_DG_INRANGE,
  84. },
  85. };
  86. #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
  87. #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
  88. #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
  89. #define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
  90. #define map_msc(c) hid_map_usage(hidinput, usage, &bit, &max, EV_MSC, (c))
  91. #define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  92. &max, EV_ABS, (c))
  93. #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  94. &max, EV_KEY, (c))
  95. static bool match_scancode(struct hid_usage *usage,
  96. unsigned int cur_idx, unsigned int scancode)
  97. {
  98. return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
  99. }
  100. static bool match_keycode(struct hid_usage *usage,
  101. unsigned int cur_idx, unsigned int keycode)
  102. {
  103. /*
  104. * We should exclude unmapped usages when doing lookup by keycode.
  105. */
  106. return (usage->type == EV_KEY && usage->code == keycode);
  107. }
  108. static bool match_index(struct hid_usage *usage,
  109. unsigned int cur_idx, unsigned int idx)
  110. {
  111. return cur_idx == idx;
  112. }
  113. typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
  114. unsigned int cur_idx, unsigned int val);
  115. static struct hid_usage *hidinput_find_key(struct hid_device *hid,
  116. hid_usage_cmp_t match,
  117. unsigned int value,
  118. unsigned int *usage_idx)
  119. {
  120. unsigned int i, j, k, cur_idx = 0;
  121. struct hid_report *report;
  122. struct hid_usage *usage;
  123. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  124. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  125. for (i = 0; i < report->maxfield; i++) {
  126. for (j = 0; j < report->field[i]->maxusage; j++) {
  127. usage = report->field[i]->usage + j;
  128. if (usage->type == EV_KEY || usage->type == 0) {
  129. if (match(usage, cur_idx, value)) {
  130. if (usage_idx)
  131. *usage_idx = cur_idx;
  132. return usage;
  133. }
  134. cur_idx++;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return NULL;
  141. }
  142. static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
  143. const struct input_keymap_entry *ke,
  144. unsigned int *index)
  145. {
  146. struct hid_usage *usage;
  147. unsigned int scancode;
  148. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  149. usage = hidinput_find_key(hid, match_index, ke->index, index);
  150. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  151. usage = hidinput_find_key(hid, match_scancode, scancode, index);
  152. else
  153. usage = NULL;
  154. return usage;
  155. }
  156. static int hidinput_getkeycode(struct input_dev *dev,
  157. struct input_keymap_entry *ke)
  158. {
  159. struct hid_device *hid = input_get_drvdata(dev);
  160. struct hid_usage *usage;
  161. unsigned int scancode, index;
  162. usage = hidinput_locate_usage(hid, ke, &index);
  163. if (usage) {
  164. ke->keycode = usage->type == EV_KEY ?
  165. usage->code : KEY_RESERVED;
  166. ke->index = index;
  167. scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
  168. ke->len = sizeof(scancode);
  169. memcpy(ke->scancode, &scancode, sizeof(scancode));
  170. return 0;
  171. }
  172. return -EINVAL;
  173. }
  174. static int hidinput_setkeycode(struct input_dev *dev,
  175. const struct input_keymap_entry *ke,
  176. unsigned int *old_keycode)
  177. {
  178. struct hid_device *hid = input_get_drvdata(dev);
  179. struct hid_usage *usage;
  180. usage = hidinput_locate_usage(hid, ke, NULL);
  181. if (usage) {
  182. *old_keycode = usage->type == EV_KEY ?
  183. usage->code : KEY_RESERVED;
  184. usage->type = EV_KEY;
  185. usage->code = ke->keycode;
  186. if (usage->code > KEY_MAX || *old_keycode > KEY_MAX)
  187. return -EINVAL;
  188. clear_bit(*old_keycode, dev->keybit);
  189. set_bit(usage->code, dev->keybit);
  190. dbg_hid("Assigned keycode %d to HID usage code %x\n",
  191. usage->code, usage->hid);
  192. /*
  193. * Set the keybit for the old keycode if the old keycode is used
  194. * by another key
  195. */
  196. if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
  197. set_bit(*old_keycode, dev->keybit);
  198. return 0;
  199. }
  200. return -EINVAL;
  201. }
  202. /**
  203. * hidinput_calc_abs_res - calculate an absolute axis resolution
  204. * @field: the HID report field to calculate resolution for
  205. * @code: axis code
  206. *
  207. * The formula is:
  208. * (logical_maximum - logical_minimum)
  209. * resolution = ----------------------------------------------------------
  210. * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
  211. *
  212. * as seen in the HID specification v1.11 6.2.2.7 Global Items.
  213. *
  214. * Only exponent 1 length units are processed. Centimeters and inches are
  215. * converted to millimeters. Degrees are converted to radians.
  216. */
  217. __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
  218. {
  219. __s32 unit_exponent = field->unit_exponent;
  220. __s32 logical_extents = field->logical_maximum -
  221. field->logical_minimum;
  222. __s32 physical_extents = field->physical_maximum -
  223. field->physical_minimum;
  224. __s32 prev;
  225. /* Check if the extents are sane */
  226. if (logical_extents <= 0 || physical_extents <= 0)
  227. return 0;
  228. /*
  229. * Verify and convert units.
  230. * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
  231. */
  232. switch (code) {
  233. case ABS_X:
  234. case ABS_Y:
  235. case ABS_Z:
  236. case ABS_MT_POSITION_X:
  237. case ABS_MT_POSITION_Y:
  238. case ABS_MT_TOOL_X:
  239. case ABS_MT_TOOL_Y:
  240. case ABS_MT_TOUCH_MAJOR:
  241. case ABS_MT_TOUCH_MINOR:
  242. if (field->unit == 0x11) { /* If centimeters */
  243. /* Convert to millimeters */
  244. unit_exponent += 1;
  245. } else if (field->unit == 0x13) { /* If inches */
  246. /* Convert to millimeters */
  247. prev = physical_extents;
  248. physical_extents *= 254;
  249. if (physical_extents < prev)
  250. return 0;
  251. unit_exponent -= 1;
  252. } else {
  253. return 0;
  254. }
  255. break;
  256. case ABS_RX:
  257. case ABS_RY:
  258. case ABS_RZ:
  259. case ABS_WHEEL:
  260. case ABS_TILT_X:
  261. case ABS_TILT_Y:
  262. if (field->unit == 0x14) { /* If degrees */
  263. /* Convert to radians */
  264. prev = logical_extents;
  265. logical_extents *= 573;
  266. if (logical_extents < prev)
  267. return 0;
  268. unit_exponent += 1;
  269. } else if (field->unit != 0x12) { /* If not radians */
  270. return 0;
  271. }
  272. break;
  273. default:
  274. return 0;
  275. }
  276. /* Apply negative unit exponent */
  277. for (; unit_exponent < 0; unit_exponent++) {
  278. prev = logical_extents;
  279. logical_extents *= 10;
  280. if (logical_extents < prev)
  281. return 0;
  282. }
  283. /* Apply positive unit exponent */
  284. for (; unit_exponent > 0; unit_exponent--) {
  285. prev = physical_extents;
  286. physical_extents *= 10;
  287. if (physical_extents < prev)
  288. return 0;
  289. }
  290. /* Calculate resolution */
  291. return DIV_ROUND_CLOSEST(logical_extents, physical_extents);
  292. }
  293. EXPORT_SYMBOL_GPL(hidinput_calc_abs_res);
  294. #ifdef CONFIG_HID_BATTERY_STRENGTH
  295. static enum power_supply_property hidinput_battery_props[] = {
  296. POWER_SUPPLY_PROP_PRESENT,
  297. POWER_SUPPLY_PROP_ONLINE,
  298. POWER_SUPPLY_PROP_CAPACITY,
  299. POWER_SUPPLY_PROP_MODEL_NAME,
  300. POWER_SUPPLY_PROP_STATUS,
  301. POWER_SUPPLY_PROP_SCOPE,
  302. };
  303. #define HID_BATTERY_QUIRK_PERCENT (1 << 0) /* always reports percent */
  304. #define HID_BATTERY_QUIRK_FEATURE (1 << 1) /* ask for feature report */
  305. #define HID_BATTERY_QUIRK_IGNORE (1 << 2) /* completely ignore the battery */
  306. #define HID_BATTERY_QUIRK_AVOID_QUERY (1 << 3) /* do not query the battery */
  307. static const struct hid_device_id hid_battery_quirks[] = {
  308. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  309. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
  310. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  311. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  312. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
  313. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  314. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  315. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
  316. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  317. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  318. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
  319. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  320. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  321. USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
  322. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  323. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM,
  324. USB_DEVICE_ID_ELECOM_BM084),
  325. HID_BATTERY_QUIRK_IGNORE },
  326. { HID_USB_DEVICE(USB_VENDOR_ID_SYMBOL,
  327. USB_DEVICE_ID_SYMBOL_SCANNER_3),
  328. HID_BATTERY_QUIRK_IGNORE },
  329. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
  330. USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD),
  331. HID_BATTERY_QUIRK_IGNORE },
  332. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
  333. USB_DEVICE_ID_LOGITECH_DINOVO_EDGE_KBD),
  334. HID_BATTERY_QUIRK_IGNORE },
  335. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_ASUS_TP420IA_TOUCHSCREEN),
  336. HID_BATTERY_QUIRK_IGNORE },
  337. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_ASUS_GV301RA_TOUCHSCREEN),
  338. HID_BATTERY_QUIRK_IGNORE },
  339. { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_ASUS_UX550_TOUCHSCREEN),
  340. HID_BATTERY_QUIRK_IGNORE },
  341. { HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_ASUS_UX550VE_TOUCHSCREEN),
  342. HID_BATTERY_QUIRK_IGNORE },
  343. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L),
  344. HID_BATTERY_QUIRK_AVOID_QUERY },
  345. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
  346. HID_BATTERY_QUIRK_AVOID_QUERY },
  347. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
  348. HID_BATTERY_QUIRK_AVOID_QUERY },
  349. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_ENVY_X360_15),
  350. HID_BATTERY_QUIRK_IGNORE },
  351. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_ENVY_X360_15T_DR100),
  352. HID_BATTERY_QUIRK_IGNORE },
  353. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_ENVY_X360_EU0009NV),
  354. HID_BATTERY_QUIRK_IGNORE },
  355. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_SPECTRE_X360_15),
  356. HID_BATTERY_QUIRK_IGNORE },
  357. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_HP_SPECTRE_X360_13_AW0020NG),
  358. HID_BATTERY_QUIRK_IGNORE },
  359. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_SURFACE_GO_TOUCHSCREEN),
  360. HID_BATTERY_QUIRK_IGNORE },
  361. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_SURFACE_GO2_TOUCHSCREEN),
  362. HID_BATTERY_QUIRK_IGNORE },
  363. { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, I2C_DEVICE_ID_LENOVO_YOGA_C630_TOUCHSCREEN),
  364. HID_BATTERY_QUIRK_IGNORE },
  365. {}
  366. };
  367. static unsigned find_battery_quirk(struct hid_device *hdev)
  368. {
  369. unsigned quirks = 0;
  370. const struct hid_device_id *match;
  371. match = hid_match_id(hdev, hid_battery_quirks);
  372. if (match != NULL)
  373. quirks = match->driver_data;
  374. return quirks;
  375. }
  376. static int hidinput_scale_battery_capacity(struct hid_device *dev,
  377. int value)
  378. {
  379. if (dev->battery_min < dev->battery_max &&
  380. value >= dev->battery_min && value <= dev->battery_max)
  381. value = ((value - dev->battery_min) * 100) /
  382. (dev->battery_max - dev->battery_min);
  383. return value;
  384. }
  385. static int hidinput_query_battery_capacity(struct hid_device *dev)
  386. {
  387. u8 *buf;
  388. int ret;
  389. buf = kmalloc(4, GFP_KERNEL);
  390. if (!buf)
  391. return -ENOMEM;
  392. ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 4,
  393. dev->battery_report_type, HID_REQ_GET_REPORT);
  394. if (ret < 2) {
  395. kfree(buf);
  396. return -ENODATA;
  397. }
  398. ret = hidinput_scale_battery_capacity(dev, buf[1]);
  399. kfree(buf);
  400. return ret;
  401. }
  402. static int hidinput_get_battery_property(struct power_supply *psy,
  403. enum power_supply_property prop,
  404. union power_supply_propval *val)
  405. {
  406. struct hid_device *dev = power_supply_get_drvdata(psy);
  407. int value;
  408. int ret = 0;
  409. switch (prop) {
  410. case POWER_SUPPLY_PROP_PRESENT:
  411. case POWER_SUPPLY_PROP_ONLINE:
  412. val->intval = 1;
  413. break;
  414. case POWER_SUPPLY_PROP_CAPACITY:
  415. if (dev->battery_status != HID_BATTERY_REPORTED &&
  416. !dev->battery_avoid_query) {
  417. value = hidinput_query_battery_capacity(dev);
  418. if (value < 0)
  419. return value;
  420. } else {
  421. value = dev->battery_capacity;
  422. }
  423. val->intval = value;
  424. break;
  425. case POWER_SUPPLY_PROP_MODEL_NAME:
  426. val->strval = dev->name;
  427. break;
  428. case POWER_SUPPLY_PROP_STATUS:
  429. if (dev->battery_status != HID_BATTERY_REPORTED &&
  430. !dev->battery_avoid_query) {
  431. value = hidinput_query_battery_capacity(dev);
  432. if (value < 0)
  433. return value;
  434. dev->battery_capacity = value;
  435. dev->battery_status = HID_BATTERY_QUERIED;
  436. }
  437. if (dev->battery_status == HID_BATTERY_UNKNOWN)
  438. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  439. else
  440. val->intval = dev->battery_charge_status;
  441. break;
  442. case POWER_SUPPLY_PROP_SCOPE:
  443. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  444. break;
  445. default:
  446. ret = -EINVAL;
  447. break;
  448. }
  449. return ret;
  450. }
  451. static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
  452. struct hid_field *field, bool is_percentage)
  453. {
  454. struct power_supply_desc *psy_desc;
  455. struct power_supply_config psy_cfg = { .drv_data = dev, };
  456. unsigned quirks;
  457. s32 min, max;
  458. int error;
  459. if (dev->battery)
  460. return 0; /* already initialized? */
  461. quirks = find_battery_quirk(dev);
  462. hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
  463. dev->bus, dev->vendor, dev->product, dev->version, quirks);
  464. if (quirks & HID_BATTERY_QUIRK_IGNORE)
  465. return 0;
  466. psy_desc = kzalloc(sizeof(*psy_desc), GFP_KERNEL);
  467. if (!psy_desc)
  468. return -ENOMEM;
  469. psy_desc->name = kasprintf(GFP_KERNEL, "hid-%s-battery",
  470. strlen(dev->uniq) ?
  471. dev->uniq : dev_name(&dev->dev));
  472. if (!psy_desc->name) {
  473. error = -ENOMEM;
  474. goto err_free_mem;
  475. }
  476. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  477. psy_desc->properties = hidinput_battery_props;
  478. psy_desc->num_properties = ARRAY_SIZE(hidinput_battery_props);
  479. psy_desc->use_for_apm = 0;
  480. psy_desc->get_property = hidinput_get_battery_property;
  481. min = field->logical_minimum;
  482. max = field->logical_maximum;
  483. if (is_percentage || (quirks & HID_BATTERY_QUIRK_PERCENT)) {
  484. min = 0;
  485. max = 100;
  486. }
  487. if (quirks & HID_BATTERY_QUIRK_FEATURE)
  488. report_type = HID_FEATURE_REPORT;
  489. dev->battery_min = min;
  490. dev->battery_max = max;
  491. dev->battery_report_type = report_type;
  492. dev->battery_report_id = field->report->id;
  493. dev->battery_charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  494. /*
  495. * Stylus is normally not connected to the device and thus we
  496. * can't query the device and get meaningful battery strength.
  497. * We have to wait for the device to report it on its own.
  498. */
  499. dev->battery_avoid_query = report_type == HID_INPUT_REPORT &&
  500. field->physical == HID_DG_STYLUS;
  501. if (quirks & HID_BATTERY_QUIRK_AVOID_QUERY)
  502. dev->battery_avoid_query = true;
  503. dev->battery = power_supply_register(&dev->dev, psy_desc, &psy_cfg);
  504. if (IS_ERR(dev->battery)) {
  505. error = PTR_ERR(dev->battery);
  506. hid_warn(dev, "can't register power supply: %d\n", error);
  507. goto err_free_name;
  508. }
  509. power_supply_powers(dev->battery, &dev->dev);
  510. return 0;
  511. err_free_name:
  512. kfree(psy_desc->name);
  513. err_free_mem:
  514. kfree(psy_desc);
  515. dev->battery = NULL;
  516. return error;
  517. }
  518. static void hidinput_cleanup_battery(struct hid_device *dev)
  519. {
  520. const struct power_supply_desc *psy_desc;
  521. if (!dev->battery)
  522. return;
  523. psy_desc = dev->battery->desc;
  524. power_supply_unregister(dev->battery);
  525. kfree(psy_desc->name);
  526. kfree(psy_desc);
  527. dev->battery = NULL;
  528. }
  529. static void hidinput_update_battery(struct hid_device *dev, int value)
  530. {
  531. int capacity;
  532. if (!dev->battery)
  533. return;
  534. if (value == 0 || value < dev->battery_min || value > dev->battery_max)
  535. return;
  536. capacity = hidinput_scale_battery_capacity(dev, value);
  537. if (dev->battery_status != HID_BATTERY_REPORTED ||
  538. capacity != dev->battery_capacity ||
  539. ktime_after(ktime_get_coarse(), dev->battery_ratelimit_time)) {
  540. dev->battery_capacity = capacity;
  541. dev->battery_status = HID_BATTERY_REPORTED;
  542. dev->battery_ratelimit_time =
  543. ktime_add_ms(ktime_get_coarse(), 30 * 1000);
  544. power_supply_changed(dev->battery);
  545. }
  546. }
  547. static bool hidinput_set_battery_charge_status(struct hid_device *dev,
  548. unsigned int usage, int value)
  549. {
  550. switch (usage) {
  551. case HID_BAT_CHARGING:
  552. dev->battery_charge_status = value ?
  553. POWER_SUPPLY_STATUS_CHARGING :
  554. POWER_SUPPLY_STATUS_DISCHARGING;
  555. power_supply_changed(dev->battery);
  556. return true;
  557. }
  558. return false;
  559. }
  560. #else /* !CONFIG_HID_BATTERY_STRENGTH */
  561. static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
  562. struct hid_field *field, bool is_percentage)
  563. {
  564. return 0;
  565. }
  566. static void hidinput_cleanup_battery(struct hid_device *dev)
  567. {
  568. }
  569. static void hidinput_update_battery(struct hid_device *dev, int value)
  570. {
  571. }
  572. static bool hidinput_set_battery_charge_status(struct hid_device *dev,
  573. unsigned int usage, int value)
  574. {
  575. return false;
  576. }
  577. #endif /* CONFIG_HID_BATTERY_STRENGTH */
  578. static bool hidinput_field_in_collection(struct hid_device *device, struct hid_field *field,
  579. unsigned int type, unsigned int usage)
  580. {
  581. struct hid_collection *collection;
  582. collection = &device->collection[field->usage->collection_index];
  583. return collection->type == type && collection->usage == usage;
  584. }
  585. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  586. struct hid_usage *usage, unsigned int usage_index)
  587. {
  588. struct input_dev *input = hidinput->input;
  589. struct hid_device *device = input_get_drvdata(input);
  590. const struct usage_priority *usage_priority = NULL;
  591. int max = 0, code;
  592. unsigned int i = 0;
  593. unsigned long *bit = NULL;
  594. field->hidinput = hidinput;
  595. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  596. goto ignore;
  597. /* Ignore if report count is out of bounds. */
  598. if (field->report_count < 1)
  599. goto ignore;
  600. /* only LED usages are supported in output fields */
  601. if (field->report_type == HID_OUTPUT_REPORT &&
  602. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  603. goto ignore;
  604. }
  605. /* assign a priority based on the static list declared here */
  606. for (i = 0; i < ARRAY_SIZE(hidinput_usages_priorities); i++) {
  607. if (usage->hid == hidinput_usages_priorities[i].usage) {
  608. usage_priority = &hidinput_usages_priorities[i];
  609. field->usages_priorities[usage_index] =
  610. (ARRAY_SIZE(hidinput_usages_priorities) - i) << 8;
  611. break;
  612. }
  613. }
  614. /*
  615. * For slotted devices, we need to also add the slot index
  616. * in the priority.
  617. */
  618. if (usage_priority && usage_priority->global)
  619. field->usages_priorities[usage_index] |=
  620. usage_priority->slot_overwrite;
  621. else
  622. field->usages_priorities[usage_index] |=
  623. (0xff - field->slot_idx) << 16;
  624. if (device->driver->input_mapping) {
  625. int ret = device->driver->input_mapping(device, hidinput, field,
  626. usage, &bit, &max);
  627. if (ret > 0)
  628. goto mapped;
  629. if (ret < 0)
  630. goto ignore;
  631. }
  632. switch (usage->hid & HID_USAGE_PAGE) {
  633. case HID_UP_UNDEFINED:
  634. goto ignore;
  635. case HID_UP_KEYBOARD:
  636. set_bit(EV_REP, input->evbit);
  637. if ((usage->hid & HID_USAGE) < 256) {
  638. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  639. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  640. } else
  641. map_key(KEY_UNKNOWN);
  642. break;
  643. case HID_UP_BUTTON:
  644. code = ((usage->hid - 1) & HID_USAGE);
  645. switch (field->application) {
  646. case HID_GD_MOUSE:
  647. case HID_GD_POINTER: code += BTN_MOUSE; break;
  648. case HID_GD_JOYSTICK:
  649. if (code <= 0xf)
  650. code += BTN_JOYSTICK;
  651. else
  652. code += BTN_TRIGGER_HAPPY - 0x10;
  653. break;
  654. case HID_GD_GAMEPAD:
  655. if (code <= 0xf)
  656. code += BTN_GAMEPAD;
  657. else
  658. code += BTN_TRIGGER_HAPPY - 0x10;
  659. break;
  660. case HID_CP_CONSUMER_CONTROL:
  661. if (hidinput_field_in_collection(device, field,
  662. HID_COLLECTION_NAMED_ARRAY,
  663. HID_CP_PROGRAMMABLEBUTTONS)) {
  664. if (code <= 0x1d)
  665. code += KEY_MACRO1;
  666. else
  667. code += BTN_TRIGGER_HAPPY - 0x1e;
  668. break;
  669. }
  670. fallthrough;
  671. default:
  672. switch (field->physical) {
  673. case HID_GD_MOUSE:
  674. case HID_GD_POINTER: code += BTN_MOUSE; break;
  675. case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
  676. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  677. default: code += BTN_MISC;
  678. }
  679. }
  680. map_key(code);
  681. break;
  682. case HID_UP_SIMULATION:
  683. switch (usage->hid & 0xffff) {
  684. case 0xba: map_abs(ABS_RUDDER); break;
  685. case 0xbb: map_abs(ABS_THROTTLE); break;
  686. case 0xc4: map_abs(ABS_GAS); break;
  687. case 0xc5: map_abs(ABS_BRAKE); break;
  688. case 0xc8: map_abs(ABS_WHEEL); break;
  689. default: goto ignore;
  690. }
  691. break;
  692. case HID_UP_GENDESK:
  693. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  694. switch (usage->hid & 0xf) {
  695. case 0x1: map_key_clear(KEY_POWER); break;
  696. case 0x2: map_key_clear(KEY_SLEEP); break;
  697. case 0x3: map_key_clear(KEY_WAKEUP); break;
  698. case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
  699. case 0x5: map_key_clear(KEY_MENU); break;
  700. case 0x6: map_key_clear(KEY_PROG1); break;
  701. case 0x7: map_key_clear(KEY_HELP); break;
  702. case 0x8: map_key_clear(KEY_EXIT); break;
  703. case 0x9: map_key_clear(KEY_SELECT); break;
  704. case 0xa: map_key_clear(KEY_RIGHT); break;
  705. case 0xb: map_key_clear(KEY_LEFT); break;
  706. case 0xc: map_key_clear(KEY_UP); break;
  707. case 0xd: map_key_clear(KEY_DOWN); break;
  708. case 0xe: map_key_clear(KEY_POWER2); break;
  709. case 0xf: map_key_clear(KEY_RESTART); break;
  710. default: goto unknown;
  711. }
  712. break;
  713. }
  714. if ((usage->hid & 0xf0) == 0xa0) { /* SystemControl */
  715. switch (usage->hid & 0xf) {
  716. case 0x9: map_key_clear(KEY_MICMUTE); break;
  717. default: goto ignore;
  718. }
  719. break;
  720. }
  721. if ((usage->hid & 0xf0) == 0xb0) { /* SC - Display */
  722. switch (usage->hid & 0xf) {
  723. case 0x05: map_key_clear(KEY_SWITCHVIDEOMODE); break;
  724. default: goto ignore;
  725. }
  726. break;
  727. }
  728. /*
  729. * Some lazy vendors declare 255 usages for System Control,
  730. * leading to the creation of ABS_X|Y axis and too many others.
  731. * It wouldn't be a problem if joydev doesn't consider the
  732. * device as a joystick then.
  733. */
  734. if (field->application == HID_GD_SYSTEM_CONTROL)
  735. goto ignore;
  736. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  737. switch (usage->hid) {
  738. case HID_GD_UP: usage->hat_dir = 1; break;
  739. case HID_GD_DOWN: usage->hat_dir = 5; break;
  740. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  741. case HID_GD_LEFT: usage->hat_dir = 7; break;
  742. default: goto unknown;
  743. }
  744. if (field->dpad) {
  745. map_abs(field->dpad);
  746. goto ignore;
  747. }
  748. map_abs(ABS_HAT0X);
  749. break;
  750. }
  751. switch (usage->hid) {
  752. /* These usage IDs map directly to the usage codes. */
  753. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  754. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  755. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  756. map_rel(usage->hid & 0xf);
  757. else
  758. map_abs_clear(usage->hid & 0xf);
  759. break;
  760. case HID_GD_WHEEL:
  761. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  762. set_bit(REL_WHEEL, input->relbit);
  763. map_rel(REL_WHEEL_HI_RES);
  764. } else {
  765. map_abs(usage->hid & 0xf);
  766. }
  767. break;
  768. case HID_GD_SLIDER: case HID_GD_DIAL:
  769. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  770. map_rel(usage->hid & 0xf);
  771. else
  772. map_abs(usage->hid & 0xf);
  773. break;
  774. case HID_GD_HATSWITCH:
  775. usage->hat_min = field->logical_minimum;
  776. usage->hat_max = field->logical_maximum;
  777. map_abs(ABS_HAT0X);
  778. break;
  779. case HID_GD_START: map_key_clear(BTN_START); break;
  780. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  781. case HID_GD_RFKILL_BTN:
  782. /* MS wireless radio ctl extension, also check CA */
  783. if (field->application == HID_GD_WIRELESS_RADIO_CTLS) {
  784. map_key_clear(KEY_RFKILL);
  785. /* We need to simulate the btn release */
  786. field->flags |= HID_MAIN_ITEM_RELATIVE;
  787. break;
  788. }
  789. goto unknown;
  790. default: goto unknown;
  791. }
  792. break;
  793. case HID_UP_LED:
  794. switch (usage->hid & 0xffff) { /* HID-Value: */
  795. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  796. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  797. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  798. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  799. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  800. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  801. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  802. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  803. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  804. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  805. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  806. default: goto ignore;
  807. }
  808. break;
  809. case HID_UP_DIGITIZER:
  810. if ((field->application & 0xff) == 0x01) /* Digitizer */
  811. __set_bit(INPUT_PROP_POINTER, input->propbit);
  812. else if ((field->application & 0xff) == 0x02) /* Pen */
  813. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  814. switch (usage->hid & 0xff) {
  815. case 0x00: /* Undefined */
  816. goto ignore;
  817. case 0x30: /* TipPressure */
  818. if (!test_bit(BTN_TOUCH, input->keybit)) {
  819. device->quirks |= HID_QUIRK_NOTOUCH;
  820. set_bit(EV_KEY, input->evbit);
  821. set_bit(BTN_TOUCH, input->keybit);
  822. }
  823. map_abs_clear(ABS_PRESSURE);
  824. break;
  825. case 0x32: /* InRange */
  826. switch (field->physical) {
  827. case HID_DG_PUCK:
  828. map_key(BTN_TOOL_MOUSE);
  829. break;
  830. case HID_DG_FINGER:
  831. map_key(BTN_TOOL_FINGER);
  832. break;
  833. default:
  834. /*
  835. * If the physical is not given,
  836. * rely on the application.
  837. */
  838. if (!field->physical) {
  839. switch (field->application) {
  840. case HID_DG_TOUCHSCREEN:
  841. case HID_DG_TOUCHPAD:
  842. map_key_clear(BTN_TOOL_FINGER);
  843. break;
  844. default:
  845. map_key_clear(BTN_TOOL_PEN);
  846. }
  847. } else {
  848. map_key(BTN_TOOL_PEN);
  849. }
  850. break;
  851. }
  852. break;
  853. case 0x3b: /* Battery Strength */
  854. hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
  855. usage->type = EV_PWR;
  856. return;
  857. case 0x3c: /* Invert */
  858. device->quirks &= ~HID_QUIRK_NOINVERT;
  859. map_key_clear(BTN_TOOL_RUBBER);
  860. break;
  861. case 0x3d: /* X Tilt */
  862. map_abs_clear(ABS_TILT_X);
  863. break;
  864. case 0x3e: /* Y Tilt */
  865. map_abs_clear(ABS_TILT_Y);
  866. break;
  867. case 0x33: /* Touch */
  868. case 0x42: /* TipSwitch */
  869. case 0x43: /* TipSwitch2 */
  870. device->quirks &= ~HID_QUIRK_NOTOUCH;
  871. map_key_clear(BTN_TOUCH);
  872. break;
  873. case 0x44: /* BarrelSwitch */
  874. map_key_clear(BTN_STYLUS);
  875. break;
  876. case 0x45: /* ERASER */
  877. /*
  878. * This event is reported when eraser tip touches the surface.
  879. * Actual eraser (BTN_TOOL_RUBBER) is set and released either
  880. * by Invert if tool reports proximity or by Eraser directly.
  881. */
  882. if (!test_bit(BTN_TOOL_RUBBER, input->keybit)) {
  883. device->quirks |= HID_QUIRK_NOINVERT;
  884. set_bit(BTN_TOOL_RUBBER, input->keybit);
  885. }
  886. map_key_clear(BTN_TOUCH);
  887. break;
  888. case 0x46: /* TabletPick */
  889. case 0x5a: /* SecondaryBarrelSwitch */
  890. map_key_clear(BTN_STYLUS2);
  891. break;
  892. case 0x5b: /* TransducerSerialNumber */
  893. case 0x6e: /* TransducerSerialNumber2 */
  894. map_msc(MSC_SERIAL);
  895. break;
  896. default: goto unknown;
  897. }
  898. break;
  899. case HID_UP_TELEPHONY:
  900. switch (usage->hid & HID_USAGE) {
  901. case 0x2f: map_key_clear(KEY_MICMUTE); break;
  902. case 0xb0: map_key_clear(KEY_NUMERIC_0); break;
  903. case 0xb1: map_key_clear(KEY_NUMERIC_1); break;
  904. case 0xb2: map_key_clear(KEY_NUMERIC_2); break;
  905. case 0xb3: map_key_clear(KEY_NUMERIC_3); break;
  906. case 0xb4: map_key_clear(KEY_NUMERIC_4); break;
  907. case 0xb5: map_key_clear(KEY_NUMERIC_5); break;
  908. case 0xb6: map_key_clear(KEY_NUMERIC_6); break;
  909. case 0xb7: map_key_clear(KEY_NUMERIC_7); break;
  910. case 0xb8: map_key_clear(KEY_NUMERIC_8); break;
  911. case 0xb9: map_key_clear(KEY_NUMERIC_9); break;
  912. case 0xba: map_key_clear(KEY_NUMERIC_STAR); break;
  913. case 0xbb: map_key_clear(KEY_NUMERIC_POUND); break;
  914. case 0xbc: map_key_clear(KEY_NUMERIC_A); break;
  915. case 0xbd: map_key_clear(KEY_NUMERIC_B); break;
  916. case 0xbe: map_key_clear(KEY_NUMERIC_C); break;
  917. case 0xbf: map_key_clear(KEY_NUMERIC_D); break;
  918. default: goto ignore;
  919. }
  920. break;
  921. case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
  922. switch (usage->hid & HID_USAGE) {
  923. case 0x000: goto ignore;
  924. case 0x030: map_key_clear(KEY_POWER); break;
  925. case 0x031: map_key_clear(KEY_RESTART); break;
  926. case 0x032: map_key_clear(KEY_SLEEP); break;
  927. case 0x034: map_key_clear(KEY_SLEEP); break;
  928. case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  929. case 0x036: map_key_clear(BTN_MISC); break;
  930. case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
  931. case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
  932. case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
  933. case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
  934. case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
  935. case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
  936. case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
  937. case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
  938. case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
  939. case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
  940. case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
  941. case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
  942. case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
  943. case 0x069: map_key_clear(KEY_RED); break;
  944. case 0x06a: map_key_clear(KEY_GREEN); break;
  945. case 0x06b: map_key_clear(KEY_BLUE); break;
  946. case 0x06c: map_key_clear(KEY_YELLOW); break;
  947. case 0x06d: map_key_clear(KEY_ASPECT_RATIO); break;
  948. case 0x06f: map_key_clear(KEY_BRIGHTNESSUP); break;
  949. case 0x070: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  950. case 0x072: map_key_clear(KEY_BRIGHTNESS_TOGGLE); break;
  951. case 0x073: map_key_clear(KEY_BRIGHTNESS_MIN); break;
  952. case 0x074: map_key_clear(KEY_BRIGHTNESS_MAX); break;
  953. case 0x075: map_key_clear(KEY_BRIGHTNESS_AUTO); break;
  954. case 0x079: map_key_clear(KEY_KBDILLUMUP); break;
  955. case 0x07a: map_key_clear(KEY_KBDILLUMDOWN); break;
  956. case 0x07c: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  957. case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
  958. case 0x083: map_key_clear(KEY_LAST); break;
  959. case 0x084: map_key_clear(KEY_ENTER); break;
  960. case 0x088: map_key_clear(KEY_PC); break;
  961. case 0x089: map_key_clear(KEY_TV); break;
  962. case 0x08a: map_key_clear(KEY_WWW); break;
  963. case 0x08b: map_key_clear(KEY_DVD); break;
  964. case 0x08c: map_key_clear(KEY_PHONE); break;
  965. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  966. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  967. case 0x08f: map_key_clear(KEY_GAMES); break;
  968. case 0x090: map_key_clear(KEY_MEMO); break;
  969. case 0x091: map_key_clear(KEY_CD); break;
  970. case 0x092: map_key_clear(KEY_VCR); break;
  971. case 0x093: map_key_clear(KEY_TUNER); break;
  972. case 0x094: map_key_clear(KEY_EXIT); break;
  973. case 0x095: map_key_clear(KEY_HELP); break;
  974. case 0x096: map_key_clear(KEY_TAPE); break;
  975. case 0x097: map_key_clear(KEY_TV2); break;
  976. case 0x098: map_key_clear(KEY_SAT); break;
  977. case 0x09a: map_key_clear(KEY_PVR); break;
  978. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  979. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  980. case 0x0a0: map_key_clear(KEY_VCR2); break;
  981. case 0x0b0: map_key_clear(KEY_PLAY); break;
  982. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  983. case 0x0b2: map_key_clear(KEY_RECORD); break;
  984. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  985. case 0x0b4: map_key_clear(KEY_REWIND); break;
  986. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  987. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  988. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  989. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  990. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  991. case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
  992. case 0x0bf: map_key_clear(KEY_SLOW); break;
  993. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  994. case 0x0cf: map_key_clear(KEY_VOICECOMMAND); break;
  995. case 0x0d8: map_key_clear(KEY_DICTATE); break;
  996. case 0x0d9: map_key_clear(KEY_EMOJI_PICKER); break;
  997. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  998. case 0x0e2: map_key_clear(KEY_MUTE); break;
  999. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  1000. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  1001. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  1002. case 0x0f5: map_key_clear(KEY_SLOW); break;
  1003. case 0x181: map_key_clear(KEY_BUTTONCONFIG); break;
  1004. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  1005. case 0x183: map_key_clear(KEY_CONFIG); break;
  1006. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  1007. case 0x185: map_key_clear(KEY_EDITOR); break;
  1008. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  1009. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  1010. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  1011. case 0x189: map_key_clear(KEY_DATABASE); break;
  1012. case 0x18a: map_key_clear(KEY_MAIL); break;
  1013. case 0x18b: map_key_clear(KEY_NEWS); break;
  1014. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  1015. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  1016. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  1017. case 0x18f: map_key_clear(KEY_TASKMANAGER); break;
  1018. case 0x190: map_key_clear(KEY_JOURNAL); break;
  1019. case 0x191: map_key_clear(KEY_FINANCE); break;
  1020. case 0x192: map_key_clear(KEY_CALC); break;
  1021. case 0x193: map_key_clear(KEY_PLAYER); break;
  1022. case 0x194: map_key_clear(KEY_FILE); break;
  1023. case 0x196: map_key_clear(KEY_WWW); break;
  1024. case 0x199: map_key_clear(KEY_CHAT); break;
  1025. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  1026. case 0x19e: map_key_clear(KEY_COFFEE); break;
  1027. case 0x19f: map_key_clear(KEY_CONTROLPANEL); break;
  1028. case 0x1a2: map_key_clear(KEY_APPSELECT); break;
  1029. case 0x1a3: map_key_clear(KEY_NEXT); break;
  1030. case 0x1a4: map_key_clear(KEY_PREVIOUS); break;
  1031. case 0x1a6: map_key_clear(KEY_HELP); break;
  1032. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  1033. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  1034. case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
  1035. case 0x1b1: map_key_clear(KEY_SCREENSAVER); break;
  1036. case 0x1b4: map_key_clear(KEY_FILE); break;
  1037. case 0x1b6: map_key_clear(KEY_IMAGES); break;
  1038. case 0x1b7: map_key_clear(KEY_AUDIO); break;
  1039. case 0x1b8: map_key_clear(KEY_VIDEO); break;
  1040. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  1041. case 0x1bd: map_key_clear(KEY_INFO); break;
  1042. case 0x1cb: map_key_clear(KEY_ASSISTANT); break;
  1043. case 0x201: map_key_clear(KEY_NEW); break;
  1044. case 0x202: map_key_clear(KEY_OPEN); break;
  1045. case 0x203: map_key_clear(KEY_CLOSE); break;
  1046. case 0x204: map_key_clear(KEY_EXIT); break;
  1047. case 0x207: map_key_clear(KEY_SAVE); break;
  1048. case 0x208: map_key_clear(KEY_PRINT); break;
  1049. case 0x209: map_key_clear(KEY_PROPS); break;
  1050. case 0x21a: map_key_clear(KEY_UNDO); break;
  1051. case 0x21b: map_key_clear(KEY_COPY); break;
  1052. case 0x21c: map_key_clear(KEY_CUT); break;
  1053. case 0x21d: map_key_clear(KEY_PASTE); break;
  1054. case 0x21f: map_key_clear(KEY_FIND); break;
  1055. case 0x221: map_key_clear(KEY_SEARCH); break;
  1056. case 0x222: map_key_clear(KEY_GOTO); break;
  1057. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  1058. case 0x224: map_key_clear(KEY_BACK); break;
  1059. case 0x225: map_key_clear(KEY_FORWARD); break;
  1060. case 0x226: map_key_clear(KEY_STOP); break;
  1061. case 0x227: map_key_clear(KEY_REFRESH); break;
  1062. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  1063. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  1064. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  1065. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  1066. case 0x232: map_key_clear(KEY_FULL_SCREEN); break;
  1067. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  1068. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  1069. case 0x238: /* AC Pan */
  1070. set_bit(REL_HWHEEL, input->relbit);
  1071. map_rel(REL_HWHEEL_HI_RES);
  1072. break;
  1073. case 0x23d: map_key_clear(KEY_EDIT); break;
  1074. case 0x25f: map_key_clear(KEY_CANCEL); break;
  1075. case 0x269: map_key_clear(KEY_INSERT); break;
  1076. case 0x26a: map_key_clear(KEY_DELETE); break;
  1077. case 0x279: map_key_clear(KEY_REDO); break;
  1078. case 0x289: map_key_clear(KEY_REPLY); break;
  1079. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  1080. case 0x28c: map_key_clear(KEY_SEND); break;
  1081. case 0x29d: map_key_clear(KEY_KBD_LAYOUT_NEXT); break;
  1082. case 0x2a2: map_key_clear(KEY_ALL_APPLICATIONS); break;
  1083. case 0x2c7: map_key_clear(KEY_KBDINPUTASSIST_PREV); break;
  1084. case 0x2c8: map_key_clear(KEY_KBDINPUTASSIST_NEXT); break;
  1085. case 0x2c9: map_key_clear(KEY_KBDINPUTASSIST_PREVGROUP); break;
  1086. case 0x2ca: map_key_clear(KEY_KBDINPUTASSIST_NEXTGROUP); break;
  1087. case 0x2cb: map_key_clear(KEY_KBDINPUTASSIST_ACCEPT); break;
  1088. case 0x2cc: map_key_clear(KEY_KBDINPUTASSIST_CANCEL); break;
  1089. case 0x29f: map_key_clear(KEY_SCALE); break;
  1090. default: map_key_clear(KEY_UNKNOWN);
  1091. }
  1092. break;
  1093. case HID_UP_GENDEVCTRLS:
  1094. switch (usage->hid) {
  1095. case HID_DC_BATTERYSTRENGTH:
  1096. hidinput_setup_battery(device, HID_INPUT_REPORT, field, false);
  1097. usage->type = EV_PWR;
  1098. return;
  1099. }
  1100. goto unknown;
  1101. case HID_UP_BATTERY:
  1102. switch (usage->hid) {
  1103. case HID_BAT_ABSOLUTESTATEOFCHARGE:
  1104. hidinput_setup_battery(device, HID_INPUT_REPORT, field, true);
  1105. usage->type = EV_PWR;
  1106. return;
  1107. case HID_BAT_CHARGING:
  1108. usage->type = EV_PWR;
  1109. return;
  1110. }
  1111. goto unknown;
  1112. case HID_UP_CAMERA:
  1113. switch (usage->hid & HID_USAGE) {
  1114. case 0x020:
  1115. map_key_clear(KEY_CAMERA_FOCUS); break;
  1116. case 0x021:
  1117. map_key_clear(KEY_CAMERA); break;
  1118. default:
  1119. goto ignore;
  1120. }
  1121. break;
  1122. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  1123. set_bit(EV_REP, input->evbit);
  1124. switch (usage->hid & HID_USAGE) {
  1125. case 0x021: map_key_clear(KEY_PRINT); break;
  1126. case 0x070: map_key_clear(KEY_HP); break;
  1127. case 0x071: map_key_clear(KEY_CAMERA); break;
  1128. case 0x072: map_key_clear(KEY_SOUND); break;
  1129. case 0x073: map_key_clear(KEY_QUESTION); break;
  1130. case 0x080: map_key_clear(KEY_EMAIL); break;
  1131. case 0x081: map_key_clear(KEY_CHAT); break;
  1132. case 0x082: map_key_clear(KEY_SEARCH); break;
  1133. case 0x083: map_key_clear(KEY_CONNECT); break;
  1134. case 0x084: map_key_clear(KEY_FINANCE); break;
  1135. case 0x085: map_key_clear(KEY_SPORT); break;
  1136. case 0x086: map_key_clear(KEY_SHOP); break;
  1137. default: goto ignore;
  1138. }
  1139. break;
  1140. case HID_UP_HPVENDOR2:
  1141. set_bit(EV_REP, input->evbit);
  1142. switch (usage->hid & HID_USAGE) {
  1143. case 0x001: map_key_clear(KEY_MICMUTE); break;
  1144. case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  1145. case 0x004: map_key_clear(KEY_BRIGHTNESSUP); break;
  1146. default: goto ignore;
  1147. }
  1148. break;
  1149. case HID_UP_MSVENDOR:
  1150. goto ignore;
  1151. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  1152. set_bit(EV_REP, input->evbit);
  1153. goto ignore;
  1154. case HID_UP_LOGIVENDOR:
  1155. /* intentional fallback */
  1156. case HID_UP_LOGIVENDOR2:
  1157. /* intentional fallback */
  1158. case HID_UP_LOGIVENDOR3:
  1159. goto ignore;
  1160. case HID_UP_PID:
  1161. switch (usage->hid & HID_USAGE) {
  1162. case 0xa4: map_key_clear(BTN_DEAD); break;
  1163. default: goto ignore;
  1164. }
  1165. break;
  1166. default:
  1167. unknown:
  1168. if (field->report_size == 1) {
  1169. if (field->report->type == HID_OUTPUT_REPORT) {
  1170. map_led(LED_MISC);
  1171. break;
  1172. }
  1173. map_key(BTN_MISC);
  1174. break;
  1175. }
  1176. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  1177. map_rel(REL_MISC);
  1178. break;
  1179. }
  1180. map_abs(ABS_MISC);
  1181. break;
  1182. }
  1183. mapped:
  1184. /* Mapping failed, bail out */
  1185. if (!bit)
  1186. return;
  1187. if (device->driver->input_mapped &&
  1188. device->driver->input_mapped(device, hidinput, field, usage,
  1189. &bit, &max) < 0) {
  1190. /*
  1191. * The driver indicated that no further generic handling
  1192. * of the usage is desired.
  1193. */
  1194. return;
  1195. }
  1196. set_bit(usage->type, input->evbit);
  1197. /*
  1198. * This part is *really* controversial:
  1199. * - HID aims at being generic so we should do our best to export
  1200. * all incoming events
  1201. * - HID describes what events are, so there is no reason for ABS_X
  1202. * to be mapped to ABS_Y
  1203. * - HID is using *_MISC+N as a default value, but nothing prevents
  1204. * *_MISC+N to overwrite a legitimate even, which confuses userspace
  1205. * (for instance ABS_MISC + 7 is ABS_MT_SLOT, which has a different
  1206. * processing)
  1207. *
  1208. * If devices still want to use this (at their own risk), they will
  1209. * have to use the quirk HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE, but
  1210. * the default should be a reliable mapping.
  1211. */
  1212. while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
  1213. if (device->quirks & HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE) {
  1214. usage->code = find_next_zero_bit(bit,
  1215. max + 1,
  1216. usage->code);
  1217. } else {
  1218. device->status |= HID_STAT_DUP_DETECTED;
  1219. goto ignore;
  1220. }
  1221. }
  1222. if (usage->code > max)
  1223. goto ignore;
  1224. if (usage->type == EV_ABS) {
  1225. int a = field->logical_minimum;
  1226. int b = field->logical_maximum;
  1227. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  1228. a = field->logical_minimum = 0;
  1229. b = field->logical_maximum = 255;
  1230. }
  1231. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  1232. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  1233. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  1234. input_abs_set_res(input, usage->code,
  1235. hidinput_calc_abs_res(field, usage->code));
  1236. /* use a larger default input buffer for MT devices */
  1237. if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
  1238. input_set_events_per_packet(input, 60);
  1239. }
  1240. if (usage->type == EV_ABS &&
  1241. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  1242. int i;
  1243. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  1244. input_set_abs_params(input, i, -1, 1, 0, 0);
  1245. set_bit(i, input->absbit);
  1246. }
  1247. if (usage->hat_dir && !field->dpad)
  1248. field->dpad = usage->code;
  1249. }
  1250. /* for those devices which produce Consumer volume usage as relative,
  1251. * we emulate pressing volumeup/volumedown appropriate number of times
  1252. * in hidinput_hid_event()
  1253. */
  1254. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  1255. (usage->code == ABS_VOLUME)) {
  1256. set_bit(KEY_VOLUMEUP, input->keybit);
  1257. set_bit(KEY_VOLUMEDOWN, input->keybit);
  1258. }
  1259. if (usage->type == EV_KEY) {
  1260. set_bit(EV_MSC, input->evbit);
  1261. set_bit(MSC_SCAN, input->mscbit);
  1262. }
  1263. return;
  1264. ignore:
  1265. usage->type = 0;
  1266. usage->code = 0;
  1267. }
  1268. static void hidinput_handle_scroll(struct hid_usage *usage,
  1269. struct input_dev *input,
  1270. __s32 value)
  1271. {
  1272. int code;
  1273. int hi_res, lo_res;
  1274. if (value == 0)
  1275. return;
  1276. if (usage->code == REL_WHEEL_HI_RES)
  1277. code = REL_WHEEL;
  1278. else
  1279. code = REL_HWHEEL;
  1280. /*
  1281. * Windows reports one wheel click as value 120. Where a high-res
  1282. * scroll wheel is present, a fraction of 120 is reported instead.
  1283. * Our REL_WHEEL_HI_RES axis does the same because all HW must
  1284. * adhere to the 120 expectation.
  1285. */
  1286. hi_res = value * 120/usage->resolution_multiplier;
  1287. usage->wheel_accumulated += hi_res;
  1288. lo_res = usage->wheel_accumulated/120;
  1289. if (lo_res)
  1290. usage->wheel_accumulated -= lo_res * 120;
  1291. input_event(input, EV_REL, code, lo_res);
  1292. input_event(input, EV_REL, usage->code, hi_res);
  1293. }
  1294. static void hid_report_release_tool(struct hid_report *report, struct input_dev *input,
  1295. unsigned int tool)
  1296. {
  1297. /* if the given tool is not currently reported, ignore */
  1298. if (!test_bit(tool, input->key))
  1299. return;
  1300. /*
  1301. * if the given tool was previously set, release it,
  1302. * release any TOUCH and send an EV_SYN
  1303. */
  1304. input_event(input, EV_KEY, BTN_TOUCH, 0);
  1305. input_event(input, EV_KEY, tool, 0);
  1306. input_event(input, EV_SYN, SYN_REPORT, 0);
  1307. report->tool = 0;
  1308. }
  1309. static void hid_report_set_tool(struct hid_report *report, struct input_dev *input,
  1310. unsigned int new_tool)
  1311. {
  1312. if (report->tool != new_tool)
  1313. hid_report_release_tool(report, input, report->tool);
  1314. input_event(input, EV_KEY, new_tool, 1);
  1315. report->tool = new_tool;
  1316. }
  1317. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  1318. {
  1319. struct input_dev *input;
  1320. struct hid_report *report = field->report;
  1321. unsigned *quirks = &hid->quirks;
  1322. if (!usage->type)
  1323. return;
  1324. if (usage->type == EV_PWR) {
  1325. bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);
  1326. if (!handled)
  1327. hidinput_update_battery(hid, value);
  1328. return;
  1329. }
  1330. if (!field->hidinput)
  1331. return;
  1332. input = field->hidinput->input;
  1333. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  1334. int hat_dir = usage->hat_dir;
  1335. if (!hat_dir)
  1336. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  1337. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  1338. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  1339. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  1340. return;
  1341. }
  1342. /*
  1343. * Ignore out-of-range values as per HID specification,
  1344. * section 5.10 and 6.2.25, when NULL state bit is present.
  1345. * When it's not, clamp the value to match Microsoft's input
  1346. * driver as mentioned in "Required HID usages for digitizers":
  1347. * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
  1348. *
  1349. * The logical_minimum < logical_maximum check is done so that we
  1350. * don't unintentionally discard values sent by devices which
  1351. * don't specify logical min and max.
  1352. */
  1353. if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1354. field->logical_minimum < field->logical_maximum) {
  1355. if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
  1356. (value < field->logical_minimum ||
  1357. value > field->logical_maximum)) {
  1358. dbg_hid("Ignoring out-of-range value %x\n", value);
  1359. return;
  1360. }
  1361. value = clamp(value,
  1362. field->logical_minimum,
  1363. field->logical_maximum);
  1364. }
  1365. switch (usage->hid) {
  1366. case HID_DG_ERASER:
  1367. report->tool_active |= !!value;
  1368. /*
  1369. * if eraser is set, we must enforce BTN_TOOL_RUBBER
  1370. * to accommodate for devices not following the spec.
  1371. */
  1372. if (value)
  1373. hid_report_set_tool(report, input, BTN_TOOL_RUBBER);
  1374. else if (report->tool != BTN_TOOL_RUBBER)
  1375. /* value is off, tool is not rubber, ignore */
  1376. return;
  1377. else if (*quirks & HID_QUIRK_NOINVERT &&
  1378. !test_bit(BTN_TOUCH, input->key)) {
  1379. /*
  1380. * There is no invert to release the tool, let hid_input
  1381. * send BTN_TOUCH with scancode and release the tool after.
  1382. */
  1383. hid_report_release_tool(report, input, BTN_TOOL_RUBBER);
  1384. return;
  1385. }
  1386. /* let hid-input set BTN_TOUCH */
  1387. break;
  1388. case HID_DG_INVERT:
  1389. report->tool_active |= !!value;
  1390. /*
  1391. * If invert is set, we store BTN_TOOL_RUBBER.
  1392. */
  1393. if (value)
  1394. hid_report_set_tool(report, input, BTN_TOOL_RUBBER);
  1395. else if (!report->tool_active)
  1396. /* tool_active not set means Invert and Eraser are not set */
  1397. hid_report_release_tool(report, input, BTN_TOOL_RUBBER);
  1398. /* no further processing */
  1399. return;
  1400. case HID_DG_INRANGE:
  1401. report->tool_active |= !!value;
  1402. if (report->tool_active) {
  1403. /*
  1404. * if tool is not set but is marked as active,
  1405. * assume ours
  1406. */
  1407. if (!report->tool)
  1408. report->tool = usage->code;
  1409. /* drivers may have changed the value behind our back, resend it */
  1410. hid_report_set_tool(report, input, report->tool);
  1411. } else {
  1412. hid_report_release_tool(report, input, usage->code);
  1413. }
  1414. /* reset tool_active for the next event */
  1415. report->tool_active = false;
  1416. /* no further processing */
  1417. return;
  1418. case HID_DG_TIPSWITCH:
  1419. report->tool_active |= !!value;
  1420. /* if tool is set to RUBBER we should ignore the current value */
  1421. if (report->tool == BTN_TOOL_RUBBER)
  1422. return;
  1423. break;
  1424. case HID_DG_TIPPRESSURE:
  1425. if (*quirks & HID_QUIRK_NOTOUCH) {
  1426. int a = field->logical_minimum;
  1427. int b = field->logical_maximum;
  1428. if (value > a + ((b - a) >> 3)) {
  1429. input_event(input, EV_KEY, BTN_TOUCH, 1);
  1430. report->tool_active = true;
  1431. }
  1432. }
  1433. break;
  1434. case HID_UP_PID | 0x83UL: /* Simultaneous Effects Max */
  1435. dbg_hid("Maximum Effects - %d\n",value);
  1436. return;
  1437. case HID_UP_PID | 0x7fUL:
  1438. dbg_hid("PID Pool Report\n");
  1439. return;
  1440. }
  1441. switch (usage->type) {
  1442. case EV_KEY:
  1443. if (usage->code == 0) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  1444. return;
  1445. break;
  1446. case EV_REL:
  1447. if (usage->code == REL_WHEEL_HI_RES ||
  1448. usage->code == REL_HWHEEL_HI_RES) {
  1449. hidinput_handle_scroll(usage, input, value);
  1450. return;
  1451. }
  1452. break;
  1453. case EV_ABS:
  1454. if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
  1455. usage->code == ABS_VOLUME) {
  1456. int count = abs(value);
  1457. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  1458. int i;
  1459. for (i = 0; i < count; i++) {
  1460. input_event(input, EV_KEY, direction, 1);
  1461. input_sync(input);
  1462. input_event(input, EV_KEY, direction, 0);
  1463. input_sync(input);
  1464. }
  1465. return;
  1466. } else if (((*quirks & HID_QUIRK_X_INVERT) && usage->code == ABS_X) ||
  1467. ((*quirks & HID_QUIRK_Y_INVERT) && usage->code == ABS_Y))
  1468. value = field->logical_maximum - value;
  1469. break;
  1470. }
  1471. /*
  1472. * Ignore reports for absolute data if the data didn't change. This is
  1473. * not only an optimization but also fixes 'dead' key reports. Some
  1474. * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
  1475. * 0x31 and 0x32) report multiple keys, even though a localized keyboard
  1476. * can only have one of them physically available. The 'dead' keys
  1477. * report constant 0. As all map to the same keycode, they'd confuse
  1478. * the input layer. If we filter the 'dead' keys on the HID level, we
  1479. * skip the keycode translation and only forward real events.
  1480. */
  1481. if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
  1482. HID_MAIN_ITEM_BUFFERED_BYTE)) &&
  1483. (field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1484. usage->usage_index < field->maxusage &&
  1485. value == field->value[usage->usage_index])
  1486. return;
  1487. /* report the usage code as scancode if the key status has changed */
  1488. if (usage->type == EV_KEY &&
  1489. (!test_bit(usage->code, input->key)) == value)
  1490. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  1491. input_event(input, usage->type, usage->code, value);
  1492. if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
  1493. usage->type == EV_KEY && value) {
  1494. input_sync(input);
  1495. input_event(input, usage->type, usage->code, 0);
  1496. }
  1497. }
  1498. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  1499. {
  1500. struct hid_input *hidinput;
  1501. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  1502. return;
  1503. list_for_each_entry(hidinput, &hid->inputs, list)
  1504. input_sync(hidinput->input);
  1505. }
  1506. EXPORT_SYMBOL_GPL(hidinput_report_event);
  1507. static int hidinput_find_field(struct hid_device *hid, unsigned int type,
  1508. unsigned int code, struct hid_field **field)
  1509. {
  1510. struct hid_report *report;
  1511. int i, j;
  1512. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  1513. for (i = 0; i < report->maxfield; i++) {
  1514. *field = report->field[i];
  1515. for (j = 0; j < (*field)->maxusage; j++)
  1516. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  1517. return j;
  1518. }
  1519. }
  1520. return -1;
  1521. }
  1522. struct hid_field *hidinput_get_led_field(struct hid_device *hid)
  1523. {
  1524. struct hid_report *report;
  1525. struct hid_field *field;
  1526. int i, j;
  1527. list_for_each_entry(report,
  1528. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1529. list) {
  1530. for (i = 0; i < report->maxfield; i++) {
  1531. field = report->field[i];
  1532. for (j = 0; j < field->maxusage; j++)
  1533. if (field->usage[j].type == EV_LED)
  1534. return field;
  1535. }
  1536. }
  1537. return NULL;
  1538. }
  1539. EXPORT_SYMBOL_GPL(hidinput_get_led_field);
  1540. unsigned int hidinput_count_leds(struct hid_device *hid)
  1541. {
  1542. struct hid_report *report;
  1543. struct hid_field *field;
  1544. int i, j;
  1545. unsigned int count = 0;
  1546. list_for_each_entry(report,
  1547. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1548. list) {
  1549. for (i = 0; i < report->maxfield; i++) {
  1550. field = report->field[i];
  1551. for (j = 0; j < field->maxusage; j++)
  1552. if (field->usage[j].type == EV_LED &&
  1553. field->value[j])
  1554. count += 1;
  1555. }
  1556. }
  1557. return count;
  1558. }
  1559. EXPORT_SYMBOL_GPL(hidinput_count_leds);
  1560. static void hidinput_led_worker(struct work_struct *work)
  1561. {
  1562. struct hid_device *hid = container_of(work, struct hid_device,
  1563. led_work);
  1564. struct hid_field *field;
  1565. struct hid_report *report;
  1566. int ret;
  1567. u32 len;
  1568. __u8 *buf;
  1569. field = hidinput_get_led_field(hid);
  1570. if (!field)
  1571. return;
  1572. /*
  1573. * field->report is accessed unlocked regarding HID core. So there might
  1574. * be another incoming SET-LED request from user-space, which changes
  1575. * the LED state while we assemble our outgoing buffer. However, this
  1576. * doesn't matter as hid_output_report() correctly converts it into a
  1577. * boolean value no matter what information is currently set on the LED
  1578. * field (even garbage). So the remote device will always get a valid
  1579. * request.
  1580. * And in case we send a wrong value, a next led worker is spawned
  1581. * for every SET-LED request so the following worker will send the
  1582. * correct value, guaranteed!
  1583. */
  1584. report = field->report;
  1585. /* use custom SET_REPORT request if possible (asynchronous) */
  1586. if (hid->ll_driver->request)
  1587. return hid->ll_driver->request(hid, report, HID_REQ_SET_REPORT);
  1588. /* fall back to generic raw-output-report */
  1589. len = hid_report_len(report);
  1590. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  1591. if (!buf)
  1592. return;
  1593. hid_output_report(report, buf);
  1594. /* synchronous output report */
  1595. ret = hid_hw_output_report(hid, buf, len);
  1596. if (ret == -ENOSYS)
  1597. hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
  1598. HID_REQ_SET_REPORT);
  1599. kfree(buf);
  1600. }
  1601. static int hidinput_input_event(struct input_dev *dev, unsigned int type,
  1602. unsigned int code, int value)
  1603. {
  1604. struct hid_device *hid = input_get_drvdata(dev);
  1605. struct hid_field *field;
  1606. int offset;
  1607. if (type == EV_FF)
  1608. return input_ff_event(dev, type, code, value);
  1609. if (type != EV_LED)
  1610. return -1;
  1611. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  1612. hid_warn(dev, "event field not found\n");
  1613. return -1;
  1614. }
  1615. hid_set_field(field, offset, value);
  1616. schedule_work(&hid->led_work);
  1617. return 0;
  1618. }
  1619. static int hidinput_open(struct input_dev *dev)
  1620. {
  1621. struct hid_device *hid = input_get_drvdata(dev);
  1622. return hid_hw_open(hid);
  1623. }
  1624. static void hidinput_close(struct input_dev *dev)
  1625. {
  1626. struct hid_device *hid = input_get_drvdata(dev);
  1627. hid_hw_close(hid);
  1628. }
  1629. static bool __hidinput_change_resolution_multipliers(struct hid_device *hid,
  1630. struct hid_report *report, bool use_logical_max)
  1631. {
  1632. struct hid_usage *usage;
  1633. bool update_needed = false;
  1634. bool get_report_completed = false;
  1635. int i, j;
  1636. if (report->maxfield == 0)
  1637. return false;
  1638. for (i = 0; i < report->maxfield; i++) {
  1639. __s32 value = use_logical_max ?
  1640. report->field[i]->logical_maximum :
  1641. report->field[i]->logical_minimum;
  1642. /* There is no good reason for a Resolution
  1643. * Multiplier to have a count other than 1.
  1644. * Ignore that case.
  1645. */
  1646. if (report->field[i]->report_count != 1)
  1647. continue;
  1648. for (j = 0; j < report->field[i]->maxusage; j++) {
  1649. usage = &report->field[i]->usage[j];
  1650. if (usage->hid != HID_GD_RESOLUTION_MULTIPLIER)
  1651. continue;
  1652. /*
  1653. * If we have more than one feature within this
  1654. * report we need to fill in the bits from the
  1655. * others before we can overwrite the ones for the
  1656. * Resolution Multiplier.
  1657. *
  1658. * But if we're not allowed to read from the device,
  1659. * we just bail. Such a device should not exist
  1660. * anyway.
  1661. */
  1662. if (!get_report_completed && report->maxfield > 1) {
  1663. if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
  1664. return update_needed;
  1665. hid_hw_request(hid, report, HID_REQ_GET_REPORT);
  1666. hid_hw_wait(hid);
  1667. get_report_completed = true;
  1668. }
  1669. report->field[i]->value[j] = value;
  1670. update_needed = true;
  1671. }
  1672. }
  1673. return update_needed;
  1674. }
  1675. static void hidinput_change_resolution_multipliers(struct hid_device *hid)
  1676. {
  1677. struct hid_report_enum *rep_enum;
  1678. struct hid_report *rep;
  1679. int ret;
  1680. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  1681. list_for_each_entry(rep, &rep_enum->report_list, list) {
  1682. bool update_needed = __hidinput_change_resolution_multipliers(hid,
  1683. rep, true);
  1684. if (update_needed) {
  1685. ret = __hid_request(hid, rep, HID_REQ_SET_REPORT);
  1686. if (ret) {
  1687. __hidinput_change_resolution_multipliers(hid,
  1688. rep, false);
  1689. return;
  1690. }
  1691. }
  1692. }
  1693. /* refresh our structs */
  1694. hid_setup_resolution_multiplier(hid);
  1695. }
  1696. static void report_features(struct hid_device *hid)
  1697. {
  1698. struct hid_driver *drv = hid->driver;
  1699. struct hid_report_enum *rep_enum;
  1700. struct hid_report *rep;
  1701. struct hid_usage *usage;
  1702. int i, j;
  1703. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  1704. list_for_each_entry(rep, &rep_enum->report_list, list)
  1705. for (i = 0; i < rep->maxfield; i++) {
  1706. /* Ignore if report count is out of bounds. */
  1707. if (rep->field[i]->report_count < 1)
  1708. continue;
  1709. for (j = 0; j < rep->field[i]->maxusage; j++) {
  1710. usage = &rep->field[i]->usage[j];
  1711. /* Verify if Battery Strength feature is available */
  1712. if (usage->hid == HID_DC_BATTERYSTRENGTH)
  1713. hidinput_setup_battery(hid, HID_FEATURE_REPORT,
  1714. rep->field[i], false);
  1715. if (drv->feature_mapping)
  1716. drv->feature_mapping(hid, rep->field[i], usage);
  1717. }
  1718. }
  1719. }
  1720. static struct hid_input *hidinput_allocate(struct hid_device *hid,
  1721. unsigned int application)
  1722. {
  1723. struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  1724. struct input_dev *input_dev = input_allocate_device();
  1725. const char *suffix = NULL;
  1726. size_t suffix_len, name_len;
  1727. if (!hidinput || !input_dev)
  1728. goto fail;
  1729. if ((hid->quirks & HID_QUIRK_INPUT_PER_APP) &&
  1730. hid->maxapplication > 1) {
  1731. switch (application) {
  1732. case HID_GD_KEYBOARD:
  1733. suffix = "Keyboard";
  1734. break;
  1735. case HID_GD_KEYPAD:
  1736. suffix = "Keypad";
  1737. break;
  1738. case HID_GD_MOUSE:
  1739. suffix = "Mouse";
  1740. break;
  1741. case HID_DG_PEN:
  1742. /*
  1743. * yes, there is an issue here:
  1744. * DG_PEN -> "Stylus"
  1745. * DG_STYLUS -> "Pen"
  1746. * But changing this now means users with config snippets
  1747. * will have to change it and the test suite will not be happy.
  1748. */
  1749. suffix = "Stylus";
  1750. break;
  1751. case HID_DG_STYLUS:
  1752. suffix = "Pen";
  1753. break;
  1754. case HID_DG_TOUCHSCREEN:
  1755. suffix = "Touchscreen";
  1756. break;
  1757. case HID_DG_TOUCHPAD:
  1758. suffix = "Touchpad";
  1759. break;
  1760. case HID_GD_SYSTEM_CONTROL:
  1761. suffix = "System Control";
  1762. break;
  1763. case HID_CP_CONSUMER_CONTROL:
  1764. suffix = "Consumer Control";
  1765. break;
  1766. case HID_GD_WIRELESS_RADIO_CTLS:
  1767. suffix = "Wireless Radio Control";
  1768. break;
  1769. case HID_GD_SYSTEM_MULTIAXIS:
  1770. suffix = "System Multi Axis";
  1771. break;
  1772. default:
  1773. break;
  1774. }
  1775. }
  1776. if (suffix) {
  1777. name_len = strlen(hid->name);
  1778. suffix_len = strlen(suffix);
  1779. if ((name_len < suffix_len) ||
  1780. strcmp(hid->name + name_len - suffix_len, suffix)) {
  1781. hidinput->name = kasprintf(GFP_KERNEL, "%s %s",
  1782. hid->name, suffix);
  1783. if (!hidinput->name)
  1784. goto fail;
  1785. }
  1786. }
  1787. input_set_drvdata(input_dev, hid);
  1788. input_dev->event = hidinput_input_event;
  1789. input_dev->open = hidinput_open;
  1790. input_dev->close = hidinput_close;
  1791. input_dev->setkeycode = hidinput_setkeycode;
  1792. input_dev->getkeycode = hidinput_getkeycode;
  1793. input_dev->name = hidinput->name ? hidinput->name : hid->name;
  1794. input_dev->phys = hid->phys;
  1795. input_dev->uniq = hid->uniq;
  1796. input_dev->id.bustype = hid->bus;
  1797. input_dev->id.vendor = hid->vendor;
  1798. input_dev->id.product = hid->product;
  1799. input_dev->id.version = hid->version;
  1800. input_dev->dev.parent = &hid->dev;
  1801. hidinput->input = input_dev;
  1802. hidinput->application = application;
  1803. list_add_tail(&hidinput->list, &hid->inputs);
  1804. INIT_LIST_HEAD(&hidinput->reports);
  1805. return hidinput;
  1806. fail:
  1807. kfree(hidinput);
  1808. input_free_device(input_dev);
  1809. hid_err(hid, "Out of memory during hid input probe\n");
  1810. return NULL;
  1811. }
  1812. static bool hidinput_has_been_populated(struct hid_input *hidinput)
  1813. {
  1814. int i;
  1815. unsigned long r = 0;
  1816. for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
  1817. r |= hidinput->input->evbit[i];
  1818. for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
  1819. r |= hidinput->input->keybit[i];
  1820. for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
  1821. r |= hidinput->input->relbit[i];
  1822. for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
  1823. r |= hidinput->input->absbit[i];
  1824. for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
  1825. r |= hidinput->input->mscbit[i];
  1826. for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
  1827. r |= hidinput->input->ledbit[i];
  1828. for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
  1829. r |= hidinput->input->sndbit[i];
  1830. for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
  1831. r |= hidinput->input->ffbit[i];
  1832. for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
  1833. r |= hidinput->input->swbit[i];
  1834. return !!r;
  1835. }
  1836. static void hidinput_cleanup_hidinput(struct hid_device *hid,
  1837. struct hid_input *hidinput)
  1838. {
  1839. struct hid_report *report;
  1840. int i, k;
  1841. list_del(&hidinput->list);
  1842. input_free_device(hidinput->input);
  1843. kfree(hidinput->name);
  1844. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1845. if (k == HID_OUTPUT_REPORT &&
  1846. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1847. continue;
  1848. list_for_each_entry(report, &hid->report_enum[k].report_list,
  1849. list) {
  1850. for (i = 0; i < report->maxfield; i++)
  1851. if (report->field[i]->hidinput == hidinput)
  1852. report->field[i]->hidinput = NULL;
  1853. }
  1854. }
  1855. kfree(hidinput);
  1856. }
  1857. static struct hid_input *hidinput_match(struct hid_report *report)
  1858. {
  1859. struct hid_device *hid = report->device;
  1860. struct hid_input *hidinput;
  1861. list_for_each_entry(hidinput, &hid->inputs, list) {
  1862. if (hidinput->report &&
  1863. hidinput->report->id == report->id)
  1864. return hidinput;
  1865. }
  1866. return NULL;
  1867. }
  1868. static struct hid_input *hidinput_match_application(struct hid_report *report)
  1869. {
  1870. struct hid_device *hid = report->device;
  1871. struct hid_input *hidinput;
  1872. list_for_each_entry(hidinput, &hid->inputs, list) {
  1873. if (hidinput->application == report->application)
  1874. return hidinput;
  1875. /*
  1876. * Keep SystemControl and ConsumerControl applications together
  1877. * with the main keyboard, if present.
  1878. */
  1879. if ((report->application == HID_GD_SYSTEM_CONTROL ||
  1880. report->application == HID_CP_CONSUMER_CONTROL) &&
  1881. hidinput->application == HID_GD_KEYBOARD) {
  1882. return hidinput;
  1883. }
  1884. }
  1885. return NULL;
  1886. }
  1887. static inline void hidinput_configure_usages(struct hid_input *hidinput,
  1888. struct hid_report *report)
  1889. {
  1890. int i, j, k;
  1891. int first_field_index = 0;
  1892. int slot_collection_index = -1;
  1893. int prev_collection_index = -1;
  1894. unsigned int slot_idx = 0;
  1895. struct hid_field *field;
  1896. /*
  1897. * First tag all the fields that are part of a slot,
  1898. * a slot needs to have one Contact ID in the collection
  1899. */
  1900. for (i = 0; i < report->maxfield; i++) {
  1901. field = report->field[i];
  1902. /* ignore fields without usage */
  1903. if (field->maxusage < 1)
  1904. continue;
  1905. /*
  1906. * janitoring when collection_index changes
  1907. */
  1908. if (prev_collection_index != field->usage->collection_index) {
  1909. prev_collection_index = field->usage->collection_index;
  1910. first_field_index = i;
  1911. }
  1912. /*
  1913. * if we already found a Contact ID in the collection,
  1914. * tag and continue to the next.
  1915. */
  1916. if (slot_collection_index == field->usage->collection_index) {
  1917. field->slot_idx = slot_idx;
  1918. continue;
  1919. }
  1920. /* check if the current field has Contact ID */
  1921. for (j = 0; j < field->maxusage; j++) {
  1922. if (field->usage[j].hid == HID_DG_CONTACTID) {
  1923. slot_collection_index = field->usage->collection_index;
  1924. slot_idx++;
  1925. /*
  1926. * mark all previous fields and this one in the
  1927. * current collection to be slotted.
  1928. */
  1929. for (k = first_field_index; k <= i; k++)
  1930. report->field[k]->slot_idx = slot_idx;
  1931. break;
  1932. }
  1933. }
  1934. }
  1935. for (i = 0; i < report->maxfield; i++)
  1936. for (j = 0; j < report->field[i]->maxusage; j++)
  1937. hidinput_configure_usage(hidinput, report->field[i],
  1938. report->field[i]->usage + j,
  1939. j);
  1940. }
  1941. /*
  1942. * Register the input device; print a message.
  1943. * Configure the input layer interface
  1944. * Read all reports and initialize the absolute field values.
  1945. */
  1946. int hidinput_connect(struct hid_device *hid, unsigned int force)
  1947. {
  1948. struct hid_driver *drv = hid->driver;
  1949. struct hid_report *report;
  1950. struct hid_input *next, *hidinput = NULL;
  1951. unsigned int application;
  1952. int i, k;
  1953. INIT_LIST_HEAD(&hid->inputs);
  1954. INIT_WORK(&hid->led_work, hidinput_led_worker);
  1955. hid->status &= ~HID_STAT_DUP_DETECTED;
  1956. if (!force) {
  1957. for (i = 0; i < hid->maxcollection; i++) {
  1958. struct hid_collection *col = &hid->collection[i];
  1959. if (col->type == HID_COLLECTION_APPLICATION ||
  1960. col->type == HID_COLLECTION_PHYSICAL)
  1961. if (IS_INPUT_APPLICATION(col->usage))
  1962. break;
  1963. }
  1964. if (i == hid->maxcollection)
  1965. return -1;
  1966. }
  1967. report_features(hid);
  1968. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1969. if (k == HID_OUTPUT_REPORT &&
  1970. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1971. continue;
  1972. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  1973. if (!report->maxfield)
  1974. continue;
  1975. application = report->application;
  1976. /*
  1977. * Find the previous hidinput report attached
  1978. * to this report id.
  1979. */
  1980. if (hid->quirks & HID_QUIRK_MULTI_INPUT)
  1981. hidinput = hidinput_match(report);
  1982. else if (hid->maxapplication > 1 &&
  1983. (hid->quirks & HID_QUIRK_INPUT_PER_APP))
  1984. hidinput = hidinput_match_application(report);
  1985. if (!hidinput) {
  1986. hidinput = hidinput_allocate(hid, application);
  1987. if (!hidinput)
  1988. goto out_unwind;
  1989. }
  1990. hidinput_configure_usages(hidinput, report);
  1991. if (hid->quirks & HID_QUIRK_MULTI_INPUT)
  1992. hidinput->report = report;
  1993. list_add_tail(&report->hidinput_list,
  1994. &hidinput->reports);
  1995. }
  1996. }
  1997. hidinput_change_resolution_multipliers(hid);
  1998. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  1999. if (drv->input_configured &&
  2000. drv->input_configured(hid, hidinput))
  2001. goto out_unwind;
  2002. if (!hidinput_has_been_populated(hidinput)) {
  2003. /* no need to register an input device not populated */
  2004. hidinput_cleanup_hidinput(hid, hidinput);
  2005. continue;
  2006. }
  2007. if (input_register_device(hidinput->input))
  2008. goto out_unwind;
  2009. hidinput->registered = true;
  2010. }
  2011. if (list_empty(&hid->inputs)) {
  2012. hid_err(hid, "No inputs registered, leaving\n");
  2013. goto out_unwind;
  2014. }
  2015. if (hid->status & HID_STAT_DUP_DETECTED)
  2016. hid_dbg(hid,
  2017. "Some usages could not be mapped, please use HID_QUIRK_INCREMENT_USAGE_ON_DUPLICATE if this is legitimate.\n");
  2018. return 0;
  2019. out_unwind:
  2020. /* unwind the ones we already registered */
  2021. hidinput_disconnect(hid);
  2022. return -1;
  2023. }
  2024. EXPORT_SYMBOL_GPL(hidinput_connect);
  2025. void hidinput_disconnect(struct hid_device *hid)
  2026. {
  2027. struct hid_input *hidinput, *next;
  2028. hidinput_cleanup_battery(hid);
  2029. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  2030. list_del(&hidinput->list);
  2031. if (hidinput->registered)
  2032. input_unregister_device(hidinput->input);
  2033. else
  2034. input_free_device(hidinput->input);
  2035. kfree(hidinput->name);
  2036. kfree(hidinput);
  2037. }
  2038. /* led_work is spawned by input_dev callbacks, but doesn't access the
  2039. * parent input_dev at all. Once all input devices are removed, we
  2040. * know that led_work will never get restarted, so we can cancel it
  2041. * synchronously and are safe. */
  2042. cancel_work_sync(&hid->led_work);
  2043. }
  2044. EXPORT_SYMBOL_GPL(hidinput_disconnect);
  2045. #ifdef CONFIG_HID_KUNIT_TEST
  2046. #include "hid-input-test.c"
  2047. #endif