dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / hid / hid-corsair.c
blob88be56321610acedd60b88c9f51176437f16a197
1 /*
2 * HID driver for Corsair devices
4 * Supported devices:
5 * - Vengeance K90 Keyboard
7 * Copyright (c) 2015 Clement Vuchener
8 */
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)
14 * any later version.
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/leds.h>
22 #include "hid-ids.h"
24 #define CORSAIR_USE_K90_MACRO (1<<0)
25 #define CORSAIR_USE_K90_BACKLIGHT (1<<1)
27 struct k90_led {
28 struct led_classdev cdev;
29 int brightness;
30 struct work_struct work;
31 bool removed;
34 struct k90_drvdata {
35 struct k90_led record_led;
38 struct corsair_drvdata {
39 unsigned long quirks;
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;
54 return 0;
57 static unsigned short corsair_gkey_map[K90_GKEY_COUNT] = {
58 BTN_TRIGGER_HAPPY1,
59 BTN_TRIGGER_HAPPY2,
60 BTN_TRIGGER_HAPPY3,
61 BTN_TRIGGER_HAPPY4,
62 BTN_TRIGGER_HAPPY5,
63 BTN_TRIGGER_HAPPY6,
64 BTN_TRIGGER_HAPPY7,
65 BTN_TRIGGER_HAPPY8,
66 BTN_TRIGGER_HAPPY9,
67 BTN_TRIGGER_HAPPY10,
68 BTN_TRIGGER_HAPPY11,
69 BTN_TRIGGER_HAPPY12,
70 BTN_TRIGGER_HAPPY13,
71 BTN_TRIGGER_HAPPY14,
72 BTN_TRIGGER_HAPPY15,
73 BTN_TRIGGER_HAPPY16,
74 BTN_TRIGGER_HAPPY17,
75 BTN_TRIGGER_HAPPY18,
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] = {
82 BTN_TRIGGER_HAPPY19,
83 BTN_TRIGGER_HAPPY20
86 module_param_array_named(recordkey_codes, corsair_record_keycodes, ushort,
87 NULL, S_IRUGO);
88 MODULE_PARM_DESC(recordkey_codes, "Key codes for the MR (start and stop record) button");
90 static unsigned short corsair_profile_keycodes[3] = {
91 BTN_TRIGGER_HAPPY21,
92 BTN_TRIGGER_HAPPY22,
93 BTN_TRIGGER_HAPPY23
96 module_param_array_named(profilekey_codes, corsair_profile_keycodes, ushort,
97 NULL, S_IRUGO);
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
137 * LED class devices
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)
145 int ret;
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);
150 int brightness;
151 char *data;
153 data = kmalloc(8, GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
157 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
158 K90_REQUEST_STATUS,
159 USB_DIR_IN | USB_TYPE_VENDOR |
160 USB_RECIP_DEVICE, 0, 0, data, 8,
161 USB_CTRL_SET_TIMEOUT);
162 if (ret < 5) {
163 dev_warn(dev, "Failed to get K90 initial state (error %d).\n",
164 ret);
165 ret = -EIO;
166 goto out;
168 brightness = data[4];
169 if (brightness < 0 || brightness > 3) {
170 dev_warn(dev,
171 "Read invalid backlight brightness: %02hhx.\n",
172 data[4]);
173 ret = -EIO;
174 goto out;
176 ret = brightness;
177 out:
178 kfree(data);
180 return ret;
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)
201 int ret;
202 struct k90_led *led = container_of(work, struct k90_led, work);
203 struct device *dev;
204 struct usb_interface *usbif;
205 struct usb_device *usbdev;
207 if (led->removed)
208 return;
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);
219 if (ret != 0)
220 dev_warn(dev, "Failed to set backlight brightness (error: %d).\n",
221 ret);
224 static void k90_record_led_work(struct work_struct *work)
226 int ret;
227 struct k90_led *led = container_of(work, struct k90_led, work);
228 struct device *dev;
229 struct usb_interface *usbif;
230 struct usb_device *usbdev;
231 int value;
233 if (led->removed)
234 return;
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;
242 else
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);
250 if (ret != 0)
251 dev_warn(dev, "Failed to set record LED state (error: %d).\n",
252 ret);
256 * Keyboard attributes
259 static ssize_t k90_show_macro_mode(struct device *dev,
260 struct device_attribute *attr, char *buf)
262 int ret;
263 struct usb_interface *usbif = to_usb_interface(dev->parent);
264 struct usb_device *usbdev = interface_to_usbdev(usbif);
265 const char *macro_mode;
266 char *data;
268 data = kmalloc(2, GFP_KERNEL);
269 if (!data)
270 return -ENOMEM;
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);
277 if (ret < 1) {
278 dev_warn(dev, "Failed to get K90 initial mode (error %d).\n",
279 ret);
280 ret = -EIO;
281 goto out;
284 switch (data[0]) {
285 case K90_MACRO_MODE_HW:
286 macro_mode = "HW";
287 break;
289 case K90_MACRO_MODE_SW:
290 macro_mode = "SW";
291 break;
292 default:
293 dev_warn(dev, "K90 in unknown mode: %02hhx.\n",
294 data[0]);
295 ret = -EIO;
296 goto out;
299 ret = snprintf(buf, PAGE_SIZE, "%s\n", macro_mode);
300 out:
301 kfree(data);
303 return ret;
306 static ssize_t k90_store_macro_mode(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t count)
310 int ret;
311 struct usb_interface *usbif = to_usb_interface(dev->parent);
312 struct usb_device *usbdev = interface_to_usbdev(usbif);
313 __u16 value;
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;
319 else
320 return -EINVAL;
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);
327 if (ret != 0) {
328 dev_warn(dev, "Failed to set macro mode.\n");
329 return ret;
332 return count;
335 static ssize_t k90_show_current_profile(struct device *dev,
336 struct device_attribute *attr,
337 char *buf)
339 int ret;
340 struct usb_interface *usbif = to_usb_interface(dev->parent);
341 struct usb_device *usbdev = interface_to_usbdev(usbif);
342 int current_profile;
343 char *data;
345 data = kmalloc(8, GFP_KERNEL);
346 if (!data)
347 return -ENOMEM;
349 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
350 K90_REQUEST_STATUS,
351 USB_DIR_IN | USB_TYPE_VENDOR |
352 USB_RECIP_DEVICE, 0, 0, data, 8,
353 USB_CTRL_SET_TIMEOUT);
354 if (ret < 8) {
355 dev_warn(dev, "Failed to get K90 initial state (error %d).\n",
356 ret);
357 ret = -EIO;
358 goto out;
360 current_profile = data[7];
361 if (current_profile < 1 || current_profile > 3) {
362 dev_warn(dev, "Read invalid current profile: %02hhx.\n",
363 data[7]);
364 ret = -EIO;
365 goto out;
368 ret = snprintf(buf, PAGE_SIZE, "%d\n", current_profile);
369 out:
370 kfree(data);
372 return ret;
375 static ssize_t k90_store_current_profile(struct device *dev,
376 struct device_attribute *attr,
377 const char *buf, size_t count)
379 int ret;
380 struct usb_interface *usbif = to_usb_interface(dev->parent);
381 struct usb_device *usbdev = interface_to_usbdev(usbif);
382 int profile;
384 if (kstrtoint(buf, 10, &profile))
385 return -EINVAL;
386 if (profile < 1 || profile > 3)
387 return -EINVAL;
389 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
390 K90_REQUEST_PROFILE,
391 USB_DIR_OUT | USB_TYPE_VENDOR |
392 USB_RECIP_DEVICE, profile, 0, NULL, 0,
393 USB_CTRL_SET_TIMEOUT);
394 if (ret != 0) {
395 dev_warn(dev, "Failed to change current profile (error %d).\n",
396 ret);
397 return ret;
400 return count;
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,
410 NULL
413 static const struct attribute_group k90_attr_group = {
414 .attrs = k90_attrs,
418 * Driver functions
421 static int k90_init_backlight(struct hid_device *dev)
423 int ret;
424 struct corsair_drvdata *drvdata = hid_get_drvdata(dev);
425 size_t name_sz;
426 char *name;
428 drvdata->backlight = kzalloc(sizeof(struct k90_led), GFP_KERNEL);
429 if (!drvdata->backlight) {
430 ret = -ENOMEM;
431 goto fail_backlight_alloc;
434 name_sz =
435 strlen(dev_name(&dev->dev)) + sizeof(K90_BACKLIGHT_LED_SUFFIX);
436 name = kzalloc(name_sz, GFP_KERNEL);
437 if (!name) {
438 ret = -ENOMEM;
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);
450 if (ret != 0)
451 goto fail_register_cdev;
453 return 0;
455 fail_register_cdev:
456 kfree(drvdata->backlight->cdev.name);
457 fail_name_alloc:
458 kfree(drvdata->backlight);
459 drvdata->backlight = NULL;
460 fail_backlight_alloc:
461 return ret;
464 static int k90_init_macro_functions(struct hid_device *dev)
466 int ret;
467 struct corsair_drvdata *drvdata = hid_get_drvdata(dev);
468 struct k90_drvdata *k90;
469 size_t name_sz;
470 char *name;
472 k90 = kzalloc(sizeof(struct k90_drvdata), GFP_KERNEL);
473 if (!k90) {
474 ret = -ENOMEM;
475 goto fail_drvdata;
477 drvdata->k90 = k90;
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);
482 if (!name) {
483 ret = -ENOMEM;
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);
496 if (ret != 0)
497 goto fail_record_led;
499 /* Init attributes */
500 ret = sysfs_create_group(&dev->dev.kobj, &k90_attr_group);
501 if (ret != 0)
502 goto fail_sysfs;
504 return 0;
506 fail_sysfs:
507 k90->record_led.removed = true;
508 led_classdev_unregister(&k90->record_led.cdev);
509 cancel_work_sync(&k90->record_led.work);
510 fail_record_led:
511 kfree(k90->record_led.cdev.name);
512 fail_record_led_alloc:
513 kfree(k90);
514 fail_drvdata:
515 drvdata->k90 = NULL;
516 return ret;
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;
537 if (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);
545 kfree(k90);
549 static int corsair_probe(struct hid_device *dev, const struct hid_device_id *id)
551 int ret;
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),
557 GFP_KERNEL);
558 if (drvdata == NULL)
559 return -ENOMEM;
560 drvdata->quirks = quirks;
561 hid_set_drvdata(dev, drvdata);
563 ret = hid_parse(dev);
564 if (ret != 0) {
565 hid_err(dev, "parse failed\n");
566 return ret;
568 ret = hid_hw_start(dev, HID_CONNECT_DEFAULT);
569 if (ret != 0) {
570 hid_err(dev, "hw start failed\n");
571 return ret;
574 if (usbif->cur_altsetting->desc.bInterfaceNumber == 0) {
575 if (quirks & CORSAIR_USE_K90_MACRO) {
576 ret = k90_init_macro_functions(dev);
577 if (ret != 0)
578 hid_warn(dev, "Failed to initialize K90 macro functions.\n");
580 if (quirks & CORSAIR_USE_K90_BACKLIGHT) {
581 ret = k90_init_backlight(dev);
582 if (ret != 0)
583 hid_warn(dev, "Failed to initialize K90 backlight.\n");
587 return 0;
590 static void corsair_remove(struct hid_device *dev)
592 k90_cleanup_macro_functions(dev);
593 k90_cleanup_backlight(dev);
595 hid_hw_stop(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);
603 if (!drvdata->k90)
604 return 0;
606 switch (usage->hid & HID_USAGE) {
607 case CORSAIR_USAGE_MACRO_RECORD_START:
608 drvdata->k90->record_led.brightness = 1;
609 break;
610 case CORSAIR_USAGE_MACRO_RECORD_STOP:
611 drvdata->k90->record_led.brightness = 0;
612 break;
613 default:
614 break;
617 return 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,
624 int *max)
626 int gkey;
628 gkey = corsair_usage_to_gkey(usage->hid & HID_USAGE);
629 if (gkey != 0) {
630 hid_map_usage_clear(input, usage, bit, max, EV_KEY,
631 corsair_gkey_map[gkey - 1]);
632 return 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]);
640 return 1;
642 case CORSAIR_USAGE_MACRO_RECORD_STOP:
643 hid_map_usage_clear(input, usage, bit, max, EV_KEY,
644 corsair_record_keycodes[1]);
645 return 1;
647 case CORSAIR_USAGE_M1:
648 hid_map_usage_clear(input, usage, bit, max, EV_KEY,
649 corsair_profile_keycodes[0]);
650 return 1;
652 case CORSAIR_USAGE_M2:
653 hid_map_usage_clear(input, usage, bit, max, EV_KEY,
654 corsair_profile_keycodes[1]);
655 return 1;
657 case CORSAIR_USAGE_M3:
658 hid_map_usage_clear(input, usage, bit, max, EV_KEY,
659 corsair_profile_keycodes[2]);
660 return 1;
662 default:
663 return -1;
667 return 0;
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 = {
680 .name = "corsair",
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");