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 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
155 USB_DIR_IN
| USB_TYPE_VENDOR
|
156 USB_RECIP_DEVICE
, 0, 0, data
, 8,
157 USB_CTRL_SET_TIMEOUT
);
159 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
163 brightness
= data
[4];
164 if (brightness
< 0 || brightness
> 3) {
166 "Read invalid backlight brightness: %02hhx.\n",
173 static enum led_brightness
k90_record_led_get(struct led_classdev
*led_cdev
)
175 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
177 return led
->brightness
;
180 static void k90_brightness_set(struct led_classdev
*led_cdev
,
181 enum led_brightness brightness
)
183 struct k90_led
*led
= container_of(led_cdev
, struct k90_led
, cdev
);
185 led
->brightness
= brightness
;
186 schedule_work(&led
->work
);
189 static void k90_backlight_work(struct work_struct
*work
)
192 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
194 struct usb_interface
*usbif
;
195 struct usb_device
*usbdev
;
200 dev
= led
->cdev
.dev
->parent
;
201 usbif
= to_usb_interface(dev
->parent
);
202 usbdev
= interface_to_usbdev(usbif
);
204 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
205 K90_REQUEST_BRIGHTNESS
,
206 USB_DIR_OUT
| USB_TYPE_VENDOR
|
207 USB_RECIP_DEVICE
, led
->brightness
, 0,
208 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
210 dev_warn(dev
, "Failed to set backlight brightness (error: %d).\n",
214 static void k90_record_led_work(struct work_struct
*work
)
217 struct k90_led
*led
= container_of(work
, struct k90_led
, work
);
219 struct usb_interface
*usbif
;
220 struct usb_device
*usbdev
;
226 dev
= led
->cdev
.dev
->parent
;
227 usbif
= to_usb_interface(dev
->parent
);
228 usbdev
= interface_to_usbdev(usbif
);
230 if (led
->brightness
> 0)
231 value
= K90_MACRO_LED_ON
;
233 value
= K90_MACRO_LED_OFF
;
235 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
236 K90_REQUEST_MACRO_MODE
,
237 USB_DIR_OUT
| USB_TYPE_VENDOR
|
238 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
239 USB_CTRL_SET_TIMEOUT
);
241 dev_warn(dev
, "Failed to set record LED state (error: %d).\n",
246 * Keyboard attributes
249 static ssize_t
k90_show_macro_mode(struct device
*dev
,
250 struct device_attribute
*attr
, char *buf
)
253 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
254 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
255 const char *macro_mode
;
258 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
259 K90_REQUEST_GET_MODE
,
260 USB_DIR_IN
| USB_TYPE_VENDOR
|
261 USB_RECIP_DEVICE
, 0, 0, data
, 2,
262 USB_CTRL_SET_TIMEOUT
);
264 dev_warn(dev
, "Failed to get K90 initial mode (error %d).\n",
270 case K90_MACRO_MODE_HW
:
274 case K90_MACRO_MODE_SW
:
278 dev_warn(dev
, "K90 in unknown mode: %02hhx.\n",
283 return snprintf(buf
, PAGE_SIZE
, "%s\n", macro_mode
);
286 static ssize_t
k90_store_macro_mode(struct device
*dev
,
287 struct device_attribute
*attr
,
288 const char *buf
, size_t count
)
291 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
292 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
295 if (strncmp(buf
, "SW", 2) == 0)
296 value
= K90_MACRO_MODE_SW
;
297 else if (strncmp(buf
, "HW", 2) == 0)
298 value
= K90_MACRO_MODE_HW
;
302 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
303 K90_REQUEST_MACRO_MODE
,
304 USB_DIR_OUT
| USB_TYPE_VENDOR
|
305 USB_RECIP_DEVICE
, value
, 0, NULL
, 0,
306 USB_CTRL_SET_TIMEOUT
);
308 dev_warn(dev
, "Failed to set macro mode.\n");
315 static ssize_t
k90_show_current_profile(struct device
*dev
,
316 struct device_attribute
*attr
,
320 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
321 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
325 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
327 USB_DIR_IN
| USB_TYPE_VENDOR
|
328 USB_RECIP_DEVICE
, 0, 0, data
, 8,
329 USB_CTRL_SET_TIMEOUT
);
331 dev_warn(dev
, "Failed to get K90 initial state (error %d).\n",
335 current_profile
= data
[7];
336 if (current_profile
< 1 || current_profile
> 3) {
337 dev_warn(dev
, "Read invalid current profile: %02hhx.\n",
342 return snprintf(buf
, PAGE_SIZE
, "%d\n", current_profile
);
345 static ssize_t
k90_store_current_profile(struct device
*dev
,
346 struct device_attribute
*attr
,
347 const char *buf
, size_t count
)
350 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
351 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
354 if (kstrtoint(buf
, 10, &profile
))
356 if (profile
< 1 || profile
> 3)
359 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0),
361 USB_DIR_OUT
| USB_TYPE_VENDOR
|
362 USB_RECIP_DEVICE
, profile
, 0, NULL
, 0,
363 USB_CTRL_SET_TIMEOUT
);
365 dev_warn(dev
, "Failed to change current profile (error %d).\n",
373 static DEVICE_ATTR(macro_mode
, 0644, k90_show_macro_mode
, k90_store_macro_mode
);
374 static DEVICE_ATTR(current_profile
, 0644, k90_show_current_profile
,
375 k90_store_current_profile
);
377 static struct attribute
*k90_attrs
[] = {
378 &dev_attr_macro_mode
.attr
,
379 &dev_attr_current_profile
.attr
,
383 static const struct attribute_group k90_attr_group
= {
391 static int k90_init_backlight(struct hid_device
*dev
)
394 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
398 drvdata
->backlight
= kzalloc(sizeof(struct k90_led
), GFP_KERNEL
);
399 if (!drvdata
->backlight
) {
401 goto fail_backlight_alloc
;
405 strlen(dev_name(&dev
->dev
)) + sizeof(K90_BACKLIGHT_LED_SUFFIX
);
406 name
= kzalloc(name_sz
, GFP_KERNEL
);
409 goto fail_name_alloc
;
411 snprintf(name
, name_sz
, "%s" K90_BACKLIGHT_LED_SUFFIX
,
412 dev_name(&dev
->dev
));
413 drvdata
->backlight
->removed
= false;
414 drvdata
->backlight
->cdev
.name
= name
;
415 drvdata
->backlight
->cdev
.max_brightness
= 3;
416 drvdata
->backlight
->cdev
.brightness_set
= k90_brightness_set
;
417 drvdata
->backlight
->cdev
.brightness_get
= k90_backlight_get
;
418 INIT_WORK(&drvdata
->backlight
->work
, k90_backlight_work
);
419 ret
= led_classdev_register(&dev
->dev
, &drvdata
->backlight
->cdev
);
421 goto fail_register_cdev
;
426 kfree(drvdata
->backlight
->cdev
.name
);
428 kfree(drvdata
->backlight
);
429 drvdata
->backlight
= NULL
;
430 fail_backlight_alloc
:
434 static int k90_init_macro_functions(struct hid_device
*dev
)
437 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
438 struct k90_drvdata
*k90
;
442 k90
= kzalloc(sizeof(struct k90_drvdata
), GFP_KERNEL
);
449 /* Init LED device for record LED */
450 name_sz
= strlen(dev_name(&dev
->dev
)) + sizeof(K90_RECORD_LED_SUFFIX
);
451 name
= kzalloc(name_sz
, GFP_KERNEL
);
454 goto fail_record_led_alloc
;
456 snprintf(name
, name_sz
, "%s" K90_RECORD_LED_SUFFIX
,
457 dev_name(&dev
->dev
));
458 k90
->record_led
.removed
= false;
459 k90
->record_led
.cdev
.name
= name
;
460 k90
->record_led
.cdev
.max_brightness
= 1;
461 k90
->record_led
.cdev
.brightness_set
= k90_brightness_set
;
462 k90
->record_led
.cdev
.brightness_get
= k90_record_led_get
;
463 INIT_WORK(&k90
->record_led
.work
, k90_record_led_work
);
464 k90
->record_led
.brightness
= 0;
465 ret
= led_classdev_register(&dev
->dev
, &k90
->record_led
.cdev
);
467 goto fail_record_led
;
469 /* Init attributes */
470 ret
= sysfs_create_group(&dev
->dev
.kobj
, &k90_attr_group
);
477 k90
->record_led
.removed
= true;
478 led_classdev_unregister(&k90
->record_led
.cdev
);
479 cancel_work_sync(&k90
->record_led
.work
);
481 kfree(k90
->record_led
.cdev
.name
);
482 fail_record_led_alloc
:
489 static void k90_cleanup_backlight(struct hid_device
*dev
)
491 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
493 if (drvdata
->backlight
) {
494 drvdata
->backlight
->removed
= true;
495 led_classdev_unregister(&drvdata
->backlight
->cdev
);
496 cancel_work_sync(&drvdata
->backlight
->work
);
497 kfree(drvdata
->backlight
->cdev
.name
);
498 kfree(drvdata
->backlight
);
502 static void k90_cleanup_macro_functions(struct hid_device
*dev
)
504 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
505 struct k90_drvdata
*k90
= drvdata
->k90
;
508 sysfs_remove_group(&dev
->dev
.kobj
, &k90_attr_group
);
510 k90
->record_led
.removed
= true;
511 led_classdev_unregister(&k90
->record_led
.cdev
);
512 cancel_work_sync(&k90
->record_led
.work
);
513 kfree(k90
->record_led
.cdev
.name
);
519 static int corsair_probe(struct hid_device
*dev
, const struct hid_device_id
*id
)
522 unsigned long quirks
= id
->driver_data
;
523 struct corsair_drvdata
*drvdata
;
524 struct usb_interface
*usbif
= to_usb_interface(dev
->dev
.parent
);
526 drvdata
= devm_kzalloc(&dev
->dev
, sizeof(struct corsair_drvdata
),
530 drvdata
->quirks
= quirks
;
531 hid_set_drvdata(dev
, drvdata
);
533 ret
= hid_parse(dev
);
535 hid_err(dev
, "parse failed\n");
538 ret
= hid_hw_start(dev
, HID_CONNECT_DEFAULT
);
540 hid_err(dev
, "hw start failed\n");
544 if (usbif
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
545 if (quirks
& CORSAIR_USE_K90_MACRO
) {
546 ret
= k90_init_macro_functions(dev
);
548 hid_warn(dev
, "Failed to initialize K90 macro functions.\n");
550 if (quirks
& CORSAIR_USE_K90_BACKLIGHT
) {
551 ret
= k90_init_backlight(dev
);
553 hid_warn(dev
, "Failed to initialize K90 backlight.\n");
560 static void corsair_remove(struct hid_device
*dev
)
562 k90_cleanup_macro_functions(dev
);
563 k90_cleanup_backlight(dev
);
568 static int corsair_event(struct hid_device
*dev
, struct hid_field
*field
,
569 struct hid_usage
*usage
, __s32 value
)
571 struct corsair_drvdata
*drvdata
= hid_get_drvdata(dev
);
576 switch (usage
->hid
& HID_USAGE
) {
577 case CORSAIR_USAGE_MACRO_RECORD_START
:
578 drvdata
->k90
->record_led
.brightness
= 1;
580 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
581 drvdata
->k90
->record_led
.brightness
= 0;
590 static int corsair_input_mapping(struct hid_device
*dev
,
591 struct hid_input
*input
,
592 struct hid_field
*field
,
593 struct hid_usage
*usage
, unsigned long **bit
,
598 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_KEYBOARD
)
601 gkey
= corsair_usage_to_gkey(usage
->hid
& HID_USAGE
);
603 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
604 corsair_gkey_map
[gkey
- 1]);
607 if ((usage
->hid
& HID_USAGE
) >= CORSAIR_USAGE_SPECIAL_MIN
&&
608 (usage
->hid
& HID_USAGE
) <= CORSAIR_USAGE_SPECIAL_MAX
) {
609 switch (usage
->hid
& HID_USAGE
) {
610 case CORSAIR_USAGE_MACRO_RECORD_START
:
611 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
612 corsair_record_keycodes
[0]);
615 case CORSAIR_USAGE_MACRO_RECORD_STOP
:
616 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
617 corsair_record_keycodes
[1]);
620 case CORSAIR_USAGE_M1
:
621 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
622 corsair_profile_keycodes
[0]);
625 case CORSAIR_USAGE_M2
:
626 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
627 corsair_profile_keycodes
[1]);
630 case CORSAIR_USAGE_M3
:
631 hid_map_usage_clear(input
, usage
, bit
, max
, EV_KEY
,
632 corsair_profile_keycodes
[2]);
643 static const struct hid_device_id corsair_devices
[] = {
644 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_DEVICE_ID_CORSAIR_K90
),
645 .driver_data
= CORSAIR_USE_K90_MACRO
|
646 CORSAIR_USE_K90_BACKLIGHT
},
650 MODULE_DEVICE_TABLE(hid
, corsair_devices
);
652 static struct hid_driver corsair_driver
= {
654 .id_table
= corsair_devices
,
655 .probe
= corsair_probe
,
656 .event
= corsair_event
,
657 .remove
= corsair_remove
,
658 .input_mapping
= corsair_input_mapping
,
661 module_hid_driver(corsair_driver
);
663 MODULE_LICENSE("GPL");
664 MODULE_AUTHOR("Clement Vuchener");
665 MODULE_DESCRIPTION("HID driver for Corsair devices");