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 hid_err(usb_dev, "got retval %d when checking write\n", *data);
100 * Reads settings from mouse and stores it in @buf
101 * @buf has to be alloced with GFP_KERNEL
102 * On success returns 0
103 * On failure returns errno
105 static int kone_get_settings(struct usb_device *usb_dev,
106 struct kone_settings *buf)
110 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
111 USB_REQ_CLEAR_FEATURE,
112 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
113 kone_command_settings, 0, buf,
114 sizeof(struct kone_settings), USB_CTRL_SET_TIMEOUT);
116 if (len != sizeof(struct kone_settings))
123 * Writes settings from @buf to mouse
124 * On success returns 0
125 * On failure returns errno
127 static int kone_set_settings(struct usb_device *usb_dev,
128 struct kone_settings const *settings)
132 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
133 USB_REQ_SET_CONFIGURATION,
134 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
135 kone_command_settings, 0, (char *)settings,
136 sizeof(struct kone_settings),
137 USB_CTRL_SET_TIMEOUT);
139 if (len != sizeof(struct kone_settings))
142 if (kone_check_write(usb_dev))
149 * Reads profile data from mouse and stores it in @buf
150 * @number: profile number to read
151 * On success returns 0
152 * On failure returns errno
154 static int kone_get_profile(struct usb_device *usb_dev,
155 struct kone_profile *buf, int number)
159 if (number < 1 || number > 5)
162 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
163 USB_REQ_CLEAR_FEATURE,
164 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
165 kone_command_profile, number, buf,
166 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
168 if (len != sizeof(struct kone_profile))
175 * Writes profile data to mouse.
176 * @number: profile number to write
177 * On success returns 0
178 * On failure returns errno
180 static int kone_set_profile(struct usb_device *usb_dev,
181 struct kone_profile const *profile, int number)
185 if (number < 1 || number > 5)
188 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
189 USB_REQ_SET_CONFIGURATION,
190 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
191 kone_command_profile, number, (char *)profile,
192 sizeof(struct kone_profile),
193 USB_CTRL_SET_TIMEOUT);
195 if (len != sizeof(struct kone_profile))
198 if (kone_check_write(usb_dev))
205 * Reads value of "fast-clip-weight" and stores it in @result
206 * On success returns 0
207 * On failure returns errno
209 static int kone_get_weight(struct usb_device *usb_dev, int *result)
214 data = kmalloc(1, GFP_KERNEL);
218 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
219 USB_REQ_CLEAR_FEATURE,
220 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
221 kone_command_weight, 0, data, 1, USB_CTRL_SET_TIMEOUT);
227 *result = (int)*data;
233 * Reads firmware_version of mouse and stores it in @result
234 * On success returns 0
235 * On failure returns errno
237 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
242 data = kmalloc(2, GFP_KERNEL);
246 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
247 USB_REQ_CLEAR_FEATURE,
248 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
249 kone_command_firmware_version, 0, data, 2,
250 USB_CTRL_SET_TIMEOUT);
256 *result = le16_to_cpu(*data);
261 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
262 struct bin_attribute *attr, char *buf,
263 loff_t off, size_t count) {
264 struct device *dev = container_of(kobj, struct device, kobj);
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) {
288 struct device *dev = container_of(kobj, struct device, kobj);
289 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
290 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
291 int retval = 0, difference;
293 /* I need to get my data in one piece */
294 if (off != 0 || count != sizeof(struct kone_settings))
297 mutex_lock(&kone->kone_lock);
298 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
300 retval = kone_set_settings(usb_dev,
301 (struct kone_settings const *)buf);
303 memcpy(&kone->settings, buf,
304 sizeof(struct kone_settings));
306 mutex_unlock(&kone->kone_lock);
312 * If we get here, treat settings as okay and update actual values
313 * according to startup_profile
315 kone->actual_profile = kone->settings.startup_profile;
316 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
318 return sizeof(struct kone_settings);
321 static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
322 struct bin_attribute *attr, char *buf,
323 loff_t off, size_t count, int number) {
324 struct device *dev = container_of(kobj, struct device, kobj);
325 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
327 if (off >= sizeof(struct kone_profile))
330 if (off + count > sizeof(struct kone_profile))
331 count = sizeof(struct kone_profile) - off;
333 mutex_lock(&kone->kone_lock);
334 memcpy(buf, ((char const *)&kone->profiles[number - 1]) + off, count);
335 mutex_unlock(&kone->kone_lock);
340 static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
341 struct bin_attribute *attr, char *buf,
342 loff_t off, size_t count) {
343 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
346 static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
347 struct bin_attribute *attr, char *buf,
348 loff_t off, size_t count) {
349 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
352 static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
353 struct bin_attribute *attr, char *buf,
354 loff_t off, size_t count) {
355 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
358 static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
359 struct bin_attribute *attr, char *buf,
360 loff_t off, size_t count) {
361 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
364 static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
365 struct bin_attribute *attr, char *buf,
366 loff_t off, size_t count) {
367 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
370 /* Writes data only if different to stored data */
371 static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
372 struct bin_attribute *attr, char *buf,
373 loff_t off, size_t count, int number) {
374 struct device *dev = container_of(kobj, struct device, kobj);
375 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
376 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
377 struct kone_profile *profile;
378 int retval = 0, difference;
380 /* I need to get my data in one piece */
381 if (off != 0 || count != sizeof(struct kone_profile))
384 profile = &kone->profiles[number - 1];
386 mutex_lock(&kone->kone_lock);
387 difference = memcmp(buf, profile, sizeof(struct kone_profile));
389 retval = kone_set_profile(usb_dev,
390 (struct kone_profile const *)buf, number);
392 memcpy(profile, buf, sizeof(struct kone_profile));
394 mutex_unlock(&kone->kone_lock);
399 return sizeof(struct kone_profile);
402 static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
403 struct bin_attribute *attr, char *buf,
404 loff_t off, size_t count) {
405 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
408 static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
409 struct bin_attribute *attr, char *buf,
410 loff_t off, size_t count) {
411 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
414 static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
415 struct bin_attribute *attr, char *buf,
416 loff_t off, size_t count) {
417 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
420 static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
421 struct bin_attribute *attr, char *buf,
422 loff_t off, size_t count) {
423 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
426 static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
427 struct bin_attribute *attr, char *buf,
428 loff_t off, size_t count) {
429 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
432 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
433 struct device_attribute *attr, char *buf)
435 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
436 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
439 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
440 struct device_attribute *attr, char *buf)
442 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
443 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
446 /* weight is read each time, since we don't get informed when it's changed */
447 static ssize_t kone_sysfs_show_weight(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
451 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
455 mutex_lock(&kone->kone_lock);
456 retval = kone_get_weight(usb_dev, &weight);
457 mutex_unlock(&kone->kone_lock);
461 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
464 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
465 struct device_attribute *attr, char *buf)
467 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
468 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
471 static ssize_t kone_sysfs_show_tcu(struct device *dev,
472 struct device_attribute *attr, char *buf)
474 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
475 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
478 static int kone_tcu_command(struct usb_device *usb_dev, int number)
483 value = kmalloc(1, GFP_KERNEL);
489 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
490 USB_REQ_SET_CONFIGURATION,
491 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
492 kone_command_calibrate, 0, value, 1,
493 USB_CTRL_SET_TIMEOUT);
496 return ((len != 1) ? -EIO : 0);
500 * Calibrating the tcu is the only action that changes settings data inside the
501 * mouse, so this data needs to be reread
503 static ssize_t kone_sysfs_set_tcu(struct device *dev,
504 struct device_attribute *attr, char const *buf, size_t size)
506 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
507 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
511 retval = strict_strtoul(buf, 10, &state);
515 if (state != 0 && state != 1)
518 mutex_lock(&kone->kone_lock);
520 if (state == 1) { /* state activate */
521 retval = kone_tcu_command(usb_dev, 1);
524 retval = kone_tcu_command(usb_dev, 2);
527 ssleep(5); /* tcu needs this time for calibration */
528 retval = kone_tcu_command(usb_dev, 3);
531 retval = kone_tcu_command(usb_dev, 0);
534 retval = kone_tcu_command(usb_dev, 4);
538 * Kone needs this time to settle things.
539 * Reading settings too early will result in invalid data.
540 * Roccat's driver waits 1 sec, maybe this time could be
546 /* calibration changes values in settings, so reread */
547 retval = kone_get_settings(usb_dev, &kone->settings);
549 goto exit_no_settings;
551 /* only write settings back if activation state is different */
552 if (kone->settings.tcu != state) {
553 kone->settings.tcu = state;
554 kone_set_settings_checksum(&kone->settings);
556 retval = kone_set_settings(usb_dev, &kone->settings);
558 hid_err(usb_dev, "couldn't set tcu state\n");
560 * try to reread valid settings into buffer overwriting
563 retval = kone_get_settings(usb_dev, &kone->settings);
565 goto exit_no_settings;
572 hid_err(usb_dev, "couldn't read settings\n");
574 mutex_unlock(&kone->kone_lock);
578 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
579 struct device_attribute *attr, char *buf)
581 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
582 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
585 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
586 struct device_attribute *attr, char const *buf, size_t size)
588 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
589 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
591 unsigned long new_startup_profile;
593 retval = strict_strtoul(buf, 10, &new_startup_profile);
597 if (new_startup_profile < 1 || new_startup_profile > 5)
600 mutex_lock(&kone->kone_lock);
602 kone->settings.startup_profile = new_startup_profile;
603 kone_set_settings_checksum(&kone->settings);
605 retval = kone_set_settings(usb_dev, &kone->settings);
607 mutex_unlock(&kone->kone_lock);
612 /* changing the startup profile immediately activates this profile */
613 kone->actual_profile = new_startup_profile;
614 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
620 * Read actual dpi settings.
621 * Returns raw value for further processing. Refer to enum kone_polling_rates to
624 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
626 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
629 * The mouse can be equipped with one of four supplied weights from 5 to 20
630 * grams which are recognized and its value can be read out.
631 * This returns the raw value reported by the mouse for easy evaluation by
632 * software. Refer to enum kone_weights to get corresponding real weight.
634 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
637 * Prints firmware version stored in mouse as integer.
638 * The raw value reported by the mouse is returned for easy evaluation, to get
639 * the real version number the decimal point has to be shifted 2 positions to
640 * the left. E.g. a value of 138 means 1.38.
642 static DEVICE_ATTR(firmware_version, 0440,
643 kone_sysfs_show_firmware_version, NULL);
646 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
647 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
649 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
651 /* Prints and takes the number of the profile the mouse starts with */
652 static DEVICE_ATTR(startup_profile, 0660,
653 kone_sysfs_show_startup_profile,
654 kone_sysfs_set_startup_profile);
656 static struct attribute *kone_attributes[] = {
657 &dev_attr_actual_dpi.attr,
658 &dev_attr_actual_profile.attr,
659 &dev_attr_weight.attr,
660 &dev_attr_firmware_version.attr,
662 &dev_attr_startup_profile.attr,
666 static struct attribute_group kone_attribute_group = {
667 .attrs = kone_attributes
670 static struct bin_attribute kone_settings_attr = {
671 .attr = { .name = "settings", .mode = 0660 },
672 .size = sizeof(struct kone_settings),
673 .read = kone_sysfs_read_settings,
674 .write = kone_sysfs_write_settings
677 static struct bin_attribute kone_profile1_attr = {
678 .attr = { .name = "profile1", .mode = 0660 },
679 .size = sizeof(struct kone_profile),
680 .read = kone_sysfs_read_profile1,
681 .write = kone_sysfs_write_profile1
684 static struct bin_attribute kone_profile2_attr = {
685 .attr = { .name = "profile2", .mode = 0660 },
686 .size = sizeof(struct kone_profile),
687 .read = kone_sysfs_read_profile2,
688 .write = kone_sysfs_write_profile2
691 static struct bin_attribute kone_profile3_attr = {
692 .attr = { .name = "profile3", .mode = 0660 },
693 .size = sizeof(struct kone_profile),
694 .read = kone_sysfs_read_profile3,
695 .write = kone_sysfs_write_profile3
698 static struct bin_attribute kone_profile4_attr = {
699 .attr = { .name = "profile4", .mode = 0660 },
700 .size = sizeof(struct kone_profile),
701 .read = kone_sysfs_read_profile4,
702 .write = kone_sysfs_write_profile4
705 static struct bin_attribute kone_profile5_attr = {
706 .attr = { .name = "profile5", .mode = 0660 },
707 .size = sizeof(struct kone_profile),
708 .read = kone_sysfs_read_profile5,
709 .write = kone_sysfs_write_profile5
712 static int kone_create_sysfs_attributes(struct usb_interface *intf)
716 retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
720 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
724 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
728 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
732 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
736 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
740 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
747 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
749 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
751 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
753 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
755 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
757 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
762 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
764 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
765 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
766 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
767 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
768 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
769 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
770 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
773 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
774 struct kone_device *kone)
779 mutex_init(&kone->kone_lock);
781 for (i = 0; i < 5; ++i) {
782 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
787 retval = kone_get_settings(usb_dev, &kone->settings);
791 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
795 kone->actual_profile = kone->settings.startup_profile;
796 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
802 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
803 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
805 * Secial behaviour is bound only to mousepart since only mouseevents contain
806 * additional notifications.
808 static int kone_init_specials(struct hid_device *hdev)
810 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
811 struct usb_device *usb_dev = interface_to_usbdev(intf);
812 struct kone_device *kone;
815 if (intf->cur_altsetting->desc.bInterfaceProtocol
816 == USB_INTERFACE_PROTOCOL_MOUSE) {
818 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
820 hid_err(hdev, "can't alloc device descriptor\n");
823 hid_set_drvdata(hdev, kone);
825 retval = kone_init_kone_device_struct(usb_dev, kone);
827 hid_err(hdev, "couldn't init struct kone_device\n");
831 retval = roccat_connect(hdev);
833 hid_err(hdev, "couldn't init char dev\n");
834 /* be tolerant about not getting chrdev */
836 kone->roccat_claimed = 1;
837 kone->chrdev_minor = retval;
840 retval = kone_create_sysfs_attributes(intf);
842 hid_err(hdev, "cannot create sysfs files\n");
846 hid_set_drvdata(hdev, NULL);
856 static void kone_remove_specials(struct hid_device *hdev)
858 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
859 struct kone_device *kone;
861 if (intf->cur_altsetting->desc.bInterfaceProtocol
862 == USB_INTERFACE_PROTOCOL_MOUSE) {
863 kone_remove_sysfs_attributes(intf);
864 kone = hid_get_drvdata(hdev);
865 if (kone->roccat_claimed)
866 roccat_disconnect(kone->chrdev_minor);
867 kfree(hid_get_drvdata(hdev));
871 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
875 retval = hid_parse(hdev);
877 hid_err(hdev, "parse failed\n");
881 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
883 hid_err(hdev, "hw start failed\n");
887 retval = kone_init_specials(hdev);
889 hid_err(hdev, "couldn't install mouse\n");
901 static void kone_remove(struct hid_device *hdev)
903 kone_remove_specials(hdev);
907 /* handle special events and keep actual profile and dpi values up to date */
908 static void kone_keep_values_up_to_date(struct kone_device *kone,
909 struct kone_mouse_event const *event)
911 switch (event->event) {
912 case kone_mouse_event_switch_profile:
913 case kone_mouse_event_osd_profile:
914 kone->actual_profile = event->value;
915 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
918 case kone_mouse_event_switch_dpi:
919 case kone_mouse_event_osd_dpi:
920 kone->actual_dpi = event->value;
925 static void kone_report_to_chrdev(struct kone_device const *kone,
926 struct kone_mouse_event const *event)
928 struct kone_roccat_report roccat_report;
930 switch (event->event) {
931 case kone_mouse_event_switch_profile:
932 case kone_mouse_event_switch_dpi:
933 case kone_mouse_event_osd_profile:
934 case kone_mouse_event_osd_dpi:
935 roccat_report.event = event->event;
936 roccat_report.value = event->value;
937 roccat_report.key = 0;
938 roccat_report_event(kone->chrdev_minor,
939 (uint8_t *)&roccat_report,
940 sizeof(struct kone_roccat_report));
942 case kone_mouse_event_call_overlong_macro:
943 if (event->value == kone_keystroke_action_press) {
944 roccat_report.event = kone_mouse_event_call_overlong_macro;
945 roccat_report.value = kone->actual_profile;
946 roccat_report.key = event->macro_key;
947 roccat_report_event(kone->chrdev_minor,
948 (uint8_t *)&roccat_report,
949 sizeof(struct kone_roccat_report));
957 * Is called for keyboard- and mousepart.
958 * Only mousepart gets informations about special events in its extended event
961 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
964 struct kone_device *kone = hid_get_drvdata(hdev);
965 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
967 /* keyboard events are always processed by default handler */
968 if (size != sizeof(struct kone_mouse_event))
972 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
973 * Pressed button is reported in each movement event.
974 * Workaround sends only one event per press.
976 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
977 memcpy(&kone->last_mouse_event, event,
978 sizeof(struct kone_mouse_event));
980 memset(&event->tilt, 0, 5);
982 kone_keep_values_up_to_date(kone, event);
984 if (kone->roccat_claimed)
985 kone_report_to_chrdev(kone, event);
987 return 0; /* always do further processing */
990 static const struct hid_device_id kone_devices[] = {
991 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
995 MODULE_DEVICE_TABLE(hid, kone_devices);
997 static struct hid_driver kone_driver = {
999 .id_table = kone_devices,
1000 .probe = kone_probe,
1001 .remove = kone_remove,
1002 .raw_event = kone_raw_event
1005 static int __init kone_init(void)
1007 return hid_register_driver(&kone_driver);
1010 static void __exit kone_exit(void)
1012 hid_unregister_driver(&kone_driver);
1015 module_init(kone_init);
1016 module_exit(kone_exit);
1018 MODULE_AUTHOR("Stefan Achatz");
1019 MODULE_DESCRIPTION("USB Roccat Kone driver");
1020 MODULE_LICENSE("GPL v2");