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 int kone_receive(struct usb_device *usb_dev, uint usb_command,
47 void *data, uint size)
52 buf = kmalloc(size, GFP_KERNEL);
56 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
58 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
59 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
61 memcpy(data, buf, size);
63 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
66 static int kone_send(struct usb_device *usb_dev, uint usb_command,
67 void const *data, uint size)
72 buf = kmalloc(size, GFP_KERNEL);
76 memcpy(buf, data, size);
78 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
80 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
81 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
84 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
87 /* kone_class is used for creating sysfs attributes via roccat char device */
88 static struct class *kone_class;
90 static void kone_set_settings_checksum(struct kone_settings *settings)
92 uint16_t checksum = 0;
93 unsigned char *address = (unsigned char *)settings;
96 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
98 settings->checksum = cpu_to_le16(checksum);
102 * Checks success after writing data to mouse
103 * On success returns 0
104 * On failure returns errno
106 static int kone_check_write(struct usb_device *usb_dev)
113 * Mouse needs 50 msecs until it says ok, but there are
114 * 30 more msecs needed for next write to work.
118 retval = kone_receive(usb_dev,
119 kone_command_confirm_write, &data, 1);
124 * value of 3 seems to mean something like
125 * "not finished yet, but it looks good"
126 * So check again after a moment.
130 if (data == 1) /* everything alright */
134 hid_err(usb_dev, "got retval %d when checking write\n", data);
139 * Reads settings from mouse and stores it in @buf
140 * On success returns 0
141 * On failure returns errno
143 static int kone_get_settings(struct usb_device *usb_dev,
144 struct kone_settings *buf)
146 return kone_receive(usb_dev, kone_command_settings, buf,
147 sizeof(struct kone_settings));
151 * Writes settings from @buf to mouse
152 * On success returns 0
153 * On failure returns errno
155 static int kone_set_settings(struct usb_device *usb_dev,
156 struct kone_settings const *settings)
159 retval = kone_send(usb_dev, kone_command_settings,
160 settings, sizeof(struct kone_settings));
163 return kone_check_write(usb_dev);
167 * Reads profile data from mouse and stores it in @buf
168 * @number: profile number to read
169 * On success returns 0
170 * On failure returns errno
172 static int kone_get_profile(struct usb_device *usb_dev,
173 struct kone_profile *buf, int number)
177 if (number < 1 || number > 5)
180 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
181 USB_REQ_CLEAR_FEATURE,
182 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
183 kone_command_profile, number, buf,
184 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
186 if (len != sizeof(struct kone_profile))
193 * Writes profile data to mouse.
194 * @number: profile number to write
195 * On success returns 0
196 * On failure returns errno
198 static int kone_set_profile(struct usb_device *usb_dev,
199 struct kone_profile const *profile, int number)
203 if (number < 1 || number > 5)
206 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
207 USB_REQ_SET_CONFIGURATION,
208 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
209 kone_command_profile, number, (void *)profile,
210 sizeof(struct kone_profile),
211 USB_CTRL_SET_TIMEOUT);
213 if (len != sizeof(struct kone_profile))
216 if (kone_check_write(usb_dev))
223 * Reads value of "fast-clip-weight" and stores it in @result
224 * On success returns 0
225 * On failure returns errno
227 static int kone_get_weight(struct usb_device *usb_dev, int *result)
232 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
242 * Reads firmware_version of mouse and stores it in @result
243 * On success returns 0
244 * On failure returns errno
246 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
251 retval = kone_receive(usb_dev, kone_command_firmware_version,
256 *result = le16_to_cpu(data);
260 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
261 struct bin_attribute *attr, char *buf,
262 loff_t off, size_t count) {
264 container_of(kobj, struct device, kobj)->parent->parent;
265 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
267 if (off >= sizeof(struct kone_settings))
270 if (off + count > sizeof(struct kone_settings))
271 count = sizeof(struct kone_settings) - off;
273 mutex_lock(&kone->kone_lock);
274 memcpy(buf, ((char const *)&kone->settings) + off, count);
275 mutex_unlock(&kone->kone_lock);
281 * Writing settings automatically activates startup_profile.
282 * This function keeps values in kone_device up to date and assumes that in
283 * case of error the old data is still valid
285 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
286 struct bin_attribute *attr, char *buf,
287 loff_t off, size_t count) {
289 container_of(kobj, struct device, kobj)->parent->parent;
290 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
291 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
292 int retval = 0, difference;
294 /* I need to get my data in one piece */
295 if (off != 0 || count != sizeof(struct kone_settings))
298 mutex_lock(&kone->kone_lock);
299 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
301 retval = kone_set_settings(usb_dev,
302 (struct kone_settings const *)buf);
304 mutex_unlock(&kone->kone_lock);
308 memcpy(&kone->settings, buf, sizeof(struct kone_settings));
310 kone_profile_activated(kone, kone->settings.startup_profile);
312 mutex_unlock(&kone->kone_lock);
314 return sizeof(struct kone_settings);
317 static ssize_t kone_sysfs_read_profilex(struct file *fp,
318 struct kobject *kobj, struct bin_attribute *attr,
319 char *buf, loff_t off, size_t count) {
321 container_of(kobj, struct device, kobj)->parent->parent;
322 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
324 if (off >= sizeof(struct kone_profile))
327 if (off + count > sizeof(struct kone_profile))
328 count = sizeof(struct kone_profile) - off;
330 mutex_lock(&kone->kone_lock);
331 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
332 mutex_unlock(&kone->kone_lock);
337 /* Writes data only if different to stored data */
338 static ssize_t kone_sysfs_write_profilex(struct file *fp,
339 struct kobject *kobj, struct bin_attribute *attr,
340 char *buf, loff_t off, size_t count) {
342 container_of(kobj, struct device, kobj)->parent->parent;
343 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
344 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
345 struct kone_profile *profile;
346 int retval = 0, difference;
348 /* I need to get my data in one piece */
349 if (off != 0 || count != sizeof(struct kone_profile))
352 profile = &kone->profiles[*(uint *)(attr->private)];
354 mutex_lock(&kone->kone_lock);
355 difference = memcmp(buf, profile, sizeof(struct kone_profile));
357 retval = kone_set_profile(usb_dev,
358 (struct kone_profile const *)buf,
359 *(uint *)(attr->private) + 1);
361 memcpy(profile, buf, sizeof(struct kone_profile));
363 mutex_unlock(&kone->kone_lock);
368 return sizeof(struct kone_profile);
371 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct kone_device *kone =
375 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
376 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
379 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 struct kone_device *kone =
383 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
384 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
387 /* weight is read each time, since we don't get informed when it's changed */
388 static ssize_t kone_sysfs_show_weight(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct kone_device *kone;
392 struct usb_device *usb_dev;
396 dev = dev->parent->parent;
397 kone = hid_get_drvdata(dev_get_drvdata(dev));
398 usb_dev = interface_to_usbdev(to_usb_interface(dev));
400 mutex_lock(&kone->kone_lock);
401 retval = kone_get_weight(usb_dev, &weight);
402 mutex_unlock(&kone->kone_lock);
406 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
409 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct kone_device *kone =
413 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
414 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
417 static ssize_t kone_sysfs_show_tcu(struct device *dev,
418 struct device_attribute *attr, char *buf)
420 struct kone_device *kone =
421 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
422 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
425 static int kone_tcu_command(struct usb_device *usb_dev, int number)
429 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
433 * Calibrating the tcu is the only action that changes settings data inside the
434 * mouse, so this data needs to be reread
436 static ssize_t kone_sysfs_set_tcu(struct device *dev,
437 struct device_attribute *attr, char const *buf, size_t size)
439 struct kone_device *kone;
440 struct usb_device *usb_dev;
444 dev = dev->parent->parent;
445 kone = hid_get_drvdata(dev_get_drvdata(dev));
446 usb_dev = interface_to_usbdev(to_usb_interface(dev));
448 retval = strict_strtoul(buf, 10, &state);
452 if (state != 0 && state != 1)
455 mutex_lock(&kone->kone_lock);
457 if (state == 1) { /* state activate */
458 retval = kone_tcu_command(usb_dev, 1);
461 retval = kone_tcu_command(usb_dev, 2);
464 ssleep(5); /* tcu needs this time for calibration */
465 retval = kone_tcu_command(usb_dev, 3);
468 retval = kone_tcu_command(usb_dev, 0);
471 retval = kone_tcu_command(usb_dev, 4);
475 * Kone needs this time to settle things.
476 * Reading settings too early will result in invalid data.
477 * Roccat's driver waits 1 sec, maybe this time could be
483 /* calibration changes values in settings, so reread */
484 retval = kone_get_settings(usb_dev, &kone->settings);
486 goto exit_no_settings;
488 /* only write settings back if activation state is different */
489 if (kone->settings.tcu != state) {
490 kone->settings.tcu = state;
491 kone_set_settings_checksum(&kone->settings);
493 retval = kone_set_settings(usb_dev, &kone->settings);
495 hid_err(usb_dev, "couldn't set tcu state\n");
497 * try to reread valid settings into buffer overwriting
500 retval = kone_get_settings(usb_dev, &kone->settings);
502 goto exit_no_settings;
505 /* calibration resets profile */
506 kone_profile_activated(kone, kone->settings.startup_profile);
511 hid_err(usb_dev, "couldn't read settings\n");
513 mutex_unlock(&kone->kone_lock);
517 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
518 struct device_attribute *attr, char *buf)
520 struct kone_device *kone =
521 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
522 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
525 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
526 struct device_attribute *attr, char const *buf, size_t size)
528 struct kone_device *kone;
529 struct usb_device *usb_dev;
531 unsigned long new_startup_profile;
533 dev = dev->parent->parent;
534 kone = hid_get_drvdata(dev_get_drvdata(dev));
535 usb_dev = interface_to_usbdev(to_usb_interface(dev));
537 retval = strict_strtoul(buf, 10, &new_startup_profile);
541 if (new_startup_profile < 1 || new_startup_profile > 5)
544 mutex_lock(&kone->kone_lock);
546 kone->settings.startup_profile = new_startup_profile;
547 kone_set_settings_checksum(&kone->settings);
549 retval = kone_set_settings(usb_dev, &kone->settings);
551 mutex_unlock(&kone->kone_lock);
555 /* changing the startup profile immediately activates this profile */
556 kone_profile_activated(kone, new_startup_profile);
558 mutex_unlock(&kone->kone_lock);
562 static struct device_attribute kone_attributes[] = {
564 * Read actual dpi settings.
565 * Returns raw value for further processing. Refer to enum
566 * kone_polling_rates to get real value.
568 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
569 __ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
572 * The mouse can be equipped with one of four supplied weights from 5
573 * to 20 grams which are recognized and its value can be read out.
574 * This returns the raw value reported by the mouse for easy evaluation
575 * by software. Refer to enum kone_weights to get corresponding real
578 __ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
581 * Prints firmware version stored in mouse as integer.
582 * The raw value reported by the mouse is returned for easy evaluation,
583 * to get the real version number the decimal point has to be shifted 2
584 * positions to the left. E.g. a value of 138 means 1.38.
586 __ATTR(firmware_version, 0440,
587 kone_sysfs_show_firmware_version, NULL),
590 * Prints state of Tracking Control Unit as number where 0 = off and
591 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
594 __ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
596 /* Prints and takes the number of the profile the mouse starts with */
597 __ATTR(startup_profile, 0660,
598 kone_sysfs_show_startup_profile,
599 kone_sysfs_set_startup_profile),
603 static struct bin_attribute kone_bin_attributes[] = {
605 .attr = { .name = "settings", .mode = 0660 },
606 .size = sizeof(struct kone_settings),
607 .read = kone_sysfs_read_settings,
608 .write = kone_sysfs_write_settings
611 .attr = { .name = "profile1", .mode = 0660 },
612 .size = sizeof(struct kone_profile),
613 .read = kone_sysfs_read_profilex,
614 .write = kone_sysfs_write_profilex,
615 .private = &profile_numbers[0]
618 .attr = { .name = "profile2", .mode = 0660 },
619 .size = sizeof(struct kone_profile),
620 .read = kone_sysfs_read_profilex,
621 .write = kone_sysfs_write_profilex,
622 .private = &profile_numbers[1]
625 .attr = { .name = "profile3", .mode = 0660 },
626 .size = sizeof(struct kone_profile),
627 .read = kone_sysfs_read_profilex,
628 .write = kone_sysfs_write_profilex,
629 .private = &profile_numbers[2]
632 .attr = { .name = "profile4", .mode = 0660 },
633 .size = sizeof(struct kone_profile),
634 .read = kone_sysfs_read_profilex,
635 .write = kone_sysfs_write_profilex,
636 .private = &profile_numbers[3]
639 .attr = { .name = "profile5", .mode = 0660 },
640 .size = sizeof(struct kone_profile),
641 .read = kone_sysfs_read_profilex,
642 .write = kone_sysfs_write_profilex,
643 .private = &profile_numbers[4]
648 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
649 struct kone_device *kone)
654 mutex_init(&kone->kone_lock);
656 for (i = 0; i < 5; ++i) {
657 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
662 retval = kone_get_settings(usb_dev, &kone->settings);
666 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
670 kone_profile_activated(kone, kone->settings.startup_profile);
676 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
677 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
679 * Secial behaviour is bound only to mousepart since only mouseevents contain
680 * additional notifications.
682 static int kone_init_specials(struct hid_device *hdev)
684 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
685 struct usb_device *usb_dev = interface_to_usbdev(intf);
686 struct kone_device *kone;
689 if (intf->cur_altsetting->desc.bInterfaceProtocol
690 == USB_INTERFACE_PROTOCOL_MOUSE) {
692 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
694 hid_err(hdev, "can't alloc device descriptor\n");
697 hid_set_drvdata(hdev, kone);
699 retval = kone_init_kone_device_struct(usb_dev, kone);
701 hid_err(hdev, "couldn't init struct kone_device\n");
705 retval = roccat_connect(kone_class, hdev,
706 sizeof(struct kone_roccat_report));
708 hid_err(hdev, "couldn't init char dev\n");
709 /* be tolerant about not getting chrdev */
711 kone->roccat_claimed = 1;
712 kone->chrdev_minor = retval;
715 hid_set_drvdata(hdev, NULL);
724 static void kone_remove_specials(struct hid_device *hdev)
726 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
727 struct kone_device *kone;
729 if (intf->cur_altsetting->desc.bInterfaceProtocol
730 == USB_INTERFACE_PROTOCOL_MOUSE) {
731 kone = hid_get_drvdata(hdev);
732 if (kone->roccat_claimed)
733 roccat_disconnect(kone->chrdev_minor);
734 kfree(hid_get_drvdata(hdev));
738 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
742 retval = hid_parse(hdev);
744 hid_err(hdev, "parse failed\n");
748 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
750 hid_err(hdev, "hw start failed\n");
754 retval = kone_init_specials(hdev);
756 hid_err(hdev, "couldn't install mouse\n");
768 static void kone_remove(struct hid_device *hdev)
770 kone_remove_specials(hdev);
774 /* handle special events and keep actual profile and dpi values up to date */
775 static void kone_keep_values_up_to_date(struct kone_device *kone,
776 struct kone_mouse_event const *event)
778 switch (event->event) {
779 case kone_mouse_event_switch_profile:
780 kone->actual_dpi = kone->profiles[event->value - 1].
782 case kone_mouse_event_osd_profile:
783 kone->actual_profile = event->value;
785 case kone_mouse_event_switch_dpi:
786 case kone_mouse_event_osd_dpi:
787 kone->actual_dpi = event->value;
792 static void kone_report_to_chrdev(struct kone_device const *kone,
793 struct kone_mouse_event const *event)
795 struct kone_roccat_report roccat_report;
797 switch (event->event) {
798 case kone_mouse_event_switch_profile:
799 case kone_mouse_event_switch_dpi:
800 case kone_mouse_event_osd_profile:
801 case kone_mouse_event_osd_dpi:
802 roccat_report.event = event->event;
803 roccat_report.value = event->value;
804 roccat_report.key = 0;
805 roccat_report_event(kone->chrdev_minor,
806 (uint8_t *)&roccat_report);
808 case kone_mouse_event_call_overlong_macro:
809 if (event->value == kone_keystroke_action_press) {
810 roccat_report.event = kone_mouse_event_call_overlong_macro;
811 roccat_report.value = kone->actual_profile;
812 roccat_report.key = event->macro_key;
813 roccat_report_event(kone->chrdev_minor,
814 (uint8_t *)&roccat_report);
822 * Is called for keyboard- and mousepart.
823 * Only mousepart gets informations about special events in its extended event
826 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
829 struct kone_device *kone = hid_get_drvdata(hdev);
830 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
832 /* keyboard events are always processed by default handler */
833 if (size != sizeof(struct kone_mouse_event))
840 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
841 * Pressed button is reported in each movement event.
842 * Workaround sends only one event per press.
844 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
845 memcpy(&kone->last_mouse_event, event,
846 sizeof(struct kone_mouse_event));
848 memset(&event->tilt, 0, 5);
850 kone_keep_values_up_to_date(kone, event);
852 if (kone->roccat_claimed)
853 kone_report_to_chrdev(kone, event);
855 return 0; /* always do further processing */
858 static const struct hid_device_id kone_devices[] = {
859 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
863 MODULE_DEVICE_TABLE(hid, kone_devices);
865 static struct hid_driver kone_driver = {
867 .id_table = kone_devices,
869 .remove = kone_remove,
870 .raw_event = kone_raw_event
873 static int __init kone_init(void)
877 /* class name has to be same as driver name */
878 kone_class = class_create(THIS_MODULE, "kone");
879 if (IS_ERR(kone_class))
880 return PTR_ERR(kone_class);
881 kone_class->dev_attrs = kone_attributes;
882 kone_class->dev_bin_attrs = kone_bin_attributes;
884 retval = hid_register_driver(&kone_driver);
886 class_destroy(kone_class);
890 static void __exit kone_exit(void)
892 hid_unregister_driver(&kone_driver);
893 class_destroy(kone_class);
896 module_init(kone_init);
897 module_exit(kone_exit);
899 MODULE_AUTHOR("Stefan Achatz");
900 MODULE_DESCRIPTION("USB Roccat Kone driver");
901 MODULE_LICENSE("GPL v2");