hid-input.c 42 KB

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