Skip to content

Commit 3a02753

Browse files
jwrdegoedeJiri Kosina
authored andcommitted
HID: Add driver for Logitech gaming keyboards (G15, G15 v2)
Add a driver to stop the extra "G" keys from sending F1 - F12 instead making them send KEY_GKEY# and also make the non-functional M1 - M3 and MR keys and the non-functional buttons below the LCD panel properly generated key events. Note the connect_mask and gkeys_settings_output_report variables may seem unnecessary since they are always set to the same value, these are there in preparation of adding support for the G, M and LCD keys on the G510 kbd. Signed-off-by: Hans de Goede <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent b5625db commit 3a02753

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

MAINTAINERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9587,6 +9587,13 @@ S: Maintained
95879587
F: Documentation/admin-guide/ldm.rst
95889588
F: block/partitions/ldm.*
95899589

9590+
LOGITECH HID GAMING KEYBOARDS
9591+
M: Hans de Goede <[email protected]>
9592+
9593+
T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git
9594+
S: Maintained
9595+
F: drivers/hid/hid-lg-g15.c
9596+
95909597
LSILOGIC MPT FUSION DRIVERS (FC/SAS/SPI)
95919598
M: Sathya Prakash <[email protected]>
95929599
M: Chaitra P B <[email protected]>

drivers/hid/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ obj-$(CONFIG_HID_KYE) += hid-kye.o
6464
obj-$(CONFIG_HID_LCPOWER) += hid-lcpower.o
6565
obj-$(CONFIG_HID_LENOVO) += hid-lenovo.o
6666
obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
67+
obj-$(CONFIG_HID_LOGITECH) += hid-lg-g15.o
6768
obj-$(CONFIG_HID_LOGITECH_DJ) += hid-logitech-dj.o
6869
obj-$(CONFIG_HID_LOGITECH_HIDPP) += hid-logitech-hidpp.o
6970
obj-$(CONFIG_HID_MACALLY) += hid-macally.o

drivers/hid/hid-ids.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@
747747
#define USB_DEVICE_ID_LOGITECH_DUAL_ACTION 0xc216
748748
#define USB_DEVICE_ID_LOGITECH_RUMBLEPAD2 0xc218
749749
#define USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2 0xc219
750+
#define USB_DEVICE_ID_LOGITECH_G15_LCD 0xc222
751+
#define USB_DEVICE_ID_LOGITECH_G15_V2_LCD 0xc227
750752
#define USB_DEVICE_ID_LOGITECH_G29_WHEEL 0xc24f
751753
#define USB_DEVICE_ID_LOGITECH_G920_WHEEL 0xc262
752754
#define USB_DEVICE_ID_LOGITECH_WINGMAN_F3D 0xc283

