1 // SPDX-License-Identifier: GPL-2.0+
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
6 * Copyright (c) 2021 Jelle van der Waa <jvanderwaa@redhat.com>
9 #include <linux/device.h>
10 #include <linux/hid.h>
11 #include <linux/module.h>
12 #include <linux/random.h>
13 #include <linux/sched.h>
14 #include <linux/usb.h>
15 #include <linux/wait.h>
19 #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
21 #define RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE 91
23 static bool macro_key_remapping
= 1;
24 module_param(macro_key_remapping
, bool, 0644);
25 MODULE_PARM_DESC(macro_key_remapping
, " on (Y) off (N)");
28 static unsigned char blackwidow_init
[RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE
] = {
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04,
31 0x02, 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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 static int razer_input_mapping(struct hid_device
*hdev
,
45 struct hid_input
*hi
, struct hid_field
*field
,
46 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
49 if (!macro_key_remapping
)
52 if ((usage
->hid
& HID_UP_KEYBOARD
) != HID_UP_KEYBOARD
)
55 switch (usage
->hid
& ~HID_UP_KEYBOARD
) {
57 map_key_clear(KEY_MACRO1
);
60 map_key_clear(KEY_MACRO2
);
63 map_key_clear(KEY_MACRO3
);
66 map_key_clear(KEY_MACRO4
);
69 map_key_clear(KEY_MACRO5
);
76 static int razer_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
81 ret
= hid_parse(hdev
);
86 * Only send the enable macro keys command for the third device
87 * identified as mouse input.
89 if (hdev
->type
== HID_TYPE_USBMOUSE
) {
90 buf
= kmemdup(blackwidow_init
, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE
, GFP_KERNEL
);
94 ret
= hid_hw_raw_request(hdev
, 0, buf
, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE
,
95 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
96 if (ret
!= RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE
)
97 hid_err(hdev
, "failed to enable macro keys: %d\n", ret
);
102 return hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
105 static const struct hid_device_id razer_devices
[] = {
106 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
,
107 USB_DEVICE_ID_RAZER_BLACKWIDOW
) },
108 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
,
109 USB_DEVICE_ID_RAZER_BLACKWIDOW_CLASSIC
) },
110 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
,
111 USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE
) },
114 MODULE_DEVICE_TABLE(hid
, razer_devices
);
116 static struct hid_driver razer_driver
= {
118 .id_table
= razer_devices
,
119 .input_mapping
= razer_input_mapping
,
120 .probe
= razer_probe
,
122 module_hid_driver(razer_driver
);
124 MODULE_AUTHOR("Jelle van der Waa <jvanderwaa@redhat.com>");
125 MODULE_DESCRIPTION("HID driver for gaming keys on Razer Blackwidow gaming keyboards");
126 MODULE_LICENSE("GPL");