GNU Linux-libre 4.4.284-gnu1
[releases.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 is it possible to overwrite group for sysfs attributes via udev?
26  */
27
28 #include <linux/device.h>
29 #include <linux/input.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/hid-roccat.h>
34 #include "hid-ids.h"
35 #include "hid-roccat-common.h"
36 #include "hid-roccat-kone.h"
37
38 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
39
40 static void kone_profile_activated(struct kone_device *kone, uint new_profile)
41 {
42         kone->actual_profile = new_profile;
43         kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
44 }
45
46 static void kone_profile_report(struct kone_device *kone, uint new_profile)
47 {
48         struct kone_roccat_report roccat_report;
49
50         roccat_report.event = kone_mouse_event_switch_profile;
51         roccat_report.value = new_profile;
52         roccat_report.key = 0;
53         roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
54 }
55
56 static int kone_receive(struct usb_device *usb_dev, uint usb_command,
57                 void *data, uint size)
58 {
59         char *buf;
60         int len;
61
62         buf = kmalloc(size, GFP_KERNEL);
63         if (buf == NULL)
64                 return -ENOMEM;
65
66         len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
67                         HID_REQ_GET_REPORT,
68                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
69                         usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
70
71         memcpy(data, buf, size);
72         kfree(buf);
73         return ((len < 0) ? len : ((len != size) ? -EIO : 0));
74 }
75
76 static int kone_send(struct usb_device *usb_dev, uint usb_command,
77                 void const *data, uint size)
78 {
79         char *buf;
80         int len;
81
82         buf = kmemdup(data, size, GFP_KERNEL);
83         if (buf == NULL)
84                 return -ENOMEM;
85
86         len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
87                         HID_REQ_SET_REPORT,
88                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
89                         usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
90
91         kfree(buf);
92         return ((len < 0) ? len : ((len != size) ? -EIO : 0));
93 }
94
95 /* kone_class is used for creating sysfs attributes via roccat char device */
96 static struct class *kone_class;
97
98 static void kone_set_settings_checksum(struct kone_settings *settings)
99 {
100         uint16_t checksum = 0;
101         unsigned char *address = (unsigned char *)settings;
102         int i;
103
104         for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
105                 checksum += *address;
106         settings->checksum = cpu_to_le16(checksum);
107 }
108
109 /*
110  * Checks success after writing data to mouse
111  * On success returns 0
112  * On failure returns errno
113  */
114 static int kone_check_write(struct usb_device *usb_dev)
115 {
116         int retval;
117         uint8_t data;
118
119         do {
120                 /*
121                  * Mouse needs 50 msecs until it says ok, but there are
122                  * 30 more msecs needed for next write to work.
123                  */
124                 msleep(80);
125
126                 retval = kone_receive(usb_dev,
127                                 kone_command_confirm_write, &data, 1);
128                 if (retval)
129                         return retval;
130
131                 /*
132                  * value of 3 seems to mean something like
133                  * "not finished yet, but it looks good"
134                  * So check again after a moment.
135                  */
136         } while (data == 3);
137
138         if (data == 1) /* everything alright */
139                 return 0;
140
141         /* unknown answer */
142         dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
143         return -EIO;
144 }
145
146 /*
147  * Reads settings from mouse and stores it in @buf
148  * On success returns 0
149  * On failure returns errno
150  */
151 static int kone_get_settings(struct usb_device *usb_dev,
152                 struct kone_settings *buf)
153 {
154         return kone_receive(usb_dev, kone_command_settings, buf,
155                         sizeof(struct kone_settings));
156 }
157
158 /*
159  * Writes settings from @buf to mouse
160  * On success returns 0
161  * On failure returns errno
162  */
163 static int kone_set_settings(struct usb_device *usb_dev,
164                 struct kone_settings const *settings)
165 {
166         int retval;
167
168         retval = kone_send(usb_dev, kone_command_settings,
169                         settings, sizeof(struct kone_settings));
170         if (retval)
171                 return retval;
172         return kone_check_write(usb_dev);
173 }
174
175 /*
176  * Reads profile data from mouse and stores it in @buf
177  * @number: profile number to read
178  * On success returns 0
179  * On failure returns errno
180  */
181 static int kone_get_profile(struct usb_device *usb_dev,
182                 struct kone_profile *buf, int number)
183 {
184         int len;
185
186         if (number < 1 || number > 5)
187                 return -EINVAL;
188
189         len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
190                         USB_REQ_CLEAR_FEATURE,
191                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
192                         kone_command_profile, number, buf,
193                         sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
194
195         if (len != sizeof(struct kone_profile))
196                 return -EIO;
197
198         return 0;
199 }
200
201 /*
202  * Writes profile data to mouse.
203  * @number: profile number to write
204  * On success returns 0
205  * On failure returns errno
206  */
207 static int kone_set_profile(struct usb_device *usb_dev,
208                 struct kone_profile const *profile, int number)
209 {
210         int len;
211
212         if (number < 1 || number > 5)
213                 return -EINVAL;
214
215         len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
216                         USB_REQ_SET_CONFIGURATION,
217                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
218                         kone_command_profile, number, (void *)profile,
219                         sizeof(struct kone_profile),
220                         USB_CTRL_SET_TIMEOUT);
221
222         if (len != sizeof(struct kone_profile))
223                 return len;
224
225         if (kone_check_write(usb_dev))
226                 return -EIO;
227
228         return 0;
229 }
230
231 /*
232  * Reads value of "fast-clip-weight" and stores it in @result
233  * On success returns 0
234  * On failure returns errno
235  */
236 static int kone_get_weight(struct usb_device *usb_dev, int *result)
237 {
238         int retval;
239         uint8_t data;
240
241         retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
242
243         if (retval)
244                 return retval;
245
246         *result = (int)data;
247         return 0;
248 }
249
250 /*
251  * Reads firmware_version of mouse and stores it in @result
252  * On success returns 0
253  * On failure returns errno
254  */
255 static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
256 {
257         int retval;
258         uint16_t data;
259
260         retval = kone_receive(usb_dev, kone_command_firmware_version,
261                         &data, 2);
262         if (retval)
263                 return retval;
264
265         *result = le16_to_cpu(data);
266         return 0;
267 }
268
269 static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
270                 struct bin_attribute *attr, char *buf,
271                 loff_t off, size_t count) {
272         struct device *dev =
273                         container_of(kobj, struct device, kobj)->parent->parent;
274         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
275
276         if (off >= sizeof(struct kone_settings))
277                 return 0;
278
279         if (off + count > sizeof(struct kone_settings))
280                 count = sizeof(struct kone_settings) - off;
281
282         mutex_lock(&kone->kone_lock);
283         memcpy(buf, ((char const *)&kone->settings) + off, count);
284         mutex_unlock(&kone->kone_lock);
285
286         return count;
287 }
288
289 /*
290  * Writing settings automatically activates startup_profile.
291  * This function keeps values in kone_device up to date and assumes that in
292  * case of error the old data is still valid
293  */
294 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
295                 struct bin_attribute *attr, char *buf,
296                 loff_t off, size_t count) {
297         struct device *dev =
298                         container_of(kobj, struct device, kobj)->parent->parent;
299         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
300         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
301         int retval = 0, difference, old_profile;
302         struct kone_settings *settings = (struct kone_settings *)buf;
303
304         /* I need to get my data in one piece */
305         if (off != 0 || count != sizeof(struct kone_settings))
306                 return -EINVAL;
307
308         mutex_lock(&kone->kone_lock);
309         difference = memcmp(settings, &kone->settings,
310                             sizeof(struct kone_settings));
311         if (difference) {
312                 if (settings->startup_profile < 1 ||
313                     settings->startup_profile > 5) {
314                         retval = -EINVAL;
315                         goto unlock;
316                 }
317
318                 retval = kone_set_settings(usb_dev, settings);
319                 if (retval)
320                         goto unlock;
321
322                 old_profile = kone->settings.startup_profile;
323                 memcpy(&kone->settings, settings, sizeof(struct kone_settings));
324
325                 kone_profile_activated(kone, kone->settings.startup_profile);
326
327                 if (kone->settings.startup_profile != old_profile)
328                         kone_profile_report(kone, kone->settings.startup_profile);
329         }
330 unlock:
331         mutex_unlock(&kone->kone_lock);
332
333         if (retval)
334                 return retval;
335
336         return sizeof(struct kone_settings);
337 }
338 static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
339                 kone_sysfs_write_settings, sizeof(struct kone_settings));
340
341 static ssize_t kone_sysfs_read_profilex(struct file *fp,
342                 struct kobject *kobj, struct bin_attribute *attr,
343                 char *buf, loff_t off, size_t count) {
344         struct device *dev =
345                         container_of(kobj, struct device, kobj)->parent->parent;
346         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
347
348         if (off >= sizeof(struct kone_profile))
349                 return 0;
350
351         if (off + count > sizeof(struct kone_profile))
352                 count = sizeof(struct kone_profile) - off;
353
354         mutex_lock(&kone->kone_lock);
355         memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
356         mutex_unlock(&kone->kone_lock);
357
358         return count;
359 }
360
361 /* Writes data only if different to stored data */
362 static ssize_t kone_sysfs_write_profilex(struct file *fp,
363                 struct kobject *kobj, struct bin_attribute *attr,
364                 char *buf, loff_t off, size_t count) {
365         struct device *dev =
366                         container_of(kobj, struct device, kobj)->parent->parent;
367         struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
368         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
369         struct kone_profile *profile;
370         int retval = 0, difference;
371
372         /* I need to get my data in one piece */
373         if (off != 0 || count != sizeof(struct kone_profile))
374                 return -EINVAL;
375
376         profile = &kone->profiles[*(uint *)(attr->private)];
377
378         mutex_lock(&kone->kone_lock);
379         difference = memcmp(buf, profile, sizeof(struct kone_profile));
380         if (difference) {
381                 retval = kone_set_profile(usb_dev,
382                                 (struct kone_profile const *)buf,
383                                 *(uint *)(attr->private) + 1);
384                 if (!retval)
385                         memcpy(profile, buf, sizeof(struct kone_profile));
386         }
387         mutex_unlock(&kone->kone_lock);
388
389         if (retval)
390                 return retval;
391
392         return sizeof(struct kone_profile);
393 }
394 #define PROFILE_ATTR(number)                                    \
395 static struct bin_attribute bin_attr_profile##number = {        \
396         .attr = { .name = "profile" #number, .mode = 0660 },    \
397         .size = sizeof(struct kone_profile),                    \
398         .read = kone_sysfs_read_profilex,                       \
399         .write = kone_sysfs_write_profilex,                     \
400         .private = &profile_numbers[number-1],                  \
401 }
402 PROFILE_ATTR(1);
403 PROFILE_ATTR(2);
404 PROFILE_ATTR(3);
405 PROFILE_ATTR(4);
406 PROFILE_ATTR(5);
407
408 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
409                 struct device_attribute *attr, char *buf)
410 {
411         struct kone_device *kone =
412                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
413         return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
414 }
415 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
416
417 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
418                 struct device_attribute *attr, char *buf)
419 {
420         struct kone_device *kone =
421                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
422         return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
423 }
424 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
425
426 /* weight is read each time, since we don't get informed when it's changed */
427 static ssize_t kone_sysfs_show_weight(struct device *dev,
428                 struct device_attribute *attr, char *buf)
429 {
430         struct kone_device *kone;
431         struct usb_device *usb_dev;
432         int weight = 0;
433         int retval;
434
435         dev = dev->parent->parent;
436         kone = hid_get_drvdata(dev_get_drvdata(dev));
437         usb_dev = interface_to_usbdev(to_usb_interface(dev));
438
439         mutex_lock(&kone->kone_lock);
440         retval = kone_get_weight(usb_dev, &weight);
441         mutex_unlock(&kone->kone_lock);
442
443         if (retval)
444                 return retval;
445         return snprintf(buf, PAGE_SIZE, "%d\n", weight);
446 }
447 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
448
449 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
450                 struct device_attribute *attr, char *buf)
451 {
452         struct kone_device *kone =
453                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
454         return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
455 }
456 static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
457                    NULL);
458
459 static ssize_t kone_sysfs_show_tcu(struct device *dev,
460                 struct device_attribute *attr, char *buf)
461 {
462         struct kone_device *kone =
463                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
464         return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
465 }
466
467 static int kone_tcu_command(struct usb_device *usb_dev, int number)
468 {
469         unsigned char value;
470
471         value = number;
472         return kone_send(usb_dev, kone_command_calibrate, &value, 1);
473 }
474
475 /*
476  * Calibrating the tcu is the only action that changes settings data inside the
477  * mouse, so this data needs to be reread
478  */
479 static ssize_t kone_sysfs_set_tcu(struct device *dev,
480                 struct device_attribute *attr, char const *buf, size_t size)
481 {
482         struct kone_device *kone;
483         struct usb_device *usb_dev;
484         int retval;
485         unsigned long state;
486
487         dev = dev->parent->parent;
488         kone = hid_get_drvdata(dev_get_drvdata(dev));
489         usb_dev = interface_to_usbdev(to_usb_interface(dev));
490
491         retval = kstrtoul(buf, 10, &state);
492         if (retval)
493                 return retval;
494
495         if (state != 0 && state != 1)
496                 return -EINVAL;
497
498         mutex_lock(&kone->kone_lock);
499
500         if (state == 1) { /* state activate */
501                 retval = kone_tcu_command(usb_dev, 1);
502                 if (retval)
503                         goto exit_unlock;
504                 retval = kone_tcu_command(usb_dev, 2);
505                 if (retval)
506                         goto exit_unlock;
507                 ssleep(5); /* tcu needs this time for calibration */
508                 retval = kone_tcu_command(usb_dev, 3);
509                 if (retval)
510                         goto exit_unlock;
511                 retval = kone_tcu_command(usb_dev, 0);
512                 if (retval)
513                         goto exit_unlock;
514                 retval = kone_tcu_command(usb_dev, 4);
515                 if (retval)
516                         goto exit_unlock;
517                 /*
518                  * Kone needs this time to settle things.
519                  * Reading settings too early will result in invalid data.
520                  * Roccat's driver waits 1 sec, maybe this time could be
521                  * shortened.
522                  */
523                 ssleep(1);
524         }
525
526         /* calibration changes values in settings, so reread */
527         retval = kone_get_settings(usb_dev, &kone->settings);
528         if (retval)
529                 goto exit_no_settings;
530
531         /* only write settings back if activation state is different */
532         if (kone->settings.tcu != state) {
533                 kone->settings.tcu = state;
534                 kone_set_settings_checksum(&kone->settings);
535
536                 retval = kone_set_settings(usb_dev, &kone->settings);
537                 if (retval) {
538                         dev_err(&usb_dev->dev, "couldn't set tcu state\n");
539                         /*
540                          * try to reread valid settings into buffer overwriting
541                          * first error code
542                          */
543                         retval = kone_get_settings(usb_dev, &kone->settings);
544                         if (retval)
545                                 goto exit_no_settings;
546                         goto exit_unlock;
547                 }
548                 /* calibration resets profile */
549                 kone_profile_activated(kone, kone->settings.startup_profile);
550         }
551
552         retval = size;
553 exit_no_settings:
554         dev_err(&usb_dev->dev, "couldn't read settings\n");
555 exit_unlock:
556         mutex_unlock(&kone->kone_lock);
557         return retval;
558 }
559 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
560
561 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
562                 struct device_attribute *attr, char *buf)
563 {
564         struct kone_device *kone =
565                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
566         return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
567 }
568
569 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
570                 struct device_attribute *attr, char const *buf, size_t size)
571 {
572         struct kone_device *kone;
573         struct usb_device *usb_dev;
574         int retval;
575         unsigned long new_startup_profile;
576
577         dev = dev->parent->parent;
578         kone = hid_get_drvdata(dev_get_drvdata(dev));
579         usb_dev = interface_to_usbdev(to_usb_interface(dev));
580
581         retval = kstrtoul(buf, 10, &new_startup_profile);
582         if (retval)
583                 return retval;
584
585         if (new_startup_profile  < 1 || new_startup_profile > 5)
586                 return -EINVAL;
587
588         mutex_lock(&kone->kone_lock);
589
590         kone->settings.startup_profile = new_startup_profile;
591         kone_set_settings_checksum(&kone->settings);
592
593         retval = kone_set_settings(usb_dev, &kone->settings);
594         if (retval) {
595                 mutex_unlock(&kone->kone_lock);
596                 return retval;
597         }
598
599         /* changing the startup profile immediately activates this profile */
600         kone_profile_activated(kone, new_startup_profile);
601         kone_profile_report(kone, new_startup_profile);
602
603         mutex_unlock(&kone->kone_lock);
604         return size;
605 }
606 static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
607                    kone_sysfs_set_startup_profile);
608
609 static struct attribute *kone_attrs[] = {
610         /*
611          * Read actual dpi settings.
612          * Returns raw value for further processing. Refer to enum
613          * kone_polling_rates to get real value.
614          */
615         &dev_attr_actual_dpi.attr,
616         &dev_attr_actual_profile.attr,
617
618         /*
619          * The mouse can be equipped with one of four supplied weights from 5
620          * to 20 grams which are recognized and its value can be read out.
621          * This returns the raw value reported by the mouse for easy evaluation
622          * by software. Refer to enum kone_weights to get corresponding real
623          * weight.
624          */
625         &dev_attr_weight.attr,
626
627         /*
628          * Prints firmware version stored in mouse as integer.
629          * The raw value reported by the mouse is returned for easy evaluation,
630          * to get the real version number the decimal point has to be shifted 2
631          * positions to the left. E.g. a value of 138 means 1.38.
632          */
633         &dev_attr_firmware_version.attr,
634
635         /*
636          * Prints state of Tracking Control Unit as number where 0 = off and
637          * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
638          * activates the tcu
639          */
640         &dev_attr_tcu.attr,
641
642         /* Prints and takes the number of the profile the mouse starts with */
643         &dev_attr_startup_profile.attr,
644         NULL,
645 };
646
647 static struct bin_attribute *kone_bin_attributes[] = {
648         &bin_attr_settings,
649         &bin_attr_profile1,
650         &bin_attr_profile2,
651         &bin_attr_profile3,
652         &bin_attr_profile4,
653         &bin_attr_profile5,
654         NULL,
655 };
656
657 static const struct attribute_group kone_group = {
658         .attrs = kone_attrs,
659         .bin_attrs = kone_bin_attributes,
660 };
661
662 static const struct attribute_group *kone_groups[] = {
663         &kone_group,
664         NULL,
665 };
666
667 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
668                 struct kone_device *kone)
669 {
670         uint i;
671         int retval;
672
673         mutex_init(&kone->kone_lock);
674
675         for (i = 0; i < 5; ++i) {
676                 retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
677                 if (retval)
678                         return retval;
679         }
680
681         retval = kone_get_settings(usb_dev, &kone->settings);
682         if (retval)
683                 return retval;
684
685         retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
686         if (retval)
687                 return retval;
688
689         kone_profile_activated(kone, kone->settings.startup_profile);
690
691         return 0;
692 }
693
694 /*
695  * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
696  * mousepart if usb_hid is compiled into the kernel and kone is compiled as
697  * module.
698  * Secial behaviour is bound only to mousepart since only mouseevents contain
699  * additional notifications.
700  */
701 static int kone_init_specials(struct hid_device *hdev)
702 {
703         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
704         struct usb_device *usb_dev = interface_to_usbdev(intf);
705         struct kone_device *kone;
706         int retval;
707
708         if (intf->cur_altsetting->desc.bInterfaceProtocol
709                         == USB_INTERFACE_PROTOCOL_MOUSE) {
710
711                 kone = kzalloc(sizeof(*kone), GFP_KERNEL);
712                 if (!kone)
713                         return -ENOMEM;
714                 hid_set_drvdata(hdev, kone);
715
716                 retval = kone_init_kone_device_struct(usb_dev, kone);
717                 if (retval) {
718                         hid_err(hdev, "couldn't init struct kone_device\n");
719                         goto exit_free;
720                 }
721
722                 retval = roccat_connect(kone_class, hdev,
723                                 sizeof(struct kone_roccat_report));
724                 if (retval < 0) {
725                         hid_err(hdev, "couldn't init char dev\n");
726                         /* be tolerant about not getting chrdev */
727                 } else {
728                         kone->roccat_claimed = 1;
729                         kone->chrdev_minor = retval;
730                 }
731         } else {
732                 hid_set_drvdata(hdev, NULL);
733         }
734
735         return 0;
736 exit_free:
737         kfree(kone);
738         return retval;
739 }
740
741 static void kone_remove_specials(struct hid_device *hdev)
742 {
743         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
744         struct kone_device *kone;
745
746         if (intf->cur_altsetting->desc.bInterfaceProtocol
747                         == USB_INTERFACE_PROTOCOL_MOUSE) {
748                 kone = hid_get_drvdata(hdev);
749                 if (kone->roccat_claimed)
750                         roccat_disconnect(kone->chrdev_minor);
751                 kfree(hid_get_drvdata(hdev));
752         }
753 }
754
755 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
756 {
757         int retval;
758
759         retval = hid_parse(hdev);
760         if (retval) {
761                 hid_err(hdev, "parse failed\n");
762                 goto exit;
763         }
764
765         retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
766         if (retval) {
767                 hid_err(hdev, "hw start failed\n");
768                 goto exit;
769         }
770
771         retval = kone_init_specials(hdev);
772         if (retval) {
773                 hid_err(hdev, "couldn't install mouse\n");
774                 goto exit_stop;
775         }
776
777         return 0;
778
779 exit_stop:
780         hid_hw_stop(hdev);
781 exit:
782         return retval;
783 }
784
785 static void kone_remove(struct hid_device *hdev)
786 {
787         kone_remove_specials(hdev);
788         hid_hw_stop(hdev);
789 }
790
791 /* handle special events and keep actual profile and dpi values up to date */
792 static void kone_keep_values_up_to_date(struct kone_device *kone,
793                 struct kone_mouse_event const *event)
794 {
795         switch (event->event) {
796         case kone_mouse_event_switch_profile:
797                 kone->actual_dpi = kone->profiles[event->value - 1].
798                                 startup_dpi;
799         case kone_mouse_event_osd_profile:
800                 kone->actual_profile = event->value;
801                 break;
802         case kone_mouse_event_switch_dpi:
803         case kone_mouse_event_osd_dpi:
804                 kone->actual_dpi = event->value;
805                 break;
806         }
807 }
808
809 static void kone_report_to_chrdev(struct kone_device const *kone,
810                 struct kone_mouse_event const *event)
811 {
812         struct kone_roccat_report roccat_report;
813
814         switch (event->event) {
815         case kone_mouse_event_switch_profile:
816         case kone_mouse_event_switch_dpi:
817         case kone_mouse_event_osd_profile:
818         case kone_mouse_event_osd_dpi:
819                 roccat_report.event = event->event;
820                 roccat_report.value = event->value;
821                 roccat_report.key = 0;
822                 roccat_report_event(kone->chrdev_minor,
823                                 (uint8_t *)&roccat_report);
824                 break;
825         case kone_mouse_event_call_overlong_macro:
826         case kone_mouse_event_multimedia:
827                 if (event->value == kone_keystroke_action_press) {
828                         roccat_report.event = event->event;
829                         roccat_report.value = kone->actual_profile;
830                         roccat_report.key = event->macro_key;
831                         roccat_report_event(kone->chrdev_minor,
832                                         (uint8_t *)&roccat_report);
833                 }
834                 break;
835         }
836
837 }
838
839 /*
840  * Is called for keyboard- and mousepart.
841  * Only mousepart gets informations about special events in its extended event
842  * structure.
843  */
844 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
845                 u8 *data, int size)
846 {
847         struct kone_device *kone = hid_get_drvdata(hdev);
848         struct kone_mouse_event *event = (struct kone_mouse_event *)data;
849
850         /* keyboard events are always processed by default handler */
851         if (size != sizeof(struct kone_mouse_event))
852                 return 0;
853
854         if (kone == NULL)
855                 return 0;
856
857         /*
858          * Firmware 1.38 introduced new behaviour for tilt and special buttons.
859          * Pressed button is reported in each movement event.
860          * Workaround sends only one event per press.
861          */
862         if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
863                 memcpy(&kone->last_mouse_event, event,
864                                 sizeof(struct kone_mouse_event));
865         else
866                 memset(&event->tilt, 0, 5);
867
868         kone_keep_values_up_to_date(kone, event);
869
870         if (kone->roccat_claimed)
871                 kone_report_to_chrdev(kone, event);
872
873         return 0; /* always do further processing */
874 }
875
876 static const struct hid_device_id kone_devices[] = {
877         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
878         { }
879 };
880
881 MODULE_DEVICE_TABLE(hid, kone_devices);
882
883 static struct hid_driver kone_driver = {
884                 .name = "kone",
885                 .id_table = kone_devices,
886                 .probe = kone_probe,
887                 .remove = kone_remove,
888                 .raw_event = kone_raw_event
889 };
890
891 static int __init kone_init(void)
892 {
893         int retval;
894
895         /* class name has to be same as driver name */
896         kone_class = class_create(THIS_MODULE, "kone");
897         if (IS_ERR(kone_class))
898                 return PTR_ERR(kone_class);
899         kone_class->dev_groups = kone_groups;
900
901         retval = hid_register_driver(&kone_driver);
902         if (retval)
903                 class_destroy(kone_class);
904         return retval;
905 }
906
907 static void __exit kone_exit(void)
908 {
909         hid_unregister_driver(&kone_driver);
910         class_destroy(kone_class);
911 }
912
913 module_init(kone_init);
914 module_exit(kone_exit);
915
916 MODULE_AUTHOR("Stefan Achatz");
917 MODULE_DESCRIPTION("USB Roccat Kone driver");
918 MODULE_LICENSE("GPL v2");