1// SPDX-License-Identifier: GPL-2.0+
2/*
3 *  HID driver for Cougar 500k Gaming Keyboard
4 *
5 *  Copyright (c) 2018 Daniel M. Lambea <dmlambea@gmail.com>
6 */
7
8#include <linux/hid.h>
9#include <linux/module.h>
10#include <linux/printk.h>
11
12#include "hid-ids.h"
13
14MODULE_AUTHOR("Daniel M. Lambea <dmlambea@gmail.com>");
15MODULE_DESCRIPTION("Cougar 500k Gaming Keyboard");
16MODULE_LICENSE("GPL");
17MODULE_INFO(key_mappings, "G1-G6 are mapped to F13-F18");
18
19static bool g6_is_space = true;
20MODULE_PARM_DESC(g6_is_space,
21	"If true, G6 programmable key sends SPACE instead of F18 (default=true)");
22
23#define COUGAR_VENDOR_USAGE	0xff00ff00
24
25#define COUGAR_FIELD_CODE	1
26#define COUGAR_FIELD_ACTION	2
27
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
41
42
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.
47 */
48static 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 },
64*/
65	{ 0, 0 },
66};
67
68struct cougar_shared {
69	struct list_head list;
70	struct kref kref;
71	bool enabled;
72	struct hid_device *dev;
73	struct input_dev *input;
74};
75
76struct cougar {
77	bool special_intf;
78	struct cougar_shared *shared;
79};
80
81static LIST_HEAD(cougar_udev_list);
82static DEFINE_MUTEX(cougar_udev_list_lock);
83
84/**
85 * cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
86 */
87static void cougar_fix_g6_mapping(void)
88{
89	int i;
90
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");
97			return;
98		}
99	}
100	pr_warn("cougar: no mappings defined for G6/spacebar");
101}
102
103/*
104 * Constant-friendly rdesc fixup for mouse interface
105 */
106static __u8 *cougar_report_fixup(struct hid_device *hdev, __u8 *rdesc,
107				 unsigned int *rsize)
108{
109	if (rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
110	    (rdesc[115] | rdesc[116] << 8) >= HID_MAX_USAGES) {
111		hid_info(hdev,
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);
115	}
116	return rdesc;
117}
118
119static struct cougar_shared *cougar_get_shared_data(struct hid_device *hdev)
120{
121	struct cougar_shared *shared;
122
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);
127			return shared;
128		}
129	}
130	return NULL;
131}
132
133static void cougar_release_shared_data(struct kref *kref)
134{
135	struct cougar_shared *shared = container_of(kref,
136						    struct cougar_shared, kref);
137
138	mutex_lock(&cougar_udev_list_lock);
139	list_del(&shared->list);
140	mutex_unlock(&cougar_udev_list_lock);
141
142	kfree(shared);
143}
144
145static void cougar_remove_shared_data(void *resource)
146{
147	struct cougar *cougar = resource;
148
149	if (cougar->shared) {
150		kref_put(&cougar->shared->kref, cougar_release_shared_data);
151		cougar->shared = NULL;
152	}
153}
154
155/*
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.
158 */
159static int cougar_bind_shared_data(struct hid_device *hdev,
160				   struct cougar *cougar)
161{
162	struct cougar_shared *shared;
163	int error = 0;
164
165	mutex_lock(&cougar_udev_list_lock);
166
167	shared = cougar_get_shared_data(hdev);
168	if (!shared) {
169		shared = kzalloc(sizeof(*shared), GFP_KERNEL);
170		if (!shared) {
171			error = -ENOMEM;
172			goto out;
173		}
174
175		kref_init(&shared->kref);
176		shared->dev = hdev;
177		list_add_tail(&shared->list, &cougar_udev_list);
178	}
179
180	cougar->shared = shared;
181
182	error = devm_add_action_or_reset(&hdev->dev, cougar_remove_shared_data, cougar);
183	if (error) {
184		mutex_unlock(&cougar_udev_list_lock);
185		return error;
186	}
187
188out:
189	mutex_unlock(&cougar_udev_list_lock);
190	return error;
191}
192
193static int cougar_probe(struct hid_device *hdev,
194			const struct hid_device_id *id)
195{
196	struct cougar *cougar;
197	struct hid_input *next, *hidinput = NULL;
198	unsigned int connect_mask;
199	int error;
200
201	cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
202	if (!cougar)
203		return -ENOMEM;
204	hid_set_drvdata(hdev, cougar);
205
206	error = hid_parse(hdev);
207	if (error) {
208		hid_err(hdev, "parse failed\n");
209		return error;
210	}
211
212	if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
213		cougar->special_intf = true;
214		connect_mask = HID_CONNECT_HIDRAW;
215	} else
216		connect_mask = HID_CONNECT_DEFAULT;
217
218	error = hid_hw_start(hdev, connect_mask);
219	if (error) {
220		hid_err(hdev, "hw start failed\n");
221		return error;
222	}
223
224	error = cougar_bind_shared_data(hdev, cougar);
225	if (error)
226		goto fail_stop_and_cleanup;
227
228	/* The custom vendor interface will use the hid_input registered
229	 * for the keyboard interface, in order to send translated key codes
230	 * to it.
231	 */
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;
237				break;
238			}
239		}
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);
244		if (error)
245			goto fail_stop_and_cleanup;
246	}
247	return 0;
248
249fail_stop_and_cleanup:
250	hid_hw_stop(hdev);
251	return error;
252}
253
254/*
255 * Convert events from vendor intf to input key events
256 */
257static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
258			    u8 *data, int size)
259{
260	struct cougar *cougar;
261	struct cougar_shared *shared;
262	unsigned char code, action;
263	int i;
264
265	cougar = hid_get_drvdata(hdev);
266	shared = cougar->shared;
267	if (!cougar->special_intf || !shared)
268		return 0;
269
270	if (!shared->enabled || !shared->input)
271		return -EPERM;
272
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);
280			return -EPERM;
281		}
282	}
283	/* Avoid warnings on the same unmapped key twice */
284	if (action != 0)
285		hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
286	return -EPERM;
287}
288
289static void cougar_remove(struct hid_device *hdev)
290{
291	struct cougar *cougar = hid_get_drvdata(hdev);
292
293	if (cougar) {
294		/* Stop the vendor intf to process more events */
295		if (cougar->shared)
296			cougar->shared->enabled = false;
297		if (cougar->special_intf)
298			hid_hw_close(hdev);
299	}
300	hid_hw_stop(hdev);
301}
302
303static int cougar_param_set_g6_is_space(const char *val,
304					const struct kernel_param *kp)
305{
306	int ret;
307
308	ret = param_set_bool(val, kp);
309	if (ret)
310		return ret;
311
312	cougar_fix_g6_mapping();
313
314	return 0;
315}
316
317static const struct kernel_param_ops cougar_g6_is_space_ops = {
318	.set	= cougar_param_set_g6_is_space,
319	.get	= param_get_bool,
320};
321module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
322
323static 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) },
328	{}
329};
330MODULE_DEVICE_TABLE(hid, cougar_id_table);
331
332static struct hid_driver cougar_driver = {
333	.name			= "cougar",
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,
339};
340
341module_hid_driver(cougar_driver);
342