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(&hdev->dev, cougar_remove_shared_data, cougar);
183	if (error) {
184		mutex_unlock(&cougar_udev_list_lock);
185		cougar_remove_shared_data(cougar);
186		return error;
187	}
188
189out:
190	mutex_unlock(&cougar_udev_list_lock);
191	return error;
192}
193
194static int cougar_probe(struct hid_device *hdev,
195			const struct hid_device_id *id)
196{
197	struct cougar *cougar;
198	struct hid_input *next, *hidinput = NULL;
199	unsigned int connect_mask;
200	int error;
201
202	cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
203	if (!cougar)
204		return -ENOMEM;
205	hid_set_drvdata(hdev, cougar);
206
207	error = hid_parse(hdev);
208	if (error) {
209		hid_err(hdev, "parse failed\n");
210		return error;
211	}
212
213	if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
214		cougar->special_intf = true;
215		connect_mask = HID_CONNECT_HIDRAW;
216	} else
217		connect_mask = HID_CONNECT_DEFAULT;
218
219	error = hid_hw_start(hdev, connect_mask);
220	if (error) {
221		hid_err(hdev, "hw start failed\n");
222		return error;
223	}
224
225	error = cougar_bind_shared_data(hdev, cougar);
226	if (error)
227		goto fail_stop_and_cleanup;
228
229	/* The custom vendor interface will use the hid_input registered
230	 * for the keyboard interface, in order to send translated key codes
231	 * to it.
232	 */
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;
238				break;
239			}
240		}
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);
245		if (error)
246			goto fail_stop_and_cleanup;
247	}
248	return 0;
249
250fail_stop_and_cleanup:
251	hid_hw_stop(hdev);
252	return error;
253}
254
255/*
256 * Convert events from vendor intf to input key events
257 */
258static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
259			    u8 *data, int size)
260{
261	struct cougar *cougar;
262	struct cougar_shared *shared;
263	unsigned char code, action;
264	int i;
265
266	cougar = hid_get_drvdata(hdev);
267	shared = cougar->shared;
268	if (!cougar->special_intf || !shared)
269		return 0;
270
271	if (!shared->enabled || !shared->input)
272		return -EPERM;
273
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);
281			return -EPERM;
282		}
283	}
284	/* Avoid warnings on the same unmapped key twice */
285	if (action != 0)
286		hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
287	return -EPERM;
288}
289
290static void cougar_remove(struct hid_device *hdev)
291{
292	struct cougar *cougar = hid_get_drvdata(hdev);
293
294	if (cougar) {
295		/* Stop the vendor intf to process more events */
296		if (cougar->shared)
297			cougar->shared->enabled = false;
298		if (cougar->special_intf)
299			hid_hw_close(hdev);
300	}
301	hid_hw_stop(hdev);
302}
303
304static int cougar_param_set_g6_is_space(const char *val,
305					const struct kernel_param *kp)
306{
307	int ret;
308
309	ret = param_set_bool(val, kp);
310	if (ret)
311		return ret;
312
313	cougar_fix_g6_mapping();
314
315	return 0;
316}
317
318static const struct kernel_param_ops cougar_g6_is_space_ops = {
319	.set	= cougar_param_set_g6_is_space,
320	.get	= param_get_bool,
321};
322module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
323
324static 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) },
329	{}
330};
331MODULE_DEVICE_TABLE(hid, cougar_id_table);
332
333static struct hid_driver cougar_driver = {
334	.name			= "cougar",
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,
340};
341
342module_hid_driver(cougar_driver);
343