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