hid-razer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for gaming keys on Razer Blackwidow gaming keyboards
  4. * Macro Key Keycodes: M1 = 191, M2 = 192, M3 = 193, M4 = 194, M5 = 195
  5. *
  6. * Copyright (c) 2021 Jelle van der Waa <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/hid.h>
  10. #include <linux/module.h>
  11. #include <linux/random.h>
  12. #include <linux/sched.h>
  13. #include <linux/usb.h>
  14. #include <linux/wait.h>
  15. #include "hid-ids.h"
  16. #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
  17. #define RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE 91
  18. static bool macro_key_remapping = 1;
  19. module_param(macro_key_remapping, bool, 0644);
  20. MODULE_PARM_DESC(macro_key_remapping, " on (Y) off (N)");
  21. static unsigned char blackwidow_init[RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE] = {
  22. 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04,
  24. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0x04, 0x00
  35. };
  36. static int razer_input_mapping(struct hid_device *hdev,
  37. struct hid_input *hi, struct hid_field *field,
  38. struct hid_usage *usage, unsigned long **bit, int *max)
  39. {
  40. if (!macro_key_remapping)
  41. return 0;
  42. if ((usage->hid & HID_UP_KEYBOARD) != HID_UP_KEYBOARD)
  43. return 0;
  44. switch (usage->hid & ~HID_UP_KEYBOARD) {
  45. case 0x68:
  46. map_key_clear(KEY_MACRO1);
  47. return 1;
  48. case 0x69:
  49. map_key_clear(KEY_MACRO2);
  50. return 1;
  51. case 0x6a:
  52. map_key_clear(KEY_MACRO3);
  53. return 1;
  54. case 0x6b:
  55. map_key_clear(KEY_MACRO4);
  56. return 1;
  57. case 0x6c:
  58. map_key_clear(KEY_MACRO5);
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. static int razer_probe(struct hid_device *hdev, const struct hid_device_id *id)
  64. {
  65. char *buf;
  66. int ret = 0;
  67. ret = hid_parse(hdev);
  68. if (ret)
  69. return ret;
  70. /*
  71. * Only send the enable macro keys command for the third device
  72. * identified as mouse input.
  73. */
  74. if (hdev->type == HID_TYPE_USBMOUSE) {
  75. buf = kmemdup(blackwidow_init, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE, GFP_KERNEL);
  76. if (buf == NULL)
  77. return -ENOMEM;
  78. ret = hid_hw_raw_request(hdev, 0, buf, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE,
  79. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  80. if (ret != RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE)
  81. hid_err(hdev, "failed to enable macro keys: %d\n", ret);
  82. kfree(buf);
  83. }
  84. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  85. }
  86. static const struct hid_device_id razer_devices[] = {
  87. { HID_USB_DEVICE(USB_VENDOR_ID_RAZER,
  88. USB_DEVICE_ID_RAZER_BLACKWIDOW) },
  89. { HID_USB_DEVICE(USB_VENDOR_ID_RAZER,
  90. USB_DEVICE_ID_RAZER_BLACKWIDOW_CLASSIC) },
  91. { HID_USB_DEVICE(USB_VENDOR_ID_RAZER,
  92. USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE) },
  93. { }
  94. };
  95. MODULE_DEVICE_TABLE(hid, razer_devices);
  96. static struct hid_driver razer_driver = {
  97. .name = "razer",
  98. .id_table = razer_devices,
  99. .input_mapping = razer_input_mapping,
  100. .probe = razer_probe,
  101. };
  102. module_hid_driver(razer_driver);
  103. MODULE_AUTHOR("Jelle van der Waa <[email protected]>");
  104. MODULE_LICENSE("GPL");