hid-input.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 2000-2001 Vojtech Pavlik
  3. * Copyright (c) 2006-2007 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,unk,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,unk,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 inline int match_scancode(int code, int scancode)
  63. {
  64. if (scancode == 0)
  65. return 1;
  66. return ((code & (HID_USAGE_PAGE | HID_USAGE)) == scancode);
  67. }
  68. static inline int match_keycode(int code, int keycode)
  69. {
  70. if (keycode == 0)
  71. return 1;
  72. return (code == keycode);
  73. }
  74. static struct hid_usage *hidinput_find_key(struct hid_device *hid,
  75. int scancode, int keycode)
  76. {
  77. int i, j, k;
  78. struct hid_report *report;
  79. struct hid_usage *usage;
  80. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  81. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  82. for (i = 0; i < report->maxfield; i++) {
  83. for ( j = 0; j < report->field[i]->maxusage; j++) {
  84. usage = report->field[i]->usage + j;
  85. if (usage->type == EV_KEY &&
  86. match_scancode(usage->hid, scancode) &&
  87. match_keycode(usage->code, keycode))
  88. return usage;
  89. }
  90. }
  91. }
  92. }
  93. return NULL;
  94. }
  95. static int hidinput_getkeycode(struct input_dev *dev, int scancode,
  96. int *keycode)
  97. {
  98. struct hid_device *hid = input_get_drvdata(dev);
  99. struct hid_usage *usage;
  100. usage = hidinput_find_key(hid, scancode, 0);
  101. if (usage) {
  102. *keycode = usage->code;
  103. return 0;
  104. }
  105. return -EINVAL;
  106. }
  107. static int hidinput_setkeycode(struct input_dev *dev, int scancode,
  108. int keycode)
  109. {
  110. struct hid_device *hid = input_get_drvdata(dev);
  111. struct hid_usage *usage;
  112. int old_keycode;
  113. if (keycode < 0 || keycode > KEY_MAX)
  114. return -EINVAL;
  115. usage = hidinput_find_key(hid, scancode, 0);
  116. if (usage) {
  117. old_keycode = usage->code;
  118. usage->code = keycode;
  119. clear_bit(old_keycode, dev->keybit);
  120. set_bit(usage->code, dev->keybit);
  121. dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
  122. /* Set the keybit for the old keycode if the old keycode is used
  123. * by another key */
  124. if (hidinput_find_key (hid, 0, old_keycode))
  125. set_bit(old_keycode, dev->keybit);
  126. return 0;
  127. }
  128. return -EINVAL;
  129. }
  130. static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
  131. struct hid_usage *usage)
  132. {
  133. struct input_dev *input = hidinput->input;
  134. struct hid_device *device = input_get_drvdata(input);
  135. int max = 0, code;
  136. unsigned long *bit = NULL;
  137. field->hidinput = hidinput;
  138. dbg_hid("Mapping: ");
  139. hid_resolv_usage(usage->hid);
  140. dbg_hid_line(" ---> ");
  141. if (field->flags & HID_MAIN_ITEM_CONSTANT)
  142. goto ignore;
  143. /* only LED usages are supported in output fields */
  144. if (field->report_type == HID_OUTPUT_REPORT &&
  145. (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
  146. dbg_hid_line(" [non-LED output field] ");
  147. goto ignore;
  148. }
  149. if (device->driver->input_mapping) {
  150. int ret = device->driver->input_mapping(device, hidinput, field,
  151. usage, &bit, &max);
  152. if (ret > 0)
  153. goto mapped;
  154. if (ret < 0)
  155. goto ignore;
  156. }
  157. switch (usage->hid & HID_USAGE_PAGE) {
  158. case HID_UP_UNDEFINED:
  159. goto ignore;
  160. case HID_UP_KEYBOARD:
  161. set_bit(EV_REP, input->evbit);
  162. if ((usage->hid & HID_USAGE) < 256) {
  163. if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
  164. map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
  165. } else
  166. map_key(KEY_UNKNOWN);
  167. break;
  168. case HID_UP_BUTTON:
  169. code = ((usage->hid - 1) & 0xf);
  170. switch (field->application) {
  171. case HID_GD_MOUSE:
  172. case HID_GD_POINTER: code += 0x110; break;
  173. case HID_GD_JOYSTICK: code += 0x120; break;
  174. case HID_GD_GAMEPAD: code += 0x130; break;
  175. default:
  176. switch (field->physical) {
  177. case HID_GD_MOUSE:
  178. case HID_GD_POINTER: code += 0x110; break;
  179. case HID_GD_JOYSTICK: code += 0x120; break;
  180. case HID_GD_GAMEPAD: code += 0x130; break;
  181. default: code += 0x100;
  182. }
  183. }
  184. map_key(code);
  185. break;
  186. case HID_UP_SIMULATION:
  187. switch (usage->hid & 0xffff) {
  188. case 0xba: map_abs(ABS_RUDDER); break;
  189. case 0xbb: map_abs(ABS_THROTTLE); break;
  190. case 0xc4: map_abs(ABS_GAS); break;
  191. case 0xc5: map_abs(ABS_BRAKE); break;
  192. case 0xc8: map_abs(ABS_WHEEL); break;
  193. default: goto ignore;
  194. }
  195. break;
  196. case HID_UP_GENDESK:
  197. if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
  198. switch (usage->hid & 0xf) {
  199. case 0x1: map_key_clear(KEY_POWER); break;
  200. case 0x2: map_key_clear(KEY_SLEEP); break;
  201. case 0x3: map_key_clear(KEY_WAKEUP); break;
  202. default: goto unknown;
  203. }
  204. break;
  205. }
  206. if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
  207. switch (usage->hid) {
  208. case HID_GD_UP: usage->hat_dir = 1; break;
  209. case HID_GD_DOWN: usage->hat_dir = 5; break;
  210. case HID_GD_RIGHT: usage->hat_dir = 3; break;
  211. case HID_GD_LEFT: usage->hat_dir = 7; break;
  212. default: goto unknown;
  213. }
  214. if (field->dpad) {
  215. map_abs(field->dpad);
  216. goto ignore;
  217. }
  218. map_abs(ABS_HAT0X);
  219. break;
  220. }
  221. switch (usage->hid) {
  222. /* These usage IDs map directly to the usage codes. */
  223. case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
  224. case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
  225. case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
  226. if (field->flags & HID_MAIN_ITEM_RELATIVE)
  227. map_rel(usage->hid & 0xf);
  228. else
  229. map_abs(usage->hid & 0xf);
  230. break;
  231. case HID_GD_HATSWITCH:
  232. usage->hat_min = field->logical_minimum;
  233. usage->hat_max = field->logical_maximum;
  234. map_abs(ABS_HAT0X);
  235. break;
  236. case HID_GD_START: map_key_clear(BTN_START); break;
  237. case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
  238. default: goto unknown;
  239. }
  240. break;
  241. case HID_UP_LED:
  242. switch (usage->hid & 0xffff) { /* HID-Value: */
  243. case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
  244. case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
  245. case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
  246. case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
  247. case 0x05: map_led (LED_KANA); break; /* "Kana" */
  248. case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
  249. case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
  250. case 0x09: map_led (LED_MUTE); break; /* "Mute" */
  251. case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
  252. case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
  253. case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
  254. default: goto ignore;
  255. }
  256. break;
  257. case HID_UP_DIGITIZER:
  258. switch (usage->hid & 0xff) {
  259. case 0x30: /* TipPressure */
  260. if (!test_bit(BTN_TOUCH, input->keybit)) {
  261. device->quirks |= HID_QUIRK_NOTOUCH;
  262. set_bit(EV_KEY, input->evbit);
  263. set_bit(BTN_TOUCH, input->keybit);
  264. }
  265. map_abs_clear(ABS_PRESSURE);
  266. break;
  267. case 0x32: /* InRange */
  268. switch (field->physical & 0xff) {
  269. case 0x21: map_key(BTN_TOOL_MOUSE); break;
  270. case 0x22: map_key(BTN_TOOL_FINGER); break;
  271. default: map_key(BTN_TOOL_PEN); break;
  272. }
  273. break;
  274. case 0x3c: /* Invert */
  275. map_key_clear(BTN_TOOL_RUBBER);
  276. break;
  277. case 0x33: /* Touch */
  278. case 0x42: /* TipSwitch */
  279. case 0x43: /* TipSwitch2 */
  280. device->quirks &= ~HID_QUIRK_NOTOUCH;
  281. map_key_clear(BTN_TOUCH);
  282. break;
  283. case 0x44: /* BarrelSwitch */
  284. map_key_clear(BTN_STYLUS);
  285. break;
  286. default: goto unknown;
  287. }
  288. break;
  289. case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
  290. switch (usage->hid & HID_USAGE) {
  291. case 0x000: goto ignore;
  292. case 0x034: map_key_clear(KEY_SLEEP); break;
  293. case 0x036: map_key_clear(BTN_MISC); break;
  294. case 0x040: map_key_clear(KEY_MENU); break;
  295. case 0x045: map_key_clear(KEY_RADIO); break;
  296. case 0x083: map_key_clear(KEY_LAST); break;
  297. case 0x088: map_key_clear(KEY_PC); break;
  298. case 0x089: map_key_clear(KEY_TV); break;
  299. case 0x08a: map_key_clear(KEY_WWW); break;
  300. case 0x08b: map_key_clear(KEY_DVD); break;
  301. case 0x08c: map_key_clear(KEY_PHONE); break;
  302. case 0x08d: map_key_clear(KEY_PROGRAM); break;
  303. case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
  304. case 0x08f: map_key_clear(KEY_GAMES); break;
  305. case 0x090: map_key_clear(KEY_MEMO); break;
  306. case 0x091: map_key_clear(KEY_CD); break;
  307. case 0x092: map_key_clear(KEY_VCR); break;
  308. case 0x093: map_key_clear(KEY_TUNER); break;
  309. case 0x094: map_key_clear(KEY_EXIT); break;
  310. case 0x095: map_key_clear(KEY_HELP); break;
  311. case 0x096: map_key_clear(KEY_TAPE); break;
  312. case 0x097: map_key_clear(KEY_TV2); break;
  313. case 0x098: map_key_clear(KEY_SAT); break;
  314. case 0x09a: map_key_clear(KEY_PVR); break;
  315. case 0x09c: map_key_clear(KEY_CHANNELUP); break;
  316. case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
  317. case 0x0a0: map_key_clear(KEY_VCR2); break;
  318. case 0x0b0: map_key_clear(KEY_PLAY); break;
  319. case 0x0b1: map_key_clear(KEY_PAUSE); break;
  320. case 0x0b2: map_key_clear(KEY_RECORD); break;
  321. case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
  322. case 0x0b4: map_key_clear(KEY_REWIND); break;
  323. case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
  324. case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
  325. case 0x0b7: map_key_clear(KEY_STOPCD); break;
  326. case 0x0b8: map_key_clear(KEY_EJECTCD); break;
  327. case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
  328. case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
  329. case 0x0e0: map_abs_clear(ABS_VOLUME); break;
  330. case 0x0e2: map_key_clear(KEY_MUTE); break;
  331. case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
  332. case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
  333. case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
  334. case 0x182: map_key_clear(KEY_BOOKMARKS); break;
  335. case 0x183: map_key_clear(KEY_CONFIG); break;
  336. case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
  337. case 0x185: map_key_clear(KEY_EDITOR); break;
  338. case 0x186: map_key_clear(KEY_SPREADSHEET); break;
  339. case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
  340. case 0x188: map_key_clear(KEY_PRESENTATION); break;
  341. case 0x189: map_key_clear(KEY_DATABASE); break;
  342. case 0x18a: map_key_clear(KEY_MAIL); break;
  343. case 0x18b: map_key_clear(KEY_NEWS); break;
  344. case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
  345. case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
  346. case 0x18e: map_key_clear(KEY_CALENDAR); break;
  347. case 0x191: map_key_clear(KEY_FINANCE); break;
  348. case 0x192: map_key_clear(KEY_CALC); break;
  349. case 0x194: map_key_clear(KEY_FILE); break;
  350. case 0x196: map_key_clear(KEY_WWW); break;
  351. case 0x19c: map_key_clear(KEY_LOGOFF); break;
  352. case 0x19e: map_key_clear(KEY_COFFEE); break;
  353. case 0x1a6: map_key_clear(KEY_HELP); break;
  354. case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
  355. case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
  356. case 0x1b6: map_key_clear(KEY_MEDIA); break;
  357. case 0x1b7: map_key_clear(KEY_SOUND); break;
  358. case 0x1bc: map_key_clear(KEY_MESSENGER); break;
  359. case 0x1bd: map_key_clear(KEY_INFO); break;
  360. case 0x201: map_key_clear(KEY_NEW); break;
  361. case 0x202: map_key_clear(KEY_OPEN); break;
  362. case 0x203: map_key_clear(KEY_CLOSE); break;
  363. case 0x204: map_key_clear(KEY_EXIT); break;
  364. case 0x207: map_key_clear(KEY_SAVE); break;
  365. case 0x208: map_key_clear(KEY_PRINT); break;
  366. case 0x209: map_key_clear(KEY_PROPS); break;
  367. case 0x21a: map_key_clear(KEY_UNDO); break;
  368. case 0x21b: map_key_clear(KEY_COPY); break;
  369. case 0x21c: map_key_clear(KEY_CUT); break;
  370. case 0x21d: map_key_clear(KEY_PASTE); break;
  371. case 0x21f: map_key_clear(KEY_FIND); break;
  372. case 0x221: map_key_clear(KEY_SEARCH); break;
  373. case 0x222: map_key_clear(KEY_GOTO); break;
  374. case 0x223: map_key_clear(KEY_HOMEPAGE); break;
  375. case 0x224: map_key_clear(KEY_BACK); break;
  376. case 0x225: map_key_clear(KEY_FORWARD); break;
  377. case 0x226: map_key_clear(KEY_STOP); break;
  378. case 0x227: map_key_clear(KEY_REFRESH); break;
  379. case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
  380. case 0x22d: map_key_clear(KEY_ZOOMIN); break;
  381. case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
  382. case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
  383. case 0x233: map_key_clear(KEY_SCROLLUP); break;
  384. case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
  385. case 0x238: map_rel(REL_HWHEEL); break;
  386. case 0x25f: map_key_clear(KEY_CANCEL); break;
  387. case 0x279: map_key_clear(KEY_REDO); break;
  388. case 0x289: map_key_clear(KEY_REPLY); break;
  389. case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
  390. case 0x28c: map_key_clear(KEY_SEND); break;
  391. default: goto ignore;
  392. }
  393. break;
  394. case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
  395. set_bit(EV_REP, input->evbit);
  396. switch (usage->hid & HID_USAGE) {
  397. case 0x021: map_key_clear(KEY_PRINT); break;
  398. case 0x070: map_key_clear(KEY_HP); break;
  399. case 0x071: map_key_clear(KEY_CAMERA); break;
  400. case 0x072: map_key_clear(KEY_SOUND); break;
  401. case 0x073: map_key_clear(KEY_QUESTION); break;
  402. case 0x080: map_key_clear(KEY_EMAIL); break;
  403. case 0x081: map_key_clear(KEY_CHAT); break;
  404. case 0x082: map_key_clear(KEY_SEARCH); break;
  405. case 0x083: map_key_clear(KEY_CONNECT); break;
  406. case 0x084: map_key_clear(KEY_FINANCE); break;
  407. case 0x085: map_key_clear(KEY_SPORT); break;
  408. case 0x086: map_key_clear(KEY_SHOP); break;
  409. default: goto ignore;
  410. }
  411. break;
  412. case HID_UP_MSVENDOR:
  413. goto ignore;
  414. case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
  415. set_bit(EV_REP, input->evbit);
  416. goto ignore;
  417. case HID_UP_LOGIVENDOR:
  418. goto ignore;
  419. case HID_UP_PID:
  420. switch (usage->hid & HID_USAGE) {
  421. case 0xa4: map_key_clear(BTN_DEAD); break;
  422. default: goto ignore;
  423. }
  424. break;
  425. default:
  426. unknown:
  427. if (field->report_size == 1) {
  428. if (field->report->type == HID_OUTPUT_REPORT) {
  429. map_led(LED_MISC);
  430. break;
  431. }
  432. map_key(BTN_MISC);
  433. break;
  434. }
  435. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  436. map_rel(REL_MISC);
  437. break;
  438. }
  439. map_abs(ABS_MISC);
  440. break;
  441. }
  442. mapped:
  443. if (device->driver->input_mapped && device->driver->input_mapped(device,
  444. hidinput, field, usage, &bit, &max) < 0)
  445. goto ignore;
  446. set_bit(usage->type, input->evbit);
  447. while (usage->code <= max && test_and_set_bit(usage->code, bit))
  448. usage->code = find_next_zero_bit(bit, max + 1, usage->code);
  449. if (usage->code > max)
  450. goto ignore;
  451. if (usage->type == EV_ABS) {
  452. int a = field->logical_minimum;
  453. int b = field->logical_maximum;
  454. if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
  455. a = field->logical_minimum = 0;
  456. b = field->logical_maximum = 255;
  457. }
  458. if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
  459. input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
  460. else input_set_abs_params(input, usage->code, a, b, 0, 0);
  461. }
  462. if (usage->type == EV_ABS &&
  463. (usage->hat_min < usage->hat_max || usage->hat_dir)) {
  464. int i;
  465. for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
  466. input_set_abs_params(input, i, -1, 1, 0, 0);
  467. set_bit(i, input->absbit);
  468. }
  469. if (usage->hat_dir && !field->dpad)
  470. field->dpad = usage->code;
  471. }
  472. /* for those devices which produce Consumer volume usage as relative,
  473. * we emulate pressing volumeup/volumedown appropriate number of times
  474. * in hidinput_hid_event()
  475. */
  476. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  477. (usage->code == ABS_VOLUME)) {
  478. set_bit(KEY_VOLUMEUP, input->keybit);
  479. set_bit(KEY_VOLUMEDOWN, input->keybit);
  480. }
  481. if (usage->type == EV_KEY) {
  482. set_bit(EV_MSC, input->evbit);
  483. set_bit(MSC_SCAN, input->mscbit);
  484. }
  485. hid_resolv_event(usage->type, usage->code);
  486. dbg_hid_line("\n");
  487. return;
  488. ignore:
  489. dbg_hid_line("IGNORED\n");
  490. return;
  491. }
  492. void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  493. {
  494. struct input_dev *input;
  495. unsigned *quirks = &hid->quirks;
  496. if (!field->hidinput)
  497. return;
  498. input = field->hidinput->input;
  499. if (!usage->type)
  500. return;
  501. if (usage->hat_min < usage->hat_max || usage->hat_dir) {
  502. int hat_dir = usage->hat_dir;
  503. if (!hat_dir)
  504. hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
  505. if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
  506. input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
  507. input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
  508. return;
  509. }
  510. if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
  511. *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
  512. return;
  513. }
  514. if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
  515. if (value) {
  516. input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
  517. return;
  518. }
  519. input_event(input, usage->type, usage->code, 0);
  520. input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
  521. return;
  522. }
  523. if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
  524. int a = field->logical_minimum;
  525. int b = field->logical_maximum;
  526. input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
  527. }
  528. if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
  529. dbg_hid("Maximum Effects - %d\n",value);
  530. return;
  531. }
  532. if (usage->hid == (HID_UP_PID | 0x7fUL)) {
  533. dbg_hid("PID Pool Report\n");
  534. return;
  535. }
  536. if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
  537. return;
  538. if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
  539. (usage->code == ABS_VOLUME)) {
  540. int count = abs(value);
  541. int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
  542. int i;
  543. for (i = 0; i < count; i++) {
  544. input_event(input, EV_KEY, direction, 1);
  545. input_sync(input);
  546. input_event(input, EV_KEY, direction, 0);
  547. input_sync(input);
  548. }
  549. return;
  550. }
  551. /* report the usage code as scancode if the key status has changed */
  552. if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
  553. input_event(input, EV_MSC, MSC_SCAN, usage->hid);
  554. input_event(input, usage->type, usage->code, value);
  555. if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
  556. input_event(input, usage->type, usage->code, 0);
  557. }
  558. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  559. {
  560. struct hid_input *hidinput;
  561. list_for_each_entry(hidinput, &hid->inputs, list)
  562. input_sync(hidinput->input);
  563. }
  564. EXPORT_SYMBOL_GPL(hidinput_report_event);
  565. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  566. {
  567. struct hid_report *report;
  568. int i, j;
  569. list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  570. for (i = 0; i < report->maxfield; i++) {
  571. *field = report->field[i];
  572. for (j = 0; j < (*field)->maxusage; j++)
  573. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  574. return j;
  575. }
  576. }
  577. return -1;
  578. }
  579. EXPORT_SYMBOL_GPL(hidinput_find_field);
  580. static int hidinput_open(struct input_dev *dev)
  581. {
  582. struct hid_device *hid = input_get_drvdata(dev);
  583. return hid->ll_driver->open(hid);
  584. }
  585. static void hidinput_close(struct input_dev *dev)
  586. {
  587. struct hid_device *hid = input_get_drvdata(dev);
  588. hid->ll_driver->close(hid);
  589. }
  590. /*
  591. * Register the input device; print a message.
  592. * Configure the input layer interface
  593. * Read all reports and initialize the absolute field values.
  594. */
  595. int hidinput_connect(struct hid_device *hid, unsigned int force)
  596. {
  597. struct hid_report *report;
  598. struct hid_input *hidinput = NULL;
  599. struct input_dev *input_dev;
  600. int i, j, k;
  601. int max_report_type = HID_OUTPUT_REPORT;
  602. INIT_LIST_HEAD(&hid->inputs);
  603. if (!force) {
  604. for (i = 0; i < hid->maxcollection; i++) {
  605. struct hid_collection *col = &hid->collection[i];
  606. if (col->type == HID_COLLECTION_APPLICATION ||
  607. col->type == HID_COLLECTION_PHYSICAL)
  608. if (IS_INPUT_APPLICATION(col->usage))
  609. break;
  610. }
  611. if (i == hid->maxcollection)
  612. return -1;
  613. }
  614. if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  615. max_report_type = HID_INPUT_REPORT;
  616. for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
  617. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  618. if (!report->maxfield)
  619. continue;
  620. if (!hidinput) {
  621. hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
  622. input_dev = input_allocate_device();
  623. if (!hidinput || !input_dev) {
  624. kfree(hidinput);
  625. input_free_device(input_dev);
  626. err_hid("Out of memory during hid input probe");
  627. goto out_unwind;
  628. }
  629. input_set_drvdata(input_dev, hid);
  630. input_dev->event =
  631. hid->ll_driver->hidinput_input_event;
  632. input_dev->open = hidinput_open;
  633. input_dev->close = hidinput_close;
  634. input_dev->setkeycode = hidinput_setkeycode;
  635. input_dev->getkeycode = hidinput_getkeycode;
  636. input_dev->name = hid->name;
  637. input_dev->phys = hid->phys;
  638. input_dev->uniq = hid->uniq;
  639. input_dev->id.bustype = hid->bus;
  640. input_dev->id.vendor = hid->vendor;
  641. input_dev->id.product = hid->product;
  642. input_dev->id.version = hid->version;
  643. input_dev->dev.parent = hid->dev.parent;
  644. hidinput->input = input_dev;
  645. list_add_tail(&hidinput->list, &hid->inputs);
  646. }
  647. for (i = 0; i < report->maxfield; i++)
  648. for (j = 0; j < report->field[i]->maxusage; j++)
  649. hidinput_configure_usage(hidinput, report->field[i],
  650. report->field[i]->usage + j);
  651. if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
  652. /* This will leave hidinput NULL, so that it
  653. * allocates another one if we have more inputs on
  654. * the same interface. Some devices (e.g. Happ's
  655. * UGCI) cram a lot of unrelated inputs into the
  656. * same interface. */
  657. hidinput->report = report;
  658. if (input_register_device(hidinput->input))
  659. goto out_cleanup;
  660. hidinput = NULL;
  661. }
  662. }
  663. if (hidinput && input_register_device(hidinput->input))
  664. goto out_cleanup;
  665. return 0;
  666. out_cleanup:
  667. input_free_device(hidinput->input);
  668. kfree(hidinput);
  669. out_unwind:
  670. /* unwind the ones we already registered */
  671. hidinput_disconnect(hid);
  672. return -1;
  673. }
  674. EXPORT_SYMBOL_GPL(hidinput_connect);
  675. void hidinput_disconnect(struct hid_device *hid)
  676. {
  677. struct hid_input *hidinput, *next;
  678. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  679. list_del(&hidinput->list);
  680. input_unregister_device(hidinput->input);
  681. kfree(hidinput);
  682. }
  683. }
  684. EXPORT_SYMBOL_GPL(hidinput_disconnect);