headers: kobject.h redux
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-roccat-kone.c
blobcbd8cc42e75ab38d203697f1752cbeabd09ad2c7
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/usb.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include "hid-ids.h"
35 #include "hid-roccat.h"
36 #include "hid-roccat-kone.h"
38 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
40 /* kone_class is used for creating sysfs attributes via roccat char device */
41 static struct class *kone_class;
43 static void kone_set_settings_checksum(struct kone_settings *settings)
45 uint16_t checksum = 0;
46 unsigned char *address = (unsigned char *)settings;
47 int i;
49 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
50 checksum += *address;
51 settings->checksum = cpu_to_le16(checksum);
55 * Checks success after writing data to mouse
56 * On success returns 0
57 * On failure returns errno
59 static int kone_check_write(struct usb_device *usb_dev)
61 int len;
62 unsigned char *data;
64 data = kmalloc(1, GFP_KERNEL);
65 if (!data)
66 return -ENOMEM;
68 do {
70 * Mouse needs 50 msecs until it says ok, but there are
71 * 30 more msecs needed for next write to work.
73 msleep(80);
75 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
76 USB_REQ_CLEAR_FEATURE,
77 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
78 USB_DIR_IN,
79 kone_command_confirm_write, 0, data, 1,
80 USB_CTRL_SET_TIMEOUT);
82 if (len != 1) {
83 kfree(data);
84 return -EIO;
88 * value of 3 seems to mean something like
89 * "not finished yet, but it looks good"
90 * So check again after a moment.
92 } while (*data == 3);
94 if (*data == 1) { /* everything alright */
95 kfree(data);
96 return 0;
97 } else { /* unknown answer */
98 hid_err(usb_dev, "got retval %d when checking write\n", *data);
99 kfree(data);
100 return -EIO;
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)
113 int len;
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))
122 return -EIO;
124 return 0;
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)
135 int len;
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))
145 return -EIO;
147 if (kone_check_write(usb_dev))
148 return -EIO;
150 return 0;
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)
162 int len;
164 if (number < 1 || number > 5)
165 return -EINVAL;
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))
174 return -EIO;
176 return 0;
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)
188 int len;
190 if (number < 1 || number > 5)
191 return -EINVAL;
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))
201 return len;
203 if (kone_check_write(usb_dev))
204 return -EIO;
206 return 0;
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)
216 int len;
217 uint8_t *data;
219 data = kmalloc(1, GFP_KERNEL);
220 if (!data)
221 return -ENOMEM;
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);
228 if (len != 1) {
229 kfree(data);
230 return -EIO;
232 *result = (int)*data;
233 kfree(data);
234 return 0;
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)
244 int len;
245 unsigned char *data;
247 data = kmalloc(2, GFP_KERNEL);
248 if (!data)
249 return -ENOMEM;
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);
257 if (len != 2) {
258 kfree(data);
259 return -EIO;
261 *result = le16_to_cpu(*data);
262 kfree(data);
263 return 0;
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 =
270 container_of(kobj, struct device, kobj)->parent->parent;
271 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
273 if (off >= sizeof(struct kone_settings))
274 return 0;
276 if (off + count > sizeof(struct kone_settings))
277 count = sizeof(struct kone_settings) - off;
279 mutex_lock(&kone->kone_lock);
280 memcpy(buf, ((char const *)&kone->settings) + off, count);
281 mutex_unlock(&kone->kone_lock);
283 return count;
287 * Writing settings automatically activates startup_profile.
288 * This function keeps values in kone_device up to date and assumes that in
289 * case of error the old data is still valid
291 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
292 struct bin_attribute *attr, char *buf,
293 loff_t off, size_t count) {
294 struct device *dev =
295 container_of(kobj, struct device, kobj)->parent->parent;
296 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
297 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
298 int retval = 0, difference;
300 /* I need to get my data in one piece */
301 if (off != 0 || count != sizeof(struct kone_settings))
302 return -EINVAL;
304 mutex_lock(&kone->kone_lock);
305 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
306 if (difference) {
307 retval = kone_set_settings(usb_dev,
308 (struct kone_settings const *)buf);
309 if (!retval)
310 memcpy(&kone->settings, buf,
311 sizeof(struct kone_settings));
313 mutex_unlock(&kone->kone_lock);
315 if (retval)
316 return retval;
319 * If we get here, treat settings as okay and update actual values
320 * according to startup_profile
322 kone->actual_profile = kone->settings.startup_profile;
323 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
325 return sizeof(struct kone_settings);
328 static ssize_t kone_sysfs_read_profilex(struct file *fp,
329 struct kobject *kobj, struct bin_attribute *attr,
330 char *buf, loff_t off, size_t count) {
331 struct device *dev =
332 container_of(kobj, struct device, kobj)->parent->parent;
333 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
335 if (off >= sizeof(struct kone_profile))
336 return 0;
338 if (off + count > sizeof(struct kone_profile))
339 count = sizeof(struct kone_profile) - off;
341 mutex_lock(&kone->kone_lock);
342 memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
343 mutex_unlock(&kone->kone_lock);
345 return count;
348 /* Writes data only if different to stored data */
349 static ssize_t kone_sysfs_write_profilex(struct file *fp,
350 struct kobject *kobj, struct bin_attribute *attr,
351 char *buf, loff_t off, size_t count) {
352 struct device *dev =
353 container_of(kobj, struct device, kobj)->parent->parent;
354 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
355 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
356 struct kone_profile *profile;
357 int retval = 0, difference;
359 /* I need to get my data in one piece */
360 if (off != 0 || count != sizeof(struct kone_profile))
361 return -EINVAL;
363 profile = &kone->profiles[*(uint *)(attr->private)];
365 mutex_lock(&kone->kone_lock);
366 difference = memcmp(buf, profile, sizeof(struct kone_profile));
367 if (difference) {
368 retval = kone_set_profile(usb_dev,
369 (struct kone_profile const *)buf,
370 *(uint *)(attr->private) + 1);
371 if (!retval)
372 memcpy(profile, buf, sizeof(struct kone_profile));
374 mutex_unlock(&kone->kone_lock);
376 if (retval)
377 return retval;
379 return sizeof(struct kone_profile);
382 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct kone_device *kone =
386 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
387 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
390 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct kone_device *kone =
394 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
395 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
398 /* weight is read each time, since we don't get informed when it's changed */
399 static ssize_t kone_sysfs_show_weight(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct kone_device *kone;
403 struct usb_device *usb_dev;
404 int weight = 0;
405 int retval;
407 dev = dev->parent->parent;
408 kone = hid_get_drvdata(dev_get_drvdata(dev));
409 usb_dev = interface_to_usbdev(to_usb_interface(dev));
411 mutex_lock(&kone->kone_lock);
412 retval = kone_get_weight(usb_dev, &weight);
413 mutex_unlock(&kone->kone_lock);
415 if (retval)
416 return retval;
417 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
420 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct kone_device *kone =
424 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
425 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
428 static ssize_t kone_sysfs_show_tcu(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct kone_device *kone =
432 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
433 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
436 static int kone_tcu_command(struct usb_device *usb_dev, int number)
438 int len;
439 char *value;
441 value = kmalloc(1, GFP_KERNEL);
442 if (!value)
443 return -ENOMEM;
445 *value = number;
447 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
448 USB_REQ_SET_CONFIGURATION,
449 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
450 kone_command_calibrate, 0, value, 1,
451 USB_CTRL_SET_TIMEOUT);
453 kfree(value);
454 return ((len != 1) ? -EIO : 0);
458 * Calibrating the tcu is the only action that changes settings data inside the
459 * mouse, so this data needs to be reread
461 static ssize_t kone_sysfs_set_tcu(struct device *dev,
462 struct device_attribute *attr, char const *buf, size_t size)
464 struct kone_device *kone;
465 struct usb_device *usb_dev;
466 int retval;
467 unsigned long state;
469 dev = dev->parent->parent;
470 kone = hid_get_drvdata(dev_get_drvdata(dev));
471 usb_dev = interface_to_usbdev(to_usb_interface(dev));
473 retval = strict_strtoul(buf, 10, &state);
474 if (retval)
475 return retval;
477 if (state != 0 && state != 1)
478 return -EINVAL;
480 mutex_lock(&kone->kone_lock);
482 if (state == 1) { /* state activate */
483 retval = kone_tcu_command(usb_dev, 1);
484 if (retval)
485 goto exit_unlock;
486 retval = kone_tcu_command(usb_dev, 2);
487 if (retval)
488 goto exit_unlock;
489 ssleep(5); /* tcu needs this time for calibration */
490 retval = kone_tcu_command(usb_dev, 3);
491 if (retval)
492 goto exit_unlock;
493 retval = kone_tcu_command(usb_dev, 0);
494 if (retval)
495 goto exit_unlock;
496 retval = kone_tcu_command(usb_dev, 4);
497 if (retval)
498 goto exit_unlock;
500 * Kone needs this time to settle things.
501 * Reading settings too early will result in invalid data.
502 * Roccat's driver waits 1 sec, maybe this time could be
503 * shortened.
505 ssleep(1);
508 /* calibration changes values in settings, so reread */
509 retval = kone_get_settings(usb_dev, &kone->settings);
510 if (retval)
511 goto exit_no_settings;
513 /* only write settings back if activation state is different */
514 if (kone->settings.tcu != state) {
515 kone->settings.tcu = state;
516 kone_set_settings_checksum(&kone->settings);
518 retval = kone_set_settings(usb_dev, &kone->settings);
519 if (retval) {
520 hid_err(usb_dev, "couldn't set tcu state\n");
522 * try to reread valid settings into buffer overwriting
523 * first error code
525 retval = kone_get_settings(usb_dev, &kone->settings);
526 if (retval)
527 goto exit_no_settings;
528 goto exit_unlock;
532 retval = size;
533 exit_no_settings:
534 hid_err(usb_dev, "couldn't read settings\n");
535 exit_unlock:
536 mutex_unlock(&kone->kone_lock);
537 return retval;
540 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct kone_device *kone =
544 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
545 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
548 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
549 struct device_attribute *attr, char const *buf, size_t size)
551 struct kone_device *kone;
552 struct usb_device *usb_dev;
553 int retval;
554 unsigned long new_startup_profile;
556 dev = dev->parent->parent;
557 kone = hid_get_drvdata(dev_get_drvdata(dev));
558 usb_dev = interface_to_usbdev(to_usb_interface(dev));
560 retval = strict_strtoul(buf, 10, &new_startup_profile);
561 if (retval)
562 return retval;
564 if (new_startup_profile < 1 || new_startup_profile > 5)
565 return -EINVAL;
567 mutex_lock(&kone->kone_lock);
569 kone->settings.startup_profile = new_startup_profile;
570 kone_set_settings_checksum(&kone->settings);
572 retval = kone_set_settings(usb_dev, &kone->settings);
574 mutex_unlock(&kone->kone_lock);
576 if (retval)
577 return retval;
579 /* changing the startup profile immediately activates this profile */
580 kone->actual_profile = new_startup_profile;
581 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
583 return size;
586 static struct device_attribute kone_attributes[] = {
588 * Read actual dpi settings.
589 * Returns raw value for further processing. Refer to enum
590 * kone_polling_rates to get real value.
592 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
593 __ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
596 * The mouse can be equipped with one of four supplied weights from 5
597 * to 20 grams which are recognized and its value can be read out.
598 * This returns the raw value reported by the mouse for easy evaluation
599 * by software. Refer to enum kone_weights to get corresponding real
600 * weight.
602 __ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
605 * Prints firmware version stored in mouse as integer.
606 * The raw value reported by the mouse is returned for easy evaluation,
607 * to get the real version number the decimal point has to be shifted 2
608 * positions to the left. E.g. a value of 138 means 1.38.
610 __ATTR(firmware_version, 0440,
611 kone_sysfs_show_firmware_version, NULL),
614 * Prints state of Tracking Control Unit as number where 0 = off and
615 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
616 * activates the tcu
618 __ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
620 /* Prints and takes the number of the profile the mouse starts with */
621 __ATTR(startup_profile, 0660,
622 kone_sysfs_show_startup_profile,
623 kone_sysfs_set_startup_profile),
624 __ATTR_NULL
627 static struct bin_attribute kone_bin_attributes[] = {
629 .attr = { .name = "settings", .mode = 0660 },
630 .size = sizeof(struct kone_settings),
631 .read = kone_sysfs_read_settings,
632 .write = kone_sysfs_write_settings
635 .attr = { .name = "profile1", .mode = 0660 },
636 .size = sizeof(struct kone_profile),
637 .read = kone_sysfs_read_profilex,
638 .write = kone_sysfs_write_profilex,
639 .private = &profile_numbers[0]
642 .attr = { .name = "profile2", .mode = 0660 },
643 .size = sizeof(struct kone_profile),
644 .read = kone_sysfs_read_profilex,
645 .write = kone_sysfs_write_profilex,
646 .private = &profile_numbers[1]
649 .attr = { .name = "profile3", .mode = 0660 },
650 .size = sizeof(struct kone_profile),
651 .read = kone_sysfs_read_profilex,
652 .write = kone_sysfs_write_profilex,
653 .private = &profile_numbers[2]
656 .attr = { .name = "profile4", .mode = 0660 },
657 .size = sizeof(struct kone_profile),
658 .read = kone_sysfs_read_profilex,
659 .write = kone_sysfs_write_profilex,
660 .private = &profile_numbers[3]
663 .attr = { .name = "profile5", .mode = 0660 },
664 .size = sizeof(struct kone_profile),
665 .read = kone_sysfs_read_profilex,
666 .write = kone_sysfs_write_profilex,
667 .private = &profile_numbers[4]
669 __ATTR_NULL
672 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
673 struct kone_device *kone)
675 uint i;
676 int retval;
678 mutex_init(&kone->kone_lock);
680 for (i = 0; i < 5; ++i) {
681 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
682 if (retval)
683 return retval;
686 retval = kone_get_settings(usb_dev, &kone->settings);
687 if (retval)
688 return retval;
690 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
691 if (retval)
692 return retval;
694 kone->actual_profile = kone->settings.startup_profile;
695 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
697 return 0;
701 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
702 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
703 * module.
704 * Secial behaviour is bound only to mousepart since only mouseevents contain
705 * additional notifications.
707 static int kone_init_specials(struct hid_device *hdev)
709 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
710 struct usb_device *usb_dev = interface_to_usbdev(intf);
711 struct kone_device *kone;
712 int retval;
714 if (intf->cur_altsetting->desc.bInterfaceProtocol
715 == USB_INTERFACE_PROTOCOL_MOUSE) {
717 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
718 if (!kone) {
719 hid_err(hdev, "can't alloc device descriptor\n");
720 return -ENOMEM;
722 hid_set_drvdata(hdev, kone);
724 retval = kone_init_kone_device_struct(usb_dev, kone);
725 if (retval) {
726 hid_err(hdev, "couldn't init struct kone_device\n");
727 goto exit_free;
730 retval = roccat_connect(kone_class, hdev);
731 if (retval < 0) {
732 hid_err(hdev, "couldn't init char dev\n");
733 /* be tolerant about not getting chrdev */
734 } else {
735 kone->roccat_claimed = 1;
736 kone->chrdev_minor = retval;
738 } else {
739 hid_set_drvdata(hdev, NULL);
742 return 0;
743 exit_free:
744 kfree(kone);
745 return retval;
748 static void kone_remove_specials(struct hid_device *hdev)
750 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
751 struct kone_device *kone;
753 if (intf->cur_altsetting->desc.bInterfaceProtocol
754 == USB_INTERFACE_PROTOCOL_MOUSE) {
755 kone = hid_get_drvdata(hdev);
756 if (kone->roccat_claimed)
757 roccat_disconnect(kone->chrdev_minor);
758 kfree(hid_get_drvdata(hdev));
762 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
764 int retval;
766 retval = hid_parse(hdev);
767 if (retval) {
768 hid_err(hdev, "parse failed\n");
769 goto exit;
772 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
773 if (retval) {
774 hid_err(hdev, "hw start failed\n");
775 goto exit;
778 retval = kone_init_specials(hdev);
779 if (retval) {
780 hid_err(hdev, "couldn't install mouse\n");
781 goto exit_stop;
784 return 0;
786 exit_stop:
787 hid_hw_stop(hdev);
788 exit:
789 return retval;
792 static void kone_remove(struct hid_device *hdev)
794 kone_remove_specials(hdev);
795 hid_hw_stop(hdev);
798 /* handle special events and keep actual profile and dpi values up to date */
799 static void kone_keep_values_up_to_date(struct kone_device *kone,
800 struct kone_mouse_event const *event)
802 switch (event->event) {
803 case kone_mouse_event_switch_profile:
804 case kone_mouse_event_osd_profile:
805 kone->actual_profile = event->value;
806 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
807 startup_dpi;
808 break;
809 case kone_mouse_event_switch_dpi:
810 case kone_mouse_event_osd_dpi:
811 kone->actual_dpi = event->value;
812 break;
816 static void kone_report_to_chrdev(struct kone_device const *kone,
817 struct kone_mouse_event const *event)
819 struct kone_roccat_report roccat_report;
821 switch (event->event) {
822 case kone_mouse_event_switch_profile:
823 case kone_mouse_event_switch_dpi:
824 case kone_mouse_event_osd_profile:
825 case kone_mouse_event_osd_dpi:
826 roccat_report.event = event->event;
827 roccat_report.value = event->value;
828 roccat_report.key = 0;
829 roccat_report_event(kone->chrdev_minor,
830 (uint8_t *)&roccat_report,
831 sizeof(struct kone_roccat_report));
832 break;
833 case kone_mouse_event_call_overlong_macro:
834 if (event->value == kone_keystroke_action_press) {
835 roccat_report.event = kone_mouse_event_call_overlong_macro;
836 roccat_report.value = kone->actual_profile;
837 roccat_report.key = event->macro_key;
838 roccat_report_event(kone->chrdev_minor,
839 (uint8_t *)&roccat_report,
840 sizeof(struct kone_roccat_report));
842 break;
848 * Is called for keyboard- and mousepart.
849 * Only mousepart gets informations about special events in its extended event
850 * structure.
852 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
853 u8 *data, int size)
855 struct kone_device *kone = hid_get_drvdata(hdev);
856 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
858 /* keyboard events are always processed by default handler */
859 if (size != sizeof(struct kone_mouse_event))
860 return 0;
863 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
864 * Pressed button is reported in each movement event.
865 * Workaround sends only one event per press.
867 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
868 memcpy(&kone->last_mouse_event, event,
869 sizeof(struct kone_mouse_event));
870 else
871 memset(&event->tilt, 0, 5);
873 kone_keep_values_up_to_date(kone, event);
875 if (kone->roccat_claimed)
876 kone_report_to_chrdev(kone, event);
878 return 0; /* always do further processing */
881 static const struct hid_device_id kone_devices[] = {
882 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
886 MODULE_DEVICE_TABLE(hid, kone_devices);
888 static struct hid_driver kone_driver = {
889 .name = "kone",
890 .id_table = kone_devices,
891 .probe = kone_probe,
892 .remove = kone_remove,
893 .raw_event = kone_raw_event
896 static int __init kone_init(void)
898 int retval;
900 /* class name has to be same as driver name */
901 kone_class = class_create(THIS_MODULE, "kone");
902 if (IS_ERR(kone_class))
903 return PTR_ERR(kone_class);
904 kone_class->dev_attrs = kone_attributes;
905 kone_class->dev_bin_attrs = kone_bin_attributes;
907 retval = hid_register_driver(&kone_driver);
908 if (retval)
909 class_destroy(kone_class);
910 return retval;
913 static void __exit kone_exit(void)
915 class_destroy(kone_class);
916 hid_unregister_driver(&kone_driver);
919 module_init(kone_init);
920 module_exit(kone_exit);
922 MODULE_AUTHOR("Stefan Achatz");
923 MODULE_DESCRIPTION("USB Roccat Kone driver");
924 MODULE_LICENSE("GPL v2");