drivers/hid/hid-lg-g15.c

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* HID driver for gaming keys on Logitech gaming keyboards (such as the G15)
4+
*
5+
* Copyright (c) 2019 Hans de Goede <[email protected]>
6+
*/
7+
8+
#include <linux/device.h>
9+
#include <linux/hid.h>
10+
#include <linux/module.h>
11+
#include <linux/random.h>
12+
#include <linux/sched.h>
13+
#include <linux/usb.h>
14+
#include <linux/wait.h>
15+
16+
#include "hid-ids.h"
17+
18+
#define LG_G15_TRANSFER_BUF_SIZE 20
19+
20+
enum lg_g15_model {
21+
LG_G15,
22+
LG_G15_V2,
23+
};
24+
25+
struct lg_g15_data {
26+
/* Must be first for proper dma alignment */
27+
u8 transfer_buf[LG_G15_TRANSFER_BUF_SIZE];
28+
struct input_dev *input;
29+
struct hid_device *hdev;
30+
enum lg_g15_model model;
31+
};
32+
33+
/* On the G15 Mark I Logitech has been quite creative with which bit is what */
34+
static int lg_g15_event(struct lg_g15_data *g15, u8 *data, int size)
35+
{
36+
int i, val;
37+
38+
/* G1 - G6 */
39+
for (i = 0; i < 6; i++) {
40+
val = data[i + 1] & (1 << i);
41+
input_report_key(g15->input, KEY_MACRO1 + i, val);
42+
}
43+
/* G7 - G12 */
44+
for (i = 0; i < 6; i++) {
45+
val = data[i + 2] & (1 << i);
46+
input_report_key(g15->input, KEY_MACRO7 + i, val);
47+
}
48+
/* G13 - G17 */
49+
for (i = 0; i < 5; i++) {
50+
val = data[i + 1] & (4 << i);
51+
input_report_key(g15->input, KEY_MACRO13 + i, val);
52+
}
53+
/* G18 */
54+
input_report_key(g15->input, KEY_MACRO18, data[8] & 0x40);
55+
56+
/* M1 - M3 */
57+
for (i = 0; i < 3; i++) {
58+
val = data[i + 6] & (1 << i);
59+
input_report_key(g15->input, KEY_MACRO_PRESET1 + i, val);
60+
}
61+
/* MR */
62+
input_report_key(g15->input, KEY_MACRO_RECORD_START, data[7] & 0x40);
63+
64+
/* Most left (round) button below the LCD */
65+
input_report_key(g15->input, KEY_KBD_LCD_MENU1, data[8] & 0x80);
66+
/* 4 other buttons below the LCD */
67+
for (i = 0; i < 4; i++) {
68+
val = data[i + 2] & 0x80;
69+
input_report_key(g15->input, KEY_KBD_LCD_MENU2 + i, val);
70+
}
71+
72+
input_sync(g15->input);
73+
return 0;
74+
}
75+
76+
static int lg_g15_v2_event(struct lg_g15_data *g15, u8 *data, int size)
77+
{
78+
int i, val;
79+
80+
/* G1 - G6 */
81+
for (i = 0; i < 6; i++) {
82+
val = data[1] & (1 << i);
83+
input_report_key(g15->input, KEY_MACRO1 + i, val);
84+
}
85+
86+
/* M1 - M3 + MR */
87+
input_report_key(g15->input, KEY_MACRO_PRESET1, data[1] & 0x40);
88+
input_report_key(g15->input, KEY_MACRO_PRESET2, data[1] & 0x80);
89+
input_report_key(g15->input, KEY_MACRO_PRESET3, data[2] & 0x20);
90+
input_report_key(g15->input, KEY_MACRO_RECORD_START, data[2] & 0x40);
91+
92+
/* Round button to the left of the LCD */
93+
input_report_key(g15->input, KEY_KBD_LCD_MENU1, data[2] & 0x80);
94+
/* 4 buttons below the LCD */
95+
for (i = 0; i < 4; i++) {
96+
val = data[2] & (2 << i);
97+
input_report_key(g15->input, KEY_KBD_LCD_MENU2 + i, val);
98+
}
99+
100+
input_sync(g15->input);
101+
return 0;
102+
}
103+
104+
static int lg_g15_raw_event(struct hid_device *hdev, struct hid_report *report,
105+
u8 *data, int size)
106+
{
107+
struct lg_g15_data *g15 = hid_get_drvdata(hdev);
108+
109+
if (g15->model == LG_G15 && data[0] == 0x02 && size == 9)
110+
return lg_g15_event(g15, data, size);
111+
112+
if (g15->model == LG_G15_V2 && data[0] == 0x02 && size == 5)
113+
return lg_g15_v2_event(g15, data, size);
114+
115+
return 0;
116+
}
117+
118+
static int lg_g15_input_open(struct input_dev *dev)
119+
{
120+
struct hid_device *hdev = input_get_drvdata(dev);
121+
122+
return hid_hw_open(hdev);
123+
}
124+
125+
static void lg_g15_input_close(struct input_dev *dev)
126+
{
127+
struct hid_device *hdev = input_get_drvdata(dev);
128+
129+
hid_hw_close(hdev);
130+
}
131+
132+
static int lg_g15_probe(struct hid_device *hdev, const struct hid_device_id *id)
133+
{
134+
u8 gkeys_settings_output_report = 0;
135+
unsigned int connect_mask = 0;
136+
struct lg_g15_data *g15;
137+
struct input_dev *input;
138+
int ret, i, gkeys = 0;
139+
140+
ret = hid_parse(hdev);
141+
if (ret)
142+
return ret;
143+
144+
g15 = devm_kzalloc(&hdev->dev, sizeof(*g15), GFP_KERNEL);
145+
if (!g15)
146+
return -ENOMEM;
147+
148+
input = devm_input_allocate_device(&hdev->dev);
149+
if (!input)
150+
return -ENOMEM;
151+
152+
g15->hdev = hdev;
153+
g15->model = id->driver_data;
154+
hid_set_drvdata(hdev, (void *)g15);
155+
156+
switch (g15->model) {
157+
case LG_G15:
158+
/*
159+
* The G15 and G15 v2 use a separate usb-device (on a builtin
160+
* hub) which emulates a keyboard for the F1 - F12 emulation
161+
* on the G-keys, which we disable, rendering the emulated kbd
162+
* non-functional, so we do not let hid-input connect.
163+
*/
164+
connect_mask = HID_CONNECT_HIDRAW;
165+
gkeys_settings_output_report = 0x02;
166+
gkeys = 18;
167+
break;
168+
case LG_G15_V2:
169+
connect_mask = HID_CONNECT_HIDRAW;
170+
gkeys_settings_output_report = 0x02;
171+
gkeys = 6;
172+
break;
173+
}
174+
175+
ret = hid_hw_start(hdev, connect_mask);
176+
if (ret)
177+
return ret;
178+
179+
/* Tell the keyboard to stop sending F1-F12 + 1-6 for G1 - G18 */
180+
if (gkeys_settings_output_report) {
181+
g15->transfer_buf[0] = gkeys_settings_output_report;
182+
memset(g15->transfer_buf + 1, 0, gkeys);
183+
/*
184+
* The kbd ignores our output report if we do not queue
185+
* an URB on the USB input endpoint first...
186+
*/
187+
ret = hid_hw_open(hdev);
188+
if (ret)
189+
goto error_hw_stop;
190+
ret = hid_hw_output_report(hdev, g15->transfer_buf, gkeys + 1);
191+
hid_hw_close(hdev);
192+
}
193+
194+
if (ret < 0) {
195+
hid_err(hdev, "Error disabling keyboard emulation for the G-keys\n");
196+
goto error_hw_stop;
197+
}
198+
199+
input->name = "Logitech Gaming Keyboard Gaming Keys";
200+
input->phys = hdev->phys;
201+
input->uniq = hdev->uniq;
202+
input->id.bustype = hdev->bus;
203+
input->id.vendor = hdev->vendor;
204+
input->id.product = hdev->product;
205+
input->id.version = hdev->version;
206+
input->dev.parent = &hdev->dev;
207+
input->open = lg_g15_input_open;
208+
input->close = lg_g15_input_close;
209+
210+
/* G-keys */
211+
for (i = 0; i < gkeys; i++)
212+
input_set_capability(input, EV_KEY, KEY_MACRO1 + i);
213+
214+
/* M1 - M3 and MR keys */
215+
for (i = 0; i < 3; i++)
216+
input_set_capability(input, EV_KEY, KEY_MACRO_PRESET1 + i);
217+
input_set_capability(input, EV_KEY, KEY_MACRO_RECORD_START);
218+
219+
/* Keys below the LCD, intended for controlling a menu on the LCD */
220+
for (i = 0; i < 5; i++)
221+
input_set_capability(input, EV_KEY, KEY_KBD_LCD_MENU1 + i);
222+
223+
g15->input = input;
224+
input_set_drvdata(input, hdev);
225+
226+
ret = input_register_device(input);
227+
if (ret)
228+
goto error_hw_stop;
229+
230+
return 0;
231+
232+
error_hw_stop:
233+
hid_hw_stop(hdev);
234+
return ret;
235+
}
236+
237+
static const struct hid_device_id lg_g15_devices[] = {
238+
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
239+
USB_DEVICE_ID_LOGITECH_G15_LCD),
240+
.driver_data = LG_G15 },
241+
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
242+
USB_DEVICE_ID_LOGITECH_G15_V2_LCD),
243+
.driver_data = LG_G15_V2 },
244+
{ }
245+
};
246+
MODULE_DEVICE_TABLE(hid, lg_g15_devices);
247+
248+
static struct hid_driver lg_g15_driver = {
249+
.name = "lg-g15",
250+
.id_table = lg_g15_devices,
251+
.raw_event = lg_g15_raw_event,
252+
.probe = lg_g15_probe,
253+
};
254+
module_hid_driver(lg_g15_driver);
255+
256+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)