nilfs2: unbreak compat ioctl
[zen-stable.git] / drivers / hid / hid-roccat-kone.c
blobe2072afb34bbb7dce51c9bdb3b44f5b2867e9bd3
1 /*
2 * Roccat Kone driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
7 /*
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)
11 * any later version.
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>
34 #include "hid-ids.h"
35 #include "hid-roccat-common.h"
36 #include "hid-roccat-kone.h"
38 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
40 static void kone_profile_activated(struct kone_device *kone, uint new_profile)
42 kone->actual_profile = new_profile;
43 kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
46 static void kone_profile_report(struct kone_device *kone, uint new_profile)
48 struct kone_roccat_report roccat_report;
49 roccat_report.event = kone_mouse_event_switch_profile;
50 roccat_report.value = new_profile;
51 roccat_report.key = 0;
52 roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
55 static int kone_receive(struct usb_device *usb_dev, uint usb_command,
56 void *data, uint size)
58 char *buf;
59 int len;
61 buf = kmalloc(size, GFP_KERNEL);
62 if (buf == NULL)
63 return -ENOMEM;
65 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
66 HID_REQ_GET_REPORT,
67 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
68 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
70 memcpy(data, buf, size);
71 kfree(buf);
72 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
75 static int kone_send(struct usb_device *usb_dev, uint usb_command,
76 void const *data, uint size)
78 char *buf;
79 int len;
81 buf = kmalloc(size, GFP_KERNEL);
82 if (buf == NULL)
83 return -ENOMEM;
85 memcpy(buf, data, size);
87 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
88 HID_REQ_SET_REPORT,
89 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
90 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
92 kfree(buf);
93 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
96 /* kone_class is used for creating sysfs attributes via roccat char device */
97 static struct class *kone_class;
99 static void kone_set_settings_checksum(struct kone_settings *settings)
101 uint16_t checksum = 0;
102 unsigned char *address = (unsigned char *)settings;
103 int i;
105 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
106 checksum += *address;
107 settings->checksum = cpu_to_le16(checksum);
111 * Checks success after writing data to mouse
112 * On success returns 0
113 * On failure returns errno
115 static int kone_check_write(struct usb_device *usb_dev)
117 int retval;
118 uint8_t data;
120 do {
122 * Mouse needs 50 msecs until it says ok, but there are
123 * 30 more msecs needed for next write to work.
125 msleep(80);
127 retval = kone_receive(usb_dev,
128 kone_command_confirm_write, &data, 1);
129 if (retval)
130 return retval;
133 * value of 3 seems to mean something like
134 * "not finished yet, but it looks good"
135 * So check again after a moment.
137 } while (data == 3);
139 if (data == 1) /* everything alright */
140 return 0;
142 /* unknown answer */
143 hid_err(usb_dev, "got retval %d when checking write\n", data);
144 return -EIO;
148 * Reads settings from mouse and stores it in @buf
149 * On success returns 0
150 * On failure returns errno
152 static int kone_get_settings(struct usb_device *usb_dev,
153 struct kone_settings *buf)
155 return kone_receive(usb_dev, kone_command_settings, buf,
156 sizeof(struct kone_settings));
160 * Writes settings from @buf to mouse
161 * On success returns 0
162 * On failure returns errno
164 static int kone_set_settings(struct usb_device *usb_dev,
165 struct kone_settings const *settings)
167 int retval;
168 retval = kone_send(usb_dev, kone_command_settings,
169 settings, sizeof(struct kone_settings));
170 if (retval)
171 return retval;
172 return kone_check_write(usb_dev);
176 * Reads profile data from mouse and stores it in @buf
177 * @number: profile number to read
178 * On success returns 0
179 * On failure returns errno
181 static int kone_get_profile(struct usb_device *usb_dev,
182 struct kone_profile *buf, int number)
184 int len;
186 if (number < 1 || number > 5)
187 return -EINVAL;
189 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
190 USB_REQ_CLEAR_FEATURE,
191 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
192 kone_command_profile, number, buf,
193 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
195 if (len != sizeof(struct kone_profile))
196 return -EIO;
198 return 0;
202 * Writes profile data to mouse.
203 * @number: profile number to write
204 * On success returns 0
205 * On failure returns errno
207 static int kone_set_profile(struct usb_device *usb_dev,
208 struct kone_profile const *profile, int number)
210 int len;
212 if (number < 1 || number > 5)
213 return -EINVAL;
215 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
216 USB_REQ_SET_CONFIGURATION,
217 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
218 kone_command_profile, number, (void *)profile,
219 sizeof(struct kone_profile),
220 USB_CTRL_SET_TIMEOUT);
222 if (len != sizeof(struct kone_profile))
223 return len;
225 if (kone_check_write(usb_dev))
226 return -EIO;
228 return 0;
232 * Reads value of "fast-clip-weight" and stores it in @result
233 * On success returns 0
234 * On failure returns errno
236 static int kone_get_weight(struct usb_device *usb_dev, int *result)
238 int retval;
239 uint8_t data;
241 retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
243 if (retval)
244 return retval;
246 *result = (int)data;
247 return 0;
251 * Reads firmware_version of mouse and stores it in @result
252 * On success returns 0
253 * On failure returns errno
255 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
257 int retval;
258 uint16_t data;
260 retval = kone_receive(usb_dev, kone_command_firmware_version,
261 &data, 2);
262 if (retval)
263 return retval;
265 *result = le16_to_cpu(data);
266 return 0;
269 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
270 struct bin_attribute *attr, char *buf,
271 loff_t off, size_t count) {
272 struct device *dev =
273 container_of(kobj, struct device, kobj)->parent->parent;
274 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
276 if (off >= sizeof(struct kone_settings))
277 return 0;
279 if (off + count > sizeof(struct kone_settings))
280 count = sizeof(struct kone_settings) - off;
282 mutex_lock(&kone->kone_lock);
283 memcpy(buf, ((char const *)&kone->settings) + off, count);
284 mutex_unlock(&kone->kone_lock);
286 return count;
290 * Writing settings automatically activates startup_profile.
291 * This function keeps values in kone_device up to date and assumes that in
292 * case of error the old data is still valid
294 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
295 struct bin_attribute *attr, char *buf,
296 loff_t off, size_t count) {
297 struct device *dev =
298 container_of(kobj, struct device, kobj)->parent->parent;
299 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
300 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
301 int retval = 0, difference, old_profile;
303 /* I need to get my data in one piece */
304 if (off != 0 || count != sizeof(struct kone_settings))
305 return -EINVAL;
307 mutex_lock(&kone->kone_lock);
308 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
309 if (difference) {
310 retval = kone_set_settings(usb_dev,
311 (struct kone_settings const *)buf);
312 if (retval) {
313 mutex_unlock(&kone->kone_lock);
314 return retval;
317 old_profile = kone->settings.startup_profile;
318 memcpy(&kone->settings, buf, sizeof(struct kone_settings));
320 kone_profile_activated(kone, kone->settings.startup_profile);
322 if (kone->settings.startup_profile != old_profile)
323 kone_profile_report(kone, kone->settings.startup_profile);
325 mutex_unlock(&kone->kone_lock);
327 return sizeof(struct kone_settings);
330 static ssize_t kone_sysfs_read_profilex(struct file *fp,
331 struct kobject *kobj, struct bin_attribute *attr,
332 char *buf, loff_t off, size_t count) {
333 struct device *dev =
334 container_of(kobj, struct device, kobj)->parent->parent;
335 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
337 if (off >= sizeof(struct kone_profile))
338 return 0;
340 if (off + count > sizeof(struct kone_profile))
341 count = sizeof(struct kone_profile) - off;
343 mutex_lock(&kone->kone_lock);
344 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
345 mutex_unlock(&kone->kone_lock);
347 return count;
350 /* Writes data only if different to stored data */
351 static ssize_t kone_sysfs_write_profilex(struct file *fp,
352 struct kobject *kobj, struct bin_attribute *attr,
353 char *buf, loff_t off, size_t count) {
354 struct device *dev =
355 container_of(kobj, struct device, kobj)->parent->parent;
356 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
357 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
358 struct kone_profile *profile;
359 int retval = 0, difference;
361 /* I need to get my data in one piece */
362 if (off != 0 || count != sizeof(struct kone_profile))
363 return -EINVAL;
365 profile = &kone->profiles[*(uint *)(attr->private)];
367 mutex_lock(&kone->kone_lock);
368 difference = memcmp(buf, profile, sizeof(struct kone_profile));
369 if (difference) {
370 retval = kone_set_profile(usb_dev,
371 (struct kone_profile const *)buf,
372 *(uint *)(attr->private) + 1);
373 if (!retval)
374 memcpy(profile, buf, sizeof(struct kone_profile));
376 mutex_unlock(&kone->kone_lock);
378 if (retval)
379 return retval;
381 return sizeof(struct kone_profile);
384 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct kone_device *kone =
388 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
389 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
392 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct kone_device *kone =
396 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
397 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
400 /* weight is read each time, since we don't get informed when it's changed */
401 static ssize_t kone_sysfs_show_weight(struct device *dev,
402 struct device_attribute *attr, char *buf)
404 struct kone_device *kone;
405 struct usb_device *usb_dev;
406 int weight = 0;
407 int retval;
409 dev = dev->parent->parent;
410 kone = hid_get_drvdata(dev_get_drvdata(dev));
411 usb_dev = interface_to_usbdev(to_usb_interface(dev));
413 mutex_lock(&kone->kone_lock);
414 retval = kone_get_weight(usb_dev, &weight);
415 mutex_unlock(&kone->kone_lock);
417 if (retval)
418 return retval;
419 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
422 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
423 struct device_attribute *attr, char *buf)
425 struct kone_device *kone =
426 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
427 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
430 static ssize_t kone_sysfs_show_tcu(struct device *dev,
431 struct device_attribute *attr, char *buf)
433 struct kone_device *kone =
434 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
435 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
438 static int kone_tcu_command(struct usb_device *usb_dev, int number)
440 unsigned char value;
441 value = number;
442 return kone_send(usb_dev, kone_command_calibrate, &value, 1);
446 * Calibrating the tcu is the only action that changes settings data inside the
447 * mouse, so this data needs to be reread
449 static ssize_t kone_sysfs_set_tcu(struct device *dev,
450 struct device_attribute *attr, char const *buf, size_t size)
452 struct kone_device *kone;
453 struct usb_device *usb_dev;
454 int retval;
455 unsigned long state;
457 dev = dev->parent->parent;
458 kone = hid_get_drvdata(dev_get_drvdata(dev));
459 usb_dev = interface_to_usbdev(to_usb_interface(dev));
461 retval = strict_strtoul(buf, 10, &state);
462 if (retval)
463 return retval;
465 if (state != 0 && state != 1)
466 return -EINVAL;
468 mutex_lock(&kone->kone_lock);
470 if (state == 1) { /* state activate */
471 retval = kone_tcu_command(usb_dev, 1);
472 if (retval)
473 goto exit_unlock;
474 retval = kone_tcu_command(usb_dev, 2);
475 if (retval)
476 goto exit_unlock;
477 ssleep(5); /* tcu needs this time for calibration */
478 retval = kone_tcu_command(usb_dev, 3);
479 if (retval)
480 goto exit_unlock;
481 retval = kone_tcu_command(usb_dev, 0);
482 if (retval)
483 goto exit_unlock;
484 retval = kone_tcu_command(usb_dev, 4);
485 if (retval)
486 goto exit_unlock;
488 * Kone needs this time to settle things.
489 * Reading settings too early will result in invalid data.
490 * Roccat's driver waits 1 sec, maybe this time could be
491 * shortened.
493 ssleep(1);
496 /* calibration changes values in settings, so reread */
497 retval = kone_get_settings(usb_dev, &kone->settings);
498 if (retval)
499 goto exit_no_settings;
501 /* only write settings back if activation state is different */
502 if (kone->settings.tcu != state) {
503 kone->settings.tcu = state;
504 kone_set_settings_checksum(&kone->settings);
506 retval = kone_set_settings(usb_dev, &kone->settings);
507 if (retval) {
508 hid_err(usb_dev, "couldn't set tcu state\n");
510 * try to reread valid settings into buffer overwriting
511 * first error code
513 retval = kone_get_settings(usb_dev, &kone->settings);
514 if (retval)
515 goto exit_no_settings;
516 goto exit_unlock;
518 /* calibration resets profile */
519 kone_profile_activated(kone, kone->settings.startup_profile);
522 retval = size;
523 exit_no_settings:
524 hid_err(usb_dev, "couldn't read settings\n");
525 exit_unlock:
526 mutex_unlock(&kone->kone_lock);
527 return retval;
530 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
531 struct device_attribute *attr, char *buf)
533 struct kone_device *kone =
534 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
535 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
538 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
539 struct device_attribute *attr, char const *buf, size_t size)
541 struct kone_device *kone;
542 struct usb_device *usb_dev;
543 int retval;
544 unsigned long new_startup_profile;
546 dev = dev->parent->parent;
547 kone = hid_get_drvdata(dev_get_drvdata(dev));
548 usb_dev = interface_to_usbdev(to_usb_interface(dev));
550 retval = strict_strtoul(buf, 10, &new_startup_profile);
551 if (retval)
552 return retval;
554 if (new_startup_profile < 1 || new_startup_profile > 5)
555 return -EINVAL;
557 mutex_lock(&kone->kone_lock);
559 kone->settings.startup_profile = new_startup_profile;
560 kone_set_settings_checksum(&kone->settings);
562 retval = kone_set_settings(usb_dev, &kone->settings);
563 if (retval) {
564 mutex_unlock(&kone->kone_lock);
565 return retval;
568 /* changing the startup profile immediately activates this profile */
569 kone_profile_activated(kone, new_startup_profile);
570 kone_profile_report(kone, new_startup_profile);
572 mutex_unlock(&kone->kone_lock);
573 return size;
576 static struct device_attribute kone_attributes[] = {
578 * Read actual dpi settings.
579 * Returns raw value for further processing. Refer to enum
580 * kone_polling_rates to get real value.
582 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
583 __ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
586 * The mouse can be equipped with one of four supplied weights from 5
587 * to 20 grams which are recognized and its value can be read out.
588 * This returns the raw value reported by the mouse for easy evaluation
589 * by software. Refer to enum kone_weights to get corresponding real
590 * weight.
592 __ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
595 * Prints firmware version stored in mouse as integer.
596 * The raw value reported by the mouse is returned for easy evaluation,
597 * to get the real version number the decimal point has to be shifted 2
598 * positions to the left. E.g. a value of 138 means 1.38.
600 __ATTR(firmware_version, 0440,
601 kone_sysfs_show_firmware_version, NULL),
604 * Prints state of Tracking Control Unit as number where 0 = off and
605 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
606 * activates the tcu
608 __ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
610 /* Prints and takes the number of the profile the mouse starts with */
611 __ATTR(startup_profile, 0660,
612 kone_sysfs_show_startup_profile,
613 kone_sysfs_set_startup_profile),
614 __ATTR_NULL
617 static struct bin_attribute kone_bin_attributes[] = {
619 .attr = { .name = "settings", .mode = 0660 },
620 .size = sizeof(struct kone_settings),
621 .read = kone_sysfs_read_settings,
622 .write = kone_sysfs_write_settings
625 .attr = { .name = "profile1", .mode = 0660 },
626 .size = sizeof(struct kone_profile),
627 .read = kone_sysfs_read_profilex,
628 .write = kone_sysfs_write_profilex,
629 .private = &profile_numbers[0]
632 .attr = { .name = "profile2", .mode = 0660 },
633 .size = sizeof(struct kone_profile),
634 .read = kone_sysfs_read_profilex,
635 .write = kone_sysfs_write_profilex,
636 .private = &profile_numbers[1]
639 .attr = { .name = "profile3", .mode = 0660 },
640 .size = sizeof(struct kone_profile),
641 .read = kone_sysfs_read_profilex,
642 .write = kone_sysfs_write_profilex,
643 .private = &profile_numbers[2]
646 .attr = { .name = "profile4", .mode = 0660 },
647 .size = sizeof(struct kone_profile),
648 .read = kone_sysfs_read_profilex,
649 .write = kone_sysfs_write_profilex,
650 .private = &profile_numbers[3]
653 .attr = { .name = "profile5", .mode = 0660 },
654 .size = sizeof(struct kone_profile),
655 .read = kone_sysfs_read_profilex,
656 .write = kone_sysfs_write_profilex,
657 .private = &profile_numbers[4]
659 __ATTR_NULL
662 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
663 struct kone_device *kone)
665 uint i;
666 int retval;
668 mutex_init(&kone->kone_lock);
670 for (i = 0; i < 5; ++i) {
671 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
672 if (retval)
673 return retval;
676 retval = kone_get_settings(usb_dev, &kone->settings);
677 if (retval)
678 return retval;
680 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
681 if (retval)
682 return retval;
684 kone_profile_activated(kone, kone->settings.startup_profile);
686 return 0;
690 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
691 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
692 * module.
693 * Secial behaviour is bound only to mousepart since only mouseevents contain
694 * additional notifications.
696 static int kone_init_specials(struct hid_device *hdev)
698 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
699 struct usb_device *usb_dev = interface_to_usbdev(intf);
700 struct kone_device *kone;
701 int retval;
703 if (intf->cur_altsetting->desc.bInterfaceProtocol
704 == USB_INTERFACE_PROTOCOL_MOUSE) {
706 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
707 if (!kone) {
708 hid_err(hdev, "can't alloc device descriptor\n");
709 return -ENOMEM;
711 hid_set_drvdata(hdev, kone);
713 retval = kone_init_kone_device_struct(usb_dev, kone);
714 if (retval) {
715 hid_err(hdev, "couldn't init struct kone_device\n");
716 goto exit_free;
719 retval = roccat_connect(kone_class, hdev,
720 sizeof(struct kone_roccat_report));
721 if (retval < 0) {
722 hid_err(hdev, "couldn't init char dev\n");
723 /* be tolerant about not getting chrdev */
724 } else {
725 kone->roccat_claimed = 1;
726 kone->chrdev_minor = retval;
728 } else {
729 hid_set_drvdata(hdev, NULL);
732 return 0;
733 exit_free:
734 kfree(kone);
735 return retval;
738 static void kone_remove_specials(struct hid_device *hdev)
740 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
741 struct kone_device *kone;
743 if (intf->cur_altsetting->desc.bInterfaceProtocol
744 == USB_INTERFACE_PROTOCOL_MOUSE) {
745 kone = hid_get_drvdata(hdev);
746 if (kone->roccat_claimed)
747 roccat_disconnect(kone->chrdev_minor);
748 kfree(hid_get_drvdata(hdev));
752 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
754 int retval;
756 retval = hid_parse(hdev);
757 if (retval) {
758 hid_err(hdev, "parse failed\n");
759 goto exit;
762 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
763 if (retval) {
764 hid_err(hdev, "hw start failed\n");
765 goto exit;
768 retval = kone_init_specials(hdev);
769 if (retval) {
770 hid_err(hdev, "couldn't install mouse\n");
771 goto exit_stop;
774 return 0;
776 exit_stop:
777 hid_hw_stop(hdev);
778 exit:
779 return retval;
782 static void kone_remove(struct hid_device *hdev)
784 kone_remove_specials(hdev);
785 hid_hw_stop(hdev);
788 /* handle special events and keep actual profile and dpi values up to date */
789 static void kone_keep_values_up_to_date(struct kone_device *kone,
790 struct kone_mouse_event const *event)
792 switch (event->event) {
793 case kone_mouse_event_switch_profile:
794 kone->actual_dpi = kone->profiles[event->value - 1].
795 startup_dpi;
796 case kone_mouse_event_osd_profile:
797 kone->actual_profile = event->value;
798 break;
799 case kone_mouse_event_switch_dpi:
800 case kone_mouse_event_osd_dpi:
801 kone->actual_dpi = event->value;
802 break;
806 static void kone_report_to_chrdev(struct kone_device const *kone,
807 struct kone_mouse_event const *event)
809 struct kone_roccat_report roccat_report;
811 switch (event->event) {
812 case kone_mouse_event_switch_profile:
813 case kone_mouse_event_switch_dpi:
814 case kone_mouse_event_osd_profile:
815 case kone_mouse_event_osd_dpi:
816 roccat_report.event = event->event;
817 roccat_report.value = event->value;
818 roccat_report.key = 0;
819 roccat_report_event(kone->chrdev_minor,
820 (uint8_t *)&roccat_report);
821 break;
822 case kone_mouse_event_call_overlong_macro:
823 if (event->value == kone_keystroke_action_press) {
824 roccat_report.event = kone_mouse_event_call_overlong_macro;
825 roccat_report.value = kone->actual_profile;
826 roccat_report.key = event->macro_key;
827 roccat_report_event(kone->chrdev_minor,
828 (uint8_t *)&roccat_report);
830 break;
836 * Is called for keyboard- and mousepart.
837 * Only mousepart gets informations about special events in its extended event
838 * structure.
840 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
841 u8 *data, int size)
843 struct kone_device *kone = hid_get_drvdata(hdev);
844 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
846 /* keyboard events are always processed by default handler */
847 if (size != sizeof(struct kone_mouse_event))
848 return 0;
850 if (kone == NULL)
851 return 0;
854 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
855 * Pressed button is reported in each movement event.
856 * Workaround sends only one event per press.
858 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
859 memcpy(&kone->last_mouse_event, event,
860 sizeof(struct kone_mouse_event));
861 else
862 memset(&event->tilt, 0, 5);
864 kone_keep_values_up_to_date(kone, event);
866 if (kone->roccat_claimed)
867 kone_report_to_chrdev(kone, event);
869 return 0; /* always do further processing */
872 static const struct hid_device_id kone_devices[] = {
873 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
877 MODULE_DEVICE_TABLE(hid, kone_devices);
879 static struct hid_driver kone_driver = {
880 .name = "kone",
881 .id_table = kone_devices,
882 .probe = kone_probe,
883 .remove = kone_remove,
884 .raw_event = kone_raw_event
887 static int __init kone_init(void)
889 int retval;
891 /* class name has to be same as driver name */
892 kone_class = class_create(THIS_MODULE, "kone");
893 if (IS_ERR(kone_class))
894 return PTR_ERR(kone_class);
895 kone_class->dev_attrs = kone_attributes;
896 kone_class->dev_bin_attrs = kone_bin_attributes;
898 retval = hid_register_driver(&kone_driver);
899 if (retval)
900 class_destroy(kone_class);
901 return retval;
904 static void __exit kone_exit(void)
906 hid_unregister_driver(&kone_driver);
907 class_destroy(kone_class);
910 module_init(kone_init);
911 module_exit(kone_exit);
913 MODULE_AUTHOR("Stefan Achatz");
914 MODULE_DESCRIPTION("USB Roccat Kone driver");
915 MODULE_LICENSE("GPL v2");