1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// SPDX-License-Identifier: GPL-2.0+
/*
* HID driver for Cougar 500k Gaming Keyboard
*
* Copyright (c) 2018 Daniel M. Lambea <dmlambea@gmail.com>
*/
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/printk.h>
#include "hid-ids.h"
MODULE_AUTHOR("Daniel M. Lambea <dmlambea@gmail.com>");
MODULE_DESCRIPTION("Cougar 500k Gaming Keyboard");
MODULE_LICENSE("GPL");
MODULE_INFO(key_mappings, "G1-G6 are mapped to F13-F18");
static bool g6_is_space = true;
MODULE_PARM_DESC(g6_is_space,
"If true, G6 programmable key sends SPACE instead of F18 (default=true)");
#define COUGAR_VENDOR_USAGE 0xff00ff00
#define COUGAR_FIELD_CODE 1
#define COUGAR_FIELD_ACTION 2
#define COUGAR_KEY_G1 0x83
#define COUGAR_KEY_G2 0x84
#define COUGAR_KEY_G3 0x85
#define COUGAR_KEY_G4 0x86
#define COUGAR_KEY_G5 0x87
#define COUGAR_KEY_G6 0x78
#define COUGAR_KEY_FN 0x0d
#define COUGAR_KEY_MR 0x6f
#define COUGAR_KEY_M1 0x80
#define COUGAR_KEY_M2 0x81
#define COUGAR_KEY_M3 0x82
#define COUGAR_KEY_LEDS 0x67
#define COUGAR_KEY_LOCK 0x6e
/* Default key mappings. The special key COUGAR_KEY_G6 is defined first
* because it is more frequent to use the spacebar rather than any other
* special keys. Depending on the value of the parameter 'g6_is_space',
* the mapping will be updated in the probe function.
*/
static unsigned char cougar_mapping[][2] = {
{ COUGAR_KEY_G6, KEY_SPACE },
{ COUGAR_KEY_G1, KEY_F13 },
{ COUGAR_KEY_G2, KEY_F14 },
{ COUGAR_KEY_G3, KEY_F15 },
{ COUGAR_KEY_G4, KEY_F16 },
{ COUGAR_KEY_G5, KEY_F17 },
{ COUGAR_KEY_LOCK, KEY_SCREENLOCK },
/* The following keys are handled by the hardware itself, so no special
* treatment is required:
{ COUGAR_KEY_FN, KEY_RESERVED },
{ COUGAR_KEY_MR, KEY_RESERVED },
{ COUGAR_KEY_M1, KEY_RESERVED },
{ COUGAR_KEY_M2, KEY_RESERVED },
{ COUGAR_KEY_M3, KEY_RESERVED },
{ COUGAR_KEY_LEDS, KEY_RESERVED },
*/
{ 0, 0 },
};
struct cougar_shared {
struct list_head list;
struct kref kref;
bool enabled;
struct hid_device *dev;
struct input_dev *input;
};
struct cougar {
bool special_intf;
struct cougar_shared *shared;
};
static LIST_HEAD(cougar_udev_list);
static DEFINE_MUTEX(cougar_udev_list_lock);
/**
* cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
*/
static void cougar_fix_g6_mapping(void)
{
int i;
for (i = 0; cougar_mapping[i][0]; i++) {
if (cougar_mapping[i][0] == COUGAR_KEY_G6) {
cougar_mapping[i][1] =
g6_is_space ? KEY_SPACE : KEY_F18;
pr_info("cougar: G6 mapped to %s\n",
g6_is_space ? "space" : "F18");
return;
}
}
pr_warn("cougar: no mappings defined for G6/spacebar");
}
/*
* Constant-friendly rdesc fixup for mouse interface
*/
static __u8 *cougar_report_fixup(struct hid_device *hdev, __u8 *rdesc,
unsigned int *rsize)
{
if (rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
(rdesc[115] | rdesc[116] << 8) >= HID_MAX_USAGES) {
hid_info(hdev,
"usage count exceeds max: fixing up report descriptor\n");
rdesc[115] = ((HID_MAX_USAGES-1) & 0xff);
rdesc[116] = ((HID_MAX_USAGES-1) >> 8);
}
return rdesc;
}
static struct cougar_shared *cougar_get_shared_data(struct hid_device *hdev)
{
struct cougar_shared *shared;
/* Try to find an already-probed interface from the same device */
list_for_each_entry(shared, &cougar_udev_list, list) {
if (hid_compare_device_paths(hdev, shared->dev, '/')) {
kref_get(&shared->kref);
return shared;
}
}
return NULL;
}
static void cougar_release_shared_data(struct kref *kref)
{
struct cougar_shared *shared = container_of(kref,
struct cougar_shared, kref);
mutex_lock(&cougar_udev_list_lock);
list_del(&shared->list);
mutex_unlock(&cougar_udev_list_lock);
kfree(shared);
}
static void cougar_remove_shared_data(void *resource)
{
struct cougar *cougar = resource;
if (cougar->shared) {
kref_put(&cougar->shared->kref, cougar_release_shared_data);
cougar->shared = NULL;
}
}
/*
* Bind the device group's shared data to this cougar struct.
* If no shared data exists for this group, create and initialize it.
*/
static int cougar_bind_shared_data(struct hid_device *hdev,
struct cougar *cougar)
{
struct cougar_shared *shared;
int error = 0;
mutex_lock(&cougar_udev_list_lock);
shared = cougar_get_shared_data(hdev);
if (!shared) {
shared = kzalloc(sizeof(*shared), GFP_KERNEL);
if (!shared) {
error = -ENOMEM;
goto out;
}
kref_init(&shared->kref);
shared->dev = hdev;
list_add_tail(&shared->list, &cougar_udev_list);
}
cougar->shared = shared;
error = devm_add_action_or_reset(&hdev->dev, cougar_remove_shared_data, cougar);
if (error) {
mutex_unlock(&cougar_udev_list_lock);
return error;
}
out:
mutex_unlock(&cougar_udev_list_lock);
return error;
}
static int cougar_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
struct cougar *cougar;
struct hid_input *next, *hidinput = NULL;
unsigned int connect_mask;
int error;
cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
if (!cougar)
return -ENOMEM;
hid_set_drvdata(hdev, cougar);
error = hid_parse(hdev);
if (error) {
hid_err(hdev, "parse failed\n");
return error;
}
if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
cougar->special_intf = true;
connect_mask = HID_CONNECT_HIDRAW;
} else
connect_mask = HID_CONNECT_DEFAULT;
error = hid_hw_start(hdev, connect_mask);
if (error) {
hid_err(hdev, "hw start failed\n");
return error;
}
error = cougar_bind_shared_data(hdev, cougar);
if (error)
goto fail_stop_and_cleanup;
/* The custom vendor interface will use the hid_input registered
* for the keyboard interface, in order to send translated key codes
* to it.
*/
if (hdev->collection->usage == HID_GD_KEYBOARD) {
list_for_each_entry_safe(hidinput, next, &hdev->inputs, list) {
if (hidinput->registered && hidinput->input != NULL) {
cougar->shared->input = hidinput->input;
cougar->shared->enabled = true;
break;
}
}
} else if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
/* Preinit the mapping table */
cougar_fix_g6_mapping();
error = hid_hw_open(hdev);
if (error)
goto fail_stop_and_cleanup;
}
return 0;
fail_stop_and_cleanup:
hid_hw_stop(hdev);
return error;
}
/*
* Convert events from vendor intf to input key events
*/
static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int size)
{
struct cougar *cougar;
struct cougar_shared *shared;
unsigned char code, action;
int i;
cougar = hid_get_drvdata(hdev);
shared = cougar->shared;
if (!cougar->special_intf || !shared)
return 0;
if (!shared->enabled || !shared->input)
return -EPERM;
code = data[COUGAR_FIELD_CODE];
action = data[COUGAR_FIELD_ACTION];
for (i = 0; cougar_mapping[i][0]; i++) {
if (code == cougar_mapping[i][0]) {
input_event(shared->input, EV_KEY,
cougar_mapping[i][1], action);
input_sync(shared->input);
return -EPERM;
}
}
/* Avoid warnings on the same unmapped key twice */
if (action != 0)
hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
return -EPERM;
}
static void cougar_remove(struct hid_device *hdev)
{
struct cougar *cougar = hid_get_drvdata(hdev);
if (cougar) {
/* Stop the vendor intf to process more events */
if (cougar->shared)
cougar->shared->enabled = false;
if (cougar->special_intf)
hid_hw_close(hdev);
}
hid_hw_stop(hdev);
}
static int cougar_param_set_g6_is_space(const char *val,
const struct kernel_param *kp)
{
int ret;
ret = param_set_bool(val, kp);
if (ret)
return ret;
cougar_fix_g6_mapping();
return 0;
}
static const struct kernel_param_ops cougar_g6_is_space_ops = {
.set = cougar_param_set_g6_is_space,
.get = param_get_bool,
};
module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
static const struct hid_device_id cougar_id_table[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
{ HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
{}
};
MODULE_DEVICE_TABLE(hid, cougar_id_table);
static struct hid_driver cougar_driver = {
.name = "cougar",
.id_table = cougar_id_table,
.report_fixup = cougar_report_fixup,
.probe = cougar_probe,
.remove = cougar_remove,
.raw_event = cougar_raw_event,
};
module_hid_driver(cougar_driver);
|