HID: roccat: propagate special events of roccat hardware to userspace
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-roccat-kone.c
blob17f2dc04f883d0eb9462444a816d50ebf9b50b41
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 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>
39 #include "hid-ids.h"
40 #include "hid-roccat.h"
41 #include "hid-roccat-kone.h"
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 dev_err(&usb_dev->dev, "got retval %d when checking write\n",
99 *data);
100 kfree(data);
101 return -EIO;
106 * Reads settings from mouse and stores it in @buf
107 * @buf has to be alloced with GFP_KERNEL
108 * On success returns 0
109 * On failure returns errno
111 static int kone_get_settings(struct usb_device *usb_dev,
112 struct kone_settings *buf)
114 int len;
116 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
117 USB_REQ_CLEAR_FEATURE,
118 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
119 kone_command_settings, 0, buf,
120 sizeof(struct kone_settings), USB_CTRL_SET_TIMEOUT);
122 if (len != sizeof(struct kone_settings))
123 return -EIO;
125 return 0;
129 * Writes settings from @buf to mouse
130 * On success returns 0
131 * On failure returns errno
133 static int kone_set_settings(struct usb_device *usb_dev,
134 struct kone_settings const *settings)
136 int len;
138 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
139 USB_REQ_SET_CONFIGURATION,
140 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
141 kone_command_settings, 0, (char *)settings,
142 sizeof(struct kone_settings),
143 USB_CTRL_SET_TIMEOUT);
145 if (len != sizeof(struct kone_settings))
146 return -EIO;
148 if (kone_check_write(usb_dev))
149 return -EIO;
151 return 0;
155 * Reads profile data from mouse and stores it in @buf
156 * @number: profile number to read
157 * On success returns 0
158 * On failure returns errno
160 static int kone_get_profile(struct usb_device *usb_dev,
161 struct kone_profile *buf, int number)
163 int len;
165 if (number < 1 || number > 5)
166 return -EINVAL;
168 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
169 USB_REQ_CLEAR_FEATURE,
170 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
171 kone_command_profile, number, buf,
172 sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
174 if (len != sizeof(struct kone_profile))
175 return -EIO;
177 return 0;
181 * Writes profile data to mouse.
182 * @number: profile number to write
183 * On success returns 0
184 * On failure returns errno
186 static int kone_set_profile(struct usb_device *usb_dev,
187 struct kone_profile const *profile, int number)
189 int len;
191 if (number < 1 || number > 5)
192 return -EINVAL;
194 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
195 USB_REQ_SET_CONFIGURATION,
196 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
197 kone_command_profile, number, (char *)profile,
198 sizeof(struct kone_profile),
199 USB_CTRL_SET_TIMEOUT);
201 if (len != sizeof(struct kone_profile))
202 return len;
204 if (kone_check_write(usb_dev))
205 return -EIO;
207 return 0;
211 * Reads value of "fast-clip-weight" and stores it in @result
212 * On success returns 0
213 * On failure returns errno
215 static int kone_get_weight(struct usb_device *usb_dev, int *result)
217 int len;
218 uint8_t *data;
220 data = kmalloc(1, GFP_KERNEL);
221 if (!data)
222 return -ENOMEM;
224 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
225 USB_REQ_CLEAR_FEATURE,
226 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
227 kone_command_weight, 0, data, 1, USB_CTRL_SET_TIMEOUT);
229 if (len != 1) {
230 kfree(data);
231 return -EIO;
233 *result = (int)*data;
234 kfree(data);
235 return 0;
239 * Reads firmware_version of mouse and stores it in @result
240 * On success returns 0
241 * On failure returns errno
243 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
245 int len;
246 unsigned char *data;
248 data = kmalloc(2, GFP_KERNEL);
249 if (!data)
250 return -ENOMEM;
252 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
253 USB_REQ_CLEAR_FEATURE,
254 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
255 kone_command_firmware_version, 0, data, 2,
256 USB_CTRL_SET_TIMEOUT);
258 if (len != 2) {
259 kfree(data);
260 return -EIO;
262 *result = le16_to_cpu(*data);
263 kfree(data);
264 return 0;
267 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
268 struct bin_attribute *attr, char *buf,
269 loff_t off, size_t count) {
270 struct device *dev = container_of(kobj, struct device, kobj);
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, &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 = container_of(kobj, struct device, kobj);
295 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
296 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
297 int retval = 0, difference;
299 /* I need to get my data in one piece */
300 if (off != 0 || count != sizeof(struct kone_settings))
301 return -EINVAL;
303 mutex_lock(&kone->kone_lock);
304 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
305 if (difference) {
306 retval = kone_set_settings(usb_dev,
307 (struct kone_settings const *)buf);
308 if (!retval)
309 memcpy(&kone->settings, buf,
310 sizeof(struct kone_settings));
312 mutex_unlock(&kone->kone_lock);
314 if (retval)
315 return retval;
318 * If we get here, treat settings as okay and update actual values
319 * according to startup_profile
321 kone->actual_profile = kone->settings.startup_profile;
322 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
324 return sizeof(struct kone_settings);
327 static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
328 struct bin_attribute *attr, char *buf,
329 loff_t off, size_t count, int number) {
330 struct device *dev = container_of(kobj, struct device, kobj);
331 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
333 if (off >= sizeof(struct kone_profile))
334 return 0;
336 if (off + count > sizeof(struct kone_profile))
337 count = sizeof(struct kone_profile) - off;
339 mutex_lock(&kone->kone_lock);
340 memcpy(buf, &kone->profiles[number - 1], sizeof(struct kone_profile));
341 mutex_unlock(&kone->kone_lock);
343 return count;
346 static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
347 struct bin_attribute *attr, char *buf,
348 loff_t off, size_t count) {
349 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
352 static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
353 struct bin_attribute *attr, char *buf,
354 loff_t off, size_t count) {
355 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
358 static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
359 struct bin_attribute *attr, char *buf,
360 loff_t off, size_t count) {
361 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
364 static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
365 struct bin_attribute *attr, char *buf,
366 loff_t off, size_t count) {
367 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
370 static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
371 struct bin_attribute *attr, char *buf,
372 loff_t off, size_t count) {
373 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
376 /* Writes data only if different to stored data */
377 static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
378 struct bin_attribute *attr, char *buf,
379 loff_t off, size_t count, int number) {
380 struct device *dev = container_of(kobj, struct device, kobj);
381 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
382 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
383 struct kone_profile *profile;
384 int retval = 0, difference;
386 /* I need to get my data in one piece */
387 if (off != 0 || count != sizeof(struct kone_profile))
388 return -EINVAL;
390 profile = &kone->profiles[number - 1];
392 mutex_lock(&kone->kone_lock);
393 difference = memcmp(buf, profile, sizeof(struct kone_profile));
394 if (difference) {
395 retval = kone_set_profile(usb_dev,
396 (struct kone_profile const *)buf, number);
397 if (!retval)
398 memcpy(profile, buf, sizeof(struct kone_profile));
400 mutex_unlock(&kone->kone_lock);
402 if (retval)
403 return retval;
405 return sizeof(struct kone_profile);
408 static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
409 struct bin_attribute *attr, char *buf,
410 loff_t off, size_t count) {
411 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
414 static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
415 struct bin_attribute *attr, char *buf,
416 loff_t off, size_t count) {
417 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
420 static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
421 struct bin_attribute *attr, char *buf,
422 loff_t off, size_t count) {
423 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
426 static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
427 struct bin_attribute *attr, char *buf,
428 loff_t off, size_t count) {
429 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
432 static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
433 struct bin_attribute *attr, char *buf,
434 loff_t off, size_t count) {
435 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
438 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
442 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
445 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
449 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
452 /* weight is read each time, since we don't get informed when it's changed */
453 static ssize_t kone_sysfs_show_weight(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
457 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
458 int weight = 0;
459 int retval;
461 mutex_lock(&kone->kone_lock);
462 retval = kone_get_weight(usb_dev, &weight);
463 mutex_unlock(&kone->kone_lock);
465 if (retval)
466 return retval;
467 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
470 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
471 struct device_attribute *attr, char *buf)
473 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
474 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
477 static ssize_t kone_sysfs_show_tcu(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
481 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
484 static int kone_tcu_command(struct usb_device *usb_dev, int number)
486 int len;
487 char *value;
489 value = kmalloc(1, GFP_KERNEL);
490 if (!value)
491 return -ENOMEM;
493 *value = number;
495 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
496 USB_REQ_SET_CONFIGURATION,
497 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
498 kone_command_calibrate, 0, value, 1,
499 USB_CTRL_SET_TIMEOUT);
501 kfree(value);
502 return ((len != 1) ? -EIO : 0);
506 * Calibrating the tcu is the only action that changes settings data inside the
507 * mouse, so this data needs to be reread
509 static ssize_t kone_sysfs_set_tcu(struct device *dev,
510 struct device_attribute *attr, char const *buf, size_t size)
512 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
513 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
514 int retval;
515 unsigned long state;
517 retval = strict_strtoul(buf, 10, &state);
518 if (retval)
519 return retval;
521 if (state != 0 && state != 1)
522 return -EINVAL;
524 mutex_lock(&kone->kone_lock);
526 if (state == 1) { /* state activate */
527 retval = kone_tcu_command(usb_dev, 1);
528 if (retval)
529 goto exit_unlock;
530 retval = kone_tcu_command(usb_dev, 2);
531 if (retval)
532 goto exit_unlock;
533 ssleep(5); /* tcu needs this time for calibration */
534 retval = kone_tcu_command(usb_dev, 3);
535 if (retval)
536 goto exit_unlock;
537 retval = kone_tcu_command(usb_dev, 0);
538 if (retval)
539 goto exit_unlock;
540 retval = kone_tcu_command(usb_dev, 4);
541 if (retval)
542 goto exit_unlock;
544 * Kone needs this time to settle things.
545 * Reading settings too early will result in invalid data.
546 * Roccat's driver waits 1 sec, maybe this time could be
547 * shortened.
549 ssleep(1);
552 /* calibration changes values in settings, so reread */
553 retval = kone_get_settings(usb_dev, &kone->settings);
554 if (retval)
555 goto exit_no_settings;
557 /* only write settings back if activation state is different */
558 if (kone->settings.tcu != state) {
559 kone->settings.tcu = state;
560 kone_set_settings_checksum(&kone->settings);
562 retval = kone_set_settings(usb_dev, &kone->settings);
563 if (retval) {
564 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
566 * try to reread valid settings into buffer overwriting
567 * first error code
569 retval = kone_get_settings(usb_dev, &kone->settings);
570 if (retval)
571 goto exit_no_settings;
572 goto exit_unlock;
576 retval = size;
577 exit_no_settings:
578 dev_err(&usb_dev->dev, "couldn't read settings\n");
579 exit_unlock:
580 mutex_unlock(&kone->kone_lock);
581 return retval;
584 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
588 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
591 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
592 struct device_attribute *attr, char const *buf, size_t size)
594 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
595 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
596 int retval;
597 unsigned long new_startup_profile;
599 retval = strict_strtoul(buf, 10, &new_startup_profile);
600 if (retval)
601 return retval;
603 if (new_startup_profile < 1 || new_startup_profile > 5)
604 return -EINVAL;
606 mutex_lock(&kone->kone_lock);
608 kone->settings.startup_profile = new_startup_profile;
609 kone_set_settings_checksum(&kone->settings);
611 retval = kone_set_settings(usb_dev, &kone->settings);
613 mutex_unlock(&kone->kone_lock);
615 if (retval)
616 return retval;
618 /* changing the startup profile immediately activates this profile */
619 kone->actual_profile = new_startup_profile;
620 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
622 return size;
626 * This file is used by userland software to find devices that are handled by
627 * this driver. This provides a consistent way for actual and older kernels
628 * where this driver replaced usbhid instead of generic-usb.
629 * Driver capabilities are determined by version number.
631 static ssize_t kone_sysfs_show_driver_version(struct device *dev,
632 struct device_attribute *attr, char *buf)
634 return snprintf(buf, PAGE_SIZE, ROCCAT_KONE_DRIVER_VERSION "\n");
638 * Read actual dpi settings.
639 * Returns raw value for further processing. Refer to enum kone_polling_rates to
640 * get real value.
642 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
644 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
647 * The mouse can be equipped with one of four supplied weights from 5 to 20
648 * grams which are recognized and its value can be read out.
649 * This returns the raw value reported by the mouse for easy evaluation by
650 * software. Refer to enum kone_weights to get corresponding real weight.
652 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
655 * Prints firmware version stored in mouse as integer.
656 * The raw value reported by the mouse is returned for easy evaluation, to get
657 * the real version number the decimal point has to be shifted 2 positions to
658 * the left. E.g. a value of 138 means 1.38.
660 static DEVICE_ATTR(firmware_version, 0440,
661 kone_sysfs_show_firmware_version, NULL);
664 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
665 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
667 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
669 /* Prints and takes the number of the profile the mouse starts with */
670 static DEVICE_ATTR(startup_profile, 0660,
671 kone_sysfs_show_startup_profile,
672 kone_sysfs_set_startup_profile);
674 static DEVICE_ATTR(kone_driver_version, 0440,
675 kone_sysfs_show_driver_version, NULL);
677 static struct attribute *kone_attributes[] = {
678 &dev_attr_actual_dpi.attr,
679 &dev_attr_actual_profile.attr,
680 &dev_attr_weight.attr,
681 &dev_attr_firmware_version.attr,
682 &dev_attr_tcu.attr,
683 &dev_attr_startup_profile.attr,
684 &dev_attr_kone_driver_version.attr,
685 NULL
688 static struct attribute_group kone_attribute_group = {
689 .attrs = kone_attributes
692 static struct bin_attribute kone_settings_attr = {
693 .attr = { .name = "settings", .mode = 0660 },
694 .size = sizeof(struct kone_settings),
695 .read = kone_sysfs_read_settings,
696 .write = kone_sysfs_write_settings
699 static struct bin_attribute kone_profile1_attr = {
700 .attr = { .name = "profile1", .mode = 0660 },
701 .size = sizeof(struct kone_profile),
702 .read = kone_sysfs_read_profile1,
703 .write = kone_sysfs_write_profile1
706 static struct bin_attribute kone_profile2_attr = {
707 .attr = { .name = "profile2", .mode = 0660 },
708 .size = sizeof(struct kone_profile),
709 .read = kone_sysfs_read_profile2,
710 .write = kone_sysfs_write_profile2
713 static struct bin_attribute kone_profile3_attr = {
714 .attr = { .name = "profile3", .mode = 0660 },
715 .size = sizeof(struct kone_profile),
716 .read = kone_sysfs_read_profile3,
717 .write = kone_sysfs_write_profile3
720 static struct bin_attribute kone_profile4_attr = {
721 .attr = { .name = "profile4", .mode = 0660 },
722 .size = sizeof(struct kone_profile),
723 .read = kone_sysfs_read_profile4,
724 .write = kone_sysfs_write_profile4
727 static struct bin_attribute kone_profile5_attr = {
728 .attr = { .name = "profile5", .mode = 0660 },
729 .size = sizeof(struct kone_profile),
730 .read = kone_sysfs_read_profile5,
731 .write = kone_sysfs_write_profile5
734 static int kone_create_sysfs_attributes(struct usb_interface *intf)
736 int retval;
738 retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
739 if (retval)
740 goto exit_1;
742 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
743 if (retval)
744 goto exit_2;
746 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
747 if (retval)
748 goto exit_3;
750 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
751 if (retval)
752 goto exit_4;
754 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
755 if (retval)
756 goto exit_5;
758 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
759 if (retval)
760 goto exit_6;
762 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
763 if (retval)
764 goto exit_7;
766 return 0;
768 exit_7:
769 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
770 exit_6:
771 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
772 exit_5:
773 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
774 exit_4:
775 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
776 exit_3:
777 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
778 exit_2:
779 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
780 exit_1:
781 return retval;
784 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
786 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
787 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
788 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
789 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
790 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
791 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
792 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
795 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
796 struct kone_device *kone)
798 uint i;
799 int retval;
801 mutex_init(&kone->kone_lock);
803 for (i = 0; i < 5; ++i) {
804 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
805 if (retval)
806 return retval;
809 retval = kone_get_settings(usb_dev, &kone->settings);
810 if (retval)
811 return retval;
813 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
814 if (retval)
815 return retval;
817 kone->actual_profile = kone->settings.startup_profile;
818 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
820 return 0;
824 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
825 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
826 * module.
827 * Secial behaviour is bound only to mousepart since only mouseevents contain
828 * additional notifications.
830 static int kone_init_specials(struct hid_device *hdev)
832 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
833 struct usb_device *usb_dev = interface_to_usbdev(intf);
834 struct kone_device *kone;
835 int retval;
837 if (intf->cur_altsetting->desc.bInterfaceProtocol
838 == USB_INTERFACE_PROTOCOL_MOUSE) {
840 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
841 if (!kone) {
842 dev_err(&hdev->dev, "can't alloc device descriptor\n");
843 return -ENOMEM;
845 hid_set_drvdata(hdev, kone);
847 retval = kone_init_kone_device_struct(usb_dev, kone);
848 if (retval) {
849 dev_err(&hdev->dev,
850 "couldn't init struct kone_device\n");
851 goto exit_free;
854 retval = roccat_connect(hdev);
855 if (retval < 0) {
856 dev_err(&hdev->dev, "couldn't init char dev\n");
857 /* be tolerant about not getting chrdev */
858 } else {
859 kone->roccat_claimed = 1;
860 kone->chrdev_minor = retval;
863 retval = kone_create_sysfs_attributes(intf);
864 if (retval) {
865 dev_err(&hdev->dev, "cannot create sysfs files\n");
866 goto exit_free;
868 } else {
869 hid_set_drvdata(hdev, NULL);
872 return 0;
873 exit_free:
874 kfree(kone);
875 return retval;
879 static void kone_remove_specials(struct hid_device *hdev)
881 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
882 struct kone_device *kone;
884 if (intf->cur_altsetting->desc.bInterfaceProtocol
885 == USB_INTERFACE_PROTOCOL_MOUSE) {
886 kone_remove_sysfs_attributes(intf);
887 kone = hid_get_drvdata(hdev);
888 if (kone->roccat_claimed)
889 roccat_disconnect(kone->chrdev_minor);
890 kfree(hid_get_drvdata(hdev));
894 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
896 int retval;
898 retval = hid_parse(hdev);
899 if (retval) {
900 dev_err(&hdev->dev, "parse failed\n");
901 goto exit;
904 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
905 if (retval) {
906 dev_err(&hdev->dev, "hw start failed\n");
907 goto exit;
910 retval = kone_init_specials(hdev);
911 if (retval) {
912 dev_err(&hdev->dev, "couldn't install mouse\n");
913 goto exit_stop;
916 return 0;
918 exit_stop:
919 hid_hw_stop(hdev);
920 exit:
921 return retval;
924 static void kone_remove(struct hid_device *hdev)
926 kone_remove_specials(hdev);
927 hid_hw_stop(hdev);
930 /* handle special events and keep actual profile and dpi values up to date */
931 static void kone_keep_values_up_to_date(struct kone_device *kone,
932 struct kone_mouse_event const *event)
934 switch (event->event) {
935 case kone_mouse_event_switch_profile:
936 case kone_mouse_event_osd_profile:
937 kone->actual_profile = event->value;
938 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
939 startup_dpi;
940 break;
941 case kone_mouse_event_switch_dpi:
942 case kone_mouse_event_osd_dpi:
943 kone->actual_dpi = event->value;
944 break;
948 static void kone_report_to_chrdev(struct kone_device const *kone,
949 struct kone_mouse_event const *event)
951 struct kone_roccat_report roccat_report;
953 switch (event->event) {
954 case kone_mouse_event_switch_profile:
955 case kone_mouse_event_switch_dpi:
956 case kone_mouse_event_osd_profile:
957 case kone_mouse_event_osd_dpi:
958 roccat_report.event = event->event;
959 roccat_report.value = event->value;
960 roccat_report.key = 0;
961 roccat_report_event(kone->chrdev_minor,
962 (uint8_t *)&roccat_report,
963 sizeof(struct kone_roccat_report));
964 break;
965 case kone_mouse_event_call_overlong_macro:
966 if (event->value == kone_keystroke_action_press) {
967 roccat_report.event = kone_mouse_event_call_overlong_macro;
968 roccat_report.value = kone->actual_profile;
969 roccat_report.key = event->macro_key;
970 roccat_report_event(kone->chrdev_minor,
971 (uint8_t *)&roccat_report,
972 sizeof(struct kone_roccat_report));
974 break;
980 * Is called for keyboard- and mousepart.
981 * Only mousepart gets informations about special events in its extended event
982 * structure.
984 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
985 u8 *data, int size)
987 struct kone_device *kone = hid_get_drvdata(hdev);
988 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
990 /* keyboard events are always processed by default handler */
991 if (size != sizeof(struct kone_mouse_event))
992 return 0;
995 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
996 * Pressed button is reported in each movement event.
997 * Workaround sends only one event per press.
999 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
1000 memcpy(&kone->last_mouse_event, event,
1001 sizeof(struct kone_mouse_event));
1002 else
1003 memset(&event->tilt, 0, 5);
1005 kone_keep_values_up_to_date(kone, event);
1007 if (kone->roccat_claimed)
1008 kone_report_to_chrdev(kone, event);
1010 return 0; /* always do further processing */
1013 static const struct hid_device_id kone_devices[] = {
1014 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1018 MODULE_DEVICE_TABLE(hid, kone_devices);
1020 static struct hid_driver kone_driver = {
1021 .name = "kone",
1022 .id_table = kone_devices,
1023 .probe = kone_probe,
1024 .remove = kone_remove,
1025 .raw_event = kone_raw_event
1028 static int __init kone_init(void)
1030 return hid_register_driver(&kone_driver);
1033 static void __exit kone_exit(void)
1035 hid_unregister_driver(&kone_driver);
1038 module_init(kone_init);
1039 module_exit(kone_exit);
1041 MODULE_AUTHOR("Stefan Achatz");
1042 MODULE_DESCRIPTION("USB Roccat Kone driver");
1043 MODULE_LICENSE("GPL v2");