1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for gaming keys on Logitech gaming keyboards (such as the G15)
5 * Copyright (c) 2019 Hans de Goede <hdegoede@redhat.com>
8 #include <linux/device.h>
10 #include <linux/leds.h>
11 #include <linux/module.h>
12 #include <linux/random.h>
13 #include <linux/sched.h>
14 #include <linux/usb.h>
15 #include <linux/wait.h>
19 #define LG_G15_TRANSFER_BUF_SIZE 20
21 #define LG_G15_FEATURE_REPORT 0x02
23 #define LG_G510_FEATURE_M_KEYS_LEDS 0x04
24 #define LG_G510_FEATURE_BACKLIGHT_RGB 0x05
25 #define LG_G510_FEATURE_POWER_ON_RGB 0x06
35 enum lg_g15_led_type {
36 LG_G15_KBD_BRIGHTNESS,
37 LG_G15_LCD_BRIGHTNESS,
38 LG_G15_BRIGHTNESS_MAX,
39 LG_G15_MACRO_PRESET1 = 2,
47 struct led_classdev cdev;
48 enum led_brightness brightness;
49 enum lg_g15_led_type led;
54 /* Must be first for proper dma alignment */
55 u8 transfer_buf[LG_G15_TRANSFER_BUF_SIZE];
56 /* Protects the transfer_buf and led brightness */
58 struct work_struct work;
59 struct input_dev *input;
60 struct hid_device *hdev;
61 enum lg_g15_model model;
62 struct lg_g15_led leds[LG_G15_LED_MAX];
63 bool game_mode_enabled;
66 /******** G15 and G15 v2 LED functions ********/
68 static int lg_g15_update_led_brightness(struct lg_g15_data *g15)
72 ret = hid_hw_raw_request(g15->hdev, LG_G15_FEATURE_REPORT,
74 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
76 hid_err(g15->hdev, "Error getting LED brightness: %d\n", ret);
77 return (ret < 0) ? ret : -EIO;
80 g15->leds[LG_G15_KBD_BRIGHTNESS].brightness = g15->transfer_buf[1];
81 g15->leds[LG_G15_LCD_BRIGHTNESS].brightness = g15->transfer_buf[2];
83 g15->leds[LG_G15_MACRO_PRESET1].brightness =
84 !(g15->transfer_buf[3] & 0x01);
85 g15->leds[LG_G15_MACRO_PRESET2].brightness =
86 !(g15->transfer_buf[3] & 0x02);
87 g15->leds[LG_G15_MACRO_PRESET3].brightness =
88 !(g15->transfer_buf[3] & 0x04);
89 g15->leds[LG_G15_MACRO_RECORD].brightness =
90 !(g15->transfer_buf[3] & 0x08);
95 static enum led_brightness lg_g15_led_get(struct led_classdev *led_cdev)
97 struct lg_g15_led *g15_led =
98 container_of(led_cdev, struct lg_g15_led, cdev);
99 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
100 enum led_brightness brightness;
102 mutex_lock(&g15->mutex);
103 lg_g15_update_led_brightness(g15);
104 brightness = g15->leds[g15_led->led].brightness;
105 mutex_unlock(&g15->mutex);
110 static int lg_g15_led_set(struct led_classdev *led_cdev,
111 enum led_brightness brightness)
113 struct lg_g15_led *g15_led =
114 container_of(led_cdev, struct lg_g15_led, cdev);
115 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
119 /* Ignore LED off on unregister / keyboard unplug */
120 if (led_cdev->flags & LED_UNREGISTERING)
123 mutex_lock(&g15->mutex);
125 g15->transfer_buf[0] = LG_G15_FEATURE_REPORT;
126 g15->transfer_buf[3] = 0;
128 if (g15_led->led < LG_G15_BRIGHTNESS_MAX) {
129 g15->transfer_buf[1] = g15_led->led + 1;
130 g15->transfer_buf[2] = brightness << (g15_led->led * 4);
132 for (i = LG_G15_MACRO_PRESET1; i < LG_G15_LED_MAX; i++) {
133 if (i == g15_led->led)
136 val = g15->leds[i].brightness;
139 mask |= 1 << (i - LG_G15_MACRO_PRESET1);
142 g15->transfer_buf[1] = 0x04;
143 g15->transfer_buf[2] = ~mask;
146 ret = hid_hw_raw_request(g15->hdev, LG_G15_FEATURE_REPORT,
147 g15->transfer_buf, 4,
148 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
151 g15_led->brightness = brightness;
154 hid_err(g15->hdev, "Error setting LED brightness: %d\n", ret);
155 ret = (ret < 0) ? ret : -EIO;
158 mutex_unlock(&g15->mutex);
163 static void lg_g15_leds_changed_work(struct work_struct *work)
165 struct lg_g15_data *g15 = container_of(work, struct lg_g15_data, work);
166 enum led_brightness old_brightness[LG_G15_BRIGHTNESS_MAX];
167 enum led_brightness brightness[LG_G15_BRIGHTNESS_MAX];
170 mutex_lock(&g15->mutex);
171 for (i = 0; i < LG_G15_BRIGHTNESS_MAX; i++)
172 old_brightness[i] = g15->leds[i].brightness;
174 ret = lg_g15_update_led_brightness(g15);
176 for (i = 0; i < LG_G15_BRIGHTNESS_MAX; i++)
177 brightness[i] = g15->leds[i].brightness;
178 mutex_unlock(&g15->mutex);
183 for (i = 0; i < LG_G15_BRIGHTNESS_MAX; i++) {
184 if (brightness[i] == old_brightness[i])
187 led_classdev_notify_brightness_hw_changed(&g15->leds[i].cdev,
192 /******** G510 LED functions ********/
194 static int lg_g510_get_initial_led_brightness(struct lg_g15_data *g15, int i)
198 ret = hid_hw_raw_request(g15->hdev, LG_G510_FEATURE_BACKLIGHT_RGB + i,
199 g15->transfer_buf, 4,
200 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
202 hid_err(g15->hdev, "Error getting LED brightness: %d\n", ret);
203 return (ret < 0) ? ret : -EIO;
206 high = max3(g15->transfer_buf[1], g15->transfer_buf[2],
207 g15->transfer_buf[3]);
211 DIV_ROUND_CLOSEST(g15->transfer_buf[1] * 255, high);
213 DIV_ROUND_CLOSEST(g15->transfer_buf[2] * 255, high);
215 DIV_ROUND_CLOSEST(g15->transfer_buf[3] * 255, high);
216 g15->leds[i].brightness = high;
218 g15->leds[i].red = 255;
219 g15->leds[i].green = 255;
220 g15->leds[i].blue = 255;
221 g15->leds[i].brightness = 0;
227 /* Must be called with g15->mutex locked */
228 static int lg_g510_kbd_led_write(struct lg_g15_data *g15,
229 struct lg_g15_led *g15_led,
230 enum led_brightness brightness)
234 g15->transfer_buf[0] = 5 + g15_led->led;
235 g15->transfer_buf[1] =
236 DIV_ROUND_CLOSEST(g15_led->red * brightness, 255);
237 g15->transfer_buf[2] =
238 DIV_ROUND_CLOSEST(g15_led->green * brightness, 255);
239 g15->transfer_buf[3] =
240 DIV_ROUND_CLOSEST(g15_led->blue * brightness, 255);
242 ret = hid_hw_raw_request(g15->hdev,
243 LG_G510_FEATURE_BACKLIGHT_RGB + g15_led->led,
244 g15->transfer_buf, 4,
245 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
248 g15_led->brightness = brightness;
251 hid_err(g15->hdev, "Error setting LED brightness: %d\n", ret);
252 ret = (ret < 0) ? ret : -EIO;
258 static int lg_g510_kbd_led_set(struct led_classdev *led_cdev,
259 enum led_brightness brightness)
261 struct lg_g15_led *g15_led =
262 container_of(led_cdev, struct lg_g15_led, cdev);
263 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
266 /* Ignore LED off on unregister / keyboard unplug */
267 if (led_cdev->flags & LED_UNREGISTERING)
270 mutex_lock(&g15->mutex);
271 ret = lg_g510_kbd_led_write(g15, g15_led, brightness);
272 mutex_unlock(&g15->mutex);
277 static enum led_brightness lg_g510_kbd_led_get(struct led_classdev *led_cdev)
279 struct lg_g15_led *g15_led =
280 container_of(led_cdev, struct lg_g15_led, cdev);
282 return g15_led->brightness;
285 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
288 struct led_classdev *led_cdev = dev_get_drvdata(dev);
289 struct lg_g15_led *g15_led =
290 container_of(led_cdev, struct lg_g15_led, cdev);
291 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
295 if (count < 7 || (count == 8 && buf[7] != '\n') || count > 8)
301 ret = kstrtoul(buf + 1, 16, &value);
305 mutex_lock(&g15->mutex);
306 g15_led->red = (value & 0xff0000) >> 16;
307 g15_led->green = (value & 0x00ff00) >> 8;
308 g15_led->blue = (value & 0x0000ff);
309 ret = lg_g510_kbd_led_write(g15, g15_led, g15_led->brightness);
310 mutex_unlock(&g15->mutex);
312 return (ret < 0) ? ret : count;
315 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
318 struct led_classdev *led_cdev = dev_get_drvdata(dev);
319 struct lg_g15_led *g15_led =
320 container_of(led_cdev, struct lg_g15_led, cdev);
321 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
324 mutex_lock(&g15->mutex);
325 ret = sprintf(buf, "#%02x%02x%02x\n",
326 g15_led->red, g15_led->green, g15_led->blue);
327 mutex_unlock(&g15->mutex);
332 static DEVICE_ATTR_RW(color);
334 static struct attribute *lg_g510_kbd_led_attrs[] = {
335 &dev_attr_color.attr,
339 static const struct attribute_group lg_g510_kbd_led_group = {
340 .attrs = lg_g510_kbd_led_attrs,
343 static const struct attribute_group *lg_g510_kbd_led_groups[] = {
344 &lg_g510_kbd_led_group,
348 static void lg_g510_leds_sync_work(struct work_struct *work)
350 struct lg_g15_data *g15 = container_of(work, struct lg_g15_data, work);
352 mutex_lock(&g15->mutex);
353 lg_g510_kbd_led_write(g15, &g15->leds[LG_G15_KBD_BRIGHTNESS],
354 g15->leds[LG_G15_KBD_BRIGHTNESS].brightness);
355 mutex_unlock(&g15->mutex);
358 static int lg_g510_update_mkey_led_brightness(struct lg_g15_data *g15)
362 ret = hid_hw_raw_request(g15->hdev, LG_G510_FEATURE_M_KEYS_LEDS,
363 g15->transfer_buf, 2,
364 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
366 hid_err(g15->hdev, "Error getting LED brightness: %d\n", ret);
367 ret = (ret < 0) ? ret : -EIO;
370 g15->leds[LG_G15_MACRO_PRESET1].brightness =
371 !!(g15->transfer_buf[1] & 0x80);
372 g15->leds[LG_G15_MACRO_PRESET2].brightness =
373 !!(g15->transfer_buf[1] & 0x40);
374 g15->leds[LG_G15_MACRO_PRESET3].brightness =
375 !!(g15->transfer_buf[1] & 0x20);
376 g15->leds[LG_G15_MACRO_RECORD].brightness =
377 !!(g15->transfer_buf[1] & 0x10);
382 static enum led_brightness lg_g510_mkey_led_get(struct led_classdev *led_cdev)
384 struct lg_g15_led *g15_led =
385 container_of(led_cdev, struct lg_g15_led, cdev);
386 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
387 enum led_brightness brightness;
389 mutex_lock(&g15->mutex);
390 lg_g510_update_mkey_led_brightness(g15);
391 brightness = g15->leds[g15_led->led].brightness;
392 mutex_unlock(&g15->mutex);
397 static int lg_g510_mkey_led_set(struct led_classdev *led_cdev,
398 enum led_brightness brightness)
400 struct lg_g15_led *g15_led =
401 container_of(led_cdev, struct lg_g15_led, cdev);
402 struct lg_g15_data *g15 = dev_get_drvdata(led_cdev->dev->parent);
406 /* Ignore LED off on unregister / keyboard unplug */
407 if (led_cdev->flags & LED_UNREGISTERING)
410 mutex_lock(&g15->mutex);
412 for (i = LG_G15_MACRO_PRESET1; i < LG_G15_LED_MAX; i++) {
413 if (i == g15_led->led)
416 val = g15->leds[i].brightness;
419 mask |= 0x80 >> (i - LG_G15_MACRO_PRESET1);
422 g15->transfer_buf[0] = LG_G510_FEATURE_M_KEYS_LEDS;
423 g15->transfer_buf[1] = mask;
425 ret = hid_hw_raw_request(g15->hdev, LG_G510_FEATURE_M_KEYS_LEDS,
426 g15->transfer_buf, 2,
427 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
430 g15_led->brightness = brightness;
433 hid_err(g15->hdev, "Error setting LED brightness: %d\n", ret);
434 ret = (ret < 0) ? ret : -EIO;
437 mutex_unlock(&g15->mutex);
442 /******** Generic LED functions ********/
443 static int lg_g15_get_initial_led_brightness(struct lg_g15_data *g15)
447 switch (g15->model) {
450 return lg_g15_update_led_brightness(g15);
452 case LG_G510_USB_AUDIO:
453 ret = lg_g510_get_initial_led_brightness(g15, 0);
457 ret = lg_g510_get_initial_led_brightness(g15, 1);
461 return lg_g510_update_mkey_led_brightness(g15);
464 * Getting the LCD backlight brightness is not supported.
465 * Reading Feature(2) fails with -EPIPE and this crashes
466 * the LCD and touch keys part of the speakers.
470 return -EINVAL; /* Never reached */
473 /******** Input functions ********/
475 /* On the G15 Mark I Logitech has been quite creative with which bit is what */
476 static void lg_g15_handle_lcd_menu_keys(struct lg_g15_data *g15, u8 *data)
480 /* Most left (round/display) button below the LCD */
481 input_report_key(g15->input, KEY_KBD_LCD_MENU1, data[8] & 0x80);
482 /* 4 other buttons below the LCD */
483 for (i = 0; i < 4; i++) {
484 val = data[i + 2] & 0x80;
485 input_report_key(g15->input, KEY_KBD_LCD_MENU2 + i, val);
489 static int lg_g15_event(struct lg_g15_data *g15, u8 *data)
494 for (i = 0; i < 6; i++) {
495 val = data[i + 1] & (1 << i);
496 input_report_key(g15->input, KEY_MACRO1 + i, val);
499 for (i = 0; i < 6; i++) {
500 val = data[i + 2] & (1 << i);
501 input_report_key(g15->input, KEY_MACRO7 + i, val);
504 for (i = 0; i < 5; i++) {
505 val = data[i + 1] & (4 << i);
506 input_report_key(g15->input, KEY_MACRO13 + i, val);
509 input_report_key(g15->input, KEY_MACRO18, data[8] & 0x40);
512 for (i = 0; i < 3; i++) {
513 val = data[i + 6] & (1 << i);
514 input_report_key(g15->input, KEY_MACRO_PRESET1 + i, val);
517 input_report_key(g15->input, KEY_MACRO_RECORD_START, data[7] & 0x40);
519 lg_g15_handle_lcd_menu_keys(g15, data);
521 /* Backlight cycle button pressed? */
523 schedule_work(&g15->work);
525 input_sync(g15->input);
529 static int lg_g15_v2_event(struct lg_g15_data *g15, u8 *data)
534 for (i = 0; i < 6; i++) {
535 val = data[1] & (1 << i);
536 input_report_key(g15->input, KEY_MACRO1 + i, val);
540 input_report_key(g15->input, KEY_MACRO_PRESET1, data[1] & 0x40);
541 input_report_key(g15->input, KEY_MACRO_PRESET2, data[1] & 0x80);
542 input_report_key(g15->input, KEY_MACRO_PRESET3, data[2] & 0x20);
543 input_report_key(g15->input, KEY_MACRO_RECORD_START, data[2] & 0x40);
545 /* Round button to the left of the LCD */
546 input_report_key(g15->input, KEY_KBD_LCD_MENU1, data[2] & 0x80);
547 /* 4 buttons below the LCD */
548 for (i = 0; i < 4; i++) {
549 val = data[2] & (2 << i);
550 input_report_key(g15->input, KEY_KBD_LCD_MENU2 + i, val);
553 /* Backlight cycle button pressed? */
555 schedule_work(&g15->work);
557 input_sync(g15->input);
561 static int lg_g510_event(struct lg_g15_data *g15, u8 *data)
563 bool game_mode_enabled;
567 for (i = 0; i < 18; i++) {
568 val = data[i / 8 + 1] & (1 << (i % 8));
569 input_report_key(g15->input, KEY_MACRO1 + i, val);
572 /* Game mode on/off slider */
573 game_mode_enabled = data[3] & 0x04;
574 if (game_mode_enabled != g15->game_mode_enabled) {
575 if (game_mode_enabled)
576 hid_info(g15->hdev, "Game Mode enabled, Windows (super) key is disabled\n");
578 hid_info(g15->hdev, "Game Mode disabled\n");
579 g15->game_mode_enabled = game_mode_enabled;
583 for (i = 0; i < 3; i++) {
584 val = data[3] & (0x10 << i);
585 input_report_key(g15->input, KEY_MACRO_PRESET1 + i, val);
588 input_report_key(g15->input, KEY_MACRO_RECORD_START, data[3] & 0x80);
591 for (i = 0; i < 5; i++) {
592 val = data[4] & (1 << i);
593 input_report_key(g15->input, KEY_KBD_LCD_MENU1 + i, val);
597 input_report_key(g15->input, KEY_MUTE, data[4] & 0x20);
598 /* Microphone Mute */
599 input_report_key(g15->input, KEY_F20, data[4] & 0x40);
601 input_sync(g15->input);
605 static int lg_g510_leds_event(struct lg_g15_data *g15, u8 *data)
607 bool backlight_disabled;
610 * The G510 ignores backlight updates when the backlight is turned off
611 * through the light toggle button on the keyboard, to work around this
612 * we queue a workitem to sync values when the backlight is turned on.
614 backlight_disabled = data[1] & 0x04;
615 if (!backlight_disabled)
616 schedule_work(&g15->work);
621 static int lg_g15_raw_event(struct hid_device *hdev, struct hid_report *report,
624 struct lg_g15_data *g15 = hid_get_drvdata(hdev);
629 switch (g15->model) {
631 if (data[0] == 0x02 && size == 9)
632 return lg_g15_event(g15, data);
635 if (data[0] == 0x02 && size == 5)
636 return lg_g15_v2_event(g15, data);
639 if (data[0] == 0x02 && size == 9) {
640 lg_g15_handle_lcd_menu_keys(g15, data);
641 input_sync(g15->input);
645 case LG_G510_USB_AUDIO:
646 if (data[0] == 0x03 && size == 5)
647 return lg_g510_event(g15, data);
648 if (data[0] == 0x04 && size == 2)
649 return lg_g510_leds_event(g15, data);
656 static int lg_g15_input_open(struct input_dev *dev)
658 struct hid_device *hdev = input_get_drvdata(dev);
660 return hid_hw_open(hdev);
663 static void lg_g15_input_close(struct input_dev *dev)
665 struct hid_device *hdev = input_get_drvdata(dev);
670 static int lg_g15_register_led(struct lg_g15_data *g15, int i, const char *name)
672 g15->leds[i].led = i;
673 g15->leds[i].cdev.name = name;
675 switch (g15->model) {
678 g15->leds[i].cdev.brightness_get = lg_g15_led_get;
681 g15->leds[i].cdev.brightness_set_blocking = lg_g15_led_set;
682 if (i < LG_G15_BRIGHTNESS_MAX) {
683 g15->leds[i].cdev.flags = LED_BRIGHT_HW_CHANGED;
684 g15->leds[i].cdev.max_brightness = 2;
686 g15->leds[i].cdev.max_brightness = 1;
690 case LG_G510_USB_AUDIO:
692 case LG_G15_LCD_BRIGHTNESS:
694 * The G510 does not have a separate LCD brightness,
695 * but it does have a separate power-on (reset) value.
697 g15->leds[i].cdev.name = "g15::power_on_backlight_val";
699 case LG_G15_KBD_BRIGHTNESS:
700 g15->leds[i].cdev.brightness_set_blocking =
702 g15->leds[i].cdev.brightness_get =
704 g15->leds[i].cdev.max_brightness = 255;
705 g15->leds[i].cdev.groups = lg_g510_kbd_led_groups;
708 g15->leds[i].cdev.brightness_set_blocking =
709 lg_g510_mkey_led_set;
710 g15->leds[i].cdev.brightness_get =
711 lg_g510_mkey_led_get;
712 g15->leds[i].cdev.max_brightness = 1;
717 return devm_led_classdev_register(&g15->hdev->dev, &g15->leds[i].cdev);
720 /* Common input device init code shared between keyboards and Z-10 speaker handling */
721 static void lg_g15_init_input_dev(struct hid_device *hdev, struct input_dev *input,
727 input->phys = hdev->phys;
728 input->uniq = hdev->uniq;
729 input->id.bustype = hdev->bus;
730 input->id.vendor = hdev->vendor;
731 input->id.product = hdev->product;
732 input->id.version = hdev->version;
733 input->dev.parent = &hdev->dev;
734 input->open = lg_g15_input_open;
735 input->close = lg_g15_input_close;
737 /* Keys below the LCD, intended for controlling a menu on the LCD */
738 for (i = 0; i < 5; i++)
739 input_set_capability(input, EV_KEY, KEY_KBD_LCD_MENU1 + i);
742 static int lg_g15_probe(struct hid_device *hdev, const struct hid_device_id *id)
744 static const char * const led_names[] = {
745 "g15::kbd_backlight",
746 "g15::lcd_backlight",
747 "g15::macro_preset1",
748 "g15::macro_preset2",
749 "g15::macro_preset3",
752 u8 gkeys_settings_output_report = 0;
753 u8 gkeys_settings_feature_report = 0;
754 struct hid_report_enum *rep_enum;
755 unsigned int connect_mask = 0;
756 bool has_ff000000 = false;
757 struct lg_g15_data *g15;
758 struct input_dev *input;
759 struct hid_report *rep;
760 int ret, i, gkeys = 0;
762 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
764 ret = hid_parse(hdev);
769 * Some models have multiple interfaces, we want the interface with
770 * the f000.0000 application input report.
772 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
773 list_for_each_entry(rep, &rep_enum->report_list, list) {
774 if (rep->application == 0xff000000)
778 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
780 g15 = devm_kzalloc(&hdev->dev, sizeof(*g15), GFP_KERNEL);
784 mutex_init(&g15->mutex);
786 input = devm_input_allocate_device(&hdev->dev);
791 g15->model = id->driver_data;
793 input_set_drvdata(input, hdev);
794 hid_set_drvdata(hdev, (void *)g15);
796 switch (g15->model) {
798 INIT_WORK(&g15->work, lg_g15_leds_changed_work);
800 * The G15 and G15 v2 use a separate usb-device (on a builtin
801 * hub) which emulates a keyboard for the F1 - F12 emulation
802 * on the G-keys, which we disable, rendering the emulated kbd
803 * non-functional, so we do not let hid-input connect.
805 connect_mask = HID_CONNECT_HIDRAW;
806 gkeys_settings_output_report = 0x02;
810 INIT_WORK(&g15->work, lg_g15_leds_changed_work);
811 connect_mask = HID_CONNECT_HIDRAW;
812 gkeys_settings_output_report = 0x02;
816 case LG_G510_USB_AUDIO:
817 INIT_WORK(&g15->work, lg_g510_leds_sync_work);
818 connect_mask = HID_CONNECT_HIDINPUT | HID_CONNECT_HIDRAW;
819 gkeys_settings_feature_report = 0x01;
823 connect_mask = HID_CONNECT_HIDRAW;
827 ret = hid_hw_start(hdev, connect_mask);
831 /* Tell the keyboard to stop sending F1-F12 + 1-6 for G1 - G18 */
832 if (gkeys_settings_output_report) {
833 g15->transfer_buf[0] = gkeys_settings_output_report;
834 memset(g15->transfer_buf + 1, 0, gkeys);
836 * The kbd ignores our output report if we do not queue
837 * an URB on the USB input endpoint first...
839 ret = hid_hw_open(hdev);
842 ret = hid_hw_output_report(hdev, g15->transfer_buf, gkeys + 1);
846 if (gkeys_settings_feature_report) {
847 g15->transfer_buf[0] = gkeys_settings_feature_report;
848 memset(g15->transfer_buf + 1, 0, gkeys);
849 ret = hid_hw_raw_request(g15->hdev,
850 gkeys_settings_feature_report,
851 g15->transfer_buf, gkeys + 1,
852 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
856 hid_err(hdev, "Error %d disabling keyboard emulation for the G-keys, falling back to generic hid-input driver\n",
858 hid_set_drvdata(hdev, NULL);
862 /* Get initial brightness levels */
863 ret = lg_g15_get_initial_led_brightness(g15);
867 if (g15->model == LG_Z10) {
868 lg_g15_init_input_dev(hdev, g15->input, "Logitech Z-10 LCD Menu Keys");
869 ret = input_register_device(g15->input);
873 ret = lg_g15_register_led(g15, 1, "z-10::lcd_backlight");
877 return 0; /* All done */
880 /* Setup and register input device */
881 lg_g15_init_input_dev(hdev, input, "Logitech Gaming Keyboard Gaming Keys");
884 for (i = 0; i < gkeys; i++)
885 input_set_capability(input, EV_KEY, KEY_MACRO1 + i);
887 /* M1 - M3 and MR keys */
888 for (i = 0; i < 3; i++)
889 input_set_capability(input, EV_KEY, KEY_MACRO_PRESET1 + i);
890 input_set_capability(input, EV_KEY, KEY_MACRO_RECORD_START);
893 * On the G510 only report headphone and mic mute keys when *not* using
894 * the builtin USB audio device. When the builtin audio is used these
895 * keys directly toggle mute (and the LEDs) on/off.
897 if (g15->model == LG_G510) {
898 input_set_capability(input, EV_KEY, KEY_MUTE);
899 /* Userspace expects F20 for micmute */
900 input_set_capability(input, EV_KEY, KEY_F20);
903 ret = input_register_device(input);
907 /* Register LED devices */
908 for (i = 0; i < LG_G15_LED_MAX; i++) {
909 ret = lg_g15_register_led(g15, i, led_names[i]);
921 static const struct hid_device_id lg_g15_devices[] = {
922 /* The G11 is a G15 without the LCD, treat it as a G15 */
923 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
924 USB_DEVICE_ID_LOGITECH_G11),
925 .driver_data = LG_G15 },
926 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
927 USB_DEVICE_ID_LOGITECH_G15_LCD),
928 .driver_data = LG_G15 },
929 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
930 USB_DEVICE_ID_LOGITECH_G15_V2_LCD),
931 .driver_data = LG_G15_V2 },
932 /* G510 without a headset plugged in */
933 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
934 USB_DEVICE_ID_LOGITECH_G510),
935 .driver_data = LG_G510 },
936 /* G510 with headset plugged in / with extra USB audio interface */
937 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
938 USB_DEVICE_ID_LOGITECH_G510_USB_AUDIO),
939 .driver_data = LG_G510_USB_AUDIO },
941 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
942 USB_DEVICE_ID_LOGITECH_Z_10_SPK),
943 .driver_data = LG_Z10 },
946 MODULE_DEVICE_TABLE(hid, lg_g15_devices);
948 static struct hid_driver lg_g15_driver = {
950 .id_table = lg_g15_devices,
951 .raw_event = lg_g15_raw_event,
952 .probe = lg_g15_probe,
954 module_hid_driver(lg_g15_driver);
956 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
957 MODULE_LICENSE("GPL");