|
| 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