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 implement notification mechanism for overlong macro execution
26 * If user wants to execute an overlong macro only the names of macroset
27 * and macro are given. Should userland tap hidraw or is there an
28 * additional streaming mechanism?
30 * TODO is it possible to overwrite group for sysfs attributes via udev?
33 #include <linux/device.h>
34 #include <linux/input.h>
35 #include <linux/hid.h>
36 #include <linux/usb.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
40 #include "hid-roccat-kone.h"
42 static void kone_set_settings_checksum(struct kone_settings
*settings
)
44 uint16_t checksum
= 0;
45 unsigned char *address
= (unsigned char *)settings
;
48 for (i
= 0; i
< sizeof(struct kone_settings
) - 2; ++i
, ++address
)
50 settings
->checksum
= cpu_to_le16(checksum
);
54 * Checks success after writing data to mouse
55 * On success returns 0
56 * On failure returns errno
58 static int kone_check_write(struct usb_device
*usb_dev
)
63 data
= kmalloc(1, GFP_KERNEL
);
69 * Mouse needs 50 msecs until it says ok, but there are
70 * 30 more msecs needed for next write to work.
74 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
75 USB_REQ_CLEAR_FEATURE
,
76 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
|
78 kone_command_confirm_write
, 0, data
, 1,
79 USB_CTRL_SET_TIMEOUT
);
87 * value of 3 seems to mean something like
88 * "not finished yet, but it looks good"
89 * So check again after a moment.
93 if (*data
== 1) { /* everything alright */
96 } else { /* unknown answer */
97 dev_err(&usb_dev
->dev
, "got retval %d when checking write\n",
105 * Reads settings from mouse and stores it in @buf
106 * @buf has to be alloced with GFP_KERNEL
107 * On success returns 0
108 * On failure returns errno
110 static int kone_get_settings(struct usb_device
*usb_dev
,
111 struct kone_settings
*buf
)
115 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
116 USB_REQ_CLEAR_FEATURE
,
117 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
118 kone_command_settings
, 0, buf
,
119 sizeof(struct kone_settings
), USB_CTRL_SET_TIMEOUT
);
121 if (len
!= sizeof(struct kone_settings
))
128 * Writes settings from @buf to mouse
129 * On success returns 0
130 * On failure returns errno
132 static int kone_set_settings(struct usb_device
*usb_dev
,
133 struct kone_settings
const *settings
)
137 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
138 USB_REQ_SET_CONFIGURATION
,
139 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
140 kone_command_settings
, 0, (char *)settings
,
141 sizeof(struct kone_settings
),
142 USB_CTRL_SET_TIMEOUT
);
144 if (len
!= sizeof(struct kone_settings
))
147 if (kone_check_write(usb_dev
))
154 * Reads profile data from mouse and stores it in @buf
155 * @number: profile number to read
156 * On success returns 0
157 * On failure returns errno
159 static int kone_get_profile(struct usb_device
*usb_dev
,
160 struct kone_profile
*buf
, int number
)
164 if (number
< 1 || number
> 5)
167 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
168 USB_REQ_CLEAR_FEATURE
,
169 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
170 kone_command_profile
, number
, buf
,
171 sizeof(struct kone_profile
), USB_CTRL_SET_TIMEOUT
);
173 if (len
!= sizeof(struct kone_profile
))
180 * Writes profile data to mouse.
181 * @number: profile number to write
182 * On success returns 0
183 * On failure returns errno
185 static int kone_set_profile(struct usb_device
*usb_dev
,
186 struct kone_profile
const *profile
, int number
)
190 if (number
< 1 || number
> 5)
193 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
194 USB_REQ_SET_CONFIGURATION
,
195 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
196 kone_command_profile
, number
, (char *)profile
,
197 sizeof(struct kone_profile
),
198 USB_CTRL_SET_TIMEOUT
);
200 if (len
!= sizeof(struct kone_profile
))
203 if (kone_check_write(usb_dev
))
210 * Reads value of "fast-clip-weight" and stores it in @result
211 * On success returns 0
212 * On failure returns errno
214 static int kone_get_weight(struct usb_device
*usb_dev
, int *result
)
219 data
= kmalloc(1, GFP_KERNEL
);
223 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
224 USB_REQ_CLEAR_FEATURE
,
225 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
226 kone_command_weight
, 0, data
, 1, USB_CTRL_SET_TIMEOUT
);
232 *result
= (int)*data
;
238 * Reads firmware_version of mouse and stores it in @result
239 * On success returns 0
240 * On failure returns errno
242 static int kone_get_firmware_version(struct usb_device
*usb_dev
, int *result
)
247 data
= kmalloc(2, GFP_KERNEL
);
251 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
252 USB_REQ_CLEAR_FEATURE
,
253 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
254 kone_command_firmware_version
, 0, data
, 2,
255 USB_CTRL_SET_TIMEOUT
);
261 *result
= le16_to_cpu(*data
);
266 static ssize_t
kone_sysfs_read_settings(struct file
*fp
, struct kobject
*kobj
,
267 struct bin_attribute
*attr
, char *buf
,
268 loff_t off
, size_t count
) {
269 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
270 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
272 if (off
>= sizeof(struct kone_settings
))
275 if (off
+ count
> sizeof(struct kone_settings
))
276 count
= sizeof(struct kone_settings
) - off
;
278 mutex_lock(&kone
->kone_lock
);
279 memcpy(buf
, &kone
->settings
+ off
, count
);
280 mutex_unlock(&kone
->kone_lock
);
286 * Writing settings automatically activates startup_profile.
287 * This function keeps values in kone_device up to date and assumes that in
288 * case of error the old data is still valid
290 static ssize_t
kone_sysfs_write_settings(struct file
*fp
, struct kobject
*kobj
,
291 struct bin_attribute
*attr
, char *buf
,
292 loff_t off
, size_t count
) {
293 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
294 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
295 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
296 int retval
= 0, difference
;
298 /* I need to get my data in one piece */
299 if (off
!= 0 || count
!= sizeof(struct kone_settings
))
302 mutex_lock(&kone
->kone_lock
);
303 difference
= memcmp(buf
, &kone
->settings
, sizeof(struct kone_settings
));
305 retval
= kone_set_settings(usb_dev
,
306 (struct kone_settings
const *)buf
);
308 memcpy(&kone
->settings
, buf
,
309 sizeof(struct kone_settings
));
311 mutex_unlock(&kone
->kone_lock
);
317 * If we get here, treat settings as okay and update actual values
318 * according to startup_profile
320 kone
->actual_profile
= kone
->settings
.startup_profile
;
321 kone
->actual_dpi
= kone
->profiles
[kone
->actual_profile
- 1].startup_dpi
;
323 return sizeof(struct kone_settings
);
326 static ssize_t
kone_sysfs_read_profilex(struct kobject
*kobj
,
327 struct bin_attribute
*attr
, char *buf
,
328 loff_t off
, size_t count
, int number
) {
329 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
330 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
332 if (off
>= sizeof(struct kone_profile
))
335 if (off
+ count
> sizeof(struct kone_profile
))
336 count
= sizeof(struct kone_profile
) - off
;
338 mutex_lock(&kone
->kone_lock
);
339 memcpy(buf
, &kone
->profiles
[number
- 1], sizeof(struct kone_profile
));
340 mutex_unlock(&kone
->kone_lock
);
345 static ssize_t
kone_sysfs_read_profile1(struct file
*fp
, struct kobject
*kobj
,
346 struct bin_attribute
*attr
, char *buf
,
347 loff_t off
, size_t count
) {
348 return kone_sysfs_read_profilex(kobj
, attr
, buf
, off
, count
, 1);
351 static ssize_t
kone_sysfs_read_profile2(struct file
*fp
, struct kobject
*kobj
,
352 struct bin_attribute
*attr
, char *buf
,
353 loff_t off
, size_t count
) {
354 return kone_sysfs_read_profilex(kobj
, attr
, buf
, off
, count
, 2);
357 static ssize_t
kone_sysfs_read_profile3(struct file
*fp
, struct kobject
*kobj
,
358 struct bin_attribute
*attr
, char *buf
,
359 loff_t off
, size_t count
) {
360 return kone_sysfs_read_profilex(kobj
, attr
, buf
, off
, count
, 3);
363 static ssize_t
kone_sysfs_read_profile4(struct file
*fp
, struct kobject
*kobj
,
364 struct bin_attribute
*attr
, char *buf
,
365 loff_t off
, size_t count
) {
366 return kone_sysfs_read_profilex(kobj
, attr
, buf
, off
, count
, 4);
369 static ssize_t
kone_sysfs_read_profile5(struct file
*fp
, struct kobject
*kobj
,
370 struct bin_attribute
*attr
, char *buf
,
371 loff_t off
, size_t count
) {
372 return kone_sysfs_read_profilex(kobj
, attr
, buf
, off
, count
, 5);
375 /* Writes data only if different to stored data */
376 static ssize_t
kone_sysfs_write_profilex(struct kobject
*kobj
,
377 struct bin_attribute
*attr
, char *buf
,
378 loff_t off
, size_t count
, int number
) {
379 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
380 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
381 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
382 struct kone_profile
*profile
;
383 int retval
= 0, difference
;
385 /* I need to get my data in one piece */
386 if (off
!= 0 || count
!= sizeof(struct kone_profile
))
389 profile
= &kone
->profiles
[number
- 1];
391 mutex_lock(&kone
->kone_lock
);
392 difference
= memcmp(buf
, profile
, sizeof(struct kone_profile
));
394 retval
= kone_set_profile(usb_dev
,
395 (struct kone_profile
const *)buf
, number
);
397 memcpy(profile
, buf
, sizeof(struct kone_profile
));
399 mutex_unlock(&kone
->kone_lock
);
404 return sizeof(struct kone_profile
);
407 static ssize_t
kone_sysfs_write_profile1(struct file
*fp
, struct kobject
*kobj
,
408 struct bin_attribute
*attr
, char *buf
,
409 loff_t off
, size_t count
) {
410 return kone_sysfs_write_profilex(kobj
, attr
, buf
, off
, count
, 1);
413 static ssize_t
kone_sysfs_write_profile2(struct file
*fp
, struct kobject
*kobj
,
414 struct bin_attribute
*attr
, char *buf
,
415 loff_t off
, size_t count
) {
416 return kone_sysfs_write_profilex(kobj
, attr
, buf
, off
, count
, 2);
419 static ssize_t
kone_sysfs_write_profile3(struct file
*fp
, struct kobject
*kobj
,
420 struct bin_attribute
*attr
, char *buf
,
421 loff_t off
, size_t count
) {
422 return kone_sysfs_write_profilex(kobj
, attr
, buf
, off
, count
, 3);
425 static ssize_t
kone_sysfs_write_profile4(struct file
*fp
, struct kobject
*kobj
,
426 struct bin_attribute
*attr
, char *buf
,
427 loff_t off
, size_t count
) {
428 return kone_sysfs_write_profilex(kobj
, attr
, buf
, off
, count
, 4);
431 static ssize_t
kone_sysfs_write_profile5(struct file
*fp
, struct kobject
*kobj
,
432 struct bin_attribute
*attr
, char *buf
,
433 loff_t off
, size_t count
) {
434 return kone_sysfs_write_profilex(kobj
, attr
, buf
, off
, count
, 5);
437 static ssize_t
kone_sysfs_show_actual_profile(struct device
*dev
,
438 struct device_attribute
*attr
, char *buf
)
440 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
441 return snprintf(buf
, PAGE_SIZE
, "%d\n", kone
->actual_profile
);
444 static ssize_t
kone_sysfs_show_actual_dpi(struct device
*dev
,
445 struct device_attribute
*attr
, char *buf
)
447 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
448 return snprintf(buf
, PAGE_SIZE
, "%d\n", kone
->actual_dpi
);
451 /* weight is read each time, since we don't get informed when it's changed */
452 static ssize_t
kone_sysfs_show_weight(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
456 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
460 mutex_lock(&kone
->kone_lock
);
461 retval
= kone_get_weight(usb_dev
, &weight
);
462 mutex_unlock(&kone
->kone_lock
);
466 return snprintf(buf
, PAGE_SIZE
, "%d\n", weight
);
469 static ssize_t
kone_sysfs_show_firmware_version(struct device
*dev
,
470 struct device_attribute
*attr
, char *buf
)
472 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
473 return snprintf(buf
, PAGE_SIZE
, "%d\n", kone
->firmware_version
);
476 static ssize_t
kone_sysfs_show_tcu(struct device
*dev
,
477 struct device_attribute
*attr
, char *buf
)
479 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
480 return snprintf(buf
, PAGE_SIZE
, "%d\n", kone
->settings
.tcu
);
483 static int kone_tcu_command(struct usb_device
*usb_dev
, int number
)
488 value
= kmalloc(1, GFP_KERNEL
);
494 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
495 USB_REQ_SET_CONFIGURATION
,
496 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
497 kone_command_calibrate
, 0, value
, 1,
498 USB_CTRL_SET_TIMEOUT
);
501 return ((len
!= 1) ? -EIO
: 0);
505 * Calibrating the tcu is the only action that changes settings data inside the
506 * mouse, so this data needs to be reread
508 static ssize_t
kone_sysfs_set_tcu(struct device
*dev
,
509 struct device_attribute
*attr
, char const *buf
, size_t size
)
511 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
512 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
516 retval
= strict_strtoul(buf
, 10, &state
);
520 if (state
!= 0 && state
!= 1)
523 mutex_lock(&kone
->kone_lock
);
525 if (state
== 1) { /* state activate */
526 retval
= kone_tcu_command(usb_dev
, 1);
529 retval
= kone_tcu_command(usb_dev
, 2);
532 ssleep(5); /* tcu needs this time for calibration */
533 retval
= kone_tcu_command(usb_dev
, 3);
536 retval
= kone_tcu_command(usb_dev
, 0);
539 retval
= kone_tcu_command(usb_dev
, 4);
543 * Kone needs this time to settle things.
544 * Reading settings too early will result in invalid data.
545 * Roccat's driver waits 1 sec, maybe this time could be
551 /* calibration changes values in settings, so reread */
552 retval
= kone_get_settings(usb_dev
, &kone
->settings
);
554 goto exit_no_settings
;
556 /* only write settings back if activation state is different */
557 if (kone
->settings
.tcu
!= state
) {
558 kone
->settings
.tcu
= state
;
559 kone_set_settings_checksum(&kone
->settings
);
561 retval
= kone_set_settings(usb_dev
, &kone
->settings
);
563 dev_err(&usb_dev
->dev
, "couldn't set tcu state\n");
565 * try to reread valid settings into buffer overwriting
568 retval
= kone_get_settings(usb_dev
, &kone
->settings
);
570 goto exit_no_settings
;
577 dev_err(&usb_dev
->dev
, "couldn't read settings\n");
579 mutex_unlock(&kone
->kone_lock
);
583 static ssize_t
kone_sysfs_show_startup_profile(struct device
*dev
,
584 struct device_attribute
*attr
, char *buf
)
586 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
587 return snprintf(buf
, PAGE_SIZE
, "%d\n", kone
->settings
.startup_profile
);
590 static ssize_t
kone_sysfs_set_startup_profile(struct device
*dev
,
591 struct device_attribute
*attr
, char const *buf
, size_t size
)
593 struct kone_device
*kone
= hid_get_drvdata(dev_get_drvdata(dev
));
594 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
596 unsigned long new_startup_profile
;
598 retval
= strict_strtoul(buf
, 10, &new_startup_profile
);
602 if (new_startup_profile
< 1 || new_startup_profile
> 5)
605 mutex_lock(&kone
->kone_lock
);
607 kone
->settings
.startup_profile
= new_startup_profile
;
608 kone_set_settings_checksum(&kone
->settings
);
610 retval
= kone_set_settings(usb_dev
, &kone
->settings
);
612 mutex_unlock(&kone
->kone_lock
);
617 /* changing the startup profile immediately activates this profile */
618 kone
->actual_profile
= new_startup_profile
;
619 kone
->actual_dpi
= kone
->profiles
[kone
->actual_profile
- 1].startup_dpi
;
625 * This file is used by userland software to find devices that are handled by
626 * this driver. This provides a consistent way for actual and older kernels
627 * where this driver replaced usbhid instead of generic-usb.
628 * Driver capabilities are determined by version number.
630 static ssize_t
kone_sysfs_show_driver_version(struct device
*dev
,
631 struct device_attribute
*attr
, char *buf
)
633 return snprintf(buf
, PAGE_SIZE
, ROCCAT_KONE_DRIVER_VERSION
"\n");
637 * Read actual dpi settings.
638 * Returns raw value for further processing. Refer to enum kone_polling_rates to
641 static DEVICE_ATTR(actual_dpi
, 0440, kone_sysfs_show_actual_dpi
, NULL
);
643 static DEVICE_ATTR(actual_profile
, 0440, kone_sysfs_show_actual_profile
, NULL
);
646 * The mouse can be equipped with one of four supplied weights from 5 to 20
647 * grams which are recognized and its value can be read out.
648 * This returns the raw value reported by the mouse for easy evaluation by
649 * software. Refer to enum kone_weights to get corresponding real weight.
651 static DEVICE_ATTR(weight
, 0440, kone_sysfs_show_weight
, NULL
);
654 * Prints firmware version stored in mouse as integer.
655 * The raw value reported by the mouse is returned for easy evaluation, to get
656 * the real version number the decimal point has to be shifted 2 positions to
657 * the left. E.g. a value of 138 means 1.38.
659 static DEVICE_ATTR(firmware_version
, 0440,
660 kone_sysfs_show_firmware_version
, NULL
);
663 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
664 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
666 static DEVICE_ATTR(tcu
, 0660, kone_sysfs_show_tcu
, kone_sysfs_set_tcu
);
668 /* Prints and takes the number of the profile the mouse starts with */
669 static DEVICE_ATTR(startup_profile
, 0660,
670 kone_sysfs_show_startup_profile
,
671 kone_sysfs_set_startup_profile
);
673 static DEVICE_ATTR(kone_driver_version
, 0440,
674 kone_sysfs_show_driver_version
, NULL
);
676 static struct attribute
*kone_attributes
[] = {
677 &dev_attr_actual_dpi
.attr
,
678 &dev_attr_actual_profile
.attr
,
679 &dev_attr_weight
.attr
,
680 &dev_attr_firmware_version
.attr
,
682 &dev_attr_startup_profile
.attr
,
683 &dev_attr_kone_driver_version
.attr
,
687 static struct attribute_group kone_attribute_group
= {
688 .attrs
= kone_attributes
691 static struct bin_attribute kone_settings_attr
= {
692 .attr
= { .name
= "settings", .mode
= 0660 },
693 .size
= sizeof(struct kone_settings
),
694 .read
= kone_sysfs_read_settings
,
695 .write
= kone_sysfs_write_settings
698 static struct bin_attribute kone_profile1_attr
= {
699 .attr
= { .name
= "profile1", .mode
= 0660 },
700 .size
= sizeof(struct kone_profile
),
701 .read
= kone_sysfs_read_profile1
,
702 .write
= kone_sysfs_write_profile1
705 static struct bin_attribute kone_profile2_attr
= {
706 .attr
= { .name
= "profile2", .mode
= 0660 },
707 .size
= sizeof(struct kone_profile
),
708 .read
= kone_sysfs_read_profile2
,
709 .write
= kone_sysfs_write_profile2
712 static struct bin_attribute kone_profile3_attr
= {
713 .attr
= { .name
= "profile3", .mode
= 0660 },
714 .size
= sizeof(struct kone_profile
),
715 .read
= kone_sysfs_read_profile3
,
716 .write
= kone_sysfs_write_profile3
719 static struct bin_attribute kone_profile4_attr
= {
720 .attr
= { .name
= "profile4", .mode
= 0660 },
721 .size
= sizeof(struct kone_profile
),
722 .read
= kone_sysfs_read_profile4
,
723 .write
= kone_sysfs_write_profile4
726 static struct bin_attribute kone_profile5_attr
= {
727 .attr
= { .name
= "profile5", .mode
= 0660 },
728 .size
= sizeof(struct kone_profile
),
729 .read
= kone_sysfs_read_profile5
,
730 .write
= kone_sysfs_write_profile5
733 static int kone_create_sysfs_attributes(struct usb_interface
*intf
)
737 retval
= sysfs_create_group(&intf
->dev
.kobj
, &kone_attribute_group
);
741 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_settings_attr
);
745 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_profile1_attr
);
749 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_profile2_attr
);
753 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_profile3_attr
);
757 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_profile4_attr
);
761 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
, &kone_profile5_attr
);
768 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile4_attr
);
770 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile3_attr
);
772 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile2_attr
);
774 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile1_attr
);
776 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_settings_attr
);
778 sysfs_remove_group(&intf
->dev
.kobj
, &kone_attribute_group
);
783 static void kone_remove_sysfs_attributes(struct usb_interface
*intf
)
785 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile5_attr
);
786 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile4_attr
);
787 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile3_attr
);
788 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile2_attr
);
789 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_profile1_attr
);
790 sysfs_remove_bin_file(&intf
->dev
.kobj
, &kone_settings_attr
);
791 sysfs_remove_group(&intf
->dev
.kobj
, &kone_attribute_group
);
794 static int kone_init_kone_device_struct(struct usb_device
*usb_dev
,
795 struct kone_device
*kone
)
800 mutex_init(&kone
->kone_lock
);
802 for (i
= 0; i
< 5; ++i
) {
803 retval
= kone_get_profile(usb_dev
, &kone
->profiles
[i
], i
+ 1);
808 retval
= kone_get_settings(usb_dev
, &kone
->settings
);
812 retval
= kone_get_firmware_version(usb_dev
, &kone
->firmware_version
);
816 kone
->actual_profile
= kone
->settings
.startup_profile
;
817 kone
->actual_dpi
= kone
->profiles
[kone
->actual_profile
].startup_dpi
;
823 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
824 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
826 * Secial behaviour is bound only to mousepart since only mouseevents contain
827 * additional notifications.
829 static int kone_init_specials(struct hid_device
*hdev
)
831 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
832 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
833 struct kone_device
*kone
;
836 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
837 == USB_INTERFACE_PROTOCOL_MOUSE
) {
839 kone
= kzalloc(sizeof(*kone
), GFP_KERNEL
);
841 dev_err(&hdev
->dev
, "can't alloc device descriptor\n");
844 hid_set_drvdata(hdev
, kone
);
846 retval
= kone_init_kone_device_struct(usb_dev
, kone
);
849 "couldn't init struct kone_device\n");
852 retval
= kone_create_sysfs_attributes(intf
);
854 dev_err(&hdev
->dev
, "cannot create sysfs files\n");
858 hid_set_drvdata(hdev
, NULL
);
868 static void kone_remove_specials(struct hid_device
*hdev
)
870 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
872 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
873 == USB_INTERFACE_PROTOCOL_MOUSE
) {
874 kone_remove_sysfs_attributes(intf
);
875 kfree(hid_get_drvdata(hdev
));
879 static int kone_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
883 retval
= hid_parse(hdev
);
885 dev_err(&hdev
->dev
, "parse failed\n");
889 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
891 dev_err(&hdev
->dev
, "hw start failed\n");
895 retval
= kone_init_specials(hdev
);
897 dev_err(&hdev
->dev
, "couldn't install mouse\n");
909 static void kone_remove(struct hid_device
*hdev
)
911 kone_remove_specials(hdev
);
915 /* handle special events and keep actual profile and dpi values up to date */
916 static void kone_keep_values_up_to_date(struct kone_device
*kone
,
917 struct kone_mouse_event
const *event
)
919 switch (event
->event
) {
920 case kone_mouse_event_switch_profile
:
921 case kone_mouse_event_osd_profile
:
922 kone
->actual_profile
= event
->value
;
923 kone
->actual_dpi
= kone
->profiles
[kone
->actual_profile
- 1].
926 case kone_mouse_event_switch_dpi
:
927 case kone_mouse_event_osd_dpi
:
928 kone
->actual_dpi
= event
->value
;
934 * Is called for keyboard- and mousepart.
935 * Only mousepart gets informations about special events in its extended event
938 static int kone_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
941 struct kone_device
*kone
= hid_get_drvdata(hdev
);
942 struct kone_mouse_event
*event
= (struct kone_mouse_event
*)data
;
944 /* keyboard events are always processed by default handler */
945 if (size
!= sizeof(struct kone_mouse_event
))
949 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
950 * Pressed button is reported in each movement event.
951 * Workaround sends only one event per press.
953 if (memcmp(&kone
->last_mouse_event
.tilt
, &event
->tilt
, 5))
954 memcpy(&kone
->last_mouse_event
, event
,
955 sizeof(struct kone_mouse_event
));
957 memset(&event
->tilt
, 0, 5);
959 kone_keep_values_up_to_date(kone
, event
);
961 return 0; /* always do further processing */
964 static const struct hid_device_id kone_devices
[] = {
965 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
, USB_DEVICE_ID_ROCCAT_KONE
) },
969 MODULE_DEVICE_TABLE(hid
, kone_devices
);
971 static struct hid_driver kone_driver
= {
973 .id_table
= kone_devices
,
975 .remove
= kone_remove
,
976 .raw_event
= kone_raw_event
979 static int __init
kone_init(void)
981 return hid_register_driver(&kone_driver
);
984 static void __exit
kone_exit(void)
986 hid_unregister_driver(&kone_driver
);
989 module_init(kone_init
);
990 module_exit(kone_exit
);
992 MODULE_AUTHOR("Stefan Achatz");
993 MODULE_DESCRIPTION("USB Roccat Kone driver");
994 MODULE_LICENSE("GPL v2");