hid-lg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * HID driver for some logitech "special" devices
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. * Copyright (c) 2006-2007 Jiri Kosina
  8. * Copyright (c) 2007 Paul Walmsley
  9. * Copyright (c) 2008 Jiri Slaby
  10. * Copyright (c) 2010 Hendrik Iben
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/hid.h>
  20. #include <linux/module.h>
  21. #include <linux/random.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include "hid-ids.h"
  25. #include "hid-lg.h"
  26. #define LG_RDESC 0x001
  27. #define LG_BAD_RELATIVE_KEYS 0x002
  28. #define LG_DUPLICATE_USAGES 0x004
  29. #define LG_EXPANDED_KEYMAP 0x010
  30. #define LG_IGNORE_DOUBLED_WHEEL 0x020
  31. #define LG_WIRELESS 0x040
  32. #define LG_INVERT_HWHEEL 0x080
  33. #define LG_NOGET 0x100
  34. #define LG_FF 0x200
  35. #define LG_FF2 0x400
  36. #define LG_RDESC_REL_ABS 0x800
  37. #define LG_FF3 0x1000
  38. #define LG_FF4 0x2000
  39. /*
  40. * Certain Logitech keyboards send in report #3 keys which are far
  41. * above the logical maximum described in descriptor. This extends
  42. * the original value of 0x28c of logical maximum to 0x104d
  43. */
  44. static __u8 *lg_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  45. unsigned int *rsize)
  46. {
  47. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  48. if ((quirks & LG_RDESC) && *rsize >= 90 && rdesc[83] == 0x26 &&
  49. rdesc[84] == 0x8c && rdesc[85] == 0x02) {
  50. hid_info(hdev,
  51. "fixing up Logitech keyboard report descriptor\n");
  52. rdesc[84] = rdesc[89] = 0x4d;
  53. rdesc[85] = rdesc[90] = 0x10;
  54. }
  55. if ((quirks & LG_RDESC_REL_ABS) && *rsize >= 50 &&
  56. rdesc[32] == 0x81 && rdesc[33] == 0x06 &&
  57. rdesc[49] == 0x81 && rdesc[50] == 0x06) {
  58. hid_info(hdev,
  59. "fixing up rel/abs in Logitech report descriptor\n");
  60. rdesc[33] = rdesc[50] = 0x02;
  61. }
  62. if ((quirks & LG_FF4) && *rsize >= 101 &&
  63. rdesc[41] == 0x95 && rdesc[42] == 0x0B &&
  64. rdesc[47] == 0x05 && rdesc[48] == 0x09) {
  65. hid_info(hdev, "fixing up Logitech Speed Force Wireless button descriptor\n");
  66. rdesc[41] = 0x05;
  67. rdesc[42] = 0x09;
  68. rdesc[47] = 0x95;
  69. rdesc[48] = 0x0B;
  70. }
  71. return rdesc;
  72. }
  73. #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
  74. EV_KEY, (c))
  75. static int lg_ultrax_remote_mapping(struct hid_input *hi,
  76. struct hid_usage *usage, unsigned long **bit, int *max)
  77. {
  78. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
  79. return 0;
  80. set_bit(EV_REP, hi->input->evbit);
  81. switch (usage->hid & HID_USAGE) {
  82. /* Reported on Logitech Ultra X Media Remote */
  83. case 0x004: lg_map_key_clear(KEY_AGAIN); break;
  84. case 0x00d: lg_map_key_clear(KEY_HOME); break;
  85. case 0x024: lg_map_key_clear(KEY_SHUFFLE); break;
  86. case 0x025: lg_map_key_clear(KEY_TV); break;
  87. case 0x026: lg_map_key_clear(KEY_MENU); break;
  88. case 0x031: lg_map_key_clear(KEY_AUDIO); break;
  89. case 0x032: lg_map_key_clear(KEY_TEXT); break;
  90. case 0x033: lg_map_key_clear(KEY_LAST); break;
  91. case 0x047: lg_map_key_clear(KEY_MP3); break;
  92. case 0x048: lg_map_key_clear(KEY_DVD); break;
  93. case 0x049: lg_map_key_clear(KEY_MEDIA); break;
  94. case 0x04a: lg_map_key_clear(KEY_VIDEO); break;
  95. case 0x04b: lg_map_key_clear(KEY_ANGLE); break;
  96. case 0x04c: lg_map_key_clear(KEY_LANGUAGE); break;
  97. case 0x04d: lg_map_key_clear(KEY_SUBTITLE); break;
  98. case 0x051: lg_map_key_clear(KEY_RED); break;
  99. case 0x052: lg_map_key_clear(KEY_CLOSE); break;
  100. default:
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. static int lg_dinovo_mapping(struct hid_input *hi, struct hid_usage *usage,
  106. unsigned long **bit, int *max)
  107. {
  108. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
  109. return 0;
  110. switch (usage->hid & HID_USAGE) {
  111. case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
  112. default:
  113. return 0;
  114. }
  115. return 1;
  116. }
  117. static int lg_wireless_mapping(struct hid_input *hi, struct hid_usage *usage,
  118. unsigned long **bit, int *max)
  119. {
  120. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
  121. return 0;
  122. switch (usage->hid & HID_USAGE) {
  123. case 0x1001: lg_map_key_clear(KEY_MESSENGER); break;
  124. case 0x1003: lg_map_key_clear(KEY_SOUND); break;
  125. case 0x1004: lg_map_key_clear(KEY_VIDEO); break;
  126. case 0x1005: lg_map_key_clear(KEY_AUDIO); break;
  127. case 0x100a: lg_map_key_clear(KEY_DOCUMENTS); break;
  128. /* The following two entries are Playlist 1 and 2 on the MX3200 */
  129. case 0x100f: lg_map_key_clear(KEY_FN_1); break;
  130. case 0x1010: lg_map_key_clear(KEY_FN_2); break;
  131. case 0x1011: lg_map_key_clear(KEY_PREVIOUSSONG); break;
  132. case 0x1012: lg_map_key_clear(KEY_NEXTSONG); break;
  133. case 0x1013: lg_map_key_clear(KEY_CAMERA); break;
  134. case 0x1014: lg_map_key_clear(KEY_MESSENGER); break;
  135. case 0x1015: lg_map_key_clear(KEY_RECORD); break;
  136. case 0x1016: lg_map_key_clear(KEY_PLAYER); break;
  137. case 0x1017: lg_map_key_clear(KEY_EJECTCD); break;
  138. case 0x1018: lg_map_key_clear(KEY_MEDIA); break;
  139. case 0x1019: lg_map_key_clear(KEY_PROG1); break;
  140. case 0x101a: lg_map_key_clear(KEY_PROG2); break;
  141. case 0x101b: lg_map_key_clear(KEY_PROG3); break;
  142. case 0x101c: lg_map_key_clear(KEY_CYCLEWINDOWS); break;
  143. case 0x101f: lg_map_key_clear(KEY_ZOOMIN); break;
  144. case 0x1020: lg_map_key_clear(KEY_ZOOMOUT); break;
  145. case 0x1021: lg_map_key_clear(KEY_ZOOMRESET); break;
  146. case 0x1023: lg_map_key_clear(KEY_CLOSE); break;
  147. case 0x1027: lg_map_key_clear(KEY_MENU); break;
  148. /* this one is marked as 'Rotate' */
  149. case 0x1028: lg_map_key_clear(KEY_ANGLE); break;
  150. case 0x1029: lg_map_key_clear(KEY_SHUFFLE); break;
  151. case 0x102a: lg_map_key_clear(KEY_BACK); break;
  152. case 0x102b: lg_map_key_clear(KEY_CYCLEWINDOWS); break;
  153. case 0x102d: lg_map_key_clear(KEY_WWW); break;
  154. /* The following two are 'Start/answer call' and 'End/reject call'
  155. on the MX3200 */
  156. case 0x1031: lg_map_key_clear(KEY_OK); break;
  157. case 0x1032: lg_map_key_clear(KEY_CANCEL); break;
  158. case 0x1041: lg_map_key_clear(KEY_BATTERY); break;
  159. case 0x1042: lg_map_key_clear(KEY_WORDPROCESSOR); break;
  160. case 0x1043: lg_map_key_clear(KEY_SPREADSHEET); break;
  161. case 0x1044: lg_map_key_clear(KEY_PRESENTATION); break;
  162. case 0x1045: lg_map_key_clear(KEY_UNDO); break;
  163. case 0x1046: lg_map_key_clear(KEY_REDO); break;
  164. case 0x1047: lg_map_key_clear(KEY_PRINT); break;
  165. case 0x1048: lg_map_key_clear(KEY_SAVE); break;
  166. case 0x1049: lg_map_key_clear(KEY_PROG1); break;
  167. case 0x104a: lg_map_key_clear(KEY_PROG2); break;
  168. case 0x104b: lg_map_key_clear(KEY_PROG3); break;
  169. case 0x104c: lg_map_key_clear(KEY_PROG4); break;
  170. default:
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. static int lg_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  176. struct hid_field *field, struct hid_usage *usage,
  177. unsigned long **bit, int *max)
  178. {
  179. /* extended mapping for certain Logitech hardware (Logitech cordless
  180. desktop LX500) */
  181. static const u8 e_keymap[] = {
  182. 0,216, 0,213,175,156, 0, 0, 0, 0,
  183. 144, 0, 0, 0, 0, 0, 0, 0, 0,212,
  184. 174,167,152,161,112, 0, 0, 0,154, 0,
  185. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  186. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 0, 0, 0, 0,183,184,185,186,187,
  189. 188,189,190,191,192,193,194, 0, 0, 0
  190. };
  191. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  192. unsigned int hid = usage->hid;
  193. if (hdev->product == USB_DEVICE_ID_LOGITECH_RECEIVER &&
  194. lg_ultrax_remote_mapping(hi, usage, bit, max))
  195. return 1;
  196. if (hdev->product == USB_DEVICE_ID_DINOVO_MINI &&
  197. lg_dinovo_mapping(hi, usage, bit, max))
  198. return 1;
  199. if ((quirks & LG_WIRELESS) && lg_wireless_mapping(hi, usage, bit, max))
  200. return 1;
  201. if ((hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
  202. return 0;
  203. hid &= HID_USAGE;
  204. /* Special handling for Logitech Cordless Desktop */
  205. if (field->application == HID_GD_MOUSE) {
  206. if ((quirks & LG_IGNORE_DOUBLED_WHEEL) &&
  207. (hid == 7 || hid == 8))
  208. return -1;
  209. } else {
  210. if ((quirks & LG_EXPANDED_KEYMAP) &&
  211. hid < ARRAY_SIZE(e_keymap) &&
  212. e_keymap[hid] != 0) {
  213. hid_map_usage(hi, usage, bit, max, EV_KEY,
  214. e_keymap[hid]);
  215. return 1;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int lg_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  221. struct hid_field *field, struct hid_usage *usage,
  222. unsigned long **bit, int *max)
  223. {
  224. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  225. if ((quirks & LG_BAD_RELATIVE_KEYS) && usage->type == EV_KEY &&
  226. (field->flags & HID_MAIN_ITEM_RELATIVE))
  227. field->flags &= ~HID_MAIN_ITEM_RELATIVE;
  228. if ((quirks & LG_DUPLICATE_USAGES) && (usage->type == EV_KEY ||
  229. usage->type == EV_REL || usage->type == EV_ABS))
  230. clear_bit(usage->code, *bit);
  231. return 0;
  232. }
  233. static int lg_event(struct hid_device *hdev, struct hid_field *field,
  234. struct hid_usage *usage, __s32 value)
  235. {
  236. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  237. if ((quirks & LG_INVERT_HWHEEL) && usage->code == REL_HWHEEL) {
  238. input_event(field->hidinput->input, usage->type, usage->code,
  239. -value);
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. static int lg_probe(struct hid_device *hdev, const struct hid_device_id *id)
  245. {
  246. unsigned long quirks = id->driver_data;
  247. unsigned int connect_mask = HID_CONNECT_DEFAULT;
  248. int ret;
  249. hid_set_drvdata(hdev, (void *)quirks);
  250. if (quirks & LG_NOGET)
  251. hdev->quirks |= HID_QUIRK_NOGET;
  252. ret = hid_parse(hdev);
  253. if (ret) {
  254. hid_err(hdev, "parse failed\n");
  255. goto err_free;
  256. }
  257. if (quirks & (LG_FF | LG_FF2 | LG_FF3))
  258. connect_mask &= ~HID_CONNECT_FF;
  259. ret = hid_hw_start(hdev, connect_mask);
  260. if (ret) {
  261. hid_err(hdev, "hw start failed\n");
  262. goto err_free;
  263. }
  264. if (quirks & LG_FF4) {
  265. unsigned char buf[] = { 0x00, 0xAF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  266. ret = hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  267. if (ret >= 0) {
  268. /* insert a little delay of 10 jiffies ~ 40ms */
  269. wait_queue_head_t wait;
  270. init_waitqueue_head (&wait);
  271. wait_event_interruptible_timeout(wait, 0, 10);
  272. /* Select random Address */
  273. buf[1] = 0xB2;
  274. get_random_bytes(&buf[2], 2);
  275. ret = hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  276. }
  277. }
  278. if (quirks & LG_FF)
  279. lgff_init(hdev);
  280. if (quirks & LG_FF2)
  281. lg2ff_init(hdev);
  282. if (quirks & LG_FF3)
  283. lg3ff_init(hdev);
  284. if (quirks & LG_FF4)
  285. lg4ff_init(hdev);
  286. return 0;
  287. err_free:
  288. return ret;
  289. }
  290. static const struct hid_device_id lg_devices[] = {
  291. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
  292. .driver_data = LG_RDESC | LG_WIRELESS },
  293. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER),
  294. .driver_data = LG_RDESC | LG_WIRELESS },
  295. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2),
  296. .driver_data = LG_RDESC | LG_WIRELESS },
  297. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER),
  298. .driver_data = LG_BAD_RELATIVE_KEYS },
  299. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP),
  300. .driver_data = LG_DUPLICATE_USAGES },
  301. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE),
  302. .driver_data = LG_DUPLICATE_USAGES },
  303. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI),
  304. .driver_data = LG_DUPLICATE_USAGES },
  305. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD),
  306. .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
  307. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500),
  308. .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
  309. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D),
  310. .driver_data = LG_NOGET },
  311. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL),
  312. .driver_data = LG_NOGET | LG_FF },
  313. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD_CORD),
  314. .driver_data = LG_FF2 },
  315. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD),
  316. .driver_data = LG_FF },
  317. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2),
  318. .driver_data = LG_FF },
  319. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D),
  320. .driver_data = LG_FF },
  321. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO),
  322. .driver_data = LG_FF },
  323. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL),
  324. .driver_data = LG_FF },
  325. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2),
  326. .driver_data = LG_FF },
  327. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL),
  328. .driver_data = LG_FF },
  329. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL),
  330. .driver_data = LG_FF },
  331. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL),
  332. .driver_data = LG_FF },
  333. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL),
  334. .driver_data = LG_FF4 },
  335. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ),
  336. .driver_data = LG_FF },
  337. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2),
  338. .driver_data = LG_FF2 },
  339. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940),
  340. .driver_data = LG_FF3 },
  341. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR),
  342. .driver_data = LG_RDESC_REL_ABS },
  343. { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER),
  344. .driver_data = LG_RDESC_REL_ABS },
  345. { }
  346. };
  347. MODULE_DEVICE_TABLE(hid, lg_devices);
  348. static struct hid_driver lg_driver = {
  349. .name = "logitech",
  350. .id_table = lg_devices,
  351. .report_fixup = lg_report_fixup,
  352. .input_mapping = lg_input_mapping,
  353. .input_mapped = lg_input_mapped,
  354. .event = lg_event,
  355. .probe = lg_probe,
  356. };
  357. static int __init lg_init(void)
  358. {
  359. return hid_register_driver(&lg_driver);
  360. }
  361. static void __exit lg_exit(void)
  362. {
  363. hid_unregister_driver(&lg_driver);
  364. }
  365. module_init(lg_init);
  366. module_exit(lg_exit);
  367. MODULE_LICENSE("GPL");