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(&hdev
->dev
, cougar_remove_shared_data
, cougar
);
184 mutex_unlock(&cougar_udev_list_lock
);
185 cougar_remove_shared_data(cougar
);
190 mutex_unlock(&cougar_udev_list_lock
);
194 static int cougar_probe(struct hid_device
*hdev
,
195 const struct hid_device_id
*id
)
197 struct cougar
*cougar
;
198 struct hid_input
*next
, *hidinput
= NULL
;
199 unsigned int connect_mask
;
202 cougar
= devm_kzalloc(&hdev
->dev
, sizeof(*cougar
), GFP_KERNEL
);
205 hid_set_drvdata(hdev
, cougar
);
207 error
= hid_parse(hdev
);
209 hid_err(hdev
, "parse failed\n");
213 if (hdev
->collection
->usage
== COUGAR_VENDOR_USAGE
) {
214 cougar
->special_intf
= true;
215 connect_mask
= HID_CONNECT_HIDRAW
;
217 connect_mask
= HID_CONNECT_DEFAULT
;
219 error
= hid_hw_start(hdev
, connect_mask
);
221 hid_err(hdev
, "hw start failed\n");
225 error
= cougar_bind_shared_data(hdev
, cougar
);
227 goto fail_stop_and_cleanup
;
229 /* The custom vendor interface will use the hid_input registered
230 * for the keyboard interface, in order to send translated key codes
233 if (hdev
->collection
->usage
== HID_GD_KEYBOARD
) {
234 list_for_each_entry_safe(hidinput
, next
, &hdev
->inputs
, list
) {
235 if (hidinput
->registered
&& hidinput
->input
!= NULL
) {
236 cougar
->shared
->input
= hidinput
->input
;
237 cougar
->shared
->enabled
= true;
241 } else if (hdev
->collection
->usage
== COUGAR_VENDOR_USAGE
) {
242 /* Preinit the mapping table */
243 cougar_fix_g6_mapping();
244 error
= hid_hw_open(hdev
);
246 goto fail_stop_and_cleanup
;
250 fail_stop_and_cleanup
:
256 * Convert events from vendor intf to input key events
258 static int cougar_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
261 struct cougar
*cougar
;
262 struct cougar_shared
*shared
;
263 unsigned char code
, action
;
266 cougar
= hid_get_drvdata(hdev
);
267 shared
= cougar
->shared
;
268 if (!cougar
->special_intf
|| !shared
)
271 if (!shared
->enabled
|| !shared
->input
)
274 code
= data
[COUGAR_FIELD_CODE
];
275 action
= data
[COUGAR_FIELD_ACTION
];
276 for (i
= 0; cougar_mapping
[i
][0]; i
++) {
277 if (code
== cougar_mapping
[i
][0]) {
278 input_event(shared
->input
, EV_KEY
,
279 cougar_mapping
[i
][1], action
);
280 input_sync(shared
->input
);
284 /* Avoid warnings on the same unmapped key twice */
286 hid_warn(hdev
, "unmapped special key code %0x: ignoring\n", code
);
290 static void cougar_remove(struct hid_device
*hdev
)
292 struct cougar
*cougar
= hid_get_drvdata(hdev
);
295 /* Stop the vendor intf to process more events */
297 cougar
->shared
->enabled
= false;
298 if (cougar
->special_intf
)
304 static int cougar_param_set_g6_is_space(const char *val
,
305 const struct kernel_param
*kp
)
309 ret
= param_set_bool(val
, kp
);
313 cougar_fix_g6_mapping();
318 static const struct kernel_param_ops cougar_g6_is_space_ops
= {
319 .set
= cougar_param_set_g6_is_space
,
320 .get
= param_get_bool
,
322 module_param_cb(g6_is_space
, &cougar_g6_is_space_ops
, &g6_is_space
, 0644);
324 static const struct hid_device_id cougar_id_table
[] = {
325 { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR
,
326 USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD
) },
327 { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR
,
328 USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD
) },
331 MODULE_DEVICE_TABLE(hid
, cougar_id_table
);
333 static struct hid_driver cougar_driver
= {
335 .id_table
= cougar_id_table
,
336 .report_fixup
= cougar_report_fixup
,
337 .probe
= cougar_probe
,
338 .remove
= cougar_remove
,
339 .raw_event
= cougar_raw_event
,
342 module_hid_driver(cougar_driver
);