2 * HID driver for Corsair devices
5 * - Vengeance K70 Keyboard
6 * - K70 RAPIDFIRE Keyboard
7 * - Vengeance K90 Keyboard
8 * - Scimitar PRO RGB Gaming Mouse
10 * Copyright (c) 2015 Clement Vuchener
11 * Copyright (c) 2017 Oscar Campos
12 * Copyright (c) 2017 Aaron Bottegal
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
22 #include <linux/hid.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/leds.h>
29 #define CORSAIR_USE_K90_MACRO (1<<0)
30 #define CORSAIR_USE_K90_BACKLIGHT (1<<1)
33 struct led_classdev cdev
;
35 struct work_struct work
;
40 struct k90_led record_led
;
43 struct corsair_drvdata
{
45 struct k90_drvdata
*k90
;
46 struct k90_led
*backlight
;
49 #define K90_GKEY_COUNT 18
51 static int corsair_usage_to_gkey(unsigned int usage
)
53 /* G1 (0xd0) to G16 (0xdf) */
54 if (usage
>= 0xd0 && usage
<= 0xdf)
55 return usage
- 0xd0 + 1;
56 /* G17 (0xe8) to G18 (0xe9) */
57 if (usage
>= 0xe8 && usage
<= 0xe9)
58 return usage
- 0xe8 + 17;
62 static unsigned short corsair_gkey_map
[K90_GKEY_COUNT
] = {
83 module_param_array_named(gkey_codes
, corsair_gkey_map
, ushort
, NULL
, S_IRUGO
);
84 MODULE_PARM_DESC(gkey_codes
, "Key codes for the G-keys");
86 static unsigned short corsair_record_keycodes
[2] = {
91 module_param_array_named(recordkey_codes
, corsair_record_keycodes
, ushort
,
93 MODULE_PARM_DESC(recordkey_codes
, "Key codes for the MR (start and stop record) button");
95 static unsigned short corsair_profile_keycodes
[3] = {
101 module_param_array_named(profilekey_codes
, corsair_profile_keycodes
, ushort
,
103 MODULE_PARM_DESC(profilekey_codes
, "Key codes for the profile buttons");
105 #define CORSAIR_USAGE_SPECIAL_MIN 0xf0
106 #define CORSAIR_USAGE_SPECIAL_MAX 0xff
108 #define CORSAIR_USAGE_MACRO_RECORD_START 0xf6
109 #define CORSAIR_USAGE_MACRO_RECORD_STOP 0xf7
111 #define CORSAIR_USAGE_PROFILE 0xf1
112 #define CORSAIR_USAGE_M1 0xf1
113 #define CORSAIR_USAGE_M2 0xf2
114 #define CORSAIR_USAGE_M3 0xf3
115 #define CORSAIR_USAGE_PROFILE_MAX 0xf3
117 #define CORSAIR_USAGE_META_OFF 0xf4
118 #define CORSAIR_USAGE_META_ON 0xf5
120 #define CORSAIR_USAGE_LIGHT 0xfa
121 #define CORSAIR_USAGE_LIGHT_OFF 0xfa
122 #define CORSAIR_USAGE_LIGHT_DIM 0xfb
123 #define CORSAIR_USAGE_LIGHT_MEDIUM 0xfc
124 #define CORSAIR_USAGE_LIGHT_BRIGHT 0xfd
125 #define CORSAIR_USAGE_LIGHT_MAX 0xfd
127 /* USB control protocol */
129 #define K90_REQUEST_BRIGHTNESS 49
130 #define K90_REQUEST_MACRO_MODE 2
131 #define K90_REQUEST_STATUS 4
132 #define K90_REQUEST_GET_MODE 5
133 #define K90_REQUEST_PROFILE 20
135 #define K90_MACRO_MODE_SW 0x0030
136 #define K90_MACRO_MODE_HW 0x0001
138 #define K90_MACRO_LED_ON 0x0020
139 #define K90_MACRO_LED_OFF 0x0040
145 #define K90_BACKLIGHT_LED_SUFFIX "::backlight"
146 #define K90_RECORD_LED_SUFFIX "::record"
148 static enum led_brightness
k90_backlight_get(struct led_classdev
*led_cdev
)
151 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
152 struct device
*dev
= led
->cdev
.dev
->parent
;
153 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
154 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
158 data
= kmalloc(8, GFP_KERNEL
);
162 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
164 USB_DIR_IN
| USB_TYPE_VENDOR
|
165 USB_RECIP_DEVICE
, 0, 0, data
, 8,
166 USB_CTRL_SET_TIMEOUT
);
168 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
173 brightness
= data
[4];
174 if (brightness
< 0 || brightness
> 3) {
176 "Read invalid backlight brightness: %02hhx.\n",
188 static enum led_brightness
k90_record_led_get(struct led_classdev
*led_cdev
)
190 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
192 return led
->brightness
;
195 static void k90_brightness_set(struct led_classdev
*led_cdev
,
196 enum led_brightness brightness
)
198 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
200 led
->brightness
= brightness
;
201 schedule_work(&led
->work
);
204 static void k90_backlight_work(struct work_struct
*work
)
207 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
209 struct usb_interface
*usbif
;
210 struct usb_device
*usbdev
;
215 dev
= led
->cdev
.dev
->parent
;
216 usbif
= to_usb_interface(dev
->parent
);
217 usbdev
= interface_to_usbdev(usbif
);
219 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
220 K90_REQUEST_BRIGHTNESS
,
221 USB_DIR_OUT
| USB_TYPE_VENDOR
|
222 USB_RECIP_DEVICE
, led
->brightness
, 0,
223 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
225 dev_warn(dev
, "Failed to set backlight brightness (error: %d).\n",
229 static void k90_record_led_work(struct work_struct
*work
)
232 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
234 struct usb_interface
*usbif
;
235 struct usb_device
*usbdev
;
241 dev
= led
->cdev
.dev
->parent
;
242 usbif
= to_usb_interface(dev
->parent
);
243 usbdev
= interface_to_usbdev(usbif
);
245 if (led
->brightness
> 0)
246 value
= K90_MACRO_LED_ON
;
248 value
= K90_MACRO_LED_OFF
;
250 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
251 K90_REQUEST_MACRO_MODE
,
252 USB_DIR_OUT
| USB_TYPE_VENDOR
|
253 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
254 USB_CTRL_SET_TIMEOUT
);
256 dev_warn(dev
, "Failed to set record LED state (error: %d).\n",
261 * Keyboard attributes
264 static ssize_t
k90_show_macro_mode(struct device
*dev
,
265 struct device_attribute
*attr
, char *buf
)
268 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
269 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
270 const char *macro_mode
;
273 data
= kmalloc(2, GFP_KERNEL
);
277 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
278 K90_REQUEST_GET_MODE
,
279 USB_DIR_IN
| USB_TYPE_VENDOR
|
280 USB_RECIP_DEVICE
, 0, 0, data
, 2,
281 USB_CTRL_SET_TIMEOUT
);
283 dev_warn(dev
, "Failed to get K90 initial mode (error %d).\n",
290 case K90_MACRO_MODE_HW
:
294 case K90_MACRO_MODE_SW
:
298 dev_warn(dev
, "K90 in unknown mode: %02hhx.\n",
304 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", macro_mode
);
311 static ssize_t
k90_store_macro_mode(struct device
*dev
,
312 struct device_attribute
*attr
,
313 const char *buf
, size_t count
)
316 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
317 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
320 if (strncmp(buf
, "SW", 2) == 0)
321 value
= K90_MACRO_MODE_SW
;
322 else if (strncmp(buf
, "HW", 2) == 0)
323 value
= K90_MACRO_MODE_HW
;
327 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
328 K90_REQUEST_MACRO_MODE
,
329 USB_DIR_OUT
| USB_TYPE_VENDOR
|
330 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
331 USB_CTRL_SET_TIMEOUT
);
333 dev_warn(dev
, "Failed to set macro mode.\n");
340 static ssize_t
k90_show_current_profile(struct device
*dev
,
341 struct device_attribute
*attr
,
345 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
346 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
350 data
= kmalloc(8, GFP_KERNEL
);
354 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
356 USB_DIR_IN
| USB_TYPE_VENDOR
|
357 USB_RECIP_DEVICE
, 0, 0, data
, 8,
358 USB_CTRL_SET_TIMEOUT
);
360 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
365 current_profile
= data
[7];
366 if (current_profile
< 1 || current_profile
> 3) {
367 dev_warn(dev
, "Read invalid current profile: %02hhx.\n",
373 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", current_profile
);
380 static ssize_t
k90_store_current_profile(struct device
*dev
,
381 struct device_attribute
*attr
,
382 const char *buf
, size_t count
)
385 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
386 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
389 if (kstrtoint(buf
, 10, &profile
))
391 if (profile
< 1 || profile
> 3)
394 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
396 USB_DIR_OUT
| USB_TYPE_VENDOR
|
397 USB_RECIP_DEVICE
, profile
, 0, NULL
, 0,
398 USB_CTRL_SET_TIMEOUT
);
400 dev_warn(dev
, "Failed to change current profile (error %d).\n",
408 static DEVICE_ATTR(macro_mode
, 0644, k90_show_macro_mode
, k90_store_macro_mode
);
409 static DEVICE_ATTR(current_profile
, 0644, k90_show_current_profile
,
410 k90_store_current_profile
);
412 static struct attribute
*k90_attrs
[] = {
413 &dev_attr_macro_mode
.attr
,
414 &dev_attr_current_profile
.attr
,
418 static const struct attribute_group k90_attr_group
= {
426 static int k90_init_backlight(struct hid_device
*dev
)
429 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
433 drvdata
->backlight
= kzalloc(sizeof(struct k90_led
), GFP_KERNEL
);
434 if (!drvdata
->backlight
) {
436 goto fail_backlight_alloc
;
440 strlen(dev_name(&dev
->dev
)) + sizeof(K90_BACKLIGHT_LED_SUFFIX
);
441 name
= kzalloc(name_sz
, GFP_KERNEL
);
444 goto fail_name_alloc
;
446 snprintf(name
, name_sz
, "%s" K90_BACKLIGHT_LED_SUFFIX
,
447 dev_name(&dev
->dev
));
448 drvdata
->backlight
->removed
= false;
449 drvdata
->backlight
->cdev
.name
= name
;
450 drvdata
->backlight
->cdev
.max_brightness
= 3;
451 drvdata
->backlight
->cdev
.brightness_set
= k90_brightness_set
;
452 drvdata
->backlight
->cdev
.brightness_get
= k90_backlight_get
;
453 INIT_WORK(&drvdata
->backlight
->work
, k90_backlight_work
);
454 ret
= led_classdev_register(&dev
->dev
, &drvdata
->backlight
->cdev
);
456 goto fail_register_cdev
;
461 kfree(drvdata
->backlight
->cdev
.name
);
463 kfree(drvdata
->backlight
);
464 drvdata
->backlight
= NULL
;
465 fail_backlight_alloc
:
469 static int k90_init_macro_functions(struct hid_device
*dev
)
472 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
473 struct k90_drvdata
*k90
;
477 k90
= kzalloc(sizeof(struct k90_drvdata
), GFP_KERNEL
);
484 /* Init LED device for record LED */
485 name_sz
= strlen(dev_name(&dev
->dev
)) + sizeof(K90_RECORD_LED_SUFFIX
);
486 name
= kzalloc(name_sz
, GFP_KERNEL
);
489 goto fail_record_led_alloc
;
491 snprintf(name
, name_sz
, "%s" K90_RECORD_LED_SUFFIX
,
492 dev_name(&dev
->dev
));
493 k90
->record_led
.removed
= false;
494 k90
->record_led
.cdev
.name
= name
;
495 k90
->record_led
.cdev
.max_brightness
= 1;
496 k90
->record_led
.cdev
.brightness_set
= k90_brightness_set
;
497 k90
->record_led
.cdev
.brightness_get
= k90_record_led_get
;
498 INIT_WORK(&k90
->record_led
.work
, k90_record_led_work
);
499 k90
->record_led
.brightness
= 0;
500 ret
= led_classdev_register(&dev
->dev
, &k90
->record_led
.cdev
);
502 goto fail_record_led
;
504 /* Init attributes */
505 ret
= sysfs_create_group(&dev
->dev
.kobj
, &k90_attr_group
);
512 k90
->record_led
.removed
= true;
513 led_classdev_unregister(&k90
->record_led
.cdev
);
514 cancel_work_sync(&k90
->record_led
.work
);
516 kfree(k90
->record_led
.cdev
.name
);
517 fail_record_led_alloc
:
524 static void k90_cleanup_backlight(struct hid_device
*dev
)
526 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
528 if (drvdata
->backlight
) {
529 drvdata
->backlight
->removed
= true;
530 led_classdev_unregister(&drvdata
->backlight
->cdev
);
531 cancel_work_sync(&drvdata
->backlight
->work
);
532 kfree(drvdata
->backlight
->cdev
.name
);
533 kfree(drvdata
->backlight
);
537 static void k90_cleanup_macro_functions(struct hid_device
*dev
)
539 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
540 struct k90_drvdata
*k90
= drvdata
->k90
;
543 sysfs_remove_group(&dev
->dev
.kobj
, &k90_attr_group
);
545 k90
->record_led
.removed
= true;
546 led_classdev_unregister(&k90
->record_led
.cdev
);
547 cancel_work_sync(&k90
->record_led
.work
);
548 kfree(k90
->record_led
.cdev
.name
);
554 static int corsair_probe(struct hid_device
*dev
, const struct hid_device_id
*id
)
557 unsigned long quirks
= id
->driver_data
;
558 struct corsair_drvdata
*drvdata
;
559 struct usb_interface
*usbif
= to_usb_interface(dev
->dev
.parent
);
561 drvdata
= devm_kzalloc(&dev
->dev
, sizeof(struct corsair_drvdata
),
565 drvdata
->quirks
= quirks
;
566 hid_set_drvdata(dev
, drvdata
);
568 ret
= hid_parse(dev
);
570 hid_err(dev
, "parse failed\n");
573 ret
= hid_hw_start(dev
, HID_CONNECT_DEFAULT
);
575 hid_err(dev
, "hw start failed\n");
579 if (usbif
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
580 if (quirks
& CORSAIR_USE_K90_MACRO
) {
581 ret
= k90_init_macro_functions(dev
);
583 hid_warn(dev
, "Failed to initialize K90 macro functions.\n");
585 if (quirks
& CORSAIR_USE_K90_BACKLIGHT
) {
586 ret
= k90_init_backlight(dev
);
588 hid_warn(dev
, "Failed to initialize K90 backlight.\n");
595 static void corsair_remove(struct hid_device
*dev
)
597 k90_cleanup_macro_functions(dev
);
598 k90_cleanup_backlight(dev
);
603 static int corsair_event(struct hid_device
*dev
, struct hid_field
*field
,
604 struct hid_usage
*usage
, __s32 value
)
606 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
611 switch (usage
->hid
& HID_USAGE
) {
612 case CORSAIR_USAGE_MACRO_RECORD_START
:
613 drvdata
->k90
->record_led
.brightness
= 1;
615 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
616 drvdata
->k90
->record_led
.brightness
= 0;
625 static int corsair_input_mapping(struct hid_device
*dev
,
626 struct hid_input
*input
,
627 struct hid_field
*field
,
628 struct hid_usage
*usage
, unsigned long **bit
,
633 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_KEYBOARD
)
636 gkey
= corsair_usage_to_gkey(usage
->hid
& HID_USAGE
);
638 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
639 corsair_gkey_map
[gkey
- 1]);
642 if ((usage
->hid
& HID_USAGE
) >= CORSAIR_USAGE_SPECIAL_MIN
&&
643 (usage
->hid
& HID_USAGE
) <= CORSAIR_USAGE_SPECIAL_MAX
) {
644 switch (usage
->hid
& HID_USAGE
) {
645 case CORSAIR_USAGE_MACRO_RECORD_START
:
646 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
647 corsair_record_keycodes
[0]);
650 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
651 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
652 corsair_record_keycodes
[1]);
655 case CORSAIR_USAGE_M1
:
656 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
657 corsair_profile_keycodes
[0]);
660 case CORSAIR_USAGE_M2
:
661 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
662 corsair_profile_keycodes
[1]);
665 case CORSAIR_USAGE_M3
:
666 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
667 corsair_profile_keycodes
[2]);
679 * The report descriptor of some of the Corsair gaming mice is
680 * non parseable as they define two consecutive Logical Minimum for
681 * the Usage Page (Consumer) in rdescs bytes 75 and 77 being 77 0x16
682 * that should be obviousy 0x26 for Logical Magimum of 16 bits. This
683 * prevents poper parsing of the report descriptor due Logical
684 * Minimum being larger than Logical Maximum.
686 * This driver fixes the report descriptor for:
687 * - USB ID 1b1c:1b34, sold as GLAIVE RGB Gaming mouse
688 * - USB ID 1b1c:1b3e, sold as Scimitar RGB Pro Gaming mouse
691 static __u8
*corsair_mouse_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
694 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
696 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 1) {
698 * Corsair GLAIVE RGB and Scimitar RGB Pro report descriptor is
699 * broken and defines two different Logical Minimum for the
700 * Consumer Application. The byte 77 should be a 0x26 defining
701 * a 16 bits integer for the Logical Maximum but it is a 0x16
702 * instead (Logical Minimum)
704 switch (hdev
->product
) {
705 case USB_DEVICE_ID_CORSAIR_GLAIVE_RGB
:
706 case USB_DEVICE_ID_CORSAIR_SCIMITAR_PRO_RGB
:
707 if (*rsize
>= 172 && rdesc
[75] == 0x15 && rdesc
[77] == 0x16
708 && rdesc
[78] == 0xff && rdesc
[79] == 0x0f) {
709 hid_info(hdev
, "Fixing up report descriptor\n");
719 static const struct hid_device_id corsair_devices
[] = {
720 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_DEVICE_ID_CORSAIR_K90
),
721 .driver_data
= CORSAIR_USE_K90_MACRO
|
722 CORSAIR_USE_K90_BACKLIGHT
},
723 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
,
724 USB_DEVICE_ID_CORSAIR_GLAIVE_RGB
) },
725 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
,
726 USB_DEVICE_ID_CORSAIR_SCIMITAR_PRO_RGB
) },
728 * Vengeance K70 and K70 RAPIDFIRE share product IDs.
730 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
,
731 USB_DEVICE_ID_CORSAIR_K70R
) },
735 MODULE_DEVICE_TABLE(hid
, corsair_devices
);
737 static struct hid_driver corsair_driver
= {
739 .id_table
= corsair_devices
,
740 .probe
= corsair_probe
,
741 .event
= corsair_event
,
742 .remove
= corsair_remove
,
743 .input_mapping
= corsair_input_mapping
,
744 .report_fixup
= corsair_mouse_report_fixup
,
747 module_hid_driver(corsair_driver
);
749 MODULE_LICENSE("GPL");
750 /* Original K90 driver author */
751 MODULE_AUTHOR("Clement Vuchener");
752 /* Scimitar PRO RGB driver author */
753 MODULE_AUTHOR("Oscar Campos");
754 MODULE_DESCRIPTION("HID driver for Corsair devices");