hid-input.c 69 KB

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