hid-input.c 57 KB

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