1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Cougar 500k Gaming Keyboard
5 * Copyright (c) 2018 Daniel M. Lambea <dmlambea@gmail.com>
9 #include <linux/module.h>
10 #include <linux/printk.h>
14 MODULE_AUTHOR("Daniel M. Lambea <dmlambea@gmail.com>");
15 MODULE_DESCRIPTION("Cougar 500k Gaming Keyboard");
16 MODULE_LICENSE("GPL");
17 MODULE_INFO(key_mappings, "G1-G6 are mapped to F13-F18");
19 static bool g6_is_space = true;
20 MODULE_PARM_DESC(g6_is_space,
21 "If true, G6 programmable key sends SPACE instead of F18 (default=true)");
23 #define COUGAR_VENDOR_USAGE 0xff00ff00
25 #define COUGAR_FIELD_CODE 1
26 #define COUGAR_FIELD_ACTION 2
28 #define COUGAR_KEY_G1 0x83
29 #define COUGAR_KEY_G2 0x84
30 #define COUGAR_KEY_G3 0x85
31 #define COUGAR_KEY_G4 0x86
32 #define COUGAR_KEY_G5 0x87
33 #define COUGAR_KEY_G6 0x78
34 #define COUGAR_KEY_FN 0x0d
35 #define COUGAR_KEY_MR 0x6f
36 #define COUGAR_KEY_M1 0x80
37 #define COUGAR_KEY_M2 0x81
38 #define COUGAR_KEY_M3 0x82
39 #define COUGAR_KEY_LEDS 0x67
40 #define COUGAR_KEY_LOCK 0x6e
43 /* Default key mappings. The special key COUGAR_KEY_G6 is defined first
44 * because it is more frequent to use the spacebar rather than any other
45 * special keys. Depending on the value of the parameter 'g6_is_space',
46 * the mapping will be updated in the probe function.
48 static unsigned char cougar_mapping[][2] = {
49 { COUGAR_KEY_G6, KEY_SPACE },
50 { COUGAR_KEY_G1, KEY_F13 },
51 { COUGAR_KEY_G2, KEY_F14 },
52 { COUGAR_KEY_G3, KEY_F15 },
53 { COUGAR_KEY_G4, KEY_F16 },
54 { COUGAR_KEY_G5, KEY_F17 },
55 { COUGAR_KEY_LOCK, KEY_SCREENLOCK },
56 /* The following keys are handled by the hardware itself, so no special
57 * treatment is required:
58 { COUGAR_KEY_FN, KEY_RESERVED },
59 { COUGAR_KEY_MR, KEY_RESERVED },
60 { COUGAR_KEY_M1, KEY_RESERVED },
61 { COUGAR_KEY_M2, KEY_RESERVED },
62 { COUGAR_KEY_M3, KEY_RESERVED },
63 { COUGAR_KEY_LEDS, KEY_RESERVED },
68 struct cougar_shared {
69 struct list_head list;
72 struct hid_device *dev;
73 struct input_dev *input;
78 struct cougar_shared *shared;
81 static LIST_HEAD(cougar_udev_list);
82 static DEFINE_MUTEX(cougar_udev_list_lock);
85 * cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
87 static void cougar_fix_g6_mapping(void)
91 for (i = 0; cougar_mapping[i][0]; i++) {
92 if (cougar_mapping[i][0] == COUGAR_KEY_G6) {
93 cougar_mapping[i][1] =
94 g6_is_space ? KEY_SPACE : KEY_F18;
95 pr_info("cougar: G6 mapped to %s\n",
96 g6_is_space ? "space" : "F18");
100 pr_warn("cougar: no mappings defined for G6/spacebar");
104 * Constant-friendly rdesc fixup for mouse interface
106 static __u8 *cougar_report_fixup(struct hid_device *hdev, __u8 *rdesc,
109 if (rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
110 (rdesc[115] | rdesc[116] << 8) >= HID_MAX_USAGES) {
112 "usage count exceeds max: fixing up report descriptor\n");
113 rdesc[115] = ((HID_MAX_USAGES-1) & 0xff);
114 rdesc[116] = ((HID_MAX_USAGES-1) >> 8);
119 static struct cougar_shared *cougar_get_shared_data(struct hid_device *hdev)
121 struct cougar_shared *shared;
123 /* Try to find an already-probed interface from the same device */
124 list_for_each_entry(shared, &cougar_udev_list, list) {
125 if (hid_compare_device_paths(hdev, shared->dev, '/')) {
126 kref_get(&shared->kref);
133 static void cougar_release_shared_data(struct kref *kref)
135 struct cougar_shared *shared = container_of(kref,
136 struct cougar_shared, kref);
138 mutex_lock(&cougar_udev_list_lock);
139 list_del(&shared->list);
140 mutex_unlock(&cougar_udev_list_lock);
145 static void cougar_remove_shared_data(void *resource)
147 struct cougar *cougar = resource;
149 if (cougar->shared) {
150 kref_put(&cougar->shared->kref, cougar_release_shared_data);
151 cougar->shared = NULL;
156 * Bind the device group's shared data to this cougar struct.
157 * If no shared data exists for this group, create and initialize it.
159 static int cougar_bind_shared_data(struct hid_device *hdev,
160 struct cougar *cougar)
162 struct cougar_shared *shared;
165 mutex_lock(&cougar_udev_list_lock);
167 shared = cougar_get_shared_data(hdev);
169 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
175 kref_init(&shared->kref);
177 list_add_tail(&shared->list, &cougar_udev_list);
180 cougar->shared = shared;
182 error = devm_add_action_or_reset(&hdev->dev, cougar_remove_shared_data, cougar);
184 mutex_unlock(&cougar_udev_list_lock);
189 mutex_unlock(&cougar_udev_list_lock);
193 static int cougar_probe(struct hid_device *hdev,
194 const struct hid_device_id *id)
196 struct cougar *cougar;
197 struct hid_input *next, *hidinput = NULL;
198 unsigned int connect_mask;
201 cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
204 hid_set_drvdata(hdev, cougar);
206 error = hid_parse(hdev);
208 hid_err(hdev, "parse failed\n");
212 if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
213 cougar->special_intf = true;
214 connect_mask = HID_CONNECT_HIDRAW;
216 connect_mask = HID_CONNECT_DEFAULT;
218 error = hid_hw_start(hdev, connect_mask);
220 hid_err(hdev, "hw start failed\n");
224 error = cougar_bind_shared_data(hdev, cougar);
226 goto fail_stop_and_cleanup;
228 /* The custom vendor interface will use the hid_input registered
229 * for the keyboard interface, in order to send translated key codes
232 if (hdev->collection->usage == HID_GD_KEYBOARD) {
233 list_for_each_entry_safe(hidinput, next, &hdev->inputs, list) {
234 if (hidinput->registered && hidinput->input != NULL) {
235 cougar->shared->input = hidinput->input;
236 cougar->shared->enabled = true;
240 } else if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
241 /* Preinit the mapping table */
242 cougar_fix_g6_mapping();
243 error = hid_hw_open(hdev);
245 goto fail_stop_and_cleanup;
249 fail_stop_and_cleanup:
255 * Convert events from vendor intf to input key events
257 static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
260 struct cougar *cougar;
261 struct cougar_shared *shared;
262 unsigned char code, action;
265 cougar = hid_get_drvdata(hdev);
266 shared = cougar->shared;
267 if (!cougar->special_intf || !shared)
270 if (!shared->enabled || !shared->input)
273 code = data[COUGAR_FIELD_CODE];
274 action = data[COUGAR_FIELD_ACTION];
275 for (i = 0; cougar_mapping[i][0]; i++) {
276 if (code == cougar_mapping[i][0]) {
277 input_event(shared->input, EV_KEY,
278 cougar_mapping[i][1], action);
279 input_sync(shared->input);
283 /* Avoid warnings on the same unmapped key twice */
285 hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
289 static void cougar_remove(struct hid_device *hdev)
291 struct cougar *cougar = hid_get_drvdata(hdev);
294 /* Stop the vendor intf to process more events */
296 cougar->shared->enabled = false;
297 if (cougar->special_intf)
303 static int cougar_param_set_g6_is_space(const char *val,
304 const struct kernel_param *kp)
308 ret = param_set_bool(val, kp);
312 cougar_fix_g6_mapping();
317 static const struct kernel_param_ops cougar_g6_is_space_ops = {
318 .set = cougar_param_set_g6_is_space,
319 .get = param_get_bool,
321 module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
323 static const struct hid_device_id cougar_id_table[] = {
324 { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
325 USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
326 { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
327 USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
330 MODULE_DEVICE_TABLE(hid, cougar_id_table);
332 static struct hid_driver cougar_driver = {
334 .id_table = cougar_id_table,
335 .report_fixup = cougar_report_fixup,
336 .probe = cougar_probe,
337 .remove = cougar_remove,
338 .raw_event = cougar_raw_event,
341 module_hid_driver(cougar_driver);