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 
kone_profile_activated(struct kone_device * kone,uint new_profile)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 
kone_profile_report(struct kone_device * kone,uint new_profile)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 
kone_receive(struct usb_device * usb_dev,uint usb_command,void * data,uint size)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 
kone_send(struct usb_device * usb_dev,uint usb_command,void const * data,uint size)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 
kone_set_settings_checksum(struct kone_settings * settings)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  */
kone_check_write(struct usb_device * usb_dev)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  */
kone_get_settings(struct usb_device * usb_dev,struct kone_settings * buf)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  */
kone_set_settings(struct usb_device * usb_dev,struct kone_settings const * settings)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  */
kone_get_profile(struct usb_device * usb_dev,struct kone_profile * buf,int number)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  */
kone_set_profile(struct usb_device * usb_dev,struct kone_profile const * profile,int number)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  */
kone_get_weight(struct usb_device * usb_dev,int * result)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  */
kone_get_firmware_version(struct usb_device * usb_dev,int * result)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 
kone_sysfs_read_settings(struct file * fp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)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 = kobj_to_dev(kobj)->parent->parent;
273 	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
274 
275 	if (off >= sizeof(struct kone_settings))
276 		return 0;
277 
278 	if (off + count > sizeof(struct kone_settings))
279 		count = sizeof(struct kone_settings) - off;
280 
281 	mutex_lock(&kone->kone_lock);
282 	memcpy(buf, ((char const *)&kone->settings) + off, count);
283 	mutex_unlock(&kone->kone_lock);
284 
285 	return count;
286 }
287 
288 /*
289  * Writing settings automatically activates startup_profile.
290  * This function keeps values in kone_device up to date and assumes that in
291  * case of error the old data is still valid
292  */
kone_sysfs_write_settings(struct file * fp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)293 static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
294 		struct bin_attribute *attr, char *buf,
295 		loff_t off, size_t count) {
296 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
297 	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
298 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 	int retval = 0, difference, old_profile;
300 	struct kone_settings *settings = (struct kone_settings *)buf;
301 
302 	/* I need to get my data in one piece */
303 	if (off != 0 || count != sizeof(struct kone_settings))
304 		return -EINVAL;
305 
306 	mutex_lock(&kone->kone_lock);
307 	difference = memcmp(settings, &kone->settings,
308 			    sizeof(struct kone_settings));
309 	if (difference) {
310 		if (settings->startup_profile < 1 ||
311 		    settings->startup_profile > 5) {
312 			retval = -EINVAL;
313 			goto unlock;
314 		}
315 
316 		retval = kone_set_settings(usb_dev, settings);
317 		if (retval)
318 			goto unlock;
319 
320 		old_profile = kone->settings.startup_profile;
321 		memcpy(&kone->settings, settings, sizeof(struct kone_settings));
322 
323 		kone_profile_activated(kone, kone->settings.startup_profile);
324 
325 		if (kone->settings.startup_profile != old_profile)
326 			kone_profile_report(kone, kone->settings.startup_profile);
327 	}
328 unlock:
329 	mutex_unlock(&kone->kone_lock);
330 
331 	if (retval)
332 		return retval;
333 
334 	return sizeof(struct kone_settings);
335 }
336 static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
337 		kone_sysfs_write_settings, sizeof(struct kone_settings));
338 
kone_sysfs_read_profilex(struct file * fp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)339 static ssize_t kone_sysfs_read_profilex(struct file *fp,
340 		struct kobject *kobj, struct bin_attribute *attr,
341 		char *buf, loff_t off, size_t count) {
342 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
343 	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
344 
345 	if (off >= sizeof(struct kone_profile))
346 		return 0;
347 
348 	if (off + count > sizeof(struct kone_profile))
349 		count = sizeof(struct kone_profile) - off;
350 
351 	mutex_lock(&kone->kone_lock);
352 	memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
353 	mutex_unlock(&kone->kone_lock);
354 
355 	return count;
356 }
357 
358 /* Writes data only if different to stored data */
kone_sysfs_write_profilex(struct file * fp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)359 static ssize_t kone_sysfs_write_profilex(struct file *fp,
360 		struct kobject *kobj, struct bin_attribute *attr,
361 		char *buf, loff_t off, size_t count) {
362 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
363 	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
364 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
365 	struct kone_profile *profile;
366 	int retval = 0, difference;
367 
368 	/* I need to get my data in one piece */
369 	if (off != 0 || count != sizeof(struct kone_profile))
370 		return -EINVAL;
371 
372 	profile = &kone->profiles[*(uint *)(attr->private)];
373 
374 	mutex_lock(&kone->kone_lock);
375 	difference = memcmp(buf, profile, sizeof(struct kone_profile));
376 	if (difference) {
377 		retval = kone_set_profile(usb_dev,
378 				(struct kone_profile const *)buf,
379 				*(uint *)(attr->private) + 1);
380 		if (!retval)
381 			memcpy(profile, buf, sizeof(struct kone_profile));
382 	}
383 	mutex_unlock(&kone->kone_lock);
384 
385 	if (retval)
386 		return retval;
387 
388 	return sizeof(struct kone_profile);
389 }
390 #define PROFILE_ATTR(number)					\
391 static struct bin_attribute bin_attr_profile##number = {	\
392 	.attr = { .name = "profile" #number, .mode = 0660 },	\
393 	.size = sizeof(struct kone_profile),			\
394 	.read = kone_sysfs_read_profilex,			\
395 	.write = kone_sysfs_write_profilex,			\
396 	.private = &profile_numbers[number-1],			\
397 }
398 PROFILE_ATTR(1);
399 PROFILE_ATTR(2);
400 PROFILE_ATTR(3);
401 PROFILE_ATTR(4);
402 PROFILE_ATTR(5);
403 
kone_sysfs_show_actual_profile(struct device * dev,struct device_attribute * attr,char * buf)404 static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
405 		struct device_attribute *attr, char *buf)
406 {
407 	struct kone_device *kone =
408 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
409 	return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
410 }
411 static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
412 
kone_sysfs_show_actual_dpi(struct device * dev,struct device_attribute * attr,char * buf)413 static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
414 		struct device_attribute *attr, char *buf)
415 {
416 	struct kone_device *kone =
417 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
418 	return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
419 }
420 static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
421 
422 /* weight is read each time, since we don't get informed when it's changed */
kone_sysfs_show_weight(struct device * dev,struct device_attribute * attr,char * buf)423 static ssize_t kone_sysfs_show_weight(struct device *dev,
424 		struct device_attribute *attr, char *buf)
425 {
426 	struct kone_device *kone;
427 	struct usb_device *usb_dev;
428 	int weight = 0;
429 	int retval;
430 
431 	dev = dev->parent->parent;
432 	kone = hid_get_drvdata(dev_get_drvdata(dev));
433 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
434 
435 	mutex_lock(&kone->kone_lock);
436 	retval = kone_get_weight(usb_dev, &weight);
437 	mutex_unlock(&kone->kone_lock);
438 
439 	if (retval)
440 		return retval;
441 	return snprintf(buf, PAGE_SIZE, "%d\n", weight);
442 }
443 static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
444 
kone_sysfs_show_firmware_version(struct device * dev,struct device_attribute * attr,char * buf)445 static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
446 		struct device_attribute *attr, char *buf)
447 {
448 	struct kone_device *kone =
449 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
450 	return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
451 }
452 static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
453 		   NULL);
454 
kone_sysfs_show_tcu(struct device * dev,struct device_attribute * attr,char * buf)455 static ssize_t kone_sysfs_show_tcu(struct device *dev,
456 		struct device_attribute *attr, char *buf)
457 {
458 	struct kone_device *kone =
459 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
460 	return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
461 }
462 
kone_tcu_command(struct usb_device * usb_dev,int number)463 static int kone_tcu_command(struct usb_device *usb_dev, int number)
464 {
465 	unsigned char value;
466 
467 	value = number;
468 	return kone_send(usb_dev, kone_command_calibrate, &value, 1);
469 }
470 
471 /*
472  * Calibrating the tcu is the only action that changes settings data inside the
473  * mouse, so this data needs to be reread
474  */
kone_sysfs_set_tcu(struct device * dev,struct device_attribute * attr,char const * buf,size_t size)475 static ssize_t kone_sysfs_set_tcu(struct device *dev,
476 		struct device_attribute *attr, char const *buf, size_t size)
477 {
478 	struct kone_device *kone;
479 	struct usb_device *usb_dev;
480 	int retval;
481 	unsigned long state;
482 
483 	dev = dev->parent->parent;
484 	kone = hid_get_drvdata(dev_get_drvdata(dev));
485 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
486 
487 	retval = kstrtoul(buf, 10, &state);
488 	if (retval)
489 		return retval;
490 
491 	if (state != 0 && state != 1)
492 		return -EINVAL;
493 
494 	mutex_lock(&kone->kone_lock);
495 
496 	if (state == 1) { /* state activate */
497 		retval = kone_tcu_command(usb_dev, 1);
498 		if (retval)
499 			goto exit_unlock;
500 		retval = kone_tcu_command(usb_dev, 2);
501 		if (retval)
502 			goto exit_unlock;
503 		ssleep(5); /* tcu needs this time for calibration */
504 		retval = kone_tcu_command(usb_dev, 3);
505 		if (retval)
506 			goto exit_unlock;
507 		retval = kone_tcu_command(usb_dev, 0);
508 		if (retval)
509 			goto exit_unlock;
510 		retval = kone_tcu_command(usb_dev, 4);
511 		if (retval)
512 			goto exit_unlock;
513 		/*
514 		 * Kone needs this time to settle things.
515 		 * Reading settings too early will result in invalid data.
516 		 * Roccat's driver waits 1 sec, maybe this time could be
517 		 * shortened.
518 		 */
519 		ssleep(1);
520 	}
521 
522 	/* calibration changes values in settings, so reread */
523 	retval = kone_get_settings(usb_dev, &kone->settings);
524 	if (retval)
525 		goto exit_no_settings;
526 
527 	/* only write settings back if activation state is different */
528 	if (kone->settings.tcu != state) {
529 		kone->settings.tcu = state;
530 		kone_set_settings_checksum(&kone->settings);
531 
532 		retval = kone_set_settings(usb_dev, &kone->settings);
533 		if (retval) {
534 			dev_err(&usb_dev->dev, "couldn't set tcu state\n");
535 			/*
536 			 * try to reread valid settings into buffer overwriting
537 			 * first error code
538 			 */
539 			retval = kone_get_settings(usb_dev, &kone->settings);
540 			if (retval)
541 				goto exit_no_settings;
542 			goto exit_unlock;
543 		}
544 		/* calibration resets profile */
545 		kone_profile_activated(kone, kone->settings.startup_profile);
546 	}
547 
548 	retval = size;
549 exit_no_settings:
550 	dev_err(&usb_dev->dev, "couldn't read settings\n");
551 exit_unlock:
552 	mutex_unlock(&kone->kone_lock);
553 	return retval;
554 }
555 static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
556 
kone_sysfs_show_startup_profile(struct device * dev,struct device_attribute * attr,char * buf)557 static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
558 		struct device_attribute *attr, char *buf)
559 {
560 	struct kone_device *kone =
561 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
562 	return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
563 }
564 
kone_sysfs_set_startup_profile(struct device * dev,struct device_attribute * attr,char const * buf,size_t size)565 static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
566 		struct device_attribute *attr, char const *buf, size_t size)
567 {
568 	struct kone_device *kone;
569 	struct usb_device *usb_dev;
570 	int retval;
571 	unsigned long new_startup_profile;
572 
573 	dev = dev->parent->parent;
574 	kone = hid_get_drvdata(dev_get_drvdata(dev));
575 	usb_dev = interface_to_usbdev(to_usb_interface(dev));
576 
577 	retval = kstrtoul(buf, 10, &new_startup_profile);
578 	if (retval)
579 		return retval;
580 
581 	if (new_startup_profile  < 1 || new_startup_profile > 5)
582 		return -EINVAL;
583 
584 	mutex_lock(&kone->kone_lock);
585 
586 	kone->settings.startup_profile = new_startup_profile;
587 	kone_set_settings_checksum(&kone->settings);
588 
589 	retval = kone_set_settings(usb_dev, &kone->settings);
590 	if (retval) {
591 		mutex_unlock(&kone->kone_lock);
592 		return retval;
593 	}
594 
595 	/* changing the startup profile immediately activates this profile */
596 	kone_profile_activated(kone, new_startup_profile);
597 	kone_profile_report(kone, new_startup_profile);
598 
599 	mutex_unlock(&kone->kone_lock);
600 	return size;
601 }
602 static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
603 		   kone_sysfs_set_startup_profile);
604 
605 static struct attribute *kone_attrs[] = {
606 	/*
607 	 * Read actual dpi settings.
608 	 * Returns raw value for further processing. Refer to enum
609 	 * kone_polling_rates to get real value.
610 	 */
611 	&dev_attr_actual_dpi.attr,
612 	&dev_attr_actual_profile.attr,
613 
614 	/*
615 	 * The mouse can be equipped with one of four supplied weights from 5
616 	 * to 20 grams which are recognized and its value can be read out.
617 	 * This returns the raw value reported by the mouse for easy evaluation
618 	 * by software. Refer to enum kone_weights to get corresponding real
619 	 * weight.
620 	 */
621 	&dev_attr_weight.attr,
622 
623 	/*
624 	 * Prints firmware version stored in mouse as integer.
625 	 * The raw value reported by the mouse is returned for easy evaluation,
626 	 * to get the real version number the decimal point has to be shifted 2
627 	 * positions to the left. E.g. a value of 138 means 1.38.
628 	 */
629 	&dev_attr_firmware_version.attr,
630 
631 	/*
632 	 * Prints state of Tracking Control Unit as number where 0 = off and
633 	 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
634 	 * activates the tcu
635 	 */
636 	&dev_attr_tcu.attr,
637 
638 	/* Prints and takes the number of the profile the mouse starts with */
639 	&dev_attr_startup_profile.attr,
640 	NULL,
641 };
642 
643 static struct bin_attribute *kone_bin_attributes[] = {
644 	&bin_attr_settings,
645 	&bin_attr_profile1,
646 	&bin_attr_profile2,
647 	&bin_attr_profile3,
648 	&bin_attr_profile4,
649 	&bin_attr_profile5,
650 	NULL,
651 };
652 
653 static const struct attribute_group kone_group = {
654 	.attrs = kone_attrs,
655 	.bin_attrs = kone_bin_attributes,
656 };
657 
658 static const struct attribute_group *kone_groups[] = {
659 	&kone_group,
660 	NULL,
661 };
662 
kone_init_kone_device_struct(struct usb_device * usb_dev,struct kone_device * kone)663 static int kone_init_kone_device_struct(struct usb_device *usb_dev,
664 		struct kone_device *kone)
665 {
666 	uint i;
667 	int retval;
668 
669 	mutex_init(&kone->kone_lock);
670 
671 	for (i = 0; i < 5; ++i) {
672 		retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
673 		if (retval)
674 			return retval;
675 	}
676 
677 	retval = kone_get_settings(usb_dev, &kone->settings);
678 	if (retval)
679 		return retval;
680 
681 	retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
682 	if (retval)
683 		return retval;
684 
685 	kone_profile_activated(kone, kone->settings.startup_profile);
686 
687 	return 0;
688 }
689 
690 /*
691  * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
692  * mousepart if usb_hid is compiled into the kernel and kone is compiled as
693  * module.
694  * Secial behaviour is bound only to mousepart since only mouseevents contain
695  * additional notifications.
696  */
kone_init_specials(struct hid_device * hdev)697 static int kone_init_specials(struct hid_device *hdev)
698 {
699 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
700 	struct usb_device *usb_dev = interface_to_usbdev(intf);
701 	struct kone_device *kone;
702 	int retval;
703 
704 	if (intf->cur_altsetting->desc.bInterfaceProtocol
705 			== USB_INTERFACE_PROTOCOL_MOUSE) {
706 
707 		kone = kzalloc(sizeof(*kone), GFP_KERNEL);
708 		if (!kone)
709 			return -ENOMEM;
710 		hid_set_drvdata(hdev, kone);
711 
712 		retval = kone_init_kone_device_struct(usb_dev, kone);
713 		if (retval) {
714 			hid_err(hdev, "couldn't init struct kone_device\n");
715 			goto exit_free;
716 		}
717 
718 		retval = roccat_connect(kone_class, hdev,
719 				sizeof(struct kone_roccat_report));
720 		if (retval < 0) {
721 			hid_err(hdev, "couldn't init char dev\n");
722 			/* be tolerant about not getting chrdev */
723 		} else {
724 			kone->roccat_claimed = 1;
725 			kone->chrdev_minor = retval;
726 		}
727 	} else {
728 		hid_set_drvdata(hdev, NULL);
729 	}
730 
731 	return 0;
732 exit_free:
733 	kfree(kone);
734 	return retval;
735 }
736 
kone_remove_specials(struct hid_device * hdev)737 static void kone_remove_specials(struct hid_device *hdev)
738 {
739 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
740 	struct kone_device *kone;
741 
742 	if (intf->cur_altsetting->desc.bInterfaceProtocol
743 			== USB_INTERFACE_PROTOCOL_MOUSE) {
744 		kone = hid_get_drvdata(hdev);
745 		if (kone->roccat_claimed)
746 			roccat_disconnect(kone->chrdev_minor);
747 		kfree(hid_get_drvdata(hdev));
748 	}
749 }
750 
kone_probe(struct hid_device * hdev,const struct hid_device_id * id)751 static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
752 {
753 	int retval;
754 
755 	if (!hid_is_usb(hdev))
756 		return -EINVAL;
757 
758 	retval = hid_parse(hdev);
759 	if (retval) {
760 		hid_err(hdev, "parse failed\n");
761 		goto exit;
762 	}
763 
764 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
765 	if (retval) {
766 		hid_err(hdev, "hw start failed\n");
767 		goto exit;
768 	}
769 
770 	retval = kone_init_specials(hdev);
771 	if (retval) {
772 		hid_err(hdev, "couldn't install mouse\n");
773 		goto exit_stop;
774 	}
775 
776 	return 0;
777 
778 exit_stop:
779 	hid_hw_stop(hdev);
780 exit:
781 	return retval;
782 }
783 
kone_remove(struct hid_device * hdev)784 static void kone_remove(struct hid_device *hdev)
785 {
786 	kone_remove_specials(hdev);
787 	hid_hw_stop(hdev);
788 }
789 
790 /* handle special events and keep actual profile and dpi values up to date */
kone_keep_values_up_to_date(struct kone_device * kone,struct kone_mouse_event const * event)791 static void kone_keep_values_up_to_date(struct kone_device *kone,
792 		struct kone_mouse_event const *event)
793 {
794 	switch (event->event) {
795 	case kone_mouse_event_switch_profile:
796 		kone->actual_dpi = kone->profiles[event->value - 1].
797 				startup_dpi;
798 	case kone_mouse_event_osd_profile:
799 		kone->actual_profile = event->value;
800 		break;
801 	case kone_mouse_event_switch_dpi:
802 	case kone_mouse_event_osd_dpi:
803 		kone->actual_dpi = event->value;
804 		break;
805 	}
806 }
807 
kone_report_to_chrdev(struct kone_device const * kone,struct kone_mouse_event const * event)808 static void kone_report_to_chrdev(struct kone_device const *kone,
809 		struct kone_mouse_event const *event)
810 {
811 	struct kone_roccat_report roccat_report;
812 
813 	switch (event->event) {
814 	case kone_mouse_event_switch_profile:
815 	case kone_mouse_event_switch_dpi:
816 	case kone_mouse_event_osd_profile:
817 	case kone_mouse_event_osd_dpi:
818 		roccat_report.event = event->event;
819 		roccat_report.value = event->value;
820 		roccat_report.key = 0;
821 		roccat_report_event(kone->chrdev_minor,
822 				(uint8_t *)&roccat_report);
823 		break;
824 	case kone_mouse_event_call_overlong_macro:
825 	case kone_mouse_event_multimedia:
826 		if (event->value == kone_keystroke_action_press) {
827 			roccat_report.event = event->event;
828 			roccat_report.value = kone->actual_profile;
829 			roccat_report.key = event->macro_key;
830 			roccat_report_event(kone->chrdev_minor,
831 					(uint8_t *)&roccat_report);
832 		}
833 		break;
834 	}
835 
836 }
837 
838 /*
839  * Is called for keyboard- and mousepart.
840  * Only mousepart gets informations about special events in its extended event
841  * structure.
842  */
kone_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)843 static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
844 		u8 *data, int size)
845 {
846 	struct kone_device *kone = hid_get_drvdata(hdev);
847 	struct kone_mouse_event *event = (struct kone_mouse_event *)data;
848 
849 	/* keyboard events are always processed by default handler */
850 	if (size != sizeof(struct kone_mouse_event))
851 		return 0;
852 
853 	if (kone == NULL)
854 		return 0;
855 
856 	/*
857 	 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
858 	 * Pressed button is reported in each movement event.
859 	 * Workaround sends only one event per press.
860 	 */
861 	if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
862 		memcpy(&kone->last_mouse_event, event,
863 				sizeof(struct kone_mouse_event));
864 	else
865 		memset(&event->tilt, 0, 5);
866 
867 	kone_keep_values_up_to_date(kone, event);
868 
869 	if (kone->roccat_claimed)
870 		kone_report_to_chrdev(kone, event);
871 
872 	return 0; /* always do further processing */
873 }
874 
875 static const struct hid_device_id kone_devices[] = {
876 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
877 	{ }
878 };
879 
880 MODULE_DEVICE_TABLE(hid, kone_devices);
881 
882 static struct hid_driver kone_driver = {
883 		.name = "kone",
884 		.id_table = kone_devices,
885 		.probe = kone_probe,
886 		.remove = kone_remove,
887 		.raw_event = kone_raw_event
888 };
889 
kone_init(void)890 static int __init kone_init(void)
891 {
892 	int retval;
893 
894 	/* class name has to be same as driver name */
895 	kone_class = class_create(THIS_MODULE, "kone");
896 	if (IS_ERR(kone_class))
897 		return PTR_ERR(kone_class);
898 	kone_class->dev_groups = kone_groups;
899 
900 	retval = hid_register_driver(&kone_driver);
901 	if (retval)
902 		class_destroy(kone_class);
903 	return retval;
904 }
905 
kone_exit(void)906 static void __exit kone_exit(void)
907 {
908 	hid_unregister_driver(&kone_driver);
909 	class_destroy(kone_class);
910 }
911 
912 module_init(kone_init);
913 module_exit(kone_exit);
914 
915 MODULE_AUTHOR("Stefan Achatz");
916 MODULE_DESCRIPTION("USB Roccat Kone driver");
917 MODULE_LICENSE("GPL v2");
918