hid-input.c 40 KB

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