Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / hid / hid-roccat-kone.c
1 /*
2  * Roccat Kone driver for Linux
3  *
4  * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
5  */
6
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.
12  */
13
14 /*
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.
18  *
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?
24  *
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?
29  *
30  * TODO is it possible to overwrite group for sysfs attributes via udev?
31  */
32
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"
42
43 static void kone_set_settings_checksum(struct kone_settings *settings)
44 {
45         uint16_t checksum = 0;
46         unsigned char *address = (unsigned char *)settings;
47         int i;
48
49         for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
50                 checksum += *address;
51         settings->checksum = cpu_to_le16(checksum);
52 }
53
54 /*
55  * Checks success after writing data to mouse
56  * On success returns 0
57  * On failure returns errno
58  */
59 static int kone_check_write(struct usb_device *usb_dev)
60 {
61         int len;
62         unsigned char *data;
63
64         data = kmalloc(1, GFP_KERNEL);
65         if (!data)
66                 return -ENOMEM;
67
68         do {
69                 /*
70                  * Mouse needs 50 msecs until it says ok, but there are
71                  * 30 more msecs needed for next write to work.
72                  */
73                 msleep(80);
74
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);
81
82                 if (len != 1) {
83                         kfree(data);
84                         return -EIO;
85                 }
86
87                 /*
88                  * value of 3 seems to mean something like
89                  * "not finished yet, but it looks good"
90                  * So check again after a moment.
91                  */
92         } while (*data == 3);
93
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;
102         }
103 }
104
105 /*
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
110  */
111 static int kone_get_settings(struct usb_device *usb_dev,
112                 struct kone_settings *buf)
113 {
114         int len;
115
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);
121
122         if (len != sizeof(struct kone_settings))
123                 return -EIO;
124
125         return 0;
126 }
127
128 /*
129  * Writes settings from @buf to mouse
130  * On success returns 0
131  * On failure returns errno
132  */
133 static int kone_set_settings(struct usb_device *usb_dev,
134                 struct kone_settings const *settings)
135 {
136         int len;
137
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);
144
145         if (len != sizeof(struct kone_settings))
146                 return -EIO;
147
148         if (kone_check_write(usb_dev))
149                 return -EIO;
150
151         return 0;
152 }
153
154 /*
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
159  */
160 static int kone_get_profile(struct usb_device *usb_dev,
161                 struct kone_profile *buf, int number)
162 {
163         int len;
164
165         if (number < 1 || number > 5)
166                 return -EINVAL;
167
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);
173
174         if (len != sizeof(struct kone_profile))
175                 return -EIO;
176
177         return 0;
178 }
179
180 /*
181  * Writes profile data to mouse.
182  * @number: profile number to write
183  * On success returns 0
184  * On failure returns errno
185  */
186 static int kone_set_profile(struct usb_device *usb_dev,
187                 struct kone_profile const *profile, int number)
188 {
189         int len;
190
191         if (number < 1 || number > 5)
192                 return -EINVAL;
193
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);
200
201         if (len != sizeof(struct kone_profile))
202                 return len;
203
204         if (kone_check_write(usb_dev))
205                 return -EIO;
206
207         return 0;
208 }
209
210 /*
211  * Reads value of "fast-clip-weight" and stores it in @result
212  * On success returns 0
213  * On failure returns errno
214  */
215 static int kone_get_weight(struct usb_device *usb_dev, int *result)
216 {
217         int len;
218         uint8_t *data;
219
220         data = kmalloc(1, GFP_KERNEL);
221         if (!data)
222                 return -ENOMEM;
223
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);
228
229         if (len != 1) {
230                 kfree(data);
231                 return -EIO;
232         }
233         *result = (int)*data;
234         kfree(data);
235         return 0;
236 }
237
238 /*
239  * Reads firmware_version of mouse and stores it in @result
240  * On success returns 0
241  * On failure returns errno
242  */
243 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
244 {
245         int len;
246         unsigned char *data;
247
248         data = kmalloc(2, GFP_KERNEL);
249         if (!data)
250                 return -ENOMEM;
251
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);
257
258         if (len != 2) {
259                 kfree(data);
260                 return -EIO;
261         }
262         *result = le16_to_cpu(*data);
263         kfree(data);
264         return 0;
265 }
266
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));
272
273         if (off >= sizeof(struct kone_settings))
274                 return 0;
275
276         if (off + count > sizeof(struct kone_settings))
277                 count = sizeof(struct kone_settings) - off;
278
279         mutex_lock(&kone->kone_lock);
280         memcpy(buf, &kone->settings + off, count);
281         mutex_unlock(&kone->kone_lock);
282
283         return count;
284 }
285
286 /*
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
290  */
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;
298
299         /* I need to get my data in one piece */
300         if (off != 0 || count != sizeof(struct kone_settings))
301                 return -EINVAL;
302
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));
311         }
312         mutex_unlock(&kone->kone_lock);
313
314         if (retval)
315                 return retval;
316
317         /*
318          * If we get here, treat settings as okay and update actual values
319          * according to startup_profile
320          */
321         kone->actual_profile = kone->settings.startup_profile;
322         kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;
323
324         return sizeof(struct kone_settings);
325 }
326
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));
332
333         if (off >= sizeof(struct kone_profile))
334                 return 0;
335
336         if (off + count > sizeof(struct kone_profile))
337                 count = sizeof(struct kone_profile) - off;
338
339         mutex_lock(&kone->kone_lock);
340         memcpy(buf, &kone->profiles[number - 1], sizeof(struct kone_profile));
341         mutex_unlock(&kone->kone_lock);
342
343         return count;
344 }
345
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);
350 }
351
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);
356 }
357
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);
362 }
363
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);
368 }
369
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);
374 }
375
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;
385
386         /* I need to get my data in one piece */
387         if (off != 0 || count != sizeof(struct kone_profile))
388                 return -EINVAL;
389
390         profile = &kone->profiles[number - 1];
391
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));
399         }
400         mutex_unlock(&kone->kone_lock);
401
402         if (retval)
403                 return retval;
404
405         return sizeof(struct kone_profile);
406 }
407
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);
412 }
413
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);
418 }
419
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);
424 }
425
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);
430 }
431
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);
436 }
437
438 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
439                 struct device_attribute *attr, char *buf)
440 {
441         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
442         return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
443 }
444
445 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
446                 struct device_attribute *attr, char *buf)
447 {
448         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
449         return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
450 }
451
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)
455 {
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;
460
461         mutex_lock(&kone->kone_lock);
462         retval = kone_get_weight(usb_dev, &weight);
463         mutex_unlock(&kone->kone_lock);
464
465         if (retval)
466                 return retval;
467         return snprintf(buf, PAGE_SIZE, "%d\n", weight);
468 }
469
470 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
471                 struct device_attribute *attr, char *buf)
472 {
473         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
474         return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
475 }
476
477 static ssize_t kone_sysfs_show_tcu(struct device *dev,
478                 struct device_attribute *attr, char *buf)
479 {
480         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
481         return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
482 }
483
484 static int kone_tcu_command(struct usb_device *usb_dev, int number)
485 {
486         int len;
487         char *value;
488
489         value = kmalloc(1, GFP_KERNEL);
490         if (!value)
491                 return -ENOMEM;
492
493         *value = number;
494
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);
500
501         kfree(value);
502         return ((len != 1) ? -EIO : 0);
503 }
504
505 /*
506  * Calibrating the tcu is the only action that changes settings data inside the
507  * mouse, so this data needs to be reread
508  */
509 static ssize_t kone_sysfs_set_tcu(struct device *dev,
510                 struct device_attribute *attr, char const *buf, size_t size)
511 {
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;
516
517         retval = strict_strtoul(buf, 10, &state);
518         if (retval)
519                 return retval;
520
521         if (state != 0 && state != 1)
522                 return -EINVAL;
523
524         mutex_lock(&kone->kone_lock);
525
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;
543                 /*
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.
548                  */
549                 ssleep(1);
550         }
551
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;
556
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);
561
562                 retval = kone_set_settings(usb_dev, &kone->settings);
563                 if (retval) {
564                         dev_err(&usb_dev->dev, "couldn't set tcu state\n");
565                         /*
566                          * try to reread valid settings into buffer overwriting
567                          * first error code
568                          */
569                         retval = kone_get_settings(usb_dev, &kone->settings);
570                         if (retval)
571                                 goto exit_no_settings;
572                         goto exit_unlock;
573                 }
574         }
575
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;
582 }
583
584 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
585                 struct device_attribute *attr, char *buf)
586 {
587         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
588         return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
589 }
590
591 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
592                 struct device_attribute *attr, char const *buf, size_t size)
593 {
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;
598
599         retval = strict_strtoul(buf, 10, &new_startup_profile);
600         if (retval)
601                 return retval;
602
603         if (new_startup_profile  < 1 || new_startup_profile > 5)
604                 return -EINVAL;
605
606         mutex_lock(&kone->kone_lock);
607
608         kone->settings.startup_profile = new_startup_profile;
609         kone_set_settings_checksum(&kone->settings);
610
611         retval = kone_set_settings(usb_dev, &kone->settings);
612
613         mutex_unlock(&kone->kone_lock);
614
615         if (retval)
616                 return retval;
617
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;
621
622         return size;
623 }
624
625 /*
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.
630  */
631 static ssize_t kone_sysfs_show_driver_version(struct device *dev,
632                 struct device_attribute *attr, char *buf)
633 {
634         return snprintf(buf, PAGE_SIZE, ROCCAT_KONE_DRIVER_VERSION "\n");
635 }
636
637 /*
638  * Read actual dpi settings.
639  * Returns raw value for further processing. Refer to enum kone_polling_rates to
640  * get real value.
641  */
642 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
643
644 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
645
646 /*
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.
651  */
652 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
653
654 /*
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.
659  */
660 static DEVICE_ATTR(firmware_version, 0440,
661                 kone_sysfs_show_firmware_version, NULL);
662
663 /*
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
666  */
667 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
668
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);
673
674 static DEVICE_ATTR(kone_driver_version, 0440,
675                 kone_sysfs_show_driver_version, NULL);
676
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
686 };
687
688 static struct attribute_group kone_attribute_group = {
689                 .attrs = kone_attributes
690 };
691
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
697 };
698
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
704 };
705
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
711 };
712
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
718 };
719
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
725 };
726
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
732 };
733
734 static int kone_create_sysfs_attributes(struct usb_interface *intf)
735 {
736         int retval;
737
738         retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
739         if (retval)
740                 goto exit_1;
741
742         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
743         if (retval)
744                 goto exit_2;
745
746         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
747         if (retval)
748                 goto exit_3;
749
750         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
751         if (retval)
752                 goto exit_4;
753
754         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
755         if (retval)
756                 goto exit_5;
757
758         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
759         if (retval)
760                 goto exit_6;
761
762         retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
763         if (retval)
764                 goto exit_7;
765
766         return 0;
767
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;
782 }
783
784 static void kone_remove_sysfs_attributes(struct usb_interface *intf)
785 {
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);
793 }
794
795 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
796                 struct kone_device *kone)
797 {
798         uint i;
799         int retval;
800
801         mutex_init(&kone->kone_lock);
802
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;
807         }
808
809         retval = kone_get_settings(usb_dev, &kone->settings);
810         if (retval)
811                 return retval;
812
813         retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
814         if (retval)
815                 return retval;
816
817         kone->actual_profile = kone->settings.startup_profile;
818         kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
819
820         return 0;
821 }
822
823 /*
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.
829  */
830 static int kone_init_specials(struct hid_device *hdev)
831 {
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;
836
837         if (intf->cur_altsetting->desc.bInterfaceProtocol
838                         == USB_INTERFACE_PROTOCOL_MOUSE) {
839
840                 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
841                 if (!kone) {
842                         dev_err(&hdev->dev, "can't alloc device descriptor\n");
843                         return -ENOMEM;
844                 }
845                 hid_set_drvdata(hdev, kone);
846
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;
852                 }
853
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;
861                 }
862
863                 retval = kone_create_sysfs_attributes(intf);
864                 if (retval) {
865                         dev_err(&hdev->dev, "cannot create sysfs files\n");
866                         goto exit_free;
867                 }
868         } else {
869                 hid_set_drvdata(hdev, NULL);
870         }
871
872         return 0;
873 exit_free:
874         kfree(kone);
875         return retval;
876 }
877
878
879 static void kone_remove_specials(struct hid_device *hdev)
880 {
881         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
882         struct kone_device *kone;
883
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));
891         }
892 }
893
894 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
895 {
896         int retval;
897
898         retval = hid_parse(hdev);
899         if (retval) {
900                 dev_err(&hdev->dev, "parse failed\n");
901                 goto exit;
902         }
903
904         retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
905         if (retval) {
906                 dev_err(&hdev->dev, "hw start failed\n");
907                 goto exit;
908         }
909
910         retval = kone_init_specials(hdev);
911         if (retval) {
912                 dev_err(&hdev->dev, "couldn't install mouse\n");
913                 goto exit_stop;
914         }
915
916         return 0;
917
918 exit_stop:
919         hid_hw_stop(hdev);
920 exit:
921         return retval;
922 }
923
924 static void kone_remove(struct hid_device *hdev)
925 {
926         kone_remove_specials(hdev);
927         hid_hw_stop(hdev);
928 }
929
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)
933 {
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;
945         }
946 }
947
948 static void kone_report_to_chrdev(struct kone_device const *kone,
949                 struct kone_mouse_event const *event)
950 {
951         struct kone_roccat_report roccat_report;
952
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));
973                 }
974                 break;
975         }
976
977 }
978
979 /*
980  * Is called for keyboard- and mousepart.
981  * Only mousepart gets informations about special events in its extended event
982  * structure.
983  */
984 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
985                 u8 *data, int size)
986 {
987         struct kone_device *kone = hid_get_drvdata(hdev);
988         struct kone_mouse_event *event = (struct kone_mouse_event *)data;
989
990         /* keyboard events are always processed by default handler */
991         if (size != sizeof(struct kone_mouse_event))
992                 return 0;
993
994         /*
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.
998          */
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);
1004
1005         kone_keep_values_up_to_date(kone, event);
1006
1007         if (kone->roccat_claimed)
1008                 kone_report_to_chrdev(kone, event);
1009
1010         return 0; /* always do further processing */
1011 }
1012
1013 static const struct hid_device_id kone_devices[] = {
1014         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
1015         { }
1016 };
1017
1018 MODULE_DEVICE_TABLE(hid, kone_devices);
1019
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
1026 };
1027
1028 static int __init kone_init(void)
1029 {
1030         return hid_register_driver(&kone_driver);
1031 }
1032
1033 static void __exit kone_exit(void)
1034 {
1035         hid_unregister_driver(&kone_driver);
1036 }
1037
1038 module_init(kone_init);
1039 module_exit(kone_exit);
1040
1041 MODULE_AUTHOR("Stefan Achatz");
1042 MODULE_DESCRIPTION("USB Roccat Kone driver");
1043 MODULE_LICENSE("GPL v2");