GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / hid / hid-roccat-kone.c
blob38d66fd232f99de71ccbd27bffd7a05af5131851
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 void kone_set_settings_checksum(struct kone_settings *settings)
40 uint16_t checksum = 0;
41 unsigned char *address = (unsigned char *)settings;
42 int i;
44 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
45 checksum += *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)
56 int len;
57 unsigned char *data;
59 data = kmalloc(1, GFP_KERNEL);
60 if (!data)
61 return -ENOMEM;
63 do {
65 * Mouse needs 50 msecs until it says ok, but there are
66 * 30 more msecs needed for next write to work.
68 msleep(80);
70 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
71 USB_REQ_CLEAR_FEATURE,
72 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
73 USB_DIR_IN,
74 kone_command_confirm_write, 0, data, 1,
75 USB_CTRL_SET_TIMEOUT);
77 if (len != 1) {
78 kfree(data);
79 return -EIO;
83 * value of 3 seems to mean something like
84 * "not finished yet, but it looks good"
85 * So check again after a moment.
87 } while (*data == 3);
89 if (*data == 1) { /* everything alright */
90 kfree(data);
91 return 0;
92 } else { /* unknown answer */
93 dev_err(&usb_dev->dev, "got retval %d when checking write\n",
94 *data);
95 kfree(data);
96 return -EIO;
101 * Reads settings from mouse and stores it in @buf
102 * @buf has to be alloced with GFP_KERNEL
103 * On success returns 0
104 * On failure returns errno
106 static int kone_get_settings(struct usb_device *usb_dev,
107 struct kone_settings *buf)
109 int len;
111 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
112 USB_REQ_CLEAR_FEATURE,
113 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
114 kone_command_settings, 0, buf,
115 sizeof(struct kone_settings), USB_CTRL_SET_TIMEOUT);
117 if (len != sizeof(struct kone_settings))
118 return -EIO;
120 return 0;
124 * Writes settings from @buf to mouse
125 * On success returns 0
126 * On failure returns errno
128 static int kone_set_settings(struct usb_device *usb_dev,
129 struct kone_settings const *settings)
131 int len;
133 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
134 USB_REQ_SET_CONFIGURATION,
135 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
136 kone_command_settings, 0, (char *)settings,
137 sizeof(struct kone_settings),
138 USB_CTRL_SET_TIMEOUT);
140 if (len != sizeof(struct kone_settings))
141 return -EIO;
143 if (kone_check_write(usb_dev))
144 return -EIO;
146 return 0;
150 * Reads profile data from mouse and stores it in @buf
151 * @number: profile number to read
152 * On success returns 0
153 * On failure returns errno
155 static int kone_get_profile(struct usb_device *usb_dev,
156 struct kone_profile *buf, int number)
158 int len;
160 if (number < 1 || number > 5)
161 return -EINVAL;
163 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
164 USB_REQ_CLEAR_FEATURE,
165 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
166 kone_command_profile, number, buf,
167 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
169 if (len != sizeof(struct kone_profile))
170 return -EIO;
172 return 0;
176 * Writes profile data to mouse.
177 * @number: profile number to write
178 * On success returns 0
179 * On failure returns errno
181 static int kone_set_profile(struct usb_device *usb_dev,
182 struct kone_profile const *profile, int number)
184 int len;
186 if (number < 1 || number > 5)
187 return -EINVAL;
189 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
190 USB_REQ_SET_CONFIGURATION,
191 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
192 kone_command_profile, number, (char *)profile,
193 sizeof(struct kone_profile),
194 USB_CTRL_SET_TIMEOUT);
196 if (len != sizeof(struct kone_profile))
197 return len;
199 if (kone_check_write(usb_dev))
200 return -EIO;
202 return 0;
206 * Reads value of "fast-clip-weight" and stores it in @result
207 * On success returns 0
208 * On failure returns errno
210 static int kone_get_weight(struct usb_device *usb_dev, int *result)
212 int len;
213 uint8_t *data;
215 data = kmalloc(1, GFP_KERNEL);
216 if (!data)
217 return -ENOMEM;
219 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
220 USB_REQ_CLEAR_FEATURE,
221 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
222 kone_command_weight, 0, data, 1, USB_CTRL_SET_TIMEOUT);
224 if (len != 1) {
225 kfree(data);
226 return -EIO;
228 *result = (int)*data;
229 kfree(data);
230 return 0;
234 * Reads firmware_version of mouse and stores it in @result
235 * On success returns 0
236 * On failure returns errno
238 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
240 int len;
241 unsigned char *data;
243 data = kmalloc(2, GFP_KERNEL);
244 if (!data)
245 return -ENOMEM;
247 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
248 USB_REQ_CLEAR_FEATURE,
249 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
250 kone_command_firmware_version, 0, data, 2,
251 USB_CTRL_SET_TIMEOUT);
253 if (len != 2) {
254 kfree(data);
255 return -EIO;
257 *result = le16_to_cpu(*data);
258 kfree(data);
259 return 0;
262 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
263 struct bin_attribute *attr, char *buf,
264 loff_t off, size_t count) {
265 struct device *dev = container_of(kobj, struct device, kobj);
266 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
268 if (off >= sizeof(struct kone_settings))
269 return 0;
271 if (off + count > sizeof(struct kone_settings))
272 count = sizeof(struct kone_settings) - off;
274 mutex_lock(&kone->kone_lock);
275 memcpy(buf, ((char const *)&kone->settings) + off, count);
276 mutex_unlock(&kone->kone_lock);
278 return count;
282 * Writing settings automatically activates startup_profile.
283 * This function keeps values in kone_device up to date and assumes that in
284 * case of error the old data is still valid
286 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
287 struct bin_attribute *attr, char *buf,
288 loff_t off, size_t count) {
289 struct device *dev = container_of(kobj, struct device, kobj);
290 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
291 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
292 int retval = 0, difference;
294 /* I need to get my data in one piece */
295 if (off != 0 || count != sizeof(struct kone_settings))
296 return -EINVAL;
298 mutex_lock(&kone->kone_lock);
299 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
300 if (difference) {
301 retval = kone_set_settings(usb_dev,
302 (struct kone_settings const *)buf);
303 if (!retval)
304 memcpy(&kone->settings, buf,
305 sizeof(struct kone_settings));
307 mutex_unlock(&kone->kone_lock);
309 if (retval)
310 return retval;
313 * If we get here, treat settings as okay and update actual values
314 * according to startup_profile
316 kone->actual_profile = kone->settings.startup_profile;
317 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
319 return sizeof(struct kone_settings);
322 static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
323 struct bin_attribute *attr, char *buf,
324 loff_t off, size_t count, int number) {
325 struct device *dev = container_of(kobj, struct device, kobj);
326 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
328 if (off >= sizeof(struct kone_profile))
329 return 0;
331 if (off + count > sizeof(struct kone_profile))
332 count = sizeof(struct kone_profile) - off;
334 mutex_lock(&kone->kone_lock);
335 memcpy(buf, ((char const *)&kone->profiles[number - 1]) + off, count);
336 mutex_unlock(&kone->kone_lock);
338 return count;
341 static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
342 struct bin_attribute *attr, char *buf,
343 loff_t off, size_t count) {
344 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
347 static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
348 struct bin_attribute *attr, char *buf,
349 loff_t off, size_t count) {
350 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
353 static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
354 struct bin_attribute *attr, char *buf,
355 loff_t off, size_t count) {
356 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
359 static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
360 struct bin_attribute *attr, char *buf,
361 loff_t off, size_t count) {
362 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
365 static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
366 struct bin_attribute *attr, char *buf,
367 loff_t off, size_t count) {
368 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
371 /* Writes data only if different to stored data */
372 static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
373 struct bin_attribute *attr, char *buf,
374 loff_t off, size_t count, int number) {
375 struct device *dev = container_of(kobj, struct device, kobj);
376 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
377 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
378 struct kone_profile *profile;
379 int retval = 0, difference;
381 /* I need to get my data in one piece */
382 if (off != 0 || count != sizeof(struct kone_profile))
383 return -EINVAL;
385 profile = &kone->profiles[number - 1];
387 mutex_lock(&kone->kone_lock);
388 difference = memcmp(buf, profile, sizeof(struct kone_profile));
389 if (difference) {
390 retval = kone_set_profile(usb_dev,
391 (struct kone_profile const *)buf, number);
392 if (!retval)
393 memcpy(profile, buf, sizeof(struct kone_profile));
395 mutex_unlock(&kone->kone_lock);
397 if (retval)
398 return retval;
400 return sizeof(struct kone_profile);
403 static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
404 struct bin_attribute *attr, char *buf,
405 loff_t off, size_t count) {
406 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
409 static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
410 struct bin_attribute *attr, char *buf,
411 loff_t off, size_t count) {
412 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
415 static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
416 struct bin_attribute *attr, char *buf,
417 loff_t off, size_t count) {
418 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
421 static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
422 struct bin_attribute *attr, char *buf,
423 loff_t off, size_t count) {
424 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
427 static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
428 struct bin_attribute *attr, char *buf,
429 loff_t off, size_t count) {
430 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
433 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
437 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
440 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
444 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
447 /* weight is read each time, since we don't get informed when it's changed */
448 static ssize_t kone_sysfs_show_weight(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
452 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
453 int weight = 0;
454 int retval;
456 mutex_lock(&kone->kone_lock);
457 retval = kone_get_weight(usb_dev, &weight);
458 mutex_unlock(&kone->kone_lock);
460 if (retval)
461 return retval;
462 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
465 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
469 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
472 static ssize_t kone_sysfs_show_tcu(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
476 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
479 static int kone_tcu_command(struct usb_device *usb_dev, int number)
481 int len;
482 char *value;
484 value = kmalloc(1, GFP_KERNEL);
485 if (!value)
486 return -ENOMEM;
488 *value = number;
490 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
491 USB_REQ_SET_CONFIGURATION,
492 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
493 kone_command_calibrate, 0, value, 1,
494 USB_CTRL_SET_TIMEOUT);
496 kfree(value);
497 return ((len != 1) ? -EIO : 0);
501 * Calibrating the tcu is the only action that changes settings data inside the
502 * mouse, so this data needs to be reread
504 static ssize_t kone_sysfs_set_tcu(struct device *dev,
505 struct device_attribute *attr, char const *buf, size_t size)
507 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
508 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
509 int retval;
510 unsigned long state;
512 retval = strict_strtoul(buf, 10, &state);
513 if (retval)
514 return retval;
516 if (state != 0 && state != 1)
517 return -EINVAL;
519 mutex_lock(&kone->kone_lock);
521 if (state == 1) { /* state activate */
522 retval = kone_tcu_command(usb_dev, 1);
523 if (retval)
524 goto exit_unlock;
525 retval = kone_tcu_command(usb_dev, 2);
526 if (retval)
527 goto exit_unlock;
528 ssleep(5); /* tcu needs this time for calibration */
529 retval = kone_tcu_command(usb_dev, 3);
530 if (retval)
531 goto exit_unlock;
532 retval = kone_tcu_command(usb_dev, 0);
533 if (retval)
534 goto exit_unlock;
535 retval = kone_tcu_command(usb_dev, 4);
536 if (retval)
537 goto exit_unlock;
539 * Kone needs this time to settle things.
540 * Reading settings too early will result in invalid data.
541 * Roccat's driver waits 1 sec, maybe this time could be
542 * shortened.
544 ssleep(1);
547 /* calibration changes values in settings, so reread */
548 retval = kone_get_settings(usb_dev, &kone->settings);
549 if (retval)
550 goto exit_no_settings;
552 /* only write settings back if activation state is different */
553 if (kone->settings.tcu != state) {
554 kone->settings.tcu = state;
555 kone_set_settings_checksum(&kone->settings);
557 retval = kone_set_settings(usb_dev, &kone->settings);
558 if (retval) {
559 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
561 * try to reread valid settings into buffer overwriting
562 * first error code
564 retval = kone_get_settings(usb_dev, &kone->settings);
565 if (retval)
566 goto exit_no_settings;
567 goto exit_unlock;
571 retval = size;
572 exit_no_settings:
573 dev_err(&usb_dev->dev, "couldn't read settings\n");
574 exit_unlock:
575 mutex_unlock(&kone->kone_lock);
576 return retval;
579 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
580 struct device_attribute *attr, char *buf)
582 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
583 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
586 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
587 struct device_attribute *attr, char const *buf, size_t size)
589 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
590 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
591 int retval;
592 unsigned long new_startup_profile;
594 retval = strict_strtoul(buf, 10, &new_startup_profile);
595 if (retval)
596 return retval;
598 if (new_startup_profile < 1 || new_startup_profile > 5)
599 return -EINVAL;
601 mutex_lock(&kone->kone_lock);
603 kone->settings.startup_profile = new_startup_profile;
604 kone_set_settings_checksum(&kone->settings);
606 retval = kone_set_settings(usb_dev, &kone->settings);
608 mutex_unlock(&kone->kone_lock);
610 if (retval)
611 return retval;
613 /* changing the startup profile immediately activates this profile */
614 kone->actual_profile = new_startup_profile;
615 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
617 return size;
621 * Read actual dpi settings.
622 * Returns raw value for further processing. Refer to enum kone_polling_rates to
623 * get real value.
625 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
627 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
630 * The mouse can be equipped with one of four supplied weights from 5 to 20
631 * grams which are recognized and its value can be read out.
632 * This returns the raw value reported by the mouse for easy evaluation by
633 * software. Refer to enum kone_weights to get corresponding real weight.
635 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
638 * Prints firmware version stored in mouse as integer.
639 * The raw value reported by the mouse is returned for easy evaluation, to get
640 * the real version number the decimal point has to be shifted 2 positions to
641 * the left. E.g. a value of 138 means 1.38.
643 static DEVICE_ATTR(firmware_version, 0440,
644 kone_sysfs_show_firmware_version, NULL);
647 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
648 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
650 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
652 /* Prints and takes the number of the profile the mouse starts with */
653 static DEVICE_ATTR(startup_profile, 0660,
654 kone_sysfs_show_startup_profile,
655 kone_sysfs_set_startup_profile);
657 static struct attribute *kone_attributes[] = {
658 &dev_attr_actual_dpi.attr,
659 &dev_attr_actual_profile.attr,
660 &dev_attr_weight.attr,
661 &dev_attr_firmware_version.attr,
662 &dev_attr_tcu.attr,
663 &dev_attr_startup_profile.attr,
664 NULL
667 static struct attribute_group kone_attribute_group = {
668 .attrs = kone_attributes
671 static struct bin_attribute kone_settings_attr = {
672 .attr = { .name = "settings", .mode = 0660 },
673 .size = sizeof(struct kone_settings),
674 .read = kone_sysfs_read_settings,
675 .write = kone_sysfs_write_settings
678 static struct bin_attribute kone_profile1_attr = {
679 .attr = { .name = "profile1", .mode = 0660 },
680 .size = sizeof(struct kone_profile),
681 .read = kone_sysfs_read_profile1,
682 .write = kone_sysfs_write_profile1
685 static struct bin_attribute kone_profile2_attr = {
686 .attr = { .name = "profile2", .mode = 0660 },
687 .size = sizeof(struct kone_profile),
688 .read = kone_sysfs_read_profile2,
689 .write = kone_sysfs_write_profile2
692 static struct bin_attribute kone_profile3_attr = {
693 .attr = { .name = "profile3", .mode = 0660 },
694 .size = sizeof(struct kone_profile),
695 .read = kone_sysfs_read_profile3,
696 .write = kone_sysfs_write_profile3
699 static struct bin_attribute kone_profile4_attr = {
700 .attr = { .name = "profile4", .mode = 0660 },
701 .size = sizeof(struct kone_profile),
702 .read = kone_sysfs_read_profile4,
703 .write = kone_sysfs_write_profile4
706 static struct bin_attribute kone_profile5_attr = {
707 .attr = { .name = "profile5", .mode = 0660 },
708 .size = sizeof(struct kone_profile),
709 .read = kone_sysfs_read_profile5,
710 .write = kone_sysfs_write_profile5
713 static int kone_create_sysfs_attributes(struct usb_interface *intf)
715 int retval;
717 retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
718 if (retval)
719 goto exit_1;
721 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
722 if (retval)
723 goto exit_2;
725 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
726 if (retval)
727 goto exit_3;
729 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
730 if (retval)
731 goto exit_4;
733 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
734 if (retval)
735 goto exit_5;
737 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
738 if (retval)
739 goto exit_6;
741 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
742 if (retval)
743 goto exit_7;
745 return 0;
747 exit_7:
748 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
749 exit_6:
750 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
751 exit_5:
752 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
753 exit_4:
754 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
755 exit_3:
756 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
757 exit_2:
758 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
759 exit_1:
760 return retval;
763 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
765 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
766 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
767 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
768 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
769 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
770 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
771 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
774 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
775 struct kone_device *kone)
777 uint i;
778 int retval;
780 mutex_init(&kone->kone_lock);
782 for (i = 0; i < 5; ++i) {
783 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
784 if (retval)
785 return retval;
788 retval = kone_get_settings(usb_dev, &kone->settings);
789 if (retval)
790 return retval;
792 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
793 if (retval)
794 return retval;
796 kone->actual_profile = kone->settings.startup_profile;
797 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
799 return 0;
803 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
804 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
805 * module.
806 * Secial behaviour is bound only to mousepart since only mouseevents contain
807 * additional notifications.
809 static int kone_init_specials(struct hid_device *hdev)
811 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
812 struct usb_device *usb_dev = interface_to_usbdev(intf);
813 struct kone_device *kone;
814 int retval;
816 if (intf->cur_altsetting->desc.bInterfaceProtocol
817 == USB_INTERFACE_PROTOCOL_MOUSE) {
819 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
820 if (!kone) {
821 dev_err(&hdev->dev, "can't alloc device descriptor\n");
822 return -ENOMEM;
824 hid_set_drvdata(hdev, kone);
826 retval = kone_init_kone_device_struct(usb_dev, kone);
827 if (retval) {
828 dev_err(&hdev->dev,
829 "couldn't init struct kone_device\n");
830 goto exit_free;
833 retval = roccat_connect(hdev);
834 if (retval < 0) {
835 dev_err(&hdev->dev, "couldn't init char dev\n");
836 /* be tolerant about not getting chrdev */
837 } else {
838 kone->roccat_claimed = 1;
839 kone->chrdev_minor = retval;
842 retval = kone_create_sysfs_attributes(intf);
843 if (retval) {
844 dev_err(&hdev->dev, "cannot create sysfs files\n");
845 goto exit_free;
847 } else {
848 hid_set_drvdata(hdev, NULL);
851 return 0;
852 exit_free:
853 kfree(kone);
854 return retval;
858 static void kone_remove_specials(struct hid_device *hdev)
860 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
861 struct kone_device *kone;
863 if (intf->cur_altsetting->desc.bInterfaceProtocol
864 == USB_INTERFACE_PROTOCOL_MOUSE) {
865 kone_remove_sysfs_attributes(intf);
866 kone = hid_get_drvdata(hdev);
867 if (kone->roccat_claimed)
868 roccat_disconnect(kone->chrdev_minor);
869 kfree(hid_get_drvdata(hdev));
873 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
875 int retval;
877 retval = hid_parse(hdev);
878 if (retval) {
879 dev_err(&hdev->dev, "parse failed\n");
880 goto exit;
883 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
884 if (retval) {
885 dev_err(&hdev->dev, "hw start failed\n");
886 goto exit;
889 retval = kone_init_specials(hdev);
890 if (retval) {
891 dev_err(&hdev->dev, "couldn't install mouse\n");
892 goto exit_stop;
895 return 0;
897 exit_stop:
898 hid_hw_stop(hdev);
899 exit:
900 return retval;
903 static void kone_remove(struct hid_device *hdev)
905 kone_remove_specials(hdev);
906 hid_hw_stop(hdev);
909 /* handle special events and keep actual profile and dpi values up to date */
910 static void kone_keep_values_up_to_date(struct kone_device *kone,
911 struct kone_mouse_event const *event)
913 switch (event->event) {
914 case kone_mouse_event_switch_profile:
915 case kone_mouse_event_osd_profile:
916 kone->actual_profile = event->value;
917 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
918 startup_dpi;
919 break;
920 case kone_mouse_event_switch_dpi:
921 case kone_mouse_event_osd_dpi:
922 kone->actual_dpi = event->value;
923 break;
927 static void kone_report_to_chrdev(struct kone_device const *kone,
928 struct kone_mouse_event const *event)
930 struct kone_roccat_report roccat_report;
932 switch (event->event) {
933 case kone_mouse_event_switch_profile:
934 case kone_mouse_event_switch_dpi:
935 case kone_mouse_event_osd_profile:
936 case kone_mouse_event_osd_dpi:
937 roccat_report.event = event->event;
938 roccat_report.value = event->value;
939 roccat_report.key = 0;
940 roccat_report_event(kone->chrdev_minor,
941 (uint8_t *)&roccat_report,
942 sizeof(struct kone_roccat_report));
943 break;
944 case kone_mouse_event_call_overlong_macro:
945 if (event->value == kone_keystroke_action_press) {
946 roccat_report.event = kone_mouse_event_call_overlong_macro;
947 roccat_report.value = kone->actual_profile;
948 roccat_report.key = event->macro_key;
949 roccat_report_event(kone->chrdev_minor,
950 (uint8_t *)&roccat_report,
951 sizeof(struct kone_roccat_report));
953 break;
959 * Is called for keyboard- and mousepart.
960 * Only mousepart gets informations about special events in its extended event
961 * structure.
963 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
964 u8 *data, int size)
966 struct kone_device *kone = hid_get_drvdata(hdev);
967 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
969 /* keyboard events are always processed by default handler */
970 if (size != sizeof(struct kone_mouse_event))
971 return 0;
973 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
974 memcpy(&kone->last_mouse_event, event,
975 sizeof(struct kone_mouse_event));
976 else
977 memset(&event->tilt, 0, 5);
979 kone_keep_values_up_to_date(kone, event);
981 if (kone->roccat_claimed)
982 kone_report_to_chrdev(kone, event);
984 return 0; /* always do further processing */
987 static const struct hid_device_id kone_devices[] = {
988 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
992 MODULE_DEVICE_TABLE(hid, kone_devices);
994 static struct hid_driver kone_driver = {
995 .name = "kone",
996 .id_table = kone_devices,
997 .probe = kone_probe,
998 .remove = kone_remove,
999 .raw_event = kone_raw_event
1002 static int __init kone_init(void)
1004 return hid_register_driver(&kone_driver);
1007 static void __exit kone_exit(void)
1009 hid_unregister_driver(&kone_driver);
1012 module_init(kone_init);
1013 module_exit(kone_exit);
1015 MODULE_AUTHOR("Stefan Achatz");
1016 MODULE_DESCRIPTION("USB Roccat Kone driver");
1017 MODULE_LICENSE("GPL v2");