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/usb.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
35 #include "hid-roccat.h"
36 #include "hid-roccat-kone.h"
38 static void kone_set_settings_checksum(struct kone_settings *settings)
40 uint16_t checksum = 0;
41 unsigned char *address = (unsigned char *)settings;
44 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
46 settings->checksum = cpu_to_le16(checksum);
50 * Checks success after writing data to mouse
51 * On success returns 0
52 * On failure returns errno
54 static int kone_check_write(struct usb_device *usb_dev)
59 data = kmalloc(1, GFP_KERNEL);
65 * Mouse needs 50 msecs until it says ok, but there are
66 * 30 more msecs needed for next write to work.
70 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
71 USB_REQ_CLEAR_FEATURE,
72 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
74 kone_command_confirm_write, 0, data, 1,
75 USB_CTRL_SET_TIMEOUT);
83 * value of 3 seems to mean something like
84 * "not finished yet, but it looks good"
85 * So check again after a moment.
89 if (*data == 1) { /* everything alright */
92 } else { /* unknown answer */
93 dev_err(&usb_dev->dev, "got retval %d when checking write\n",
101 * Reads settings from mouse and stores it in @buf
102 * @buf has to be alloced with GFP_KERNEL
103 * On success returns 0
104 * On failure returns errno
106 static int kone_get_settings(struct usb_device *usb_dev,
107 struct kone_settings *buf)
111 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
112 USB_REQ_CLEAR_FEATURE,
113 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
114 kone_command_settings, 0, buf,
115 sizeof(struct kone_settings), USB_CTRL_SET_TIMEOUT);
117 if (len != sizeof(struct kone_settings))
124 * Writes settings from @buf to mouse
125 * On success returns 0
126 * On failure returns errno
128 static int kone_set_settings(struct usb_device *usb_dev,
129 struct kone_settings const *settings)
133 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
134 USB_REQ_SET_CONFIGURATION,
135 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
136 kone_command_settings, 0, (char *)settings,
137 sizeof(struct kone_settings),
138 USB_CTRL_SET_TIMEOUT);
140 if (len != sizeof(struct kone_settings))
143 if (kone_check_write(usb_dev))
150 * Reads profile data from mouse and stores it in @buf
151 * @number: profile number to read
152 * On success returns 0
153 * On failure returns errno
155 static int kone_get_profile(struct usb_device *usb_dev,
156 struct kone_profile *buf, int number)
160 if (number < 1 || number > 5)
163 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
164 USB_REQ_CLEAR_FEATURE,
165 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
166 kone_command_profile, number, buf,
167 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
169 if (len != sizeof(struct kone_profile))
176 * Writes profile data to mouse.
177 * @number: profile number to write
178 * On success returns 0
179 * On failure returns errno
181 static int kone_set_profile(struct usb_device *usb_dev,
182 struct kone_profile const *profile, int number)
186 if (number < 1 || number > 5)
189 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
190 USB_REQ_SET_CONFIGURATION,
191 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
192 kone_command_profile, number, (char *)profile,
193 sizeof(struct kone_profile),
194 USB_CTRL_SET_TIMEOUT);
196 if (len != sizeof(struct kone_profile))
199 if (kone_check_write(usb_dev))
206 * Reads value of "fast-clip-weight" and stores it in @result
207 * On success returns 0
208 * On failure returns errno
210 static int kone_get_weight(struct usb_device *usb_dev, int *result)
215 data = kmalloc(1, GFP_KERNEL);
219 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
220 USB_REQ_CLEAR_FEATURE,
221 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
222 kone_command_weight, 0, data, 1, USB_CTRL_SET_TIMEOUT);
228 *result = (int)*data;
234 * Reads firmware_version of mouse and stores it in @result
235 * On success returns 0
236 * On failure returns errno
238 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
243 data = kmalloc(2, GFP_KERNEL);
247 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
248 USB_REQ_CLEAR_FEATURE,
249 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
250 kone_command_firmware_version, 0, data, 2,
251 USB_CTRL_SET_TIMEOUT);
257 *result = le16_to_cpu(*data);
262 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
263 struct bin_attribute *attr, char *buf,
264 loff_t off, size_t count) {
265 struct device *dev = container_of(kobj, struct device, kobj);
266 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
268 if (off >= sizeof(struct kone_settings))
271 if (off + count > sizeof(struct kone_settings))
272 count = sizeof(struct kone_settings) - off;
274 mutex_lock(&kone->kone_lock);
275 memcpy(buf, ((char const *)&kone->settings) + off, count);
276 mutex_unlock(&kone->kone_lock);
282 * Writing settings automatically activates startup_profile.
283 * This function keeps values in kone_device up to date and assumes that in
284 * case of error the old data is still valid
286 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
287 struct bin_attribute *attr, char *buf,
288 loff_t off, size_t count) {
289 struct device *dev = container_of(kobj, struct device, kobj);
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 memcpy(&kone->settings, buf,
305 sizeof(struct kone_settings));
307 mutex_unlock(&kone->kone_lock);
313 * If we get here, treat settings as okay and update actual values
314 * according to startup_profile
316 kone->actual_profile = kone->settings.startup_profile;
317 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
319 return sizeof(struct kone_settings);
322 static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
323 struct bin_attribute *attr, char *buf,
324 loff_t off, size_t count, int number) {
325 struct device *dev = container_of(kobj, struct device, kobj);
326 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
328 if (off >= sizeof(struct kone_profile))
331 if (off + count > sizeof(struct kone_profile))
332 count = sizeof(struct kone_profile) - off;
334 mutex_lock(&kone->kone_lock);
335 memcpy(buf, ((char const *)&kone->profiles[number - 1]) + off, count);
336 mutex_unlock(&kone->kone_lock);
341 static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
342 struct bin_attribute *attr, char *buf,
343 loff_t off, size_t count) {
344 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
347 static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
348 struct bin_attribute *attr, char *buf,
349 loff_t off, size_t count) {
350 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
353 static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
354 struct bin_attribute *attr, char *buf,
355 loff_t off, size_t count) {
356 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
359 static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
360 struct bin_attribute *attr, char *buf,
361 loff_t off, size_t count) {
362 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
365 static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
366 struct bin_attribute *attr, char *buf,
367 loff_t off, size_t count) {
368 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
371 /* Writes data only if different to stored data */
372 static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
373 struct bin_attribute *attr, char *buf,
374 loff_t off, size_t count, int number) {
375 struct device *dev = container_of(kobj, struct device, kobj);
376 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
377 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
378 struct kone_profile *profile;
379 int retval = 0, difference;
381 /* I need to get my data in one piece */
382 if (off != 0 || count != sizeof(struct kone_profile))
385 profile = &kone->profiles[number - 1];
387 mutex_lock(&kone->kone_lock);
388 difference = memcmp(buf, profile, sizeof(struct kone_profile));
390 retval = kone_set_profile(usb_dev,
391 (struct kone_profile const *)buf, number);
393 memcpy(profile, buf, sizeof(struct kone_profile));
395 mutex_unlock(&kone->kone_lock);
400 return sizeof(struct kone_profile);
403 static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
404 struct bin_attribute *attr, char *buf,
405 loff_t off, size_t count) {
406 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
409 static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
410 struct bin_attribute *attr, char *buf,
411 loff_t off, size_t count) {
412 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
415 static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
416 struct bin_attribute *attr, char *buf,
417 loff_t off, size_t count) {
418 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
421 static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
422 struct bin_attribute *attr, char *buf,
423 loff_t off, size_t count) {
424 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
427 static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
428 struct bin_attribute *attr, char *buf,
429 loff_t off, size_t count) {
430 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
433 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
437 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
440 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
444 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
447 /* weight is read each time, since we don't get informed when it's changed */
448 static ssize_t kone_sysfs_show_weight(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
452 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
456 mutex_lock(&kone->kone_lock);
457 retval = kone_get_weight(usb_dev, &weight);
458 mutex_unlock(&kone->kone_lock);
462 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
465 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
469 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
472 static ssize_t kone_sysfs_show_tcu(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
476 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
479 static int kone_tcu_command(struct usb_device *usb_dev, int number)
484 value = kmalloc(1, GFP_KERNEL);
490 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
491 USB_REQ_SET_CONFIGURATION,
492 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
493 kone_command_calibrate, 0, value, 1,
494 USB_CTRL_SET_TIMEOUT);
497 return ((len != 1) ? -EIO : 0);
501 * Calibrating the tcu is the only action that changes settings data inside the
502 * mouse, so this data needs to be reread
504 static ssize_t kone_sysfs_set_tcu(struct device *dev,
505 struct device_attribute *attr, char const *buf, size_t size)
507 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
508 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
512 retval = strict_strtoul(buf, 10, &state);
516 if (state != 0 && state != 1)
519 mutex_lock(&kone->kone_lock);
521 if (state == 1) { /* state activate */
522 retval = kone_tcu_command(usb_dev, 1);
525 retval = kone_tcu_command(usb_dev, 2);
528 ssleep(5); /* tcu needs this time for calibration */
529 retval = kone_tcu_command(usb_dev, 3);
532 retval = kone_tcu_command(usb_dev, 0);
535 retval = kone_tcu_command(usb_dev, 4);
539 * Kone needs this time to settle things.
540 * Reading settings too early will result in invalid data.
541 * Roccat's driver waits 1 sec, maybe this time could be
547 /* calibration changes values in settings, so reread */
548 retval = kone_get_settings(usb_dev, &kone->settings);
550 goto exit_no_settings;
552 /* only write settings back if activation state is different */
553 if (kone->settings.tcu != state) {
554 kone->settings.tcu = state;
555 kone_set_settings_checksum(&kone->settings);
557 retval = kone_set_settings(usb_dev, &kone->settings);
559 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
561 * try to reread valid settings into buffer overwriting
564 retval = kone_get_settings(usb_dev, &kone->settings);
566 goto exit_no_settings;
573 dev_err(&usb_dev->dev, "couldn't read settings\n");
575 mutex_unlock(&kone->kone_lock);
579 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
580 struct device_attribute *attr, char *buf)
582 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
583 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
586 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
587 struct device_attribute *attr, char const *buf, size_t size)
589 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
590 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
592 unsigned long new_startup_profile;
594 retval = strict_strtoul(buf, 10, &new_startup_profile);
598 if (new_startup_profile < 1 || new_startup_profile > 5)
601 mutex_lock(&kone->kone_lock);
603 kone->settings.startup_profile = new_startup_profile;
604 kone_set_settings_checksum(&kone->settings);
606 retval = kone_set_settings(usb_dev, &kone->settings);
608 mutex_unlock(&kone->kone_lock);
613 /* changing the startup profile immediately activates this profile */
614 kone->actual_profile = new_startup_profile;
615 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
621 * This file is used by userland software to find devices that are handled by
622 * this driver. This provides a consistent way for actual and older kernels
623 * where this driver replaced usbhid instead of generic-usb.
624 * Driver capabilities are determined by returned number.
626 static ssize_t kone_sysfs_show_abi_version(struct device *dev,
627 struct device_attribute *attr, char *buf)
629 return snprintf(buf, PAGE_SIZE, ROCCAT_KONE_ABI_VERSION "\n");
633 * Read actual dpi settings.
634 * Returns raw value for further processing. Refer to enum kone_polling_rates to
637 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
639 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
642 * The mouse can be equipped with one of four supplied weights from 5 to 20
643 * grams which are recognized and its value can be read out.
644 * This returns the raw value reported by the mouse for easy evaluation by
645 * software. Refer to enum kone_weights to get corresponding real weight.
647 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
650 * Prints firmware version stored in mouse as integer.
651 * The raw value reported by the mouse is returned for easy evaluation, to get
652 * the real version number the decimal point has to be shifted 2 positions to
653 * the left. E.g. a value of 138 means 1.38.
655 static DEVICE_ATTR(firmware_version, 0440,
656 kone_sysfs_show_firmware_version, NULL);
659 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
660 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
662 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
664 /* Prints and takes the number of the profile the mouse starts with */
665 static DEVICE_ATTR(startup_profile, 0660,
666 kone_sysfs_show_startup_profile,
667 kone_sysfs_set_startup_profile);
669 static DEVICE_ATTR(kone_abi_version, 0440,
670 kone_sysfs_show_abi_version, NULL);
672 static struct attribute *kone_attributes[] = {
673 &dev_attr_actual_dpi.attr,
674 &dev_attr_actual_profile.attr,
675 &dev_attr_weight.attr,
676 &dev_attr_firmware_version.attr,
678 &dev_attr_startup_profile.attr,
679 &dev_attr_kone_abi_version.attr,
683 static struct attribute_group kone_attribute_group = {
684 .attrs = kone_attributes
687 static struct bin_attribute kone_settings_attr = {
688 .attr = { .name = "settings", .mode = 0660 },
689 .size = sizeof(struct kone_settings),
690 .read = kone_sysfs_read_settings,
691 .write = kone_sysfs_write_settings
694 static struct bin_attribute kone_profile1_attr = {
695 .attr = { .name = "profile1", .mode = 0660 },
696 .size = sizeof(struct kone_profile),
697 .read = kone_sysfs_read_profile1,
698 .write = kone_sysfs_write_profile1
701 static struct bin_attribute kone_profile2_attr = {
702 .attr = { .name = "profile2", .mode = 0660 },
703 .size = sizeof(struct kone_profile),
704 .read = kone_sysfs_read_profile2,
705 .write = kone_sysfs_write_profile2
708 static struct bin_attribute kone_profile3_attr = {
709 .attr = { .name = "profile3", .mode = 0660 },
710 .size = sizeof(struct kone_profile),
711 .read = kone_sysfs_read_profile3,
712 .write = kone_sysfs_write_profile3
715 static struct bin_attribute kone_profile4_attr = {
716 .attr = { .name = "profile4", .mode = 0660 },
717 .size = sizeof(struct kone_profile),
718 .read = kone_sysfs_read_profile4,
719 .write = kone_sysfs_write_profile4
722 static struct bin_attribute kone_profile5_attr = {
723 .attr = { .name = "profile5", .mode = 0660 },
724 .size = sizeof(struct kone_profile),
725 .read = kone_sysfs_read_profile5,
726 .write = kone_sysfs_write_profile5
729 static int kone_create_sysfs_attributes(struct usb_interface *intf)
733 retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
737 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
741 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
745 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
749 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
753 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
757 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
764 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
766 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
768 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
770 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
772 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
774 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
779 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
781 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
782 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
783 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
784 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
785 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
786 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
787 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
790 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
791 struct kone_device *kone)
796 mutex_init(&kone->kone_lock);
798 for (i = 0; i < 5; ++i) {
799 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
804 retval = kone_get_settings(usb_dev, &kone->settings);
808 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
812 kone->actual_profile = kone->settings.startup_profile;
813 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
819 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
820 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
822 * Secial behaviour is bound only to mousepart since only mouseevents contain
823 * additional notifications.
825 static int kone_init_specials(struct hid_device *hdev)
827 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
828 struct usb_device *usb_dev = interface_to_usbdev(intf);
829 struct kone_device *kone;
832 if (intf->cur_altsetting->desc.bInterfaceProtocol
833 == USB_INTERFACE_PROTOCOL_MOUSE) {
835 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
837 dev_err(&hdev->dev, "can't alloc device descriptor\n");
840 hid_set_drvdata(hdev, kone);
842 retval = kone_init_kone_device_struct(usb_dev, kone);
845 "couldn't init struct kone_device\n");
849 retval = roccat_connect(hdev);
851 dev_err(&hdev->dev, "couldn't init char dev\n");
852 /* be tolerant about not getting chrdev */
854 kone->roccat_claimed = 1;
855 kone->chrdev_minor = retval;
858 retval = kone_create_sysfs_attributes(intf);
860 dev_err(&hdev->dev, "cannot create sysfs files\n");
864 hid_set_drvdata(hdev, NULL);
874 static void kone_remove_specials(struct hid_device *hdev)
876 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
877 struct kone_device *kone;
879 if (intf->cur_altsetting->desc.bInterfaceProtocol
880 == USB_INTERFACE_PROTOCOL_MOUSE) {
881 kone_remove_sysfs_attributes(intf);
882 kone = hid_get_drvdata(hdev);
883 if (kone->roccat_claimed)
884 roccat_disconnect(kone->chrdev_minor);
885 kfree(hid_get_drvdata(hdev));
889 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
893 retval = hid_parse(hdev);
895 dev_err(&hdev->dev, "parse failed\n");
899 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
901 dev_err(&hdev->dev, "hw start failed\n");
905 retval = kone_init_specials(hdev);
907 dev_err(&hdev->dev, "couldn't install mouse\n");
919 static void kone_remove(struct hid_device *hdev)
921 kone_remove_specials(hdev);
925 /* handle special events and keep actual profile and dpi values up to date */
926 static void kone_keep_values_up_to_date(struct kone_device *kone,
927 struct kone_mouse_event const *event)
929 switch (event->event) {
930 case kone_mouse_event_switch_profile:
931 case kone_mouse_event_osd_profile:
932 kone->actual_profile = event->value;
933 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
936 case kone_mouse_event_switch_dpi:
937 case kone_mouse_event_osd_dpi:
938 kone->actual_dpi = event->value;
943 static void kone_report_to_chrdev(struct kone_device const *kone,
944 struct kone_mouse_event const *event)
946 struct kone_roccat_report roccat_report;
948 switch (event->event) {
949 case kone_mouse_event_switch_profile:
950 case kone_mouse_event_switch_dpi:
951 case kone_mouse_event_osd_profile:
952 case kone_mouse_event_osd_dpi:
953 roccat_report.event = event->event;
954 roccat_report.value = event->value;
955 roccat_report.key = 0;
956 roccat_report_event(kone->chrdev_minor,
957 (uint8_t *)&roccat_report,
958 sizeof(struct kone_roccat_report));
960 case kone_mouse_event_call_overlong_macro:
961 if (event->value == kone_keystroke_action_press) {
962 roccat_report.event = kone_mouse_event_call_overlong_macro;
963 roccat_report.value = kone->actual_profile;
964 roccat_report.key = event->macro_key;
965 roccat_report_event(kone->chrdev_minor,
966 (uint8_t *)&roccat_report,
967 sizeof(struct kone_roccat_report));
975 * Is called for keyboard- and mousepart.
976 * Only mousepart gets informations about special events in its extended event
979 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
982 struct kone_device *kone = hid_get_drvdata(hdev);
983 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
985 /* keyboard events are always processed by default handler */
986 if (size != sizeof(struct kone_mouse_event))
990 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
991 * Pressed button is reported in each movement event.
992 * Workaround sends only one event per press.
994 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
995 memcpy(&kone->last_mouse_event, event,
996 sizeof(struct kone_mouse_event));
998 memset(&event->tilt, 0, 5);
1000 kone_keep_values_up_to_date(kone, event);
1002 if (kone->roccat_claimed)
1003 kone_report_to_chrdev(kone, event);
1005 return 0; /* always do further processing */
1008 static const struct hid_device_id kone_devices[] = {
1009 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1013 MODULE_DEVICE_TABLE(hid, kone_devices);
1015 static struct hid_driver kone_driver = {
1017 .id_table = kone_devices,
1018 .probe = kone_probe,
1019 .remove = kone_remove,
1020 .raw_event = kone_raw_event
1023 static int __init kone_init(void)
1025 return hid_register_driver(&kone_driver);
1028 static void __exit kone_exit(void)
1030 hid_unregister_driver(&kone_driver);
1033 module_init(kone_init);
1034 module_exit(kone_exit);
1036 MODULE_AUTHOR("Stefan Achatz");
1037 MODULE_DESCRIPTION("USB Roccat Kone driver");
1038 MODULE_LICENSE("GPL v2");