2 * Roccat Kone driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 * Roccat Kone is a gamer mouse which consists of a mouse part and a keyboard
16 * part. The keyboard part enables the mouse to execute stored macros with mixed
17 * key- and button-events.
19 * TODO implement on-the-fly polling-rate change
20 * The windows driver has the ability to change the polling rate of the
21 * device on the press of a mousebutton.
22 * Is it possible to remove and reinstall the urb in raw-event- or any
23 * other handler, or to defer this action to be executed somewhere else?
25 * TODO is it possible to overwrite group for sysfs attributes via udev?
28 #include <linux/device.h>
29 #include <linux/input.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/hid-roccat.h>
35 #include "hid-roccat-common.h"
36 #include "hid-roccat-kone.h"
38 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
40 static void kone_profile_activated(struct kone_device *kone, uint new_profile)
42 kone->actual_profile = new_profile;
43 kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
46 static void kone_profile_report(struct kone_device *kone, uint new_profile)
48 struct kone_roccat_report roccat_report;
50 roccat_report.event = kone_mouse_event_switch_profile;
51 roccat_report.value = new_profile;
52 roccat_report.key = 0;
53 roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
56 static int kone_receive(struct usb_device *usb_dev, uint usb_command,
57 void *data, uint size)
62 buf = kmalloc(size, GFP_KERNEL);
66 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
68 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
69 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
71 memcpy(data, buf, size);
73 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
76 static int kone_send(struct usb_device *usb_dev, uint usb_command,
77 void const *data, uint size)
82 buf = kmemdup(data, size, GFP_KERNEL);
86 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
88 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
89 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
92 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
95 /* kone_class is used for creating sysfs attributes via roccat char device */
96 static struct class *kone_class;
98 static void kone_set_settings_checksum(struct kone_settings *settings)
100 uint16_t checksum = 0;
101 unsigned char *address = (unsigned char *)settings;
104 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
105 checksum += *address;
106 settings->checksum = cpu_to_le16(checksum);
110 * Checks success after writing data to mouse
111 * On success returns 0
112 * On failure returns errno
114 static int kone_check_write(struct usb_device *usb_dev)
121 * Mouse needs 50 msecs until it says ok, but there are
122 * 30 more msecs needed for next write to work.
126 retval = kone_receive(usb_dev,
127 kone_command_confirm_write, &data, 1);
132 * value of 3 seems to mean something like
133 * "not finished yet, but it looks good"
134 * So check again after a moment.
138 if (data == 1) /* everything alright */
142 dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
147 * Reads settings from mouse and stores it in @buf
148 * On success returns 0
149 * On failure returns errno
151 static int kone_get_settings(struct usb_device *usb_dev,
152 struct kone_settings *buf)
154 return kone_receive(usb_dev, kone_command_settings, buf,
155 sizeof(struct kone_settings));
159 * Writes settings from @buf to mouse
160 * On success returns 0
161 * On failure returns errno
163 static int kone_set_settings(struct usb_device *usb_dev,
164 struct kone_settings const *settings)
168 retval = kone_send(usb_dev, kone_command_settings,
169 settings, sizeof(struct kone_settings));
172 return kone_check_write(usb_dev);
176 * Reads profile data from mouse and stores it in @buf
177 * @number: profile number to read
178 * On success returns 0
179 * On failure returns errno
181 static int kone_get_profile(struct usb_device *usb_dev,
182 struct kone_profile *buf, int number)
186 if (number < 1 || number > 5)
189 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
190 USB_REQ_CLEAR_FEATURE,
191 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
192 kone_command_profile, number, buf,
193 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
195 if (len != sizeof(struct kone_profile))
202 * Writes profile data to mouse.
203 * @number: profile number to write
204 * On success returns 0
205 * On failure returns errno
207 static int kone_set_profile(struct usb_device *usb_dev,
208 struct kone_profile const *profile, int number)
212 if (number < 1 || number > 5)
215 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
216 USB_REQ_SET_CONFIGURATION,
217 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
218 kone_command_profile, number, (void *)profile,
219 sizeof(struct kone_profile),
220 USB_CTRL_SET_TIMEOUT);
222 if (len != sizeof(struct kone_profile))
225 if (kone_check_write(usb_dev))
232 * Reads value of "fast-clip-weight" and stores it in @result
233 * On success returns 0
234 * On failure returns errno
236 static int kone_get_weight(struct usb_device *usb_dev, int *result)
241 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
251 * Reads firmware_version of mouse and stores it in @result
252 * On success returns 0
253 * On failure returns errno
255 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
260 retval = kone_receive(usb_dev, kone_command_firmware_version,
265 *result = le16_to_cpu(data);
269 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
270 struct bin_attribute *attr, char *buf,
271 loff_t off, size_t count) {
272 struct device *dev = kobj_to_dev(kobj)->parent->parent;
273 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
275 if (off >= sizeof(struct kone_settings))
278 if (off + count > sizeof(struct kone_settings))
279 count = sizeof(struct kone_settings) - off;
281 mutex_lock(&kone->kone_lock);
282 memcpy(buf, ((char const *)&kone->settings) + off, count);
283 mutex_unlock(&kone->kone_lock);
289 * Writing settings automatically activates startup_profile.
290 * This function keeps values in kone_device up to date and assumes that in
291 * case of error the old data is still valid
293 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
294 struct bin_attribute *attr, char *buf,
295 loff_t off, size_t count) {
296 struct device *dev = kobj_to_dev(kobj)->parent->parent;
297 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 int retval = 0, difference, old_profile;
300 struct kone_settings *settings = (struct kone_settings *)buf;
302 /* I need to get my data in one piece */
303 if (off != 0 || count != sizeof(struct kone_settings))
306 mutex_lock(&kone->kone_lock);
307 difference = memcmp(settings, &kone->settings,
308 sizeof(struct kone_settings));
310 if (settings->startup_profile < 1 ||
311 settings->startup_profile > 5) {
316 retval = kone_set_settings(usb_dev, settings);
320 old_profile = kone->settings.startup_profile;
321 memcpy(&kone->settings, settings, sizeof(struct kone_settings));
323 kone_profile_activated(kone, kone->settings.startup_profile);
325 if (kone->settings.startup_profile != old_profile)
326 kone_profile_report(kone, kone->settings.startup_profile);
329 mutex_unlock(&kone->kone_lock);
334 return sizeof(struct kone_settings);
336 static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
337 kone_sysfs_write_settings, sizeof(struct kone_settings));
339 static ssize_t kone_sysfs_read_profilex(struct file *fp,
340 struct kobject *kobj, struct bin_attribute *attr,
341 char *buf, loff_t off, size_t count) {
342 struct device *dev = kobj_to_dev(kobj)->parent->parent;
343 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
345 if (off >= sizeof(struct kone_profile))
348 if (off + count > sizeof(struct kone_profile))
349 count = sizeof(struct kone_profile) - off;
351 mutex_lock(&kone->kone_lock);
352 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
353 mutex_unlock(&kone->kone_lock);
358 /* Writes data only if different to stored data */
359 static ssize_t kone_sysfs_write_profilex(struct file *fp,
360 struct kobject *kobj, struct bin_attribute *attr,
361 char *buf, loff_t off, size_t count) {
362 struct device *dev = kobj_to_dev(kobj)->parent->parent;
363 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
364 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
365 struct kone_profile *profile;
366 int retval = 0, difference;
368 /* I need to get my data in one piece */
369 if (off != 0 || count != sizeof(struct kone_profile))
372 profile = &kone->profiles[*(uint *)(attr->private)];
374 mutex_lock(&kone->kone_lock);
375 difference = memcmp(buf, profile, sizeof(struct kone_profile));
377 retval = kone_set_profile(usb_dev,
378 (struct kone_profile const *)buf,
379 *(uint *)(attr->private) + 1);
381 memcpy(profile, buf, sizeof(struct kone_profile));
383 mutex_unlock(&kone->kone_lock);
388 return sizeof(struct kone_profile);
390 #define PROFILE_ATTR(number) \
391 static struct bin_attribute bin_attr_profile##number = { \
392 .attr = { .name = "profile" #number, .mode = 0660 }, \
393 .size = sizeof(struct kone_profile), \
394 .read = kone_sysfs_read_profilex, \
395 .write = kone_sysfs_write_profilex, \
396 .private = &profile_numbers[number-1], \
404 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct kone_device *kone =
408 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
409 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
411 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
413 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct kone_device *kone =
417 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
418 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
420 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
422 /* weight is read each time, since we don't get informed when it's changed */
423 static ssize_t kone_sysfs_show_weight(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 struct kone_device *kone;
427 struct usb_device *usb_dev;
431 dev = dev->parent->parent;
432 kone = hid_get_drvdata(dev_get_drvdata(dev));
433 usb_dev = interface_to_usbdev(to_usb_interface(dev));
435 mutex_lock(&kone->kone_lock);
436 retval = kone_get_weight(usb_dev, &weight);
437 mutex_unlock(&kone->kone_lock);
441 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
443 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
445 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 struct kone_device *kone =
449 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
450 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
452 static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
455 static ssize_t kone_sysfs_show_tcu(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 struct kone_device *kone =
459 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
460 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
463 static int kone_tcu_command(struct usb_device *usb_dev, int number)
468 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
472 * Calibrating the tcu is the only action that changes settings data inside the
473 * mouse, so this data needs to be reread
475 static ssize_t kone_sysfs_set_tcu(struct device *dev,
476 struct device_attribute *attr, char const *buf, size_t size)
478 struct kone_device *kone;
479 struct usb_device *usb_dev;
483 dev = dev->parent->parent;
484 kone = hid_get_drvdata(dev_get_drvdata(dev));
485 usb_dev = interface_to_usbdev(to_usb_interface(dev));
487 retval = kstrtoul(buf, 10, &state);
491 if (state != 0 && state != 1)
494 mutex_lock(&kone->kone_lock);
496 if (state == 1) { /* state activate */
497 retval = kone_tcu_command(usb_dev, 1);
500 retval = kone_tcu_command(usb_dev, 2);
503 ssleep(5); /* tcu needs this time for calibration */
504 retval = kone_tcu_command(usb_dev, 3);
507 retval = kone_tcu_command(usb_dev, 0);
510 retval = kone_tcu_command(usb_dev, 4);
514 * Kone needs this time to settle things.
515 * Reading settings too early will result in invalid data.
516 * Roccat's driver waits 1 sec, maybe this time could be
522 /* calibration changes values in settings, so reread */
523 retval = kone_get_settings(usb_dev, &kone->settings);
525 goto exit_no_settings;
527 /* only write settings back if activation state is different */
528 if (kone->settings.tcu != state) {
529 kone->settings.tcu = state;
530 kone_set_settings_checksum(&kone->settings);
532 retval = kone_set_settings(usb_dev, &kone->settings);
534 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
536 * try to reread valid settings into buffer overwriting
539 retval = kone_get_settings(usb_dev, &kone->settings);
541 goto exit_no_settings;
544 /* calibration resets profile */
545 kone_profile_activated(kone, kone->settings.startup_profile);
550 dev_err(&usb_dev->dev, "couldn't read settings\n");
552 mutex_unlock(&kone->kone_lock);
555 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
557 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
558 struct device_attribute *attr, char *buf)
560 struct kone_device *kone =
561 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
562 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
565 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
566 struct device_attribute *attr, char const *buf, size_t size)
568 struct kone_device *kone;
569 struct usb_device *usb_dev;
571 unsigned long new_startup_profile;
573 dev = dev->parent->parent;
574 kone = hid_get_drvdata(dev_get_drvdata(dev));
575 usb_dev = interface_to_usbdev(to_usb_interface(dev));
577 retval = kstrtoul(buf, 10, &new_startup_profile);
581 if (new_startup_profile < 1 || new_startup_profile > 5)
584 mutex_lock(&kone->kone_lock);
586 kone->settings.startup_profile = new_startup_profile;
587 kone_set_settings_checksum(&kone->settings);
589 retval = kone_set_settings(usb_dev, &kone->settings);
591 mutex_unlock(&kone->kone_lock);
595 /* changing the startup profile immediately activates this profile */
596 kone_profile_activated(kone, new_startup_profile);
597 kone_profile_report(kone, new_startup_profile);
599 mutex_unlock(&kone->kone_lock);
602 static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
603 kone_sysfs_set_startup_profile);
605 static struct attribute *kone_attrs[] = {
607 * Read actual dpi settings.
608 * Returns raw value for further processing. Refer to enum
609 * kone_polling_rates to get real value.
611 &dev_attr_actual_dpi.attr,
612 &dev_attr_actual_profile.attr,
615 * The mouse can be equipped with one of four supplied weights from 5
616 * to 20 grams which are recognized and its value can be read out.
617 * This returns the raw value reported by the mouse for easy evaluation
618 * by software. Refer to enum kone_weights to get corresponding real
621 &dev_attr_weight.attr,
624 * Prints firmware version stored in mouse as integer.
625 * The raw value reported by the mouse is returned for easy evaluation,
626 * to get the real version number the decimal point has to be shifted 2
627 * positions to the left. E.g. a value of 138 means 1.38.
629 &dev_attr_firmware_version.attr,
632 * Prints state of Tracking Control Unit as number where 0 = off and
633 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
638 /* Prints and takes the number of the profile the mouse starts with */
639 &dev_attr_startup_profile.attr,
643 static struct bin_attribute *kone_bin_attributes[] = {
653 static const struct attribute_group kone_group = {
655 .bin_attrs = kone_bin_attributes,
658 static const struct attribute_group *kone_groups[] = {
663 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
664 struct kone_device *kone)
669 mutex_init(&kone->kone_lock);
671 for (i = 0; i < 5; ++i) {
672 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
677 retval = kone_get_settings(usb_dev, &kone->settings);
681 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
685 kone_profile_activated(kone, kone->settings.startup_profile);
691 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
692 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
694 * Secial behaviour is bound only to mousepart since only mouseevents contain
695 * additional notifications.
697 static int kone_init_specials(struct hid_device *hdev)
699 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
700 struct usb_device *usb_dev = interface_to_usbdev(intf);
701 struct kone_device *kone;
704 if (intf->cur_altsetting->desc.bInterfaceProtocol
705 == USB_INTERFACE_PROTOCOL_MOUSE) {
707 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
710 hid_set_drvdata(hdev, kone);
712 retval = kone_init_kone_device_struct(usb_dev, kone);
714 hid_err(hdev, "couldn't init struct kone_device\n");
718 retval = roccat_connect(kone_class, hdev,
719 sizeof(struct kone_roccat_report));
721 hid_err(hdev, "couldn't init char dev\n");
722 /* be tolerant about not getting chrdev */
724 kone->roccat_claimed = 1;
725 kone->chrdev_minor = retval;
728 hid_set_drvdata(hdev, NULL);
737 static void kone_remove_specials(struct hid_device *hdev)
739 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
740 struct kone_device *kone;
742 if (intf->cur_altsetting->desc.bInterfaceProtocol
743 == USB_INTERFACE_PROTOCOL_MOUSE) {
744 kone = hid_get_drvdata(hdev);
745 if (kone->roccat_claimed)
746 roccat_disconnect(kone->chrdev_minor);
747 kfree(hid_get_drvdata(hdev));
751 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
755 retval = hid_parse(hdev);
757 hid_err(hdev, "parse failed\n");
761 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
763 hid_err(hdev, "hw start failed\n");
767 retval = kone_init_specials(hdev);
769 hid_err(hdev, "couldn't install mouse\n");
781 static void kone_remove(struct hid_device *hdev)
783 kone_remove_specials(hdev);
787 /* handle special events and keep actual profile and dpi values up to date */
788 static void kone_keep_values_up_to_date(struct kone_device *kone,
789 struct kone_mouse_event const *event)
791 switch (event->event) {
792 case kone_mouse_event_switch_profile:
793 kone->actual_dpi = kone->profiles[event->value - 1].
795 case kone_mouse_event_osd_profile:
796 kone->actual_profile = event->value;
798 case kone_mouse_event_switch_dpi:
799 case kone_mouse_event_osd_dpi:
800 kone->actual_dpi = event->value;
805 static void kone_report_to_chrdev(struct kone_device const *kone,
806 struct kone_mouse_event const *event)
808 struct kone_roccat_report roccat_report;
810 switch (event->event) {
811 case kone_mouse_event_switch_profile:
812 case kone_mouse_event_switch_dpi:
813 case kone_mouse_event_osd_profile:
814 case kone_mouse_event_osd_dpi:
815 roccat_report.event = event->event;
816 roccat_report.value = event->value;
817 roccat_report.key = 0;
818 roccat_report_event(kone->chrdev_minor,
819 (uint8_t *)&roccat_report);
821 case kone_mouse_event_call_overlong_macro:
822 case kone_mouse_event_multimedia:
823 if (event->value == kone_keystroke_action_press) {
824 roccat_report.event = event->event;
825 roccat_report.value = kone->actual_profile;
826 roccat_report.key = event->macro_key;
827 roccat_report_event(kone->chrdev_minor,
828 (uint8_t *)&roccat_report);
836 * Is called for keyboard- and mousepart.
837 * Only mousepart gets informations about special events in its extended event
840 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
843 struct kone_device *kone = hid_get_drvdata(hdev);
844 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
846 /* keyboard events are always processed by default handler */
847 if (size != sizeof(struct kone_mouse_event))
854 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
855 * Pressed button is reported in each movement event.
856 * Workaround sends only one event per press.
858 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
859 memcpy(&kone->last_mouse_event, event,
860 sizeof(struct kone_mouse_event));
862 memset(&event->tilt, 0, 5);
864 kone_keep_values_up_to_date(kone, event);
866 if (kone->roccat_claimed)
867 kone_report_to_chrdev(kone, event);
869 return 0; /* always do further processing */
872 static const struct hid_device_id kone_devices[] = {
873 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
877 MODULE_DEVICE_TABLE(hid, kone_devices);
879 static struct hid_driver kone_driver = {
881 .id_table = kone_devices,
883 .remove = kone_remove,
884 .raw_event = kone_raw_event
887 static int __init kone_init(void)
891 /* class name has to be same as driver name */
892 kone_class = class_create(THIS_MODULE, "kone");
893 if (IS_ERR(kone_class))
894 return PTR_ERR(kone_class);
895 kone_class->dev_groups = kone_groups;
897 retval = hid_register_driver(&kone_driver);
899 class_destroy(kone_class);
903 static void __exit kone_exit(void)
905 hid_unregister_driver(&kone_driver);
906 class_destroy(kone_class);
909 module_init(kone_init);
910 module_exit(kone_exit);
912 MODULE_AUTHOR("Stefan Achatz");
913 MODULE_DESCRIPTION("USB Roccat Kone driver");
914 MODULE_LICENSE("GPL v2");