hid-input.c 47 KB

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