hid-input.c 67 KB

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