HID: fix hid-roccat-kone for bin_attr API change
[linux-2.6.git] / drivers / hid / hid-roccat-kone.c
blobb6371d3fe0f9d2b612960fa23ee879f0bb85e0ba
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-kone.h"
42 static void kone_set_settings_checksum(struct kone_settings *settings)
44 uint16_t checksum = 0;
45 unsigned char *address = (unsigned char *)settings;
46 int i;
48 for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
49 checksum += *address;
50 settings->checksum = cpu_to_le16(checksum);
54 * Checks success after writing data to mouse
55 * On success returns 0
56 * On failure returns errno
58 static int kone_check_write(struct usb_device *usb_dev)
60 int len;
61 unsigned char *data;
63 data = kmalloc(1, GFP_KERNEL);
64 if (!data)
65 return -ENOMEM;
67 do {
69 * Mouse needs 50 msecs until it says ok, but there are
70 * 30 more msecs needed for next write to work.
72 msleep(80);
74 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
75 USB_REQ_CLEAR_FEATURE,
76 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
77 USB_DIR_IN,
78 kone_command_confirm_write, 0, data, 1,
79 USB_CTRL_SET_TIMEOUT);
81 if (len != 1) {
82 kfree(data);
83 return -EIO;
87 * value of 3 seems to mean something like
88 * "not finished yet, but it looks good"
89 * So check again after a moment.
91 } while (*data == 3);
93 if (*data == 1) { /* everything alright */
94 kfree(data);
95 return 0;
96 } else { /* unknown answer */
97 dev_err(&usb_dev->dev, "got retval %d when checking write\n",
98 *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 = container_of(kobj, struct device, kobj);
270 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
272 if (off >= sizeof(struct kone_settings))
273 return 0;
275 if (off + count > sizeof(struct kone_settings))
276 count = sizeof(struct kone_settings) - off;
278 mutex_lock(&kone->kone_lock);
279 memcpy(buf, &kone->settings + off, count);
280 mutex_unlock(&kone->kone_lock);
282 return count;
286 * Writing settings automatically activates startup_profile.
287 * This function keeps values in kone_device up to date and assumes that in
288 * case of error the old data is still valid
290 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
291 struct bin_attribute *attr, char *buf,
292 loff_t off, size_t count) {
293 struct device *dev = container_of(kobj, struct device, kobj);
294 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
295 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
296 int retval = 0, difference;
298 /* I need to get my data in one piece */
299 if (off != 0 || count != sizeof(struct kone_settings))
300 return -EINVAL;
302 mutex_lock(&kone->kone_lock);
303 difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
304 if (difference) {
305 retval = kone_set_settings(usb_dev,
306 (struct kone_settings const *)buf);
307 if (!retval)
308 memcpy(&kone->settings, buf,
309 sizeof(struct kone_settings));
311 mutex_unlock(&kone->kone_lock);
313 if (retval)
314 return retval;
317 * If we get here, treat settings as okay and update actual values
318 * according to startup_profile
320 kone->actual_profile = kone->settings.startup_profile;
321 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
323 return sizeof(struct kone_settings);
326 static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
327 struct bin_attribute *attr, char *buf,
328 loff_t off, size_t count, int number) {
329 struct device *dev = container_of(kobj, struct device, kobj);
330 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
332 if (off >= sizeof(struct kone_profile))
333 return 0;
335 if (off + count > sizeof(struct kone_profile))
336 count = sizeof(struct kone_profile) - off;
338 mutex_lock(&kone->kone_lock);
339 memcpy(buf, &kone->profiles[number - 1], sizeof(struct kone_profile));
340 mutex_unlock(&kone->kone_lock);
342 return count;
345 static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
346 struct bin_attribute *attr, char *buf,
347 loff_t off, size_t count) {
348 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
351 static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
352 struct bin_attribute *attr, char *buf,
353 loff_t off, size_t count) {
354 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
357 static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
358 struct bin_attribute *attr, char *buf,
359 loff_t off, size_t count) {
360 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
363 static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
364 struct bin_attribute *attr, char *buf,
365 loff_t off, size_t count) {
366 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
369 static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
370 struct bin_attribute *attr, char *buf,
371 loff_t off, size_t count) {
372 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
375 /* Writes data only if different to stored data */
376 static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
377 struct bin_attribute *attr, char *buf,
378 loff_t off, size_t count, int number) {
379 struct device *dev = container_of(kobj, struct device, kobj);
380 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
381 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
382 struct kone_profile *profile;
383 int retval = 0, difference;
385 /* I need to get my data in one piece */
386 if (off != 0 || count != sizeof(struct kone_profile))
387 return -EINVAL;
389 profile = &kone->profiles[number - 1];
391 mutex_lock(&kone->kone_lock);
392 difference = memcmp(buf, profile, sizeof(struct kone_profile));
393 if (difference) {
394 retval = kone_set_profile(usb_dev,
395 (struct kone_profile const *)buf, number);
396 if (!retval)
397 memcpy(profile, buf, sizeof(struct kone_profile));
399 mutex_unlock(&kone->kone_lock);
401 if (retval)
402 return retval;
404 return sizeof(struct kone_profile);
407 static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
408 struct bin_attribute *attr, char *buf,
409 loff_t off, size_t count) {
410 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
413 static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
414 struct bin_attribute *attr, char *buf,
415 loff_t off, size_t count) {
416 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
419 static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
420 struct bin_attribute *attr, char *buf,
421 loff_t off, size_t count) {
422 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
425 static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
426 struct bin_attribute *attr, char *buf,
427 loff_t off, size_t count) {
428 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
431 static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
432 struct bin_attribute *attr, char *buf,
433 loff_t off, size_t count) {
434 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
437 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
438 struct device_attribute *attr, char *buf)
440 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
441 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
444 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
448 return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
451 /* weight is read each time, since we don't get informed when it's changed */
452 static ssize_t kone_sysfs_show_weight(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
456 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
457 int weight = 0;
458 int retval;
460 mutex_lock(&kone->kone_lock);
461 retval = kone_get_weight(usb_dev, &weight);
462 mutex_unlock(&kone->kone_lock);
464 if (retval)
465 return retval;
466 return snprintf(buf, PAGE_SIZE, "%d\n", weight);
469 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
470 struct device_attribute *attr, char *buf)
472 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
473 return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
476 static ssize_t kone_sysfs_show_tcu(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
480 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
483 static int kone_tcu_command(struct usb_device *usb_dev, int number)
485 int len;
486 char *value;
488 value = kmalloc(1, GFP_KERNEL);
489 if (!value)
490 return -ENOMEM;
492 *value = number;
494 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
495 USB_REQ_SET_CONFIGURATION,
496 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
497 kone_command_calibrate, 0, value, 1,
498 USB_CTRL_SET_TIMEOUT);
500 kfree(value);
501 return ((len != 1) ? -EIO : 0);
505 * Calibrating the tcu is the only action that changes settings data inside the
506 * mouse, so this data needs to be reread
508 static ssize_t kone_sysfs_set_tcu(struct device *dev,
509 struct device_attribute *attr, char const *buf, size_t size)
511 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
512 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
513 int retval;
514 unsigned long state;
516 retval = strict_strtoul(buf, 10, &state);
517 if (retval)
518 return retval;
520 if (state != 0 && state != 1)
521 return -EINVAL;
523 mutex_lock(&kone->kone_lock);
525 if (state == 1) { /* state activate */
526 retval = kone_tcu_command(usb_dev, 1);
527 if (retval)
528 goto exit_unlock;
529 retval = kone_tcu_command(usb_dev, 2);
530 if (retval)
531 goto exit_unlock;
532 ssleep(5); /* tcu needs this time for calibration */
533 retval = kone_tcu_command(usb_dev, 3);
534 if (retval)
535 goto exit_unlock;
536 retval = kone_tcu_command(usb_dev, 0);
537 if (retval)
538 goto exit_unlock;
539 retval = kone_tcu_command(usb_dev, 4);
540 if (retval)
541 goto exit_unlock;
543 * Kone needs this time to settle things.
544 * Reading settings too early will result in invalid data.
545 * Roccat's driver waits 1 sec, maybe this time could be
546 * shortened.
548 ssleep(1);
551 /* calibration changes values in settings, so reread */
552 retval = kone_get_settings(usb_dev, &kone->settings);
553 if (retval)
554 goto exit_no_settings;
556 /* only write settings back if activation state is different */
557 if (kone->settings.tcu != state) {
558 kone->settings.tcu = state;
559 kone_set_settings_checksum(&kone->settings);
561 retval = kone_set_settings(usb_dev, &kone->settings);
562 if (retval) {
563 dev_err(&usb_dev->dev, "couldn't set tcu state\n");
565 * try to reread valid settings into buffer overwriting
566 * first error code
568 retval = kone_get_settings(usb_dev, &kone->settings);
569 if (retval)
570 goto exit_no_settings;
571 goto exit_unlock;
575 retval = size;
576 exit_no_settings:
577 dev_err(&usb_dev->dev, "couldn't read settings\n");
578 exit_unlock:
579 mutex_unlock(&kone->kone_lock);
580 return retval;
583 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
587 return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
590 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
591 struct device_attribute *attr, char const *buf, size_t size)
593 struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
594 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
595 int retval;
596 unsigned long new_startup_profile;
598 retval = strict_strtoul(buf, 10, &new_startup_profile);
599 if (retval)
600 return retval;
602 if (new_startup_profile < 1 || new_startup_profile > 5)
603 return -EINVAL;
605 mutex_lock(&kone->kone_lock);
607 kone->settings.startup_profile = new_startup_profile;
608 kone_set_settings_checksum(&kone->settings);
610 retval = kone_set_settings(usb_dev, &kone->settings);
612 mutex_unlock(&kone->kone_lock);
614 if (retval)
615 return retval;
617 /* changing the startup profile immediately activates this profile */
618 kone->actual_profile = new_startup_profile;
619 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
621 return size;
625 * This file is used by userland software to find devices that are handled by
626 * this driver. This provides a consistent way for actual and older kernels
627 * where this driver replaced usbhid instead of generic-usb.
628 * Driver capabilities are determined by version number.
630 static ssize_t kone_sysfs_show_driver_version(struct device *dev,
631 struct device_attribute *attr, char *buf)
633 return snprintf(buf, PAGE_SIZE, ROCCAT_KONE_DRIVER_VERSION "\n");
637 * Read actual dpi settings.
638 * Returns raw value for further processing. Refer to enum kone_polling_rates to
639 * get real value.
641 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
643 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
646 * The mouse can be equipped with one of four supplied weights from 5 to 20
647 * grams which are recognized and its value can be read out.
648 * This returns the raw value reported by the mouse for easy evaluation by
649 * software. Refer to enum kone_weights to get corresponding real weight.
651 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
654 * Prints firmware version stored in mouse as integer.
655 * The raw value reported by the mouse is returned for easy evaluation, to get
656 * the real version number the decimal point has to be shifted 2 positions to
657 * the left. E.g. a value of 138 means 1.38.
659 static DEVICE_ATTR(firmware_version, 0440,
660 kone_sysfs_show_firmware_version, NULL);
663 * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
664 * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
666 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
668 /* Prints and takes the number of the profile the mouse starts with */
669 static DEVICE_ATTR(startup_profile, 0660,
670 kone_sysfs_show_startup_profile,
671 kone_sysfs_set_startup_profile);
673 static DEVICE_ATTR(kone_driver_version, 0440,
674 kone_sysfs_show_driver_version, NULL);
676 static struct attribute *kone_attributes[] = {
677 &dev_attr_actual_dpi.attr,
678 &dev_attr_actual_profile.attr,
679 &dev_attr_weight.attr,
680 &dev_attr_firmware_version.attr,
681 &dev_attr_tcu.attr,
682 &dev_attr_startup_profile.attr,
683 &dev_attr_kone_driver_version.attr,
684 NULL
687 static struct attribute_group kone_attribute_group = {
688 .attrs = kone_attributes
691 static struct bin_attribute kone_settings_attr = {
692 .attr = { .name = "settings", .mode = 0660 },
693 .size = sizeof(struct kone_settings),
694 .read = kone_sysfs_read_settings,
695 .write = kone_sysfs_write_settings
698 static struct bin_attribute kone_profile1_attr = {
699 .attr = { .name = "profile1", .mode = 0660 },
700 .size = sizeof(struct kone_profile),
701 .read = kone_sysfs_read_profile1,
702 .write = kone_sysfs_write_profile1
705 static struct bin_attribute kone_profile2_attr = {
706 .attr = { .name = "profile2", .mode = 0660 },
707 .size = sizeof(struct kone_profile),
708 .read = kone_sysfs_read_profile2,
709 .write = kone_sysfs_write_profile2
712 static struct bin_attribute kone_profile3_attr = {
713 .attr = { .name = "profile3", .mode = 0660 },
714 .size = sizeof(struct kone_profile),
715 .read = kone_sysfs_read_profile3,
716 .write = kone_sysfs_write_profile3
719 static struct bin_attribute kone_profile4_attr = {
720 .attr = { .name = "profile4", .mode = 0660 },
721 .size = sizeof(struct kone_profile),
722 .read = kone_sysfs_read_profile4,
723 .write = kone_sysfs_write_profile4
726 static struct bin_attribute kone_profile5_attr = {
727 .attr = { .name = "profile5", .mode = 0660 },
728 .size = sizeof(struct kone_profile),
729 .read = kone_sysfs_read_profile5,
730 .write = kone_sysfs_write_profile5
733 static int kone_create_sysfs_attributes(struct usb_interface *intf)
735 int retval;
737 retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
738 if (retval)
739 goto exit_1;
741 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
742 if (retval)
743 goto exit_2;
745 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
746 if (retval)
747 goto exit_3;
749 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
750 if (retval)
751 goto exit_4;
753 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
754 if (retval)
755 goto exit_5;
757 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
758 if (retval)
759 goto exit_6;
761 retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
762 if (retval)
763 goto exit_7;
765 return 0;
767 exit_7:
768 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
769 exit_6:
770 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
771 exit_5:
772 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
773 exit_4:
774 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
775 exit_3:
776 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
777 exit_2:
778 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
779 exit_1:
780 return retval;
783 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
785 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
786 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
787 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
788 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
789 sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
790 sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
791 sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
794 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
795 struct kone_device *kone)
797 uint i;
798 int retval;
800 mutex_init(&kone->kone_lock);
802 for (i = 0; i < 5; ++i) {
803 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
804 if (retval)
805 return retval;
808 retval = kone_get_settings(usb_dev, &kone->settings);
809 if (retval)
810 return retval;
812 retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
813 if (retval)
814 return retval;
816 kone->actual_profile = kone->settings.startup_profile;
817 kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
819 return 0;
823 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
824 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
825 * module.
826 * Secial behaviour is bound only to mousepart since only mouseevents contain
827 * additional notifications.
829 static int kone_init_specials(struct hid_device *hdev)
831 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
832 struct usb_device *usb_dev = interface_to_usbdev(intf);
833 struct kone_device *kone;
834 int retval;
836 if (intf->cur_altsetting->desc.bInterfaceProtocol
837 == USB_INTERFACE_PROTOCOL_MOUSE) {
839 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
840 if (!kone) {
841 dev_err(&hdev->dev, "can't alloc device descriptor\n");
842 return -ENOMEM;
844 hid_set_drvdata(hdev, kone);
846 retval = kone_init_kone_device_struct(usb_dev, kone);
847 if (retval) {
848 dev_err(&hdev->dev,
849 "couldn't init struct kone_device\n");
850 goto exit_free;
852 retval = kone_create_sysfs_attributes(intf);
853 if (retval) {
854 dev_err(&hdev->dev, "cannot create sysfs files\n");
855 goto exit_free;
857 } else {
858 hid_set_drvdata(hdev, NULL);
861 return 0;
862 exit_free:
863 kfree(kone);
864 return retval;
868 static void kone_remove_specials(struct hid_device *hdev)
870 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
872 if (intf->cur_altsetting->desc.bInterfaceProtocol
873 == USB_INTERFACE_PROTOCOL_MOUSE) {
874 kone_remove_sysfs_attributes(intf);
875 kfree(hid_get_drvdata(hdev));
879 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
881 int retval;
883 retval = hid_parse(hdev);
884 if (retval) {
885 dev_err(&hdev->dev, "parse failed\n");
886 goto exit;
889 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
890 if (retval) {
891 dev_err(&hdev->dev, "hw start failed\n");
892 goto exit;
895 retval = kone_init_specials(hdev);
896 if (retval) {
897 dev_err(&hdev->dev, "couldn't install mouse\n");
898 goto exit_stop;
901 return 0;
903 exit_stop:
904 hid_hw_stop(hdev);
905 exit:
906 return retval;
909 static void kone_remove(struct hid_device *hdev)
911 kone_remove_specials(hdev);
912 hid_hw_stop(hdev);
915 /* handle special events and keep actual profile and dpi values up to date */
916 static void kone_keep_values_up_to_date(struct kone_device *kone,
917 struct kone_mouse_event const *event)
919 switch (event->event) {
920 case kone_mouse_event_switch_profile:
921 case kone_mouse_event_osd_profile:
922 kone->actual_profile = event->value;
923 kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
924 startup_dpi;
925 break;
926 case kone_mouse_event_switch_dpi:
927 case kone_mouse_event_osd_dpi:
928 kone->actual_dpi = event->value;
929 break;
934 * Is called for keyboard- and mousepart.
935 * Only mousepart gets informations about special events in its extended event
936 * structure.
938 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
939 u8 *data, int size)
941 struct kone_device *kone = hid_get_drvdata(hdev);
942 struct kone_mouse_event *event = (struct kone_mouse_event *)data;
944 /* keyboard events are always processed by default handler */
945 if (size != sizeof(struct kone_mouse_event))
946 return 0;
949 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
950 * Pressed button is reported in each movement event.
951 * Workaround sends only one event per press.
953 if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
954 memcpy(&kone->last_mouse_event, event,
955 sizeof(struct kone_mouse_event));
956 else
957 memset(&event->tilt, 0, 5);
959 kone_keep_values_up_to_date(kone, event);
961 return 0; /* always do further processing */
964 static const struct hid_device_id kone_devices[] = {
965 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
969 MODULE_DEVICE_TABLE(hid, kone_devices);
971 static struct hid_driver kone_driver = {
972 .name = "kone",
973 .id_table = kone_devices,
974 .probe = kone_probe,
975 .remove = kone_remove,
976 .raw_event = kone_raw_event
979 static int __init kone_init(void)
981 return hid_register_driver(&kone_driver);
984 static void __exit kone_exit(void)
986 hid_unregister_driver(&kone_driver);
989 module_init(kone_init);
990 module_exit(kone_exit);
992 MODULE_AUTHOR("Stefan Achatz");
993 MODULE_DESCRIPTION("USB Roccat Kone driver");
994 MODULE_LICENSE("GPL v2");