hid-input.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. #define unk KEY_UNKNOWN
  32. static const unsigned char hid_keyboard[256] = {
  33. 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
  34. 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
  35. 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
  36. 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
  37. 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
  38. 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
  39. 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
  40. 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
  41. 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
  42. 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
  43. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  44. unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
  45. unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
  46. unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
  47. 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
  48. 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
  49. };
  50. static const struct {
  51. __s32 x;
  52. __s32 y;
  53. } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  54. #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
  55. #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
  56. #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
  57. #define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
  58. #define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  59. &max, EV_ABS, (c))
  60. #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
  61. &max, EV_KEY, (c))
  62. static bool match_scancode(struct hid_usage *usage,
  63. unsigned int cur_idx, unsigned int scancode)
  64. {
  65. return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
  66. }
  67. static bool match_keycode(struct hid_usage *usage,
  68. unsigned int cur_idx, unsigned int keycode)
  69. {
  70. /*
  71. * We should exclude unmapped usages when doing lookup by keycode.
  72. */
  73. return (usage->type == EV_KEY && usage->code == keycode);
  74. }
  75. static bool match_index(struct hid_usage *usage,
  76. unsigned int cur_idx, unsigned int idx)
  77. {
  78. return cur_idx == idx;
  79. }
  80. typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
  81. unsigned int cur_idx, unsigned int val);
  82. static struct hid_usage *hidinput_find_key(struct hid_device *hid,
  83. hid_usage_cmp_t match,
  84. unsigned int value,
  85. unsigned int *usage_idx)
  86. {
  87. unsigned int i, j, k, cur_idx = 0;
  88. struct hid_report *report;
  89. struct hid_usage *usage;
  90. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  91. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  92. for (i = 0; i < report->maxfield; i++) {
  93. for (j = 0; j < report->field[i]->maxusage; j++) {
  94. usage = report->field[i]->usage + j;
  95. if (usage->type == EV_KEY || usage->type == 0) {
  96. if (match(usage, cur_idx, value)) {
  97. if (usage_idx)
  98. *usage_idx = cur_idx;
  99. return usage;
  100. }
  101. cur_idx++;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return NULL;
  108. }
  109. static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
  110. const struct input_keymap_entry *ke,
  111. unsigned int *index)
  112. {
  113. struct hid_usage *usage;
  114. unsigned int scancode;
  115. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  116. usage = hidinput_find_key(hid, match_index, ke->index, index);
  117. else if (input_scancode_to_scalar(ke, &scancode) == 0)
  118. usage = hidinput_find_key(hid, match_scancode, scancode, index);
  119. else
  120. usage = NULL;
  121. return usage;
  122. }
  123. static int hidinput_getkeycode(struct input_dev *dev,
  124. struct input_keymap_entry *ke)
  125. {
  126. struct hid_device *hid = input_get_drvdata(dev);
  127. struct hid_usage *usage;
  128. unsigned int scancode, index;
  129. usage = hidinput_locate_usage(hid, ke, &index);
  130. if (usage) {
  131. ke->keycode = usage->type == EV_KEY ?
  132. usage->code : KEY_RESERVED;
  133. ke->index = index;
  134. scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
  135. ke->len = sizeof(scancode);
  136. memcpy(ke->scancode, &scancode, sizeof(scancode));
  137. return 0;
  138. }
  139. return -EINVAL;
  140. }
  141. static int hidinput_setkeycode(struct input_dev *dev,
  142. const struct input_keymap_entry *ke,
  143. unsigned int *old_keycode)
  144. {
  145. struct hid_device *hid = input_get_drvdata(dev);
  146. struct hid_usage *usage;
  147. usage = hidinput_locate_usage(hid, ke, NULL);
  148. if (usage) {
  149. *old_keycode = usage->type == EV_KEY ?
  150. usage->code : KEY_RESERVED;
  151. usage->code = ke->keycode;
  152. clear_bit(*old_keycode, dev->keybit);
  153. set_bit(usage->code, dev->keybit);
  154. dbg_hid("Assigned keycode %d to HID usage code %x\n",
  155. usage->code, usage->hid);
  156. /*
  157. * Set the keybit for the old keycode if the old keycode is used
  158. * by another key
  159. */
  160. if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
  161. set_bit(*old_keycode, dev->keybit);
  162. return 0;
  163. }
  164. return -EINVAL;
  165. }
  166. /**
  167. * hidinput_calc_abs_res - calculate an absolute axis resolution
  168. * @field: the HID report field to calculate resolution for
  169. * @code: axis code
  170. *
  171. * The formula is:
  172. * (logical_maximum - logical_minimum)
  173. * resolution = ----------------------------------------------------------
  174. * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
  175. *
  176. * as seen in the HID specification v1.11 6.2.2.7 Global Items.
  177. *
  178. * Only exponent 1 length units are processed. Centimeters and inches are
  179. * converted to millimeters. Degrees are converted to radians.
  180. */
  181. static __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
  182. {
  183. __s32 unit_exponent = field->unit_exponent;
  184. __s32 logical_extents = field->logical_maximum -
  185. field->logical_minimum;
  186. __s32 physical_extents = field->physical_maximum -
  187. field->physical_minimum;
  188. __s32 prev;
  189. /* Check if the extents are sane */
  190. if (logical_extents <= 0 || physical_extents <= 0)
  191. return 0;
  192. /*
  193. * Verify and convert units.
  194. * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
  195. */
  196. if (code == ABS_X || code == ABS_Y || code == ABS_Z) {
  197. if (field->unit == 0x11) { /* If centimeters */
  198. /* Convert to millimeters */
  199. unit_exponent += 1;
  200. } else if (field->unit == 0x13) { /* If inches */
  201. /* Convert to millimeters */
  202. prev = physical_extents;
  203. physical_extents *= 254;
  204. if (physical_extents < prev)
  205. return 0;
  206. unit_exponent -= 1;
  207. } else {
  208. return 0;
  209. }
  210. } else if (code == ABS_RX || code == ABS_RY || code == ABS_RZ) {
  211. if (field->unit == 0x14) { /* If degrees */
  212. /* Convert to radians */
  213. prev = logical_extents;
  214. logical_extents *= 573;
  215. if (logical_extents < prev)
  216. return 0;
  217. unit_exponent += 1;
  218. } else if (field->unit != 0x12) { /* If not radians */
  219. return 0;
  220. }
  221. } else {
  222. return 0;
  223. }
  224. /* Apply negative unit exponent */
  225. for (; unit_exponent < 0; unit_exponent++) {
  226. prev = logical_extents;
  227. logical_extents *= 10;
  228. if (logical_extents < prev)
  229. return 0;
  230. }
  231. /* Apply positive unit exponent */
  232. for (; unit_exponent > 0; unit_exponent--) {
  233. prev = physical_extents;
  234. physical_extents *= 10;
  235. if (physical_extents < prev)
  236. return 0;
  237. }
  238. /* Calculate resolution */
  239. return logical_extents / physical_extents;
  240. }
  241. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  242. struct hid_usage *usage)
  243. {
  244. struct input_dev *input = hidinput->input;
  245. struct hid_device *device = input_get_drvdata(input);
  246. int max = 0, code;
  247. unsigned long *bit = NULL;
  248. field->hidinput = hidinput;
  249. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  250. goto ignore;
  251. /* only LED usages are supported in output fields */
  252. if (field->report_type == HID_OUTPUT_REPORT &&
  253. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  254. goto ignore;
  255. }
  256. if (device->driver->input_mapping) {
  257. int ret = device->driver->input_mapping(device, hidinput, field,
  258. usage, &bit, &max);
  259. if (ret > 0)
  260. goto mapped;
  261. if (ret < 0)
  262. goto ignore;
  263. }
  264. switch (usage->hid & HID_USAGE_PAGE) {
  265. case HID_UP_UNDEFINED:
  266. goto ignore;
  267. case HID_UP_KEYBOARD:
  268. set_bit(EV_REP, input->evbit);
  269. if ((usage->hid & HID_USAGE) < 256) {
  270. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  271. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  272. } else
  273. map_key(KEY_UNKNOWN);
  274. break;
  275. case HID_UP_BUTTON:
  276. code = ((usage->hid - 1) & HID_USAGE);
  277. switch (field->application) {
  278. case HID_GD_MOUSE:
  279. case HID_GD_POINTER: code += BTN_MOUSE; break;
  280. case HID_GD_JOYSTICK:
  281. if (code <= 0xf)
  282. code += BTN_JOYSTICK;
  283. else
  284. code += BTN_TRIGGER_HAPPY;
  285. break;
  286. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  287. default:
  288. switch (field->physical) {
  289. case HID_GD_MOUSE:
  290. case HID_GD_POINTER: code += BTN_MOUSE; break;
  291. case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
  292. case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
  293. default: code += BTN_MISC;
  294. }
  295. }
  296. map_key(code);
  297. break;
  298. case HID_UP_SIMULATION:
  299. switch (usage->hid & 0xffff) {
  300. case 0xba: map_abs(ABS_RUDDER); break;
  301. case 0xbb: map_abs(ABS_THROTTLE); break;
  302. case 0xc4: map_abs(ABS_GAS); break;
  303. case 0xc5: map_abs(ABS_BRAKE); break;
  304. case 0xc8: map_abs(ABS_WHEEL); break;
  305. default: goto ignore;
  306. }
  307. break;
  308. case HID_UP_GENDESK:
  309. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  310. switch (usage->hid & 0xf) {
  311. case 0x1: map_key_clear(KEY_POWER); break;
  312. case 0x2: map_key_clear(KEY_SLEEP); break;
  313. case 0x3: map_key_clear(KEY_WAKEUP); break;
  314. case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
  315. case 0x5: map_key_clear(KEY_MENU); break;
  316. case 0x6: map_key_clear(KEY_PROG1); break;
  317. case 0x7: map_key_clear(KEY_HELP); break;
  318. case 0x8: map_key_clear(KEY_EXIT); break;
  319. case 0x9: map_key_clear(KEY_SELECT); break;
  320. case 0xa: map_key_clear(KEY_RIGHT); break;
  321. case 0xb: map_key_clear(KEY_LEFT); break;
  322. case 0xc: map_key_clear(KEY_UP); break;
  323. case 0xd: map_key_clear(KEY_DOWN); break;
  324. case 0xe: map_key_clear(KEY_POWER2); break;
  325. case 0xf: map_key_clear(KEY_RESTART); break;
  326. default: goto unknown;
  327. }
  328. break;
  329. }
  330. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  331. switch (usage->hid) {
  332. case HID_GD_UP: usage->hat_dir = 1; break;
  333. case HID_GD_DOWN: usage->hat_dir = 5; break;
  334. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  335. case HID_GD_LEFT: usage->hat_dir = 7; break;
  336. default: goto unknown;
  337. }
  338. if (field->dpad) {
  339. map_abs(field->dpad);
  340. goto ignore;
  341. }
  342. map_abs(ABS_HAT0X);
  343. break;
  344. }
  345. switch (usage->hid) {
  346. /* These usage IDs map directly to the usage codes. */
  347. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  348. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  349. case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
  350. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  351. map_rel(usage->hid & 0xf);
  352. else
  353. map_abs(usage->hid & 0xf);
  354. break;
  355. case HID_GD_HATSWITCH:
  356. usage->hat_min = field->logical_minimum;
  357. usage->hat_max = field->logical_maximum;
  358. map_abs(ABS_HAT0X);
  359. break;
  360. case HID_GD_START: map_key_clear(BTN_START); break;
  361. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  362. default: goto unknown;
  363. }
  364. break;
  365. case HID_UP_LED:
  366. switch (usage->hid & 0xffff) { /* HID-Value: */
  367. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  368. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  369. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  370. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  371. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  372. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  373. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  374. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  375. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  376. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  377. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  378. default: goto ignore;
  379. }
  380. break;
  381. case HID_UP_DIGITIZER:
  382. switch (usage->hid & 0xff) {
  383. case 0x00: /* Undefined */
  384. goto ignore;
  385. case 0x30: /* TipPressure */
  386. if (!test_bit(BTN_TOUCH, input->keybit)) {
  387. device->quirks |= HID_QUIRK_NOTOUCH;
  388. set_bit(EV_KEY, input->evbit);
  389. set_bit(BTN_TOUCH, input->keybit);
  390. }
  391. map_abs_clear(ABS_PRESSURE);
  392. break;
  393. case 0x32: /* InRange */
  394. switch (field->physical & 0xff) {
  395. case 0x21: map_key(BTN_TOOL_MOUSE); break;
  396. case 0x22: map_key(BTN_TOOL_FINGER); break;
  397. default: map_key(BTN_TOOL_PEN); break;
  398. }
  399. break;
  400. case 0x3c: /* Invert */
  401. map_key_clear(BTN_TOOL_RUBBER);
  402. break;
  403. case 0x33: /* Touch */
  404. case 0x42: /* TipSwitch */
  405. case 0x43: /* TipSwitch2 */
  406. device->quirks &= ~HID_QUIRK_NOTOUCH;
  407. map_key_clear(BTN_TOUCH);
  408. break;
  409. case 0x44: /* BarrelSwitch */
  410. map_key_clear(BTN_STYLUS);
  411. break;
  412. case 0x46: /* TabletPick */
  413. map_key_clear(BTN_STYLUS2);
  414. break;
  415. default: goto unknown;
  416. }
  417. break;
  418. case HID_UP_CONSUMER: /* USB HUT v1.12, pages 75-84 */
  419. switch (usage->hid & HID_USAGE) {
  420. case 0x000: goto ignore;
  421. case 0x030: map_key_clear(KEY_POWER); break;
  422. case 0x031: map_key_clear(KEY_RESTART); break;
  423. case 0x032: map_key_clear(KEY_SLEEP); break;
  424. case 0x034: map_key_clear(KEY_SLEEP); break;
  425. case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE); break;
  426. case 0x036: map_key_clear(BTN_MISC); break;
  427. case 0x040: map_key_clear(KEY_MENU); break; /* Menu */
  428. case 0x041: map_key_clear(KEY_SELECT); break; /* Menu Pick */
  429. case 0x042: map_key_clear(KEY_UP); break; /* Menu Up */
  430. case 0x043: map_key_clear(KEY_DOWN); break; /* Menu Down */
  431. case 0x044: map_key_clear(KEY_LEFT); break; /* Menu Left */
  432. case 0x045: map_key_clear(KEY_RIGHT); break; /* Menu Right */
  433. case 0x046: map_key_clear(KEY_ESC); break; /* Menu Escape */
  434. case 0x047: map_key_clear(KEY_KPPLUS); break; /* Menu Value Increase */
  435. case 0x048: map_key_clear(KEY_KPMINUS); break; /* Menu Value Decrease */
  436. case 0x060: map_key_clear(KEY_INFO); break; /* Data On Screen */
  437. case 0x061: map_key_clear(KEY_SUBTITLE); break; /* Closed Caption */
  438. case 0x063: map_key_clear(KEY_VCR); break; /* VCR/TV */
  439. case 0x065: map_key_clear(KEY_CAMERA); break; /* Snapshot */
  440. case 0x069: map_key_clear(KEY_RED); break;
  441. case 0x06a: map_key_clear(KEY_GREEN); break;
  442. case 0x06b: map_key_clear(KEY_BLUE); break;
  443. case 0x06c: map_key_clear(KEY_YELLOW); break;
  444. case 0x06d: map_key_clear(KEY_ZOOM); break;
  445. case 0x082: map_key_clear(KEY_VIDEO_NEXT); break;
  446. case 0x083: map_key_clear(KEY_LAST); break;
  447. case 0x084: map_key_clear(KEY_ENTER); break;
  448. case 0x088: map_key_clear(KEY_PC); break;
  449. case 0x089: map_key_clear(KEY_TV); break;
  450. case 0x08a: map_key_clear(KEY_WWW); break;
  451. case 0x08b: map_key_clear(KEY_DVD); break;
  452. case 0x08c: map_key_clear(KEY_PHONE); break;
  453. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  454. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  455. case 0x08f: map_key_clear(KEY_GAMES); break;
  456. case 0x090: map_key_clear(KEY_MEMO); break;
  457. case 0x091: map_key_clear(KEY_CD); break;
  458. case 0x092: map_key_clear(KEY_VCR); break;
  459. case 0x093: map_key_clear(KEY_TUNER); break;
  460. case 0x094: map_key_clear(KEY_EXIT); break;
  461. case 0x095: map_key_clear(KEY_HELP); break;
  462. case 0x096: map_key_clear(KEY_TAPE); break;
  463. case 0x097: map_key_clear(KEY_TV2); break;
  464. case 0x098: map_key_clear(KEY_SAT); break;
  465. case 0x09a: map_key_clear(KEY_PVR); break;
  466. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  467. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  468. case 0x0a0: map_key_clear(KEY_VCR2); break;
  469. case 0x0b0: map_key_clear(KEY_PLAY); break;
  470. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  471. case 0x0b2: map_key_clear(KEY_RECORD); break;
  472. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  473. case 0x0b4: map_key_clear(KEY_REWIND); break;
  474. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  475. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  476. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  477. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  478. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  479. case 0x0b9: map_key_clear(KEY_SHUFFLE); break;
  480. case 0x0bf: map_key_clear(KEY_SLOW); break;
  481. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  482. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  483. case 0x0e2: map_key_clear(KEY_MUTE); break;
  484. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  485. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  486. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  487. case 0x0f5: map_key_clear(KEY_SLOW); break;
  488. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  489. case 0x183: map_key_clear(KEY_CONFIG); break;
  490. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  491. case 0x185: map_key_clear(KEY_EDITOR); break;
  492. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  493. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  494. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  495. case 0x189: map_key_clear(KEY_DATABASE); break;
  496. case 0x18a: map_key_clear(KEY_MAIL); break;
  497. case 0x18b: map_key_clear(KEY_NEWS); break;
  498. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  499. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  500. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  501. case 0x191: map_key_clear(KEY_FINANCE); break;
  502. case 0x192: map_key_clear(KEY_CALC); break;
  503. case 0x193: map_key_clear(KEY_PLAYER); break;
  504. case 0x194: map_key_clear(KEY_FILE); break;
  505. case 0x196: map_key_clear(KEY_WWW); break;
  506. case 0x199: map_key_clear(KEY_CHAT); break;
  507. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  508. case 0x19e: map_key_clear(KEY_COFFEE); break;
  509. case 0x1a6: map_key_clear(KEY_HELP); break;
  510. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  511. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  512. case 0x1ae: map_key_clear(KEY_KEYBOARD); break;
  513. case 0x1b6: map_key_clear(KEY_IMAGES); break;
  514. case 0x1b7: map_key_clear(KEY_AUDIO); break;
  515. case 0x1b8: map_key_clear(KEY_VIDEO); break;
  516. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  517. case 0x1bd: map_key_clear(KEY_INFO); break;
  518. case 0x201: map_key_clear(KEY_NEW); break;
  519. case 0x202: map_key_clear(KEY_OPEN); break;
  520. case 0x203: map_key_clear(KEY_CLOSE); break;
  521. case 0x204: map_key_clear(KEY_EXIT); break;
  522. case 0x207: map_key_clear(KEY_SAVE); break;
  523. case 0x208: map_key_clear(KEY_PRINT); break;
  524. case 0x209: map_key_clear(KEY_PROPS); break;
  525. case 0x21a: map_key_clear(KEY_UNDO); break;
  526. case 0x21b: map_key_clear(KEY_COPY); break;
  527. case 0x21c: map_key_clear(KEY_CUT); break;
  528. case 0x21d: map_key_clear(KEY_PASTE); break;
  529. case 0x21f: map_key_clear(KEY_FIND); break;
  530. case 0x221: map_key_clear(KEY_SEARCH); break;
  531. case 0x222: map_key_clear(KEY_GOTO); break;
  532. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  533. case 0x224: map_key_clear(KEY_BACK); break;
  534. case 0x225: map_key_clear(KEY_FORWARD); break;
  535. case 0x226: map_key_clear(KEY_STOP); break;
  536. case 0x227: map_key_clear(KEY_REFRESH); break;
  537. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  538. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  539. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  540. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  541. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  542. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  543. case 0x238: map_rel(REL_HWHEEL); break;
  544. case 0x23d: map_key_clear(KEY_EDIT); break;
  545. case 0x25f: map_key_clear(KEY_CANCEL); break;
  546. case 0x269: map_key_clear(KEY_INSERT); break;
  547. case 0x26a: map_key_clear(KEY_DELETE); break;
  548. case 0x279: map_key_clear(KEY_REDO); break;
  549. case 0x289: map_key_clear(KEY_REPLY); break;
  550. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  551. case 0x28c: map_key_clear(KEY_SEND); break;
  552. default: goto ignore;
  553. }
  554. break;
  555. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  556. set_bit(EV_REP, input->evbit);
  557. switch (usage->hid & HID_USAGE) {
  558. case 0x021: map_key_clear(KEY_PRINT); break;
  559. case 0x070: map_key_clear(KEY_HP); break;
  560. case 0x071: map_key_clear(KEY_CAMERA); break;
  561. case 0x072: map_key_clear(KEY_SOUND); break;
  562. case 0x073: map_key_clear(KEY_QUESTION); break;
  563. case 0x080: map_key_clear(KEY_EMAIL); break;
  564. case 0x081: map_key_clear(KEY_CHAT); break;
  565. case 0x082: map_key_clear(KEY_SEARCH); break;
  566. case 0x083: map_key_clear(KEY_CONNECT); break;
  567. case 0x084: map_key_clear(KEY_FINANCE); break;
  568. case 0x085: map_key_clear(KEY_SPORT); break;
  569. case 0x086: map_key_clear(KEY_SHOP); break;
  570. default: goto ignore;
  571. }
  572. break;
  573. case HID_UP_MSVENDOR:
  574. goto ignore;
  575. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  576. set_bit(EV_REP, input->evbit);
  577. goto ignore;
  578. case HID_UP_LOGIVENDOR:
  579. goto ignore;
  580. case HID_UP_PID:
  581. switch (usage->hid & HID_USAGE) {
  582. case 0xa4: map_key_clear(BTN_DEAD); break;
  583. default: goto ignore;
  584. }
  585. break;
  586. default:
  587. unknown:
  588. if (field->report_size == 1) {
  589. if (field->report->type == HID_OUTPUT_REPORT) {
  590. map_led(LED_MISC);
  591. break;
  592. }
  593. map_key(BTN_MISC);
  594. break;
  595. }
  596. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  597. map_rel(REL_MISC);
  598. break;
  599. }
  600. map_abs(ABS_MISC);
  601. break;
  602. }
  603. mapped:
  604. if (device->driver->input_mapped && device->driver->input_mapped(device,
  605. hidinput, field, usage, &bit, &max) < 0)
  606. goto ignore;
  607. set_bit(usage->type, input->evbit);
  608. while (usage->code <= max && test_and_set_bit(usage->code, bit))
  609. usage->code = find_next_zero_bit(bit, max + 1, usage->code);
  610. if (usage->code > max)
  611. goto ignore;
  612. if (usage->type == EV_ABS) {
  613. int a = field->logical_minimum;
  614. int b = field->logical_maximum;
  615. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  616. a = field->logical_minimum = 0;
  617. b = field->logical_maximum = 255;
  618. }
  619. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  620. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  621. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  622. input_abs_set_res(input, usage->code,
  623. hidinput_calc_abs_res(field, usage->code));
  624. /* use a larger default input buffer for MT devices */
  625. if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
  626. input_set_events_per_packet(input, 60);
  627. }
  628. if (usage->type == EV_ABS &&
  629. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  630. int i;
  631. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  632. input_set_abs_params(input, i, -1, 1, 0, 0);
  633. set_bit(i, input->absbit);
  634. }
  635. if (usage->hat_dir && !field->dpad)
  636. field->dpad = usage->code;
  637. }
  638. /* for those devices which produce Consumer volume usage as relative,
  639. * we emulate pressing volumeup/volumedown appropriate number of times
  640. * in hidinput_hid_event()
  641. */
  642. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  643. (usage->code == ABS_VOLUME)) {
  644. set_bit(KEY_VOLUMEUP, input->keybit);
  645. set_bit(KEY_VOLUMEDOWN, input->keybit);
  646. }
  647. if (usage->type == EV_KEY) {
  648. set_bit(EV_MSC, input->evbit);
  649. set_bit(MSC_SCAN, input->mscbit);
  650. }
  651. ignore:
  652. return;
  653. }
  654. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  655. {
  656. struct input_dev *input;
  657. unsigned *quirks = &hid->quirks;
  658. if (!field->hidinput)
  659. return;
  660. input = field->hidinput->input;
  661. if (!usage->type)
  662. return;
  663. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  664. int hat_dir = usage->hat_dir;
  665. if (!hat_dir)
  666. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  667. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  668. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  669. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  670. return;
  671. }
  672. if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
  673. *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
  674. return;
  675. }
  676. if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
  677. if (value) {
  678. input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
  679. return;
  680. }
  681. input_event(input, usage->type, usage->code, 0);
  682. input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
  683. return;
  684. }
  685. if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
  686. int a = field->logical_minimum;
  687. int b = field->logical_maximum;
  688. input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
  689. }
  690. if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
  691. dbg_hid("Maximum Effects - %d\n",value);
  692. return;
  693. }
  694. if (usage->hid == (HID_UP_PID | 0x7fUL)) {
  695. dbg_hid("PID Pool Report\n");
  696. return;
  697. }
  698. if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  699. return;
  700. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  701. (usage->code == ABS_VOLUME)) {
  702. int count = abs(value);
  703. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  704. int i;
  705. for (i = 0; i < count; i++) {
  706. input_event(input, EV_KEY, direction, 1);
  707. input_sync(input);
  708. input_event(input, EV_KEY, direction, 0);
  709. input_sync(input);
  710. }
  711. return;
  712. }
  713. /* report the usage code as scancode if the key status has changed */
  714. if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
  715. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  716. input_event(input, usage->type, usage->code, value);
  717. if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
  718. input_event(input, usage->type, usage->code, 0);
  719. }
  720. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  721. {
  722. struct hid_input *hidinput;
  723. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  724. return;
  725. list_for_each_entry(hidinput, &hid->inputs, list)
  726. input_sync(hidinput->input);
  727. }
  728. EXPORT_SYMBOL_GPL(hidinput_report_event);
  729. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  730. {
  731. struct hid_report *report;
  732. int i, j;
  733. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  734. for (i = 0; i < report->maxfield; i++) {
  735. *field = report->field[i];
  736. for (j = 0; j < (*field)->maxusage; j++)
  737. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  738. return j;
  739. }
  740. }
  741. return -1;
  742. }
  743. EXPORT_SYMBOL_GPL(hidinput_find_field);
  744. static int hidinput_open(struct input_dev *dev)
  745. {
  746. struct hid_device *hid = input_get_drvdata(dev);
  747. return hid_hw_open(hid);
  748. }
  749. static void hidinput_close(struct input_dev *dev)
  750. {
  751. struct hid_device *hid = input_get_drvdata(dev);
  752. hid_hw_close(hid);
  753. }
  754. static void report_features(struct hid_device *hid)
  755. {
  756. struct hid_driver *drv = hid->driver;
  757. struct hid_report_enum *rep_enum;
  758. struct hid_report *rep;
  759. int i, j;
  760. if (!drv->feature_mapping)
  761. return;
  762. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  763. list_for_each_entry(rep, &rep_enum->report_list, list)
  764. for (i = 0; i < rep->maxfield; i++)
  765. for (j = 0; j < rep->field[i]->maxusage; j++)
  766. drv->feature_mapping(hid, rep->field[i],
  767. rep->field[i]->usage + j);
  768. }
  769. /*
  770. * Register the input device; print a message.
  771. * Configure the input layer interface
  772. * Read all reports and initialize the absolute field values.
  773. */
  774. int hidinput_connect(struct hid_device *hid, unsigned int force)
  775. {
  776. struct hid_report *report;
  777. struct hid_input *hidinput = NULL;
  778. struct input_dev *input_dev;
  779. int i, j, k;
  780. INIT_LIST_HEAD(&hid->inputs);
  781. if (!force) {
  782. for (i = 0; i < hid->maxcollection; i++) {
  783. struct hid_collection *col = &hid->collection[i];
  784. if (col->type == HID_COLLECTION_APPLICATION ||
  785. col->type == HID_COLLECTION_PHYSICAL)
  786. if (IS_INPUT_APPLICATION(col->usage))
  787. break;
  788. }
  789. if (i == hid->maxcollection)
  790. return -1;
  791. }
  792. report_features(hid);
  793. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  794. if (k == HID_OUTPUT_REPORT &&
  795. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  796. continue;
  797. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  798. if (!report->maxfield)
  799. continue;
  800. if (!hidinput) {
  801. hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  802. input_dev = input_allocate_device();
  803. if (!hidinput || !input_dev) {
  804. kfree(hidinput);
  805. input_free_device(input_dev);
  806. hid_err(hid, "Out of memory during hid input probe\n");
  807. goto out_unwind;
  808. }
  809. input_set_drvdata(input_dev, hid);
  810. input_dev->event =
  811. hid->ll_driver->hidinput_input_event;
  812. input_dev->open = hidinput_open;
  813. input_dev->close = hidinput_close;
  814. input_dev->setkeycode = hidinput_setkeycode;
  815. input_dev->getkeycode = hidinput_getkeycode;
  816. input_dev->name = hid->name;
  817. input_dev->phys = hid->phys;
  818. input_dev->uniq = hid->uniq;
  819. input_dev->id.bustype = hid->bus;
  820. input_dev->id.vendor = hid->vendor;
  821. input_dev->id.product = hid->product;
  822. input_dev->id.version = hid->version;
  823. input_dev->dev.parent = hid->dev.parent;
  824. hidinput->input = input_dev;
  825. list_add_tail(&hidinput->list, &hid->inputs);
  826. }
  827. for (i = 0; i < report->maxfield; i++)
  828. for (j = 0; j < report->field[i]->maxusage; j++)
  829. hidinput_configure_usage(hidinput, report->field[i],
  830. report->field[i]->usage + j);
  831. if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
  832. /* This will leave hidinput NULL, so that it
  833. * allocates another one if we have more inputs on
  834. * the same interface. Some devices (e.g. Happ's
  835. * UGCI) cram a lot of unrelated inputs into the
  836. * same interface. */
  837. hidinput->report = report;
  838. if (hid->driver->input_register &&
  839. hid->driver->input_register(hid, hidinput))
  840. goto out_cleanup;
  841. if (input_register_device(hidinput->input))
  842. goto out_cleanup;
  843. hidinput = NULL;
  844. }
  845. }
  846. }
  847. if (hidinput && hid->driver->input_register &&
  848. hid->driver->input_register(hid, hidinput))
  849. goto out_cleanup;
  850. if (hidinput && input_register_device(hidinput->input))
  851. goto out_cleanup;
  852. return 0;
  853. out_cleanup:
  854. list_del(&hidinput->list);
  855. input_free_device(hidinput->input);
  856. kfree(hidinput);
  857. out_unwind:
  858. /* unwind the ones we already registered */
  859. hidinput_disconnect(hid);
  860. return -1;
  861. }
  862. EXPORT_SYMBOL_GPL(hidinput_connect);
  863. void hidinput_disconnect(struct hid_device *hid)
  864. {
  865. struct hid_input *hidinput, *next;
  866. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  867. list_del(&hidinput->list);
  868. input_unregister_device(hidinput->input);
  869. kfree(hidinput);
  870. }
  871. }
  872. EXPORT_SYMBOL_GPL(hidinput_disconnect);