hid-input.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*
  2. * Copyright (c) 2000-2001 Vojtech Pavlik
  3. * Copyright (c) 2006-2010 Jiri Kosina
  4. *
  5. * HID to Linux Input mapping
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/kernel.h>
  29. #include <linux/hid.h>
  30. #include <linux/hid-debug.h>
  31. #include "hid-ids.h"
  32. #define unk KEY_UNKNOWN
  33. static const unsigned char hid_keyboard[256] = {
  34. 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
  35. 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
  36. 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
  37. 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
  38. 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
  39. 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
  40. 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
  41. 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
  42. 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
  43. 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
  44. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  45. unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
  46. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  47. unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
  48. 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
  49. 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
  50. };
  51. static const struct {
  52. __s32 x;
  53. __s32 y;
  54. } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  55. #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
  56. #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
  57. #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
  58. #define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
  59. #define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  60. &max, EV_ABS, (c))
  61. #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  62. &max, EV_KEY, (c))
  63. static bool match_scancode(struct hid_usage *usage,
  64. unsigned int cur_idx, unsigned int scancode)
  65. {
  66. return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
  67. }
  68. static bool match_keycode(struct hid_usage *usage,
  69. unsigned int cur_idx, unsigned int keycode)
  70. {
  71. /*
  72. * We should exclude unmapped usages when doing lookup by keycode.
  73. */
  74. return (usage->type == EV_KEY && usage->code == keycode);
  75. }
  76. static bool match_index(struct hid_usage *usage,
  77. unsigned int cur_idx, unsigned int idx)
  78. {
  79. return cur_idx == idx;
  80. }
  81. typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
  82. unsigned int cur_idx, unsigned int val);
  83. static struct hid_usage *hidinput_find_key(struct hid_device *hid,
  84. hid_usage_cmp_t match,
  85. unsigned int value,
  86. unsigned int *usage_idx)
  87. {
  88. unsigned int i, j, k, cur_idx = 0;
  89. struct hid_report *report;
  90. struct hid_usage *usage;
  91. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  92. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  93. for (i = 0; i < report->maxfield; i++) {
  94. for (j = 0; j < report->field[i]->maxusage; j++) {
  95. usage = report->field[i]->usage + j;
  96. if (usage->type == EV_KEY || usage->type == 0) {
  97. if (match(usage, cur_idx, value)) {
  98. if (usage_idx)
  99. *usage_idx = cur_idx;
  100. return usage;
  101. }
  102. cur_idx++;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return NULL;
  109. }
  110. static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
  111. const struct input_keymap_entry *ke,
  112. unsigned int *index)
  113. {
  114. struct hid_usage *usage;
  115. unsigned int scancode;
  116. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  117. usage = hidinput_find_key(hid, match_index, ke->index, index);
  118. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  119. usage = hidinput_find_key(hid, match_scancode, scancode, index);
  120. else
  121. usage = NULL;
  122. return usage;
  123. }
  124. static int hidinput_getkeycode(struct input_dev *dev,
  125. struct input_keymap_entry *ke)
  126. {
  127. struct hid_device *hid = input_get_drvdata(dev);
  128. struct hid_usage *usage;
  129. unsigned int scancode, index;
  130. usage = hidinput_locate_usage(hid, ke, &index);
  131. if (usage) {
  132. ke->keycode = usage->type == EV_KEY ?
  133. usage->code : KEY_RESERVED;
  134. ke->index = index;
  135. scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
  136. ke->len = sizeof(scancode);
  137. memcpy(ke->scancode, &scancode, sizeof(scancode));
  138. return 0;
  139. }
  140. return -EINVAL;
  141. }
  142. static int hidinput_setkeycode(struct input_dev *dev,
  143. const struct input_keymap_entry *ke,
  144. unsigned int *old_keycode)
  145. {
  146. struct hid_device *hid = input_get_drvdata(dev);
  147. struct hid_usage *usage;
  148. usage = hidinput_locate_usage(hid, ke, NULL);
  149. if (usage) {
  150. *old_keycode = usage->type == EV_KEY ?
  151. usage->code : KEY_RESERVED;
  152. usage->code = ke->keycode;
  153. clear_bit(*old_keycode, dev->keybit);
  154. set_bit(usage->code, dev->keybit);
  155. dbg_hid("Assigned keycode %d to HID usage code %x\n",
  156. usage->code, usage->hid);
  157. /*
  158. * Set the keybit for the old keycode if the old keycode is used
  159. * by another key
  160. */
  161. if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
  162. set_bit(*old_keycode, dev->keybit);
  163. return 0;
  164. }
  165. return -EINVAL;
  166. }
  167. /**
  168. * hidinput_calc_abs_res - calculate an absolute axis resolution
  169. * @field: the HID report field to calculate resolution for
  170. * @code: axis code
  171. *
  172. * The formula is:
  173. * (logical_maximum - logical_minimum)
  174. * resolution = ----------------------------------------------------------
  175. * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
  176. *
  177. * as seen in the HID specification v1.11 6.2.2.7 Global Items.
  178. *
  179. * Only exponent 1 length units are processed. Centimeters and inches are
  180. * converted to millimeters. Degrees are converted to radians.
  181. */
  182. __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
  183. {
  184. __s32 unit_exponent = field->unit_exponent;
  185. __s32 logical_extents = field->logical_maximum -
  186. field->logical_minimum;
  187. __s32 physical_extents = field->physical_maximum -
  188. field->physical_minimum;
  189. __s32 prev;
  190. /* Check if the extents are sane */
  191. if (logical_extents <= 0 || physical_extents <= 0)
  192. return 0;
  193. /*
  194. * Verify and convert units.
  195. * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
  196. */
  197. switch (code) {
  198. case ABS_X:
  199. case ABS_Y:
  200. case ABS_Z:
  201. case ABS_MT_POSITION_X:
  202. case ABS_MT_POSITION_Y:
  203. case ABS_MT_TOOL_X:
  204. case ABS_MT_TOOL_Y:
  205. case ABS_MT_TOUCH_MAJOR:
  206. case ABS_MT_TOUCH_MINOR:
  207. if (field->unit == 0x11) { /* If centimeters */
  208. /* Convert to millimeters */
  209. unit_exponent += 1;
  210. } else if (field->unit == 0x13) { /* If inches */
  211. /* Convert to millimeters */
  212. prev = physical_extents;
  213. physical_extents *= 254;
  214. if (physical_extents < prev)
  215. return 0;
  216. unit_exponent -= 1;
  217. } else {
  218. return 0;
  219. }
  220. break;
  221. case ABS_RX:
  222. case ABS_RY:
  223. case ABS_RZ:
  224. case ABS_TILT_X:
  225. case ABS_TILT_Y:
  226. if (field->unit == 0x14) { /* If degrees */
  227. /* Convert to radians */
  228. prev = logical_extents;
  229. logical_extents *= 573;
  230. if (logical_extents < prev)
  231. return 0;
  232. unit_exponent += 1;
  233. } else if (field->unit != 0x12) { /* If not radians */
  234. return 0;
  235. }
  236. break;
  237. default:
  238. return 0;
  239. }
  240. /* Apply negative unit exponent */
  241. for (; unit_exponent < 0; unit_exponent++) {
  242. prev = logical_extents;
  243. logical_extents *= 10;
  244. if (logical_extents < prev)
  245. return 0;
  246. }
  247. /* Apply positive unit exponent */
  248. for (; unit_exponent > 0; unit_exponent--) {
  249. prev = physical_extents;
  250. physical_extents *= 10;
  251. if (physical_extents < prev)
  252. return 0;
  253. }
  254. /* Calculate resolution */
  255. return DIV_ROUND_CLOSEST(logical_extents, physical_extents);
  256. }
  257. EXPORT_SYMBOL_GPL(hidinput_calc_abs_res);
  258. #ifdef CONFIG_HID_BATTERY_STRENGTH
  259. static enum power_supply_property hidinput_battery_props[] = {
  260. POWER_SUPPLY_PROP_PRESENT,
  261. POWER_SUPPLY_PROP_ONLINE,
  262. POWER_SUPPLY_PROP_CAPACITY,
  263. POWER_SUPPLY_PROP_MODEL_NAME,
  264. POWER_SUPPLY_PROP_STATUS,
  265. POWER_SUPPLY_PROP_SCOPE,
  266. };
  267. #define HID_BATTERY_QUIRK_PERCENT (1 << 0) /* always reports percent */
  268. #define HID_BATTERY_QUIRK_FEATURE (1 << 1) /* ask for feature report */
  269. static const struct hid_device_id hid_battery_quirks[] = {
  270. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  271. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
  272. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  273. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  274. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
  275. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  276. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  277. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI),
  278. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  279. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  280. USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO),
  281. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  282. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  283. USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
  284. HID_BATTERY_QUIRK_PERCENT | HID_BATTERY_QUIRK_FEATURE },
  285. {}
  286. };
  287. static unsigned find_battery_quirk(struct hid_device *hdev)
  288. {
  289. unsigned quirks = 0;
  290. const struct hid_device_id *match;
  291. match = hid_match_id(hdev, hid_battery_quirks);
  292. if (match != NULL)
  293. quirks = match->driver_data;
  294. return quirks;
  295. }
  296. static int hidinput_get_battery_property(struct power_supply *psy,
  297. enum power_supply_property prop,
  298. union power_supply_propval *val)
  299. {
  300. struct hid_device *dev = power_supply_get_drvdata(psy);
  301. int ret = 0;
  302. __u8 *buf;
  303. switch (prop) {
  304. case POWER_SUPPLY_PROP_PRESENT:
  305. case POWER_SUPPLY_PROP_ONLINE:
  306. val->intval = 1;
  307. break;
  308. case POWER_SUPPLY_PROP_CAPACITY:
  309. buf = kmalloc(2 * sizeof(__u8), GFP_KERNEL);
  310. if (!buf) {
  311. ret = -ENOMEM;
  312. break;
  313. }
  314. ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 2,
  315. dev->battery_report_type,
  316. HID_REQ_GET_REPORT);
  317. if (ret != 2) {
  318. ret = -ENODATA;
  319. kfree(buf);
  320. break;
  321. }
  322. ret = 0;
  323. if (dev->battery_min < dev->battery_max &&
  324. buf[1] >= dev->battery_min &&
  325. buf[1] <= dev->battery_max)
  326. val->intval = (100 * (buf[1] - dev->battery_min)) /
  327. (dev->battery_max - dev->battery_min);
  328. kfree(buf);
  329. break;
  330. case POWER_SUPPLY_PROP_MODEL_NAME:
  331. val->strval = dev->name;
  332. break;
  333. case POWER_SUPPLY_PROP_STATUS:
  334. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  335. break;
  336. case POWER_SUPPLY_PROP_SCOPE:
  337. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  338. break;
  339. default:
  340. ret = -EINVAL;
  341. break;
  342. }
  343. return ret;
  344. }
  345. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type, struct hid_field *field)
  346. {
  347. struct power_supply_desc *psy_desc = NULL;
  348. struct power_supply_config psy_cfg = { .drv_data = dev, };
  349. unsigned quirks;
  350. s32 min, max;
  351. if (field->usage->hid != HID_DC_BATTERYSTRENGTH)
  352. return false; /* no match */
  353. if (dev->battery != NULL)
  354. goto out; /* already initialized? */
  355. psy_desc = kzalloc(sizeof(*psy_desc), GFP_KERNEL);
  356. if (psy_desc == NULL)
  357. goto out;
  358. psy_desc->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
  359. if (psy_desc->name == NULL) {
  360. kfree(psy_desc);
  361. goto out;
  362. }
  363. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  364. psy_desc->properties = hidinput_battery_props;
  365. psy_desc->num_properties = ARRAY_SIZE(hidinput_battery_props);
  366. psy_desc->use_for_apm = 0;
  367. psy_desc->get_property = hidinput_get_battery_property;
  368. quirks = find_battery_quirk(dev);
  369. hid_dbg(dev, "device %x:%x:%x %d quirks %d\n",
  370. dev->bus, dev->vendor, dev->product, dev->version, quirks);
  371. min = field->logical_minimum;
  372. max = field->logical_maximum;
  373. if (quirks & HID_BATTERY_QUIRK_PERCENT) {
  374. min = 0;
  375. max = 100;
  376. }
  377. if (quirks & HID_BATTERY_QUIRK_FEATURE)
  378. report_type = HID_FEATURE_REPORT;
  379. dev->battery_min = min;
  380. dev->battery_max = max;
  381. dev->battery_report_type = report_type;
  382. dev->battery_report_id = field->report->id;
  383. dev->battery = power_supply_register(&dev->dev, psy_desc, &psy_cfg);
  384. if (IS_ERR(dev->battery)) {
  385. hid_warn(dev, "can't register power supply: %ld\n",
  386. PTR_ERR(dev->battery));
  387. kfree(psy_desc->name);
  388. kfree(psy_desc);
  389. dev->battery = NULL;
  390. } else {
  391. power_supply_powers(dev->battery, &dev->dev);
  392. }
  393. out:
  394. return true;
  395. }
  396. static void hidinput_cleanup_battery(struct hid_device *dev)
  397. {
  398. const struct power_supply_desc *psy_desc;
  399. if (!dev->battery)
  400. return;
  401. psy_desc = dev->battery->desc;
  402. power_supply_unregister(dev->battery);
  403. kfree(psy_desc->name);
  404. kfree(psy_desc);
  405. dev->battery = NULL;
  406. }
  407. #else /* !CONFIG_HID_BATTERY_STRENGTH */
  408. static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
  409. struct hid_field *field)
  410. {
  411. return false;
  412. }
  413. static void hidinput_cleanup_battery(struct hid_device *dev)
  414. {
  415. }
  416. #endif /* CONFIG_HID_BATTERY_STRENGTH */
  417. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  418. struct hid_usage *usage)
  419. {
  420. struct input_dev *input = hidinput->input;
  421. struct hid_device *device = input_get_drvdata(input);
  422. int max = 0, code;
  423. unsigned long *bit = NULL;
  424. field->hidinput = hidinput;
  425. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  426. goto ignore;
  427. /* Ignore if report count is out of bounds. */
  428. if (field->report_count < 1)
  429. goto ignore;
  430. /* only LED usages are supported in output fields */
  431. if (field->report_type == HID_OUTPUT_REPORT &&
  432. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  433. goto ignore;
  434. }
  435. if (device->driver->input_mapping) {
  436. int ret = device->driver->input_mapping(device, hidinput, field,
  437. usage, &bit, &max);
  438. if (ret > 0)
  439. goto mapped;
  440. if (ret < 0)
  441. goto ignore;
  442. }
  443. switch (usage->hid & HID_USAGE_PAGE) {
  444. case HID_UP_UNDEFINED:
  445. goto ignore;
  446. case HID_UP_KEYBOARD:
  447. set_bit(EV_REP, input->evbit);
  448. if ((usage->hid & HID_USAGE) < 256) {
  449. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  450. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  451. } else
  452. map_key(KEY_UNKNOWN);
  453. break;
  454. case HID_UP_BUTTON:
  455. code = ((usage->hid - 1) & HID_USAGE);
  456. switch (field->application) {
  457. case HID_GD_MOUSE:
  458. case HID_GD_POINTER: code += BTN_MOUSE; break;
  459. case HID_GD_JOYSTICK:
  460. if (code <= 0xf)
  461. code += BTN_JOYSTICK;
  462. else
  463. code += BTN_TRIGGER_HAPPY - 0x10;
  464. break;
  465. case HID_GD_GAMEPAD:
  466. if (code <= 0xf)
  467. code += BTN_GAMEPAD;
  468. else
  469. code += BTN_TRIGGER_HAPPY - 0x10;
  470. break;
  471. default:
  472. switch (field->physical) {
  473. case HID_GD_MOUSE:
  474. case HID_GD_POINTER: code += BTN_MOUSE; break;
  475. case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
  476. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  477. default: code += BTN_MISC;
  478. }
  479. }
  480. map_key(code);
  481. break;
  482. case HID_UP_SIMULATION:
  483. switch (usage->hid & 0xffff) {
  484. case 0xba: map_abs(ABS_RUDDER); break;
  485. case 0xbb: map_abs(ABS_THROTTLE); break;
  486. case 0xc4: map_abs(ABS_GAS); break;
  487. case 0xc5: map_abs(ABS_BRAKE); break;
  488. case 0xc8: map_abs(ABS_WHEEL); break;
  489. default: goto ignore;
  490. }
  491. break;
  492. case HID_UP_GENDESK:
  493. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  494. switch (usage->hid & 0xf) {
  495. case 0x1: map_key_clear(KEY_POWER); break;
  496. case 0x2: map_key_clear(KEY_SLEEP); break;
  497. case 0x3: map_key_clear(KEY_WAKEUP); break;
  498. case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
  499. case 0x5: map_key_clear(KEY_MENU); break;
  500. case 0x6: map_key_clear(KEY_PROG1); break;
  501. case 0x7: map_key_clear(KEY_HELP); break;
  502. case 0x8: map_key_clear(KEY_EXIT); break;
  503. case 0x9: map_key_clear(KEY_SELECT); break;
  504. case 0xa: map_key_clear(KEY_RIGHT); break;
  505. case 0xb: map_key_clear(KEY_LEFT); break;
  506. case 0xc: map_key_clear(KEY_UP); break;
  507. case 0xd: map_key_clear(KEY_DOWN); break;
  508. case 0xe: map_key_clear(KEY_POWER2); break;
  509. case 0xf: map_key_clear(KEY_RESTART); break;
  510. default: goto unknown;
  511. }
  512. break;
  513. }
  514. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  515. switch (usage->hid) {
  516. case HID_GD_UP: usage->hat_dir = 1; break;
  517. case HID_GD_DOWN: usage->hat_dir = 5; break;
  518. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  519. case HID_GD_LEFT: usage->hat_dir = 7; break;
  520. default: goto unknown;
  521. }
  522. if (field->dpad) {
  523. map_abs(field->dpad);
  524. goto ignore;
  525. }
  526. map_abs(ABS_HAT0X);
  527. break;
  528. }
  529. switch (usage->hid) {
  530. /* These usage IDs map directly to the usage codes. */
  531. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  532. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  533. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  534. map_rel(usage->hid & 0xf);
  535. else
  536. map_abs_clear(usage->hid & 0xf);
  537. break;
  538. case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
  539. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  540. map_rel(usage->hid & 0xf);
  541. else
  542. map_abs(usage->hid & 0xf);
  543. break;
  544. case HID_GD_HATSWITCH:
  545. usage->hat_min = field->logical_minimum;
  546. usage->hat_max = field->logical_maximum;
  547. map_abs(ABS_HAT0X);
  548. break;
  549. case HID_GD_START: map_key_clear(BTN_START); break;
  550. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  551. default: goto unknown;
  552. }
  553. break;
  554. case HID_UP_LED:
  555. switch (usage->hid & 0xffff) { /* HID-Value: */
  556. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  557. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  558. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  559. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  560. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  561. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  562. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  563. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  564. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  565. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  566. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  567. default: goto ignore;
  568. }
  569. break;
  570. case HID_UP_DIGITIZER:
  571. switch (usage->hid & 0xff) {
  572. case 0x00: /* Undefined */
  573. goto ignore;
  574. case 0x30: /* TipPressure */
  575. if (!test_bit(BTN_TOUCH, input->keybit)) {
  576. device->quirks |= HID_QUIRK_NOTOUCH;
  577. set_bit(EV_KEY, input->evbit);
  578. set_bit(BTN_TOUCH, input->keybit);
  579. }
  580. map_abs_clear(ABS_PRESSURE);
  581. break;
  582. case 0x32: /* InRange */
  583. switch (field->physical & 0xff) {
  584. case 0x21: map_key(BTN_TOOL_MOUSE); break;
  585. case 0x22: map_key(BTN_TOOL_FINGER); break;
  586. default: map_key(BTN_TOOL_PEN); break;
  587. }
  588. break;
  589. case 0x3c: /* Invert */
  590. map_key_clear(BTN_TOOL_RUBBER);
  591. break;
  592. case 0x3d: /* X Tilt */
  593. map_abs_clear(ABS_TILT_X);
  594. break;
  595. case 0x3e: /* Y Tilt */
  596. map_abs_clear(ABS_TILT_Y);
  597. break;
  598. case 0x33: /* Touch */
  599. case 0x42: /* TipSwitch */
  600. case 0x43: /* TipSwitch2 */
  601. device->quirks &= ~HID_QUIRK_NOTOUCH;
  602. map_key_clear(BTN_TOUCH);
  603. break;
  604. case 0x44: /* BarrelSwitch */
  605. map_key_clear(BTN_STYLUS);
  606. break;
  607. case 0x46: /* TabletPick */
  608. case 0x5a: /* SecondaryBarrelSwitch */
  609. map_key_clear(BTN_STYLUS2);
  610. break;
  611. case 0x5b: /* TransducerSerialNumber */
  612. usage->type = EV_MSC;
  613. usage->code = MSC_SERIAL;
  614. bit = input->mscbit;
  615. max = MSC_MAX;
  616. break;
  617. default: goto unknown;
  618. }
  619. break;
  620. case HID_UP_TELEPHONY:
  621. switch (usage->hid & HID_USAGE) {
  622. case 0x2f: map_key_clear(KEY_MICMUTE); break;
  623. case 0xb0: map_key_clear(KEY_NUMERIC_0); break;
  624. case 0xb1: map_key_clear(KEY_NUMERIC_1); break;
  625. case 0xb2: map_key_clear(KEY_NUMERIC_2); break;
  626. case 0xb3: map_key_clear(KEY_NUMERIC_3); break;
  627. case 0xb4: map_key_clear(KEY_NUMERIC_4); break;
  628. case 0xb5: map_key_clear(KEY_NUMERIC_5); break;
  629. case 0xb6: map_key_clear(KEY_NUMERIC_6); break;
  630. case 0xb7: map_key_clear(KEY_NUMERIC_7); break;
  631. case 0xb8: map_key_clear(KEY_NUMERIC_8); break;
  632. case 0xb9: map_key_clear(KEY_NUMERIC_9); break;
  633. case 0xba: map_key_clear(KEY_NUMERIC_STAR); break;
  634. case 0xbb: map_key_clear(KEY_NUMERIC_POUND); break;
  635. case 0xbc: map_key_clear(KEY_NUMERIC_A); break;
  636. case 0xbd: map_key_clear(KEY_NUMERIC_B); break;
  637. case 0xbe: map_key_clear(KEY_NUMERIC_C); break;
  638. case 0xbf: map_key_clear(KEY_NUMERIC_D); break;
  639. default: goto ignore;
  640. }
  641. break;
  642. case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
  643. switch (usage->hid & HID_USAGE) {
  644. case 0x000: goto ignore;
  645. case 0x030: map_key_clear(KEY_POWER); break;
  646. case 0x031: map_key_clear(KEY_RESTART); break;
  647. case 0x032: map_key_clear(KEY_SLEEP); break;
  648. case 0x034: map_key_clear(KEY_SLEEP); break;
  649. case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  650. case 0x036: map_key_clear(BTN_MISC); break;
  651. case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
  652. case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
  653. case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
  654. case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
  655. case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
  656. case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
  657. case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
  658. case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
  659. case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
  660. case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
  661. case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
  662. case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
  663. case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
  664. case 0x069: map_key_clear(KEY_RED); break;
  665. case 0x06a: map_key_clear(KEY_GREEN); break;
  666. case 0x06b: map_key_clear(KEY_BLUE); break;
  667. case 0x06c: map_key_clear(KEY_YELLOW); break;
  668. case 0x06d: map_key_clear(KEY_ZOOM); break;
  669. case 0x06f: map_key_clear(KEY_BRIGHTNESSUP); break;
  670. case 0x070: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  671. case 0x072: map_key_clear(KEY_BRIGHTNESS_TOGGLE); break;
  672. case 0x073: map_key_clear(KEY_BRIGHTNESS_MIN); break;
  673. case 0x074: map_key_clear(KEY_BRIGHTNESS_MAX); break;
  674. case 0x075: map_key_clear(KEY_BRIGHTNESS_AUTO); break;
  675. case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
  676. case 0x083: map_key_clear(KEY_LAST); break;
  677. case 0x084: map_key_clear(KEY_ENTER); break;
  678. case 0x088: map_key_clear(KEY_PC); break;
  679. case 0x089: map_key_clear(KEY_TV); break;
  680. case 0x08a: map_key_clear(KEY_WWW); break;
  681. case 0x08b: map_key_clear(KEY_DVD); break;
  682. case 0x08c: map_key_clear(KEY_PHONE); break;
  683. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  684. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  685. case 0x08f: map_key_clear(KEY_GAMES); break;
  686. case 0x090: map_key_clear(KEY_MEMO); break;
  687. case 0x091: map_key_clear(KEY_CD); break;
  688. case 0x092: map_key_clear(KEY_VCR); break;
  689. case 0x093: map_key_clear(KEY_TUNER); break;
  690. case 0x094: map_key_clear(KEY_EXIT); break;
  691. case 0x095: map_key_clear(KEY_HELP); break;
  692. case 0x096: map_key_clear(KEY_TAPE); break;
  693. case 0x097: map_key_clear(KEY_TV2); break;
  694. case 0x098: map_key_clear(KEY_SAT); break;
  695. case 0x09a: map_key_clear(KEY_PVR); break;
  696. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  697. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  698. case 0x0a0: map_key_clear(KEY_VCR2); break;
  699. case 0x0b0: map_key_clear(KEY_PLAY); break;
  700. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  701. case 0x0b2: map_key_clear(KEY_RECORD); break;
  702. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  703. case 0x0b4: map_key_clear(KEY_REWIND); break;
  704. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  705. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  706. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  707. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  708. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  709. case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
  710. case 0x0bf: map_key_clear(KEY_SLOW); break;
  711. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  712. case 0x0cf: map_key_clear(KEY_VOICECOMMAND); break;
  713. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  714. case 0x0e2: map_key_clear(KEY_MUTE); break;
  715. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  716. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  717. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  718. case 0x0f5: map_key_clear(KEY_SLOW); break;
  719. case 0x181: map_key_clear(KEY_BUTTONCONFIG); break;
  720. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  721. case 0x183: map_key_clear(KEY_CONFIG); break;
  722. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  723. case 0x185: map_key_clear(KEY_EDITOR); break;
  724. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  725. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  726. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  727. case 0x189: map_key_clear(KEY_DATABASE); break;
  728. case 0x18a: map_key_clear(KEY_MAIL); break;
  729. case 0x18b: map_key_clear(KEY_NEWS); break;
  730. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  731. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  732. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  733. case 0x18f: map_key_clear(KEY_TASKMANAGER); break;
  734. case 0x190: map_key_clear(KEY_JOURNAL); break;
  735. case 0x191: map_key_clear(KEY_FINANCE); break;
  736. case 0x192: map_key_clear(KEY_CALC); break;
  737. case 0x193: map_key_clear(KEY_PLAYER); break;
  738. case 0x194: map_key_clear(KEY_FILE); break;
  739. case 0x196: map_key_clear(KEY_WWW); break;
  740. case 0x199: map_key_clear(KEY_CHAT); break;
  741. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  742. case 0x19e: map_key_clear(KEY_COFFEE); break;
  743. case 0x19f: map_key_clear(KEY_CONTROLPANEL); break;
  744. case 0x1a2: map_key_clear(KEY_APPSELECT); break;
  745. case 0x1a3: map_key_clear(KEY_NEXT); break;
  746. case 0x1a4: map_key_clear(KEY_PREVIOUS); break;
  747. case 0x1a6: map_key_clear(KEY_HELP); break;
  748. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  749. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  750. case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
  751. case 0x1b1: map_key_clear(KEY_SCREENSAVER); break;
  752. case 0x1b4: map_key_clear(KEY_FILE); break;
  753. case 0x1b6: map_key_clear(KEY_IMAGES); break;
  754. case 0x1b7: map_key_clear(KEY_AUDIO); break;
  755. case 0x1b8: map_key_clear(KEY_VIDEO); break;
  756. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  757. case 0x1bd: map_key_clear(KEY_INFO); break;
  758. case 0x201: map_key_clear(KEY_NEW); break;
  759. case 0x202: map_key_clear(KEY_OPEN); break;
  760. case 0x203: map_key_clear(KEY_CLOSE); break;
  761. case 0x204: map_key_clear(KEY_EXIT); break;
  762. case 0x207: map_key_clear(KEY_SAVE); break;
  763. case 0x208: map_key_clear(KEY_PRINT); break;
  764. case 0x209: map_key_clear(KEY_PROPS); break;
  765. case 0x21a: map_key_clear(KEY_UNDO); break;
  766. case 0x21b: map_key_clear(KEY_COPY); break;
  767. case 0x21c: map_key_clear(KEY_CUT); break;
  768. case 0x21d: map_key_clear(KEY_PASTE); break;
  769. case 0x21f: map_key_clear(KEY_FIND); break;
  770. case 0x221: map_key_clear(KEY_SEARCH); break;
  771. case 0x222: map_key_clear(KEY_GOTO); break;
  772. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  773. case 0x224: map_key_clear(KEY_BACK); break;
  774. case 0x225: map_key_clear(KEY_FORWARD); break;
  775. case 0x226: map_key_clear(KEY_STOP); break;
  776. case 0x227: map_key_clear(KEY_REFRESH); break;
  777. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  778. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  779. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  780. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  781. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  782. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  783. case 0x238: map_rel(REL_HWHEEL); break;
  784. case 0x23d: map_key_clear(KEY_EDIT); break;
  785. case 0x25f: map_key_clear(KEY_CANCEL); break;
  786. case 0x269: map_key_clear(KEY_INSERT); break;
  787. case 0x26a: map_key_clear(KEY_DELETE); break;
  788. case 0x279: map_key_clear(KEY_REDO); break;
  789. case 0x289: map_key_clear(KEY_REPLY); break;
  790. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  791. case 0x28c: map_key_clear(KEY_SEND); break;
  792. case 0x2c7: map_key_clear(KEY_KBDINPUTASSIST_PREV); break;
  793. case 0x2c8: map_key_clear(KEY_KBDINPUTASSIST_NEXT); break;
  794. case 0x2c9: map_key_clear(KEY_KBDINPUTASSIST_PREVGROUP); break;
  795. case 0x2ca: map_key_clear(KEY_KBDINPUTASSIST_NEXTGROUP); break;
  796. case 0x2cb: map_key_clear(KEY_KBDINPUTASSIST_ACCEPT); break;
  797. case 0x2cc: map_key_clear(KEY_KBDINPUTASSIST_CANCEL); break;
  798. default: map_key_clear(KEY_UNKNOWN);
  799. }
  800. break;
  801. case HID_UP_GENDEVCTRLS:
  802. if (hidinput_setup_battery(device, HID_INPUT_REPORT, field))
  803. goto ignore;
  804. else
  805. goto unknown;
  806. break;
  807. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  808. set_bit(EV_REP, input->evbit);
  809. switch (usage->hid & HID_USAGE) {
  810. case 0x021: map_key_clear(KEY_PRINT); break;
  811. case 0x070: map_key_clear(KEY_HP); break;
  812. case 0x071: map_key_clear(KEY_CAMERA); break;
  813. case 0x072: map_key_clear(KEY_SOUND); break;
  814. case 0x073: map_key_clear(KEY_QUESTION); break;
  815. case 0x080: map_key_clear(KEY_EMAIL); break;
  816. case 0x081: map_key_clear(KEY_CHAT); break;
  817. case 0x082: map_key_clear(KEY_SEARCH); break;
  818. case 0x083: map_key_clear(KEY_CONNECT); break;
  819. case 0x084: map_key_clear(KEY_FINANCE); break;
  820. case 0x085: map_key_clear(KEY_SPORT); break;
  821. case 0x086: map_key_clear(KEY_SHOP); break;
  822. default: goto ignore;
  823. }
  824. break;
  825. case HID_UP_HPVENDOR2:
  826. set_bit(EV_REP, input->evbit);
  827. switch (usage->hid & HID_USAGE) {
  828. case 0x003: map_key_clear(KEY_BRIGHTNESSDOWN); break;
  829. case 0x004: map_key_clear(KEY_BRIGHTNESSUP); break;
  830. default: goto ignore;
  831. }
  832. break;
  833. case HID_UP_MSVENDOR:
  834. goto ignore;
  835. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  836. set_bit(EV_REP, input->evbit);
  837. goto ignore;
  838. case HID_UP_LOGIVENDOR:
  839. goto ignore;
  840. case HID_UP_PID:
  841. switch (usage->hid & HID_USAGE) {
  842. case 0xa4: map_key_clear(BTN_DEAD); break;
  843. default: goto ignore;
  844. }
  845. break;
  846. default:
  847. unknown:
  848. if (field->report_size == 1) {
  849. if (field->report->type == HID_OUTPUT_REPORT) {
  850. map_led(LED_MISC);
  851. break;
  852. }
  853. map_key(BTN_MISC);
  854. break;
  855. }
  856. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  857. map_rel(REL_MISC);
  858. break;
  859. }
  860. map_abs(ABS_MISC);
  861. break;
  862. }
  863. mapped:
  864. if (device->driver->input_mapped && device->driver->input_mapped(device,
  865. hidinput, field, usage, &bit, &max) < 0)
  866. goto ignore;
  867. set_bit(usage->type, input->evbit);
  868. while (usage->code <= max && test_and_set_bit(usage->code, bit))
  869. usage->code = find_next_zero_bit(bit, max + 1, usage->code);
  870. if (usage->code > max)
  871. goto ignore;
  872. if (usage->type == EV_ABS) {
  873. int a = field->logical_minimum;
  874. int b = field->logical_maximum;
  875. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  876. a = field->logical_minimum = 0;
  877. b = field->logical_maximum = 255;
  878. }
  879. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  880. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  881. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  882. input_abs_set_res(input, usage->code,
  883. hidinput_calc_abs_res(field, usage->code));
  884. /* use a larger default input buffer for MT devices */
  885. if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
  886. input_set_events_per_packet(input, 60);
  887. }
  888. if (usage->type == EV_ABS &&
  889. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  890. int i;
  891. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  892. input_set_abs_params(input, i, -1, 1, 0, 0);
  893. set_bit(i, input->absbit);
  894. }
  895. if (usage->hat_dir && !field->dpad)
  896. field->dpad = usage->code;
  897. }
  898. /* for those devices which produce Consumer volume usage as relative,
  899. * we emulate pressing volumeup/volumedown appropriate number of times
  900. * in hidinput_hid_event()
  901. */
  902. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  903. (usage->code == ABS_VOLUME)) {
  904. set_bit(KEY_VOLUMEUP, input->keybit);
  905. set_bit(KEY_VOLUMEDOWN, input->keybit);
  906. }
  907. if (usage->type == EV_KEY) {
  908. set_bit(EV_MSC, input->evbit);
  909. set_bit(MSC_SCAN, input->mscbit);
  910. }
  911. ignore:
  912. return;
  913. }
  914. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  915. {
  916. struct input_dev *input;
  917. unsigned *quirks = &hid->quirks;
  918. if (!field->hidinput)
  919. return;
  920. input = field->hidinput->input;
  921. if (!usage->type)
  922. return;
  923. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  924. int hat_dir = usage->hat_dir;
  925. if (!hat_dir)
  926. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  927. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  928. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  929. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  930. return;
  931. }
  932. if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
  933. *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
  934. return;
  935. }
  936. if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
  937. if (value) {
  938. input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
  939. return;
  940. }
  941. input_event(input, usage->type, usage->code, 0);
  942. input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
  943. return;
  944. }
  945. if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
  946. int a = field->logical_minimum;
  947. int b = field->logical_maximum;
  948. input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
  949. }
  950. if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
  951. dbg_hid("Maximum Effects - %d\n",value);
  952. return;
  953. }
  954. if (usage->hid == (HID_UP_PID | 0x7fUL)) {
  955. dbg_hid("PID Pool Report\n");
  956. return;
  957. }
  958. if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  959. return;
  960. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  961. (usage->code == ABS_VOLUME)) {
  962. int count = abs(value);
  963. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  964. int i;
  965. for (i = 0; i < count; i++) {
  966. input_event(input, EV_KEY, direction, 1);
  967. input_sync(input);
  968. input_event(input, EV_KEY, direction, 0);
  969. input_sync(input);
  970. }
  971. return;
  972. }
  973. /*
  974. * Ignore out-of-range values as per HID specification,
  975. * section 5.10 and 6.2.25, when NULL state bit is present.
  976. * When it's not, clamp the value to match Microsoft's input
  977. * driver as mentioned in "Required HID usages for digitizers":
  978. * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
  979. *
  980. * The logical_minimum < logical_maximum check is done so that we
  981. * don't unintentionally discard values sent by devices which
  982. * don't specify logical min and max.
  983. */
  984. if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
  985. (field->logical_minimum < field->logical_maximum)) {
  986. if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
  987. (value < field->logical_minimum ||
  988. value > field->logical_maximum)) {
  989. dbg_hid("Ignoring out-of-range value %x\n", value);
  990. return;
  991. }
  992. value = clamp(value,
  993. field->logical_minimum,
  994. field->logical_maximum);
  995. }
  996. /*
  997. * Ignore reports for absolute data if the data didn't change. This is
  998. * not only an optimization but also fixes 'dead' key reports. Some
  999. * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
  1000. * 0x31 and 0x32) report multiple keys, even though a localized keyboard
  1001. * can only have one of them physically available. The 'dead' keys
  1002. * report constant 0. As all map to the same keycode, they'd confuse
  1003. * the input layer. If we filter the 'dead' keys on the HID level, we
  1004. * skip the keycode translation and only forward real events.
  1005. */
  1006. if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
  1007. HID_MAIN_ITEM_BUFFERED_BYTE)) &&
  1008. (field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1009. usage->usage_index < field->maxusage &&
  1010. value == field->value[usage->usage_index])
  1011. return;
  1012. /* report the usage code as scancode if the key status has changed */
  1013. if (usage->type == EV_KEY &&
  1014. (!test_bit(usage->code, input->key)) == value)
  1015. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  1016. input_event(input, usage->type, usage->code, value);
  1017. if ((field->flags & HID_MAIN_ITEM_RELATIVE) &&
  1018. usage->type == EV_KEY && value) {
  1019. input_sync(input);
  1020. input_event(input, usage->type, usage->code, 0);
  1021. }
  1022. }
  1023. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  1024. {
  1025. struct hid_input *hidinput;
  1026. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  1027. return;
  1028. list_for_each_entry(hidinput, &hid->inputs, list)
  1029. input_sync(hidinput->input);
  1030. }
  1031. EXPORT_SYMBOL_GPL(hidinput_report_event);
  1032. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  1033. {
  1034. struct hid_report *report;
  1035. int i, j;
  1036. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  1037. for (i = 0; i < report->maxfield; i++) {
  1038. *field = report->field[i];
  1039. for (j = 0; j < (*field)->maxusage; j++)
  1040. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  1041. return j;
  1042. }
  1043. }
  1044. return -1;
  1045. }
  1046. EXPORT_SYMBOL_GPL(hidinput_find_field);
  1047. struct hid_field *hidinput_get_led_field(struct hid_device *hid)
  1048. {
  1049. struct hid_report *report;
  1050. struct hid_field *field;
  1051. int i, j;
  1052. list_for_each_entry(report,
  1053. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1054. list) {
  1055. for (i = 0; i < report->maxfield; i++) {
  1056. field = report->field[i];
  1057. for (j = 0; j < field->maxusage; j++)
  1058. if (field->usage[j].type == EV_LED)
  1059. return field;
  1060. }
  1061. }
  1062. return NULL;
  1063. }
  1064. EXPORT_SYMBOL_GPL(hidinput_get_led_field);
  1065. unsigned int hidinput_count_leds(struct hid_device *hid)
  1066. {
  1067. struct hid_report *report;
  1068. struct hid_field *field;
  1069. int i, j;
  1070. unsigned int count = 0;
  1071. list_for_each_entry(report,
  1072. &hid->report_enum[HID_OUTPUT_REPORT].report_list,
  1073. list) {
  1074. for (i = 0; i < report->maxfield; i++) {
  1075. field = report->field[i];
  1076. for (j = 0; j < field->maxusage; j++)
  1077. if (field->usage[j].type == EV_LED &&
  1078. field->value[j])
  1079. count += 1;
  1080. }
  1081. }
  1082. return count;
  1083. }
  1084. EXPORT_SYMBOL_GPL(hidinput_count_leds);
  1085. static void hidinput_led_worker(struct work_struct *work)
  1086. {
  1087. struct hid_device *hid = container_of(work, struct hid_device,
  1088. led_work);
  1089. struct hid_field *field;
  1090. struct hid_report *report;
  1091. int ret;
  1092. u32 len;
  1093. __u8 *buf;
  1094. field = hidinput_get_led_field(hid);
  1095. if (!field)
  1096. return;
  1097. /*
  1098. * field->report is accessed unlocked regarding HID core. So there might
  1099. * be another incoming SET-LED request from user-space, which changes
  1100. * the LED state while we assemble our outgoing buffer. However, this
  1101. * doesn't matter as hid_output_report() correctly converts it into a
  1102. * boolean value no matter what information is currently set on the LED
  1103. * field (even garbage). So the remote device will always get a valid
  1104. * request.
  1105. * And in case we send a wrong value, a next led worker is spawned
  1106. * for every SET-LED request so the following worker will send the
  1107. * correct value, guaranteed!
  1108. */
  1109. report = field->report;
  1110. /* use custom SET_REPORT request if possible (asynchronous) */
  1111. if (hid->ll_driver->request)
  1112. return hid->ll_driver->request(hid, report, HID_REQ_SET_REPORT);
  1113. /* fall back to generic raw-output-report */
  1114. len = hid_report_len(report);
  1115. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  1116. if (!buf)
  1117. return;
  1118. hid_output_report(report, buf);
  1119. /* synchronous output report */
  1120. ret = hid_hw_output_report(hid, buf, len);
  1121. if (ret == -ENOSYS)
  1122. hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
  1123. HID_REQ_SET_REPORT);
  1124. kfree(buf);
  1125. }
  1126. static int hidinput_input_event(struct input_dev *dev, unsigned int type,
  1127. unsigned int code, int value)
  1128. {
  1129. struct hid_device *hid = input_get_drvdata(dev);
  1130. struct hid_field *field;
  1131. int offset;
  1132. if (type == EV_FF)
  1133. return input_ff_event(dev, type, code, value);
  1134. if (type != EV_LED)
  1135. return -1;
  1136. if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
  1137. hid_warn(dev, "event field not found\n");
  1138. return -1;
  1139. }
  1140. hid_set_field(field, offset, value);
  1141. schedule_work(&hid->led_work);
  1142. return 0;
  1143. }
  1144. static int hidinput_open(struct input_dev *dev)
  1145. {
  1146. struct hid_device *hid = input_get_drvdata(dev);
  1147. return hid_hw_open(hid);
  1148. }
  1149. static void hidinput_close(struct input_dev *dev)
  1150. {
  1151. struct hid_device *hid = input_get_drvdata(dev);
  1152. hid_hw_close(hid);
  1153. }
  1154. static void report_features(struct hid_device *hid)
  1155. {
  1156. struct hid_driver *drv = hid->driver;
  1157. struct hid_report_enum *rep_enum;
  1158. struct hid_report *rep;
  1159. int i, j;
  1160. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  1161. list_for_each_entry(rep, &rep_enum->report_list, list)
  1162. for (i = 0; i < rep->maxfield; i++) {
  1163. /* Ignore if report count is out of bounds. */
  1164. if (rep->field[i]->report_count < 1)
  1165. continue;
  1166. for (j = 0; j < rep->field[i]->maxusage; j++) {
  1167. /* Verify if Battery Strength feature is available */
  1168. hidinput_setup_battery(hid, HID_FEATURE_REPORT, rep->field[i]);
  1169. if (drv->feature_mapping)
  1170. drv->feature_mapping(hid, rep->field[i],
  1171. rep->field[i]->usage + j);
  1172. }
  1173. }
  1174. }
  1175. static struct hid_input *hidinput_allocate(struct hid_device *hid)
  1176. {
  1177. struct hid_input *hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  1178. struct input_dev *input_dev = input_allocate_device();
  1179. if (!hidinput || !input_dev) {
  1180. kfree(hidinput);
  1181. input_free_device(input_dev);
  1182. hid_err(hid, "Out of memory during hid input probe\n");
  1183. return NULL;
  1184. }
  1185. input_set_drvdata(input_dev, hid);
  1186. input_dev->event = hidinput_input_event;
  1187. input_dev->open = hidinput_open;
  1188. input_dev->close = hidinput_close;
  1189. input_dev->setkeycode = hidinput_setkeycode;
  1190. input_dev->getkeycode = hidinput_getkeycode;
  1191. input_dev->name = hid->name;
  1192. input_dev->phys = hid->phys;
  1193. input_dev->uniq = hid->uniq;
  1194. input_dev->id.bustype = hid->bus;
  1195. input_dev->id.vendor = hid->vendor;
  1196. input_dev->id.product = hid->product;
  1197. input_dev->id.version = hid->version;
  1198. input_dev->dev.parent = &hid->dev;
  1199. hidinput->input = input_dev;
  1200. list_add_tail(&hidinput->list, &hid->inputs);
  1201. return hidinput;
  1202. }
  1203. static bool hidinput_has_been_populated(struct hid_input *hidinput)
  1204. {
  1205. int i;
  1206. unsigned long r = 0;
  1207. for (i = 0; i < BITS_TO_LONGS(EV_CNT); i++)
  1208. r |= hidinput->input->evbit[i];
  1209. for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++)
  1210. r |= hidinput->input->keybit[i];
  1211. for (i = 0; i < BITS_TO_LONGS(REL_CNT); i++)
  1212. r |= hidinput->input->relbit[i];
  1213. for (i = 0; i < BITS_TO_LONGS(ABS_CNT); i++)
  1214. r |= hidinput->input->absbit[i];
  1215. for (i = 0; i < BITS_TO_LONGS(MSC_CNT); i++)
  1216. r |= hidinput->input->mscbit[i];
  1217. for (i = 0; i < BITS_TO_LONGS(LED_CNT); i++)
  1218. r |= hidinput->input->ledbit[i];
  1219. for (i = 0; i < BITS_TO_LONGS(SND_CNT); i++)
  1220. r |= hidinput->input->sndbit[i];
  1221. for (i = 0; i < BITS_TO_LONGS(FF_CNT); i++)
  1222. r |= hidinput->input->ffbit[i];
  1223. for (i = 0; i < BITS_TO_LONGS(SW_CNT); i++)
  1224. r |= hidinput->input->swbit[i];
  1225. return !!r;
  1226. }
  1227. static void hidinput_cleanup_hidinput(struct hid_device *hid,
  1228. struct hid_input *hidinput)
  1229. {
  1230. struct hid_report *report;
  1231. int i, k;
  1232. list_del(&hidinput->list);
  1233. input_free_device(hidinput->input);
  1234. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1235. if (k == HID_OUTPUT_REPORT &&
  1236. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1237. continue;
  1238. list_for_each_entry(report, &hid->report_enum[k].report_list,
  1239. list) {
  1240. for (i = 0; i < report->maxfield; i++)
  1241. if (report->field[i]->hidinput == hidinput)
  1242. report->field[i]->hidinput = NULL;
  1243. }
  1244. }
  1245. kfree(hidinput);
  1246. }
  1247. /*
  1248. * Register the input device; print a message.
  1249. * Configure the input layer interface
  1250. * Read all reports and initialize the absolute field values.
  1251. */
  1252. int hidinput_connect(struct hid_device *hid, unsigned int force)
  1253. {
  1254. struct hid_driver *drv = hid->driver;
  1255. struct hid_report *report;
  1256. struct hid_input *hidinput = NULL;
  1257. int i, j, k;
  1258. INIT_LIST_HEAD(&hid->inputs);
  1259. INIT_WORK(&hid->led_work, hidinput_led_worker);
  1260. if (!force) {
  1261. for (i = 0; i < hid->maxcollection; i++) {
  1262. struct hid_collection *col = &hid->collection[i];
  1263. if (col->type == HID_COLLECTION_APPLICATION ||
  1264. col->type == HID_COLLECTION_PHYSICAL)
  1265. if (IS_INPUT_APPLICATION(col->usage))
  1266. break;
  1267. }
  1268. if (i == hid->maxcollection)
  1269. return -1;
  1270. }
  1271. report_features(hid);
  1272. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  1273. if (k == HID_OUTPUT_REPORT &&
  1274. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  1275. continue;
  1276. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  1277. if (!report->maxfield)
  1278. continue;
  1279. if (!hidinput) {
  1280. hidinput = hidinput_allocate(hid);
  1281. if (!hidinput)
  1282. goto out_unwind;
  1283. }
  1284. for (i = 0; i < report->maxfield; i++)
  1285. for (j = 0; j < report->field[i]->maxusage; j++)
  1286. hidinput_configure_usage(hidinput, report->field[i],
  1287. report->field[i]->usage + j);
  1288. if ((hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
  1289. !hidinput_has_been_populated(hidinput))
  1290. continue;
  1291. if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
  1292. /* This will leave hidinput NULL, so that it
  1293. * allocates another one if we have more inputs on
  1294. * the same interface. Some devices (e.g. Happ's
  1295. * UGCI) cram a lot of unrelated inputs into the
  1296. * same interface. */
  1297. hidinput->report = report;
  1298. if (drv->input_configured &&
  1299. drv->input_configured(hid, hidinput))
  1300. goto out_cleanup;
  1301. if (input_register_device(hidinput->input))
  1302. goto out_cleanup;
  1303. hidinput = NULL;
  1304. }
  1305. }
  1306. }
  1307. if (hidinput && (hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
  1308. !hidinput_has_been_populated(hidinput)) {
  1309. /* no need to register an input device not populated */
  1310. hidinput_cleanup_hidinput(hid, hidinput);
  1311. hidinput = NULL;
  1312. }
  1313. if (list_empty(&hid->inputs)) {
  1314. hid_err(hid, "No inputs registered, leaving\n");
  1315. goto out_unwind;
  1316. }
  1317. if (hidinput) {
  1318. if (drv->input_configured &&
  1319. drv->input_configured(hid, hidinput))
  1320. goto out_cleanup;
  1321. if (input_register_device(hidinput->input))
  1322. goto out_cleanup;
  1323. }
  1324. return 0;
  1325. out_cleanup:
  1326. list_del(&hidinput->list);
  1327. input_free_device(hidinput->input);
  1328. kfree(hidinput);
  1329. out_unwind:
  1330. /* unwind the ones we already registered */
  1331. hidinput_disconnect(hid);
  1332. return -1;
  1333. }
  1334. EXPORT_SYMBOL_GPL(hidinput_connect);
  1335. void hidinput_disconnect(struct hid_device *hid)
  1336. {
  1337. struct hid_input *hidinput, *next;
  1338. hidinput_cleanup_battery(hid);
  1339. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  1340. list_del(&hidinput->list);
  1341. input_unregister_device(hidinput->input);
  1342. kfree(hidinput);
  1343. }
  1344. /* led_work is spawned by input_dev callbacks, but doesn't access the
  1345. * parent input_dev at all. Once all input devices are removed, we
  1346. * know that led_work will never get restarted, so we can cancel it
  1347. * synchronously and are safe. */
  1348. cancel_work_sync(&hid->led_work);
  1349. }
  1350. EXPORT_SYMBOL_GPL(hidinput_disconnect);