hid-input.c 27 KB

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