2 * HID driver for Corsair devices
5 * - Vengeance K90 Keyboard
6 * - Scimitar PRO RGB Gaming Mouse
8 * Copyright (c) 2015 Clement Vuchener
9 * Copyright (c) 2017 Oscar Campos
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <linux/hid.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/leds.h>
26 #define CORSAIR_USE_K90_MACRO (1<<0)
27 #define CORSAIR_USE_K90_BACKLIGHT (1<<1)
30 struct led_classdev cdev
;
32 struct work_struct work
;
37 struct k90_led record_led
;
40 struct corsair_drvdata
{
42 struct k90_drvdata
*k90
;
43 struct k90_led
*backlight
;
46 #define K90_GKEY_COUNT 18
48 static int corsair_usage_to_gkey(unsigned int usage
)
50 /* G1 (0xd0) to G16 (0xdf) */
51 if (usage
>= 0xd0 && usage
<= 0xdf)
52 return usage
- 0xd0 + 1;
53 /* G17 (0xe8) to G18 (0xe9) */
54 if (usage
>= 0xe8 && usage
<= 0xe9)
55 return usage
- 0xe8 + 17;
59 static unsigned short corsair_gkey_map
[K90_GKEY_COUNT
] = {
80 module_param_array_named(gkey_codes
, corsair_gkey_map
, ushort
, NULL
, S_IRUGO
);
81 MODULE_PARM_DESC(gkey_codes
, "Key codes for the G-keys");
83 static unsigned short corsair_record_keycodes
[2] = {
88 module_param_array_named(recordkey_codes
, corsair_record_keycodes
, ushort
,
90 MODULE_PARM_DESC(recordkey_codes
, "Key codes for the MR (start and stop record) button");
92 static unsigned short corsair_profile_keycodes
[3] = {
98 module_param_array_named(profilekey_codes
, corsair_profile_keycodes
, ushort
,
100 MODULE_PARM_DESC(profilekey_codes
, "Key codes for the profile buttons");
102 #define CORSAIR_USAGE_SPECIAL_MIN 0xf0
103 #define CORSAIR_USAGE_SPECIAL_MAX 0xff
105 #define CORSAIR_USAGE_MACRO_RECORD_START 0xf6
106 #define CORSAIR_USAGE_MACRO_RECORD_STOP 0xf7
108 #define CORSAIR_USAGE_PROFILE 0xf1
109 #define CORSAIR_USAGE_M1 0xf1
110 #define CORSAIR_USAGE_M2 0xf2
111 #define CORSAIR_USAGE_M3 0xf3
112 #define CORSAIR_USAGE_PROFILE_MAX 0xf3
114 #define CORSAIR_USAGE_META_OFF 0xf4
115 #define CORSAIR_USAGE_META_ON 0xf5
117 #define CORSAIR_USAGE_LIGHT 0xfa
118 #define CORSAIR_USAGE_LIGHT_OFF 0xfa
119 #define CORSAIR_USAGE_LIGHT_DIM 0xfb
120 #define CORSAIR_USAGE_LIGHT_MEDIUM 0xfc
121 #define CORSAIR_USAGE_LIGHT_BRIGHT 0xfd
122 #define CORSAIR_USAGE_LIGHT_MAX 0xfd
124 /* USB control protocol */
126 #define K90_REQUEST_BRIGHTNESS 49
127 #define K90_REQUEST_MACRO_MODE 2
128 #define K90_REQUEST_STATUS 4
129 #define K90_REQUEST_GET_MODE 5
130 #define K90_REQUEST_PROFILE 20
132 #define K90_MACRO_MODE_SW 0x0030
133 #define K90_MACRO_MODE_HW 0x0001
135 #define K90_MACRO_LED_ON 0x0020
136 #define K90_MACRO_LED_OFF 0x0040
142 #define K90_BACKLIGHT_LED_SUFFIX "::backlight"
143 #define K90_RECORD_LED_SUFFIX "::record"
145 static enum led_brightness
k90_backlight_get(struct led_classdev
*led_cdev
)
148 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
149 struct device
*dev
= led
->cdev
.dev
->parent
;
150 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
151 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
155 data
= kmalloc(8, GFP_KERNEL
);
159 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
161 USB_DIR_IN
| USB_TYPE_VENDOR
|
162 USB_RECIP_DEVICE
, 0, 0, data
, 8,
163 USB_CTRL_SET_TIMEOUT
);
165 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
170 brightness
= data
[4];
171 if (brightness
< 0 || brightness
> 3) {
173 "Read invalid backlight brightness: %02hhx.\n",
185 static enum led_brightness
k90_record_led_get(struct led_classdev
*led_cdev
)
187 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
189 return led
->brightness
;
192 static void k90_brightness_set(struct led_classdev
*led_cdev
,
193 enum led_brightness brightness
)
195 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
197 led
->brightness
= brightness
;
198 schedule_work(&led
->work
);
201 static void k90_backlight_work(struct work_struct
*work
)
204 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
206 struct usb_interface
*usbif
;
207 struct usb_device
*usbdev
;
212 dev
= led
->cdev
.dev
->parent
;
213 usbif
= to_usb_interface(dev
->parent
);
214 usbdev
= interface_to_usbdev(usbif
);
216 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
217 K90_REQUEST_BRIGHTNESS
,
218 USB_DIR_OUT
| USB_TYPE_VENDOR
|
219 USB_RECIP_DEVICE
, led
->brightness
, 0,
220 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
222 dev_warn(dev
, "Failed to set backlight brightness (error: %d).\n",
226 static void k90_record_led_work(struct work_struct
*work
)
229 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
231 struct usb_interface
*usbif
;
232 struct usb_device
*usbdev
;
238 dev
= led
->cdev
.dev
->parent
;
239 usbif
= to_usb_interface(dev
->parent
);
240 usbdev
= interface_to_usbdev(usbif
);
242 if (led
->brightness
> 0)
243 value
= K90_MACRO_LED_ON
;
245 value
= K90_MACRO_LED_OFF
;
247 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
248 K90_REQUEST_MACRO_MODE
,
249 USB_DIR_OUT
| USB_TYPE_VENDOR
|
250 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
251 USB_CTRL_SET_TIMEOUT
);
253 dev_warn(dev
, "Failed to set record LED state (error: %d).\n",
258 * Keyboard attributes
261 static ssize_t
k90_show_macro_mode(struct device
*dev
,
262 struct device_attribute
*attr
, char *buf
)
265 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
266 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
267 const char *macro_mode
;
270 data
= kmalloc(2, GFP_KERNEL
);
274 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
275 K90_REQUEST_GET_MODE
,
276 USB_DIR_IN
| USB_TYPE_VENDOR
|
277 USB_RECIP_DEVICE
, 0, 0, data
, 2,
278 USB_CTRL_SET_TIMEOUT
);
280 dev_warn(dev
, "Failed to get K90 initial mode (error %d).\n",
287 case K90_MACRO_MODE_HW
:
291 case K90_MACRO_MODE_SW
:
295 dev_warn(dev
, "K90 in unknown mode: %02hhx.\n",
301 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", macro_mode
);
308 static ssize_t
k90_store_macro_mode(struct device
*dev
,
309 struct device_attribute
*attr
,
310 const char *buf
, size_t count
)
313 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
314 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
317 if (strncmp(buf
, "SW", 2) == 0)
318 value
= K90_MACRO_MODE_SW
;
319 else if (strncmp(buf
, "HW", 2) == 0)
320 value
= K90_MACRO_MODE_HW
;
324 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
325 K90_REQUEST_MACRO_MODE
,
326 USB_DIR_OUT
| USB_TYPE_VENDOR
|
327 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
328 USB_CTRL_SET_TIMEOUT
);
330 dev_warn(dev
, "Failed to set macro mode.\n");
337 static ssize_t
k90_show_current_profile(struct device
*dev
,
338 struct device_attribute
*attr
,
342 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
343 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
347 data
= kmalloc(8, GFP_KERNEL
);
351 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
353 USB_DIR_IN
| USB_TYPE_VENDOR
|
354 USB_RECIP_DEVICE
, 0, 0, data
, 8,
355 USB_CTRL_SET_TIMEOUT
);
357 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
362 current_profile
= data
[7];
363 if (current_profile
< 1 || current_profile
> 3) {
364 dev_warn(dev
, "Read invalid current profile: %02hhx.\n",
370 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", current_profile
);
377 static ssize_t
k90_store_current_profile(struct device
*dev
,
378 struct device_attribute
*attr
,
379 const char *buf
, size_t count
)
382 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
383 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
386 if (kstrtoint(buf
, 10, &profile
))
388 if (profile
< 1 || profile
> 3)
391 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
393 USB_DIR_OUT
| USB_TYPE_VENDOR
|
394 USB_RECIP_DEVICE
, profile
, 0, NULL
, 0,
395 USB_CTRL_SET_TIMEOUT
);
397 dev_warn(dev
, "Failed to change current profile (error %d).\n",
405 static DEVICE_ATTR(macro_mode
, 0644, k90_show_macro_mode
, k90_store_macro_mode
);
406 static DEVICE_ATTR(current_profile
, 0644, k90_show_current_profile
,
407 k90_store_current_profile
);
409 static struct attribute
*k90_attrs
[] = {
410 &dev_attr_macro_mode
.attr
,
411 &dev_attr_current_profile
.attr
,
415 static const struct attribute_group k90_attr_group
= {
423 static int k90_init_backlight(struct hid_device
*dev
)
426 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
430 drvdata
->backlight
= kzalloc(sizeof(struct k90_led
), GFP_KERNEL
);
431 if (!drvdata
->backlight
) {
433 goto fail_backlight_alloc
;
437 strlen(dev_name(&dev
->dev
)) + sizeof(K90_BACKLIGHT_LED_SUFFIX
);
438 name
= kzalloc(name_sz
, GFP_KERNEL
);
441 goto fail_name_alloc
;
443 snprintf(name
, name_sz
, "%s" K90_BACKLIGHT_LED_SUFFIX
,
444 dev_name(&dev
->dev
));
445 drvdata
->backlight
->removed
= false;
446 drvdata
->backlight
->cdev
.name
= name
;
447 drvdata
->backlight
->cdev
.max_brightness
= 3;
448 drvdata
->backlight
->cdev
.brightness_set
= k90_brightness_set
;
449 drvdata
->backlight
->cdev
.brightness_get
= k90_backlight_get
;
450 INIT_WORK(&drvdata
->backlight
->work
, k90_backlight_work
);
451 ret
= led_classdev_register(&dev
->dev
, &drvdata
->backlight
->cdev
);
453 goto fail_register_cdev
;
458 kfree(drvdata
->backlight
->cdev
.name
);
460 kfree(drvdata
->backlight
);
461 drvdata
->backlight
= NULL
;
462 fail_backlight_alloc
:
466 static int k90_init_macro_functions(struct hid_device
*dev
)
469 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
470 struct k90_drvdata
*k90
;
474 k90
= kzalloc(sizeof(struct k90_drvdata
), GFP_KERNEL
);
481 /* Init LED device for record LED */
482 name_sz
= strlen(dev_name(&dev
->dev
)) + sizeof(K90_RECORD_LED_SUFFIX
);
483 name
= kzalloc(name_sz
, GFP_KERNEL
);
486 goto fail_record_led_alloc
;
488 snprintf(name
, name_sz
, "%s" K90_RECORD_LED_SUFFIX
,
489 dev_name(&dev
->dev
));
490 k90
->record_led
.removed
= false;
491 k90
->record_led
.cdev
.name
= name
;
492 k90
->record_led
.cdev
.max_brightness
= 1;
493 k90
->record_led
.cdev
.brightness_set
= k90_brightness_set
;
494 k90
->record_led
.cdev
.brightness_get
= k90_record_led_get
;
495 INIT_WORK(&k90
->record_led
.work
, k90_record_led_work
);
496 k90
->record_led
.brightness
= 0;
497 ret
= led_classdev_register(&dev
->dev
, &k90
->record_led
.cdev
);
499 goto fail_record_led
;
501 /* Init attributes */
502 ret
= sysfs_create_group(&dev
->dev
.kobj
, &k90_attr_group
);
509 k90
->record_led
.removed
= true;
510 led_classdev_unregister(&k90
->record_led
.cdev
);
511 cancel_work_sync(&k90
->record_led
.work
);
513 kfree(k90
->record_led
.cdev
.name
);
514 fail_record_led_alloc
:
521 static void k90_cleanup_backlight(struct hid_device
*dev
)
523 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
525 if (drvdata
->backlight
) {
526 drvdata
->backlight
->removed
= true;
527 led_classdev_unregister(&drvdata
->backlight
->cdev
);
528 cancel_work_sync(&drvdata
->backlight
->work
);
529 kfree(drvdata
->backlight
->cdev
.name
);
530 kfree(drvdata
->backlight
);
534 static void k90_cleanup_macro_functions(struct hid_device
*dev
)
536 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
537 struct k90_drvdata
*k90
= drvdata
->k90
;
540 sysfs_remove_group(&dev
->dev
.kobj
, &k90_attr_group
);
542 k90
->record_led
.removed
= true;
543 led_classdev_unregister(&k90
->record_led
.cdev
);
544 cancel_work_sync(&k90
->record_led
.work
);
545 kfree(k90
->record_led
.cdev
.name
);
551 static int corsair_probe(struct hid_device
*dev
, const struct hid_device_id
*id
)
554 unsigned long quirks
= id
->driver_data
;
555 struct corsair_drvdata
*drvdata
;
556 struct usb_interface
*usbif
= to_usb_interface(dev
->dev
.parent
);
558 drvdata
= devm_kzalloc(&dev
->dev
, sizeof(struct corsair_drvdata
),
562 drvdata
->quirks
= quirks
;
563 hid_set_drvdata(dev
, drvdata
);
565 ret
= hid_parse(dev
);
567 hid_err(dev
, "parse failed\n");
570 ret
= hid_hw_start(dev
, HID_CONNECT_DEFAULT
);
572 hid_err(dev
, "hw start failed\n");
576 if (usbif
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
577 if (quirks
& CORSAIR_USE_K90_MACRO
) {
578 ret
= k90_init_macro_functions(dev
);
580 hid_warn(dev
, "Failed to initialize K90 macro functions.\n");
582 if (quirks
& CORSAIR_USE_K90_BACKLIGHT
) {
583 ret
= k90_init_backlight(dev
);
585 hid_warn(dev
, "Failed to initialize K90 backlight.\n");
592 static void corsair_remove(struct hid_device
*dev
)
594 k90_cleanup_macro_functions(dev
);
595 k90_cleanup_backlight(dev
);
600 static int corsair_event(struct hid_device
*dev
, struct hid_field
*field
,
601 struct hid_usage
*usage
, __s32 value
)
603 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
608 switch (usage
->hid
& HID_USAGE
) {
609 case CORSAIR_USAGE_MACRO_RECORD_START
:
610 drvdata
->k90
->record_led
.brightness
= 1;
612 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
613 drvdata
->k90
->record_led
.brightness
= 0;
622 static int corsair_input_mapping(struct hid_device
*dev
,
623 struct hid_input
*input
,
624 struct hid_field
*field
,
625 struct hid_usage
*usage
, unsigned long **bit
,
630 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_KEYBOARD
)
633 gkey
= corsair_usage_to_gkey(usage
->hid
& HID_USAGE
);
635 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
636 corsair_gkey_map
[gkey
- 1]);
639 if ((usage
->hid
& HID_USAGE
) >= CORSAIR_USAGE_SPECIAL_MIN
&&
640 (usage
->hid
& HID_USAGE
) <= CORSAIR_USAGE_SPECIAL_MAX
) {
641 switch (usage
->hid
& HID_USAGE
) {
642 case CORSAIR_USAGE_MACRO_RECORD_START
:
643 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
644 corsair_record_keycodes
[0]);
647 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
648 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
649 corsair_record_keycodes
[1]);
652 case CORSAIR_USAGE_M1
:
653 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
654 corsair_profile_keycodes
[0]);
657 case CORSAIR_USAGE_M2
:
658 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
659 corsair_profile_keycodes
[1]);
662 case CORSAIR_USAGE_M3
:
663 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
664 corsair_profile_keycodes
[2]);
676 * The report descriptor of Corsair Scimitar RGB Pro gaming mouse is
677 * non parseable as they define two consecutive Logical Minimum for
678 * the Usage Page (Consumer) in rdescs bytes 75 and 77 being 77 0x16
679 * that should be obviousy 0x26 for Logical Magimum of 16 bits. This
680 * prevents poper parsing of the report descriptor due Logical
681 * Minimum being larger than Logical Maximum.
683 * This driver fixes the report descriptor for:
684 * - USB ID b1c:1b3e, sold as Scimitar RGB Pro Gaming mouse
687 static __u8
*corsair_mouse_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
690 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
692 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 1) {
694 * Corsair Scimitar RGB Pro report descriptor is broken and
695 * defines two different Logical Minimum for the Consumer
696 * Application. The byte 77 should be a 0x26 defining a 16
697 * bits integer for the Logical Maximum but it is a 0x16
698 * instead (Logical Minimum)
700 switch (hdev
->product
) {
701 case USB_DEVICE_ID_CORSAIR_SCIMITAR_PRO_RGB
:
702 if (*rsize
>= 172 && rdesc
[75] == 0x15 && rdesc
[77] == 0x16
703 && rdesc
[78] == 0xff && rdesc
[79] == 0x0f) {
704 hid_info(hdev
, "Fixing up report descriptor\n");
714 static const struct hid_device_id corsair_devices
[] = {
715 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_DEVICE_ID_CORSAIR_K90
),
716 .driver_data
= CORSAIR_USE_K90_MACRO
|
717 CORSAIR_USE_K90_BACKLIGHT
},
718 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
,
719 USB_DEVICE_ID_CORSAIR_SCIMITAR_PRO_RGB
) },
723 MODULE_DEVICE_TABLE(hid
, corsair_devices
);
725 static struct hid_driver corsair_driver
= {
727 .id_table
= corsair_devices
,
728 .probe
= corsair_probe
,
729 .event
= corsair_event
,
730 .remove
= corsair_remove
,
731 .input_mapping
= corsair_input_mapping
,
732 .report_fixup
= corsair_mouse_report_fixup
,
735 module_hid_driver(corsair_driver
);
737 MODULE_LICENSE("GPL");
738 /* Original K90 driver author */
739 MODULE_AUTHOR("Clement Vuchener");
740 /* Scimitar PRO RGB driver author */
741 MODULE_AUTHOR("Oscar Campos");
742 MODULE_DESCRIPTION("HID driver for Corsair devices");