xref: /openbmc/linux/drivers/hid/hid-cougar.c (revision 8f762fe5)
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 
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");
18 
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)");
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  */
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 },
64 */
65 	{ 0, 0 },
66 };
67 
68 struct 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 
76 struct cougar {
77 	bool special_intf;
78 	struct cougar_shared *shared;
79 };
80 
81 static LIST_HEAD(cougar_udev_list);
82 static DEFINE_MUTEX(cougar_udev_list_lock);
83 
84 /**
85  * cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
86  */
87 static 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  */
106 static __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 
119 static 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 
133 static 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 
145 static 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  */
159 static 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 
189 out:
190 	mutex_unlock(&cougar_udev_list_lock);
191 	return error;
192 }
193 
194 static 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 		goto fail;
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 		goto fail;
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 
250 fail_stop_and_cleanup:
251 	hid_hw_stop(hdev);
252 fail:
253 	hid_set_drvdata(hdev, NULL);
254 	return error;
255 }
256 
257 /*
258  * Convert events from vendor intf to input key events
259  */
260 static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
261 			    u8 *data, int size)
262 {
263 	struct cougar *cougar;
264 	struct cougar_shared *shared;
265 	unsigned char code, action;
266 	int i;
267 
268 	cougar = hid_get_drvdata(hdev);
269 	shared = cougar->shared;
270 	if (!cougar->special_intf || !shared)
271 		return 0;
272 
273 	if (!shared->enabled || !shared->input)
274 		return -EPERM;
275 
276 	code = data[COUGAR_FIELD_CODE];
277 	action = data[COUGAR_FIELD_ACTION];
278 	for (i = 0; cougar_mapping[i][0]; i++) {
279 		if (code == cougar_mapping[i][0]) {
280 			input_event(shared->input, EV_KEY,
281 				    cougar_mapping[i][1], action);
282 			input_sync(shared->input);
283 			return -EPERM;
284 		}
285 	}
286 	/* Avoid warnings on the same unmapped key twice */
287 	if (action != 0)
288 		hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
289 	return -EPERM;
290 }
291 
292 static void cougar_remove(struct hid_device *hdev)
293 {
294 	struct cougar *cougar = hid_get_drvdata(hdev);
295 
296 	if (cougar) {
297 		/* Stop the vendor intf to process more events */
298 		if (cougar->shared)
299 			cougar->shared->enabled = false;
300 		if (cougar->special_intf)
301 			hid_hw_close(hdev);
302 	}
303 	hid_hw_stop(hdev);
304 }
305 
306 static int cougar_param_set_g6_is_space(const char *val,
307 					const struct kernel_param *kp)
308 {
309 	int ret;
310 
311 	ret = param_set_bool(val, kp);
312 	if (ret)
313 		return ret;
314 
315 	cougar_fix_g6_mapping();
316 
317 	return 0;
318 }
319 
320 static const struct kernel_param_ops cougar_g6_is_space_ops = {
321 	.set	= cougar_param_set_g6_is_space,
322 	.get	= param_get_bool,
323 };
324 module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
325 
326 static struct hid_device_id cougar_id_table[] = {
327 	{ HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
328 			 USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
329 	{ HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
330 			 USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
331 	{}
332 };
333 MODULE_DEVICE_TABLE(hid, cougar_id_table);
334 
335 static struct hid_driver cougar_driver = {
336 	.name			= "cougar",
337 	.id_table		= cougar_id_table,
338 	.report_fixup		= cougar_report_fixup,
339 	.probe			= cougar_probe,
340 	.remove			= cougar_remove,
341 	.raw_event		= cougar_raw_event,
342 };
343 
344 module_hid_driver(cougar_driver);
345