2 * HID driver for Corsair devices
5 * - Vengeance K90 Keyboard
7 * Copyright (c) 2015 Clement Vuchener
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/leds.h>
24 #define CORSAIR_USE_K90_MACRO (1<<0)
25 #define CORSAIR_USE_K90_BACKLIGHT (1<<1)
28 struct led_classdev cdev
;
30 struct work_struct work
;
35 struct k90_led record_led
;
38 struct corsair_drvdata
{
40 struct k90_drvdata
*k90
;
41 struct k90_led
*backlight
;
44 #define K90_GKEY_COUNT 18
46 static int corsair_usage_to_gkey(unsigned int usage
)
48 /* G1 (0xd0) to G16 (0xdf) */
49 if (usage
>= 0xd0 && usage
<= 0xdf)
50 return usage
- 0xd0 + 1;
51 /* G17 (0xe8) to G18 (0xe9) */
52 if (usage
>= 0xe8 && usage
<= 0xe9)
53 return usage
- 0xe8 + 17;
57 static unsigned short corsair_gkey_map
[K90_GKEY_COUNT
] = {
78 module_param_array_named(gkey_codes
, corsair_gkey_map
, ushort
, NULL
, S_IRUGO
);
79 MODULE_PARM_DESC(gkey_codes
, "Key codes for the G-keys");
81 static unsigned short corsair_record_keycodes
[2] = {
86 module_param_array_named(recordkey_codes
, corsair_record_keycodes
, ushort
,
88 MODULE_PARM_DESC(recordkey_codes
, "Key codes for the MR (start and stop record) button");
90 static unsigned short corsair_profile_keycodes
[3] = {
96 module_param_array_named(profilekey_codes
, corsair_profile_keycodes
, ushort
,
98 MODULE_PARM_DESC(profilekey_codes
, "Key codes for the profile buttons");
100 #define CORSAIR_USAGE_SPECIAL_MIN 0xf0
101 #define CORSAIR_USAGE_SPECIAL_MAX 0xff
103 #define CORSAIR_USAGE_MACRO_RECORD_START 0xf6
104 #define CORSAIR_USAGE_MACRO_RECORD_STOP 0xf7
106 #define CORSAIR_USAGE_PROFILE 0xf1
107 #define CORSAIR_USAGE_M1 0xf1
108 #define CORSAIR_USAGE_M2 0xf2
109 #define CORSAIR_USAGE_M3 0xf3
110 #define CORSAIR_USAGE_PROFILE_MAX 0xf3
112 #define CORSAIR_USAGE_META_OFF 0xf4
113 #define CORSAIR_USAGE_META_ON 0xf5
115 #define CORSAIR_USAGE_LIGHT 0xfa
116 #define CORSAIR_USAGE_LIGHT_OFF 0xfa
117 #define CORSAIR_USAGE_LIGHT_DIM 0xfb
118 #define CORSAIR_USAGE_LIGHT_MEDIUM 0xfc
119 #define CORSAIR_USAGE_LIGHT_BRIGHT 0xfd
120 #define CORSAIR_USAGE_LIGHT_MAX 0xfd
122 /* USB control protocol */
124 #define K90_REQUEST_BRIGHTNESS 49
125 #define K90_REQUEST_MACRO_MODE 2
126 #define K90_REQUEST_STATUS 4
127 #define K90_REQUEST_GET_MODE 5
128 #define K90_REQUEST_PROFILE 20
130 #define K90_MACRO_MODE_SW 0x0030
131 #define K90_MACRO_MODE_HW 0x0001
133 #define K90_MACRO_LED_ON 0x0020
134 #define K90_MACRO_LED_OFF 0x0040
140 #define K90_BACKLIGHT_LED_SUFFIX "::backlight"
141 #define K90_RECORD_LED_SUFFIX "::record"
143 static enum led_brightness
k90_backlight_get(struct led_classdev
*led_cdev
)
146 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
147 struct device
*dev
= led
->cdev
.dev
->parent
;
148 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
149 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
153 data
= kmalloc(8, GFP_KERNEL
);
157 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
159 USB_DIR_IN
| USB_TYPE_VENDOR
|
160 USB_RECIP_DEVICE
, 0, 0, data
, 8,
161 USB_CTRL_SET_TIMEOUT
);
163 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
168 brightness
= data
[4];
169 if (brightness
< 0 || brightness
> 3) {
171 "Read invalid backlight brightness: %02hhx.\n",
183 static enum led_brightness
k90_record_led_get(struct led_classdev
*led_cdev
)
185 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
187 return led
->brightness
;
190 static void k90_brightness_set(struct led_classdev
*led_cdev
,
191 enum led_brightness brightness
)
193 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
195 led
->brightness
= brightness
;
196 schedule_work(&led
->work
);
199 static void k90_backlight_work(struct work_struct
*work
)
202 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
204 struct usb_interface
*usbif
;
205 struct usb_device
*usbdev
;
210 dev
= led
->cdev
.dev
->parent
;
211 usbif
= to_usb_interface(dev
->parent
);
212 usbdev
= interface_to_usbdev(usbif
);
214 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
215 K90_REQUEST_BRIGHTNESS
,
216 USB_DIR_OUT
| USB_TYPE_VENDOR
|
217 USB_RECIP_DEVICE
, led
->brightness
, 0,
218 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
220 dev_warn(dev
, "Failed to set backlight brightness (error: %d).\n",
224 static void k90_record_led_work(struct work_struct
*work
)
227 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
229 struct usb_interface
*usbif
;
230 struct usb_device
*usbdev
;
236 dev
= led
->cdev
.dev
->parent
;
237 usbif
= to_usb_interface(dev
->parent
);
238 usbdev
= interface_to_usbdev(usbif
);
240 if (led
->brightness
> 0)
241 value
= K90_MACRO_LED_ON
;
243 value
= K90_MACRO_LED_OFF
;
245 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
246 K90_REQUEST_MACRO_MODE
,
247 USB_DIR_OUT
| USB_TYPE_VENDOR
|
248 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
249 USB_CTRL_SET_TIMEOUT
);
251 dev_warn(dev
, "Failed to set record LED state (error: %d).\n",
256 * Keyboard attributes
259 static ssize_t
k90_show_macro_mode(struct device
*dev
,
260 struct device_attribute
*attr
, char *buf
)
263 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
264 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
265 const char *macro_mode
;
268 data
= kmalloc(2, GFP_KERNEL
);
272 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
273 K90_REQUEST_GET_MODE
,
274 USB_DIR_IN
| USB_TYPE_VENDOR
|
275 USB_RECIP_DEVICE
, 0, 0, data
, 2,
276 USB_CTRL_SET_TIMEOUT
);
278 dev_warn(dev
, "Failed to get K90 initial mode (error %d).\n",
285 case K90_MACRO_MODE_HW
:
289 case K90_MACRO_MODE_SW
:
293 dev_warn(dev
, "K90 in unknown mode: %02hhx.\n",
299 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", macro_mode
);
306 static ssize_t
k90_store_macro_mode(struct device
*dev
,
307 struct device_attribute
*attr
,
308 const char *buf
, size_t count
)
311 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
312 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
315 if (strncmp(buf
, "SW", 2) == 0)
316 value
= K90_MACRO_MODE_SW
;
317 else if (strncmp(buf
, "HW", 2) == 0)
318 value
= K90_MACRO_MODE_HW
;
322 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
323 K90_REQUEST_MACRO_MODE
,
324 USB_DIR_OUT
| USB_TYPE_VENDOR
|
325 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
326 USB_CTRL_SET_TIMEOUT
);
328 dev_warn(dev
, "Failed to set macro mode.\n");
335 static ssize_t
k90_show_current_profile(struct device
*dev
,
336 struct device_attribute
*attr
,
340 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
341 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
345 data
= kmalloc(8, GFP_KERNEL
);
349 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
351 USB_DIR_IN
| USB_TYPE_VENDOR
|
352 USB_RECIP_DEVICE
, 0, 0, data
, 8,
353 USB_CTRL_SET_TIMEOUT
);
355 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
360 current_profile
= data
[7];
361 if (current_profile
< 1 || current_profile
> 3) {
362 dev_warn(dev
, "Read invalid current profile: %02hhx.\n",
368 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", current_profile
);
375 static ssize_t
k90_store_current_profile(struct device
*dev
,
376 struct device_attribute
*attr
,
377 const char *buf
, size_t count
)
380 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
381 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
384 if (kstrtoint(buf
, 10, &profile
))
386 if (profile
< 1 || profile
> 3)
389 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
391 USB_DIR_OUT
| USB_TYPE_VENDOR
|
392 USB_RECIP_DEVICE
, profile
, 0, NULL
, 0,
393 USB_CTRL_SET_TIMEOUT
);
395 dev_warn(dev
, "Failed to change current profile (error %d).\n",
403 static DEVICE_ATTR(macro_mode
, 0644, k90_show_macro_mode
, k90_store_macro_mode
);
404 static DEVICE_ATTR(current_profile
, 0644, k90_show_current_profile
,
405 k90_store_current_profile
);
407 static struct attribute
*k90_attrs
[] = {
408 &dev_attr_macro_mode
.attr
,
409 &dev_attr_current_profile
.attr
,
413 static const struct attribute_group k90_attr_group
= {
421 static int k90_init_backlight(struct hid_device
*dev
)
424 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
428 drvdata
->backlight
= kzalloc(sizeof(struct k90_led
), GFP_KERNEL
);
429 if (!drvdata
->backlight
) {
431 goto fail_backlight_alloc
;
435 strlen(dev_name(&dev
->dev
)) + sizeof(K90_BACKLIGHT_LED_SUFFIX
);
436 name
= kzalloc(name_sz
, GFP_KERNEL
);
439 goto fail_name_alloc
;
441 snprintf(name
, name_sz
, "%s" K90_BACKLIGHT_LED_SUFFIX
,
442 dev_name(&dev
->dev
));
443 drvdata
->backlight
->removed
= false;
444 drvdata
->backlight
->cdev
.name
= name
;
445 drvdata
->backlight
->cdev
.max_brightness
= 3;
446 drvdata
->backlight
->cdev
.brightness_set
= k90_brightness_set
;
447 drvdata
->backlight
->cdev
.brightness_get
= k90_backlight_get
;
448 INIT_WORK(&drvdata
->backlight
->work
, k90_backlight_work
);
449 ret
= led_classdev_register(&dev
->dev
, &drvdata
->backlight
->cdev
);
451 goto fail_register_cdev
;
456 kfree(drvdata
->backlight
->cdev
.name
);
458 kfree(drvdata
->backlight
);
459 drvdata
->backlight
= NULL
;
460 fail_backlight_alloc
:
464 static int k90_init_macro_functions(struct hid_device
*dev
)
467 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
468 struct k90_drvdata
*k90
;
472 k90
= kzalloc(sizeof(struct k90_drvdata
), GFP_KERNEL
);
479 /* Init LED device for record LED */
480 name_sz
= strlen(dev_name(&dev
->dev
)) + sizeof(K90_RECORD_LED_SUFFIX
);
481 name
= kzalloc(name_sz
, GFP_KERNEL
);
484 goto fail_record_led_alloc
;
486 snprintf(name
, name_sz
, "%s" K90_RECORD_LED_SUFFIX
,
487 dev_name(&dev
->dev
));
488 k90
->record_led
.removed
= false;
489 k90
->record_led
.cdev
.name
= name
;
490 k90
->record_led
.cdev
.max_brightness
= 1;
491 k90
->record_led
.cdev
.brightness_set
= k90_brightness_set
;
492 k90
->record_led
.cdev
.brightness_get
= k90_record_led_get
;
493 INIT_WORK(&k90
->record_led
.work
, k90_record_led_work
);
494 k90
->record_led
.brightness
= 0;
495 ret
= led_classdev_register(&dev
->dev
, &k90
->record_led
.cdev
);
497 goto fail_record_led
;
499 /* Init attributes */
500 ret
= sysfs_create_group(&dev
->dev
.kobj
, &k90_attr_group
);
507 k90
->record_led
.removed
= true;
508 led_classdev_unregister(&k90
->record_led
.cdev
);
509 cancel_work_sync(&k90
->record_led
.work
);
511 kfree(k90
->record_led
.cdev
.name
);
512 fail_record_led_alloc
:
519 static void k90_cleanup_backlight(struct hid_device
*dev
)
521 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
523 if (drvdata
->backlight
) {
524 drvdata
->backlight
->removed
= true;
525 led_classdev_unregister(&drvdata
->backlight
->cdev
);
526 cancel_work_sync(&drvdata
->backlight
->work
);
527 kfree(drvdata
->backlight
->cdev
.name
);
528 kfree(drvdata
->backlight
);
532 static void k90_cleanup_macro_functions(struct hid_device
*dev
)
534 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
535 struct k90_drvdata
*k90
= drvdata
->k90
;
538 sysfs_remove_group(&dev
->dev
.kobj
, &k90_attr_group
);
540 k90
->record_led
.removed
= true;
541 led_classdev_unregister(&k90
->record_led
.cdev
);
542 cancel_work_sync(&k90
->record_led
.work
);
543 kfree(k90
->record_led
.cdev
.name
);
549 static int corsair_probe(struct hid_device
*dev
, const struct hid_device_id
*id
)
552 unsigned long quirks
= id
->driver_data
;
553 struct corsair_drvdata
*drvdata
;
554 struct usb_interface
*usbif
= to_usb_interface(dev
->dev
.parent
);
556 drvdata
= devm_kzalloc(&dev
->dev
, sizeof(struct corsair_drvdata
),
560 drvdata
->quirks
= quirks
;
561 hid_set_drvdata(dev
, drvdata
);
563 ret
= hid_parse(dev
);
565 hid_err(dev
, "parse failed\n");
568 ret
= hid_hw_start(dev
, HID_CONNECT_DEFAULT
);
570 hid_err(dev
, "hw start failed\n");
574 if (usbif
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
575 if (quirks
& CORSAIR_USE_K90_MACRO
) {
576 ret
= k90_init_macro_functions(dev
);
578 hid_warn(dev
, "Failed to initialize K90 macro functions.\n");
580 if (quirks
& CORSAIR_USE_K90_BACKLIGHT
) {
581 ret
= k90_init_backlight(dev
);
583 hid_warn(dev
, "Failed to initialize K90 backlight.\n");
590 static void corsair_remove(struct hid_device
*dev
)
592 k90_cleanup_macro_functions(dev
);
593 k90_cleanup_backlight(dev
);
598 static int corsair_event(struct hid_device
*dev
, struct hid_field
*field
,
599 struct hid_usage
*usage
, __s32 value
)
601 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
606 switch (usage
->hid
& HID_USAGE
) {
607 case CORSAIR_USAGE_MACRO_RECORD_START
:
608 drvdata
->k90
->record_led
.brightness
= 1;
610 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
611 drvdata
->k90
->record_led
.brightness
= 0;
620 static int corsair_input_mapping(struct hid_device
*dev
,
621 struct hid_input
*input
,
622 struct hid_field
*field
,
623 struct hid_usage
*usage
, unsigned long **bit
,
628 gkey
= corsair_usage_to_gkey(usage
->hid
& HID_USAGE
);
630 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
631 corsair_gkey_map
[gkey
- 1]);
634 if ((usage
->hid
& HID_USAGE
) >= CORSAIR_USAGE_SPECIAL_MIN
&&
635 (usage
->hid
& HID_USAGE
) <= CORSAIR_USAGE_SPECIAL_MAX
) {
636 switch (usage
->hid
& HID_USAGE
) {
637 case CORSAIR_USAGE_MACRO_RECORD_START
:
638 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
639 corsair_record_keycodes
[0]);
642 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
643 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
644 corsair_record_keycodes
[1]);
647 case CORSAIR_USAGE_M1
:
648 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
649 corsair_profile_keycodes
[0]);
652 case CORSAIR_USAGE_M2
:
653 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
654 corsair_profile_keycodes
[1]);
657 case CORSAIR_USAGE_M3
:
658 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
659 corsair_profile_keycodes
[2]);
670 static const struct hid_device_id corsair_devices
[] = {
671 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_DEVICE_ID_CORSAIR_K90
),
672 .driver_data
= CORSAIR_USE_K90_MACRO
|
673 CORSAIR_USE_K90_BACKLIGHT
},
677 MODULE_DEVICE_TABLE(hid
, corsair_devices
);
679 static struct hid_driver corsair_driver
= {
681 .id_table
= corsair_devices
,
682 .probe
= corsair_probe
,
683 .event
= corsair_event
,
684 .remove
= corsair_remove
,
685 .input_mapping
= corsair_input_mapping
,
688 static int __init
corsair_init(void)
690 return hid_register_driver(&corsair_driver
);
693 static void corsair_exit(void)
695 hid_unregister_driver(&corsair_driver
);
698 module_init(corsair_init
);
699 module_exit(corsair_exit
);
701 MODULE_LICENSE("GPL");
702 MODULE_AUTHOR("Clement Vuchener");
703 MODULE_DESCRIPTION("HID driver for Corsair devices");