2 * Roccat Kone[+] driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
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)
15 * Roccat Kone[+] is an updated/improved version of the Kone with more memory
16 * and functionality and without the non-standard behaviours the Kone had.
19 #include <linux/device.h>
20 #include <linux/input.h>
21 #include <linux/hid.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/hid-roccat.h>
26 #include "hid-roccat-common.h"
27 #include "hid-roccat-koneplus.h"
29 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
31 static struct class *koneplus_class;
33 static void koneplus_profile_activated(struct koneplus_device *koneplus,
36 koneplus->actual_profile = new_profile;
39 static int koneplus_send_control(struct usb_device *usb_dev, uint value,
40 enum koneplus_control_requests request)
42 struct roccat_common2_control control;
44 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
45 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
49 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
50 control.value = value;
51 control.request = request;
53 return roccat_common2_send_with_status(usb_dev,
54 ROCCAT_COMMON_COMMAND_CONTROL,
55 &control, sizeof(struct roccat_common2_control));
58 static int koneplus_get_info(struct usb_device *usb_dev,
59 struct koneplus_info *buf)
61 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
62 buf, sizeof(struct koneplus_info));
65 static int koneplus_get_profile_settings(struct usb_device *usb_dev,
66 struct koneplus_profile_settings *buf, uint number)
70 retval = koneplus_send_control(usb_dev, number,
71 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
75 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_SETTINGS,
76 buf, sizeof(struct koneplus_profile_settings));
79 static int koneplus_set_profile_settings(struct usb_device *usb_dev,
80 struct koneplus_profile_settings const *settings)
82 return roccat_common2_send_with_status(usb_dev,
83 KONEPLUS_COMMAND_PROFILE_SETTINGS,
84 settings, sizeof(struct koneplus_profile_settings));
87 static int koneplus_get_profile_buttons(struct usb_device *usb_dev,
88 struct koneplus_profile_buttons *buf, int number)
92 retval = koneplus_send_control(usb_dev, number,
93 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
97 return roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_BUTTONS,
98 buf, sizeof(struct koneplus_profile_buttons));
101 static int koneplus_set_profile_buttons(struct usb_device *usb_dev,
102 struct koneplus_profile_buttons const *buttons)
104 return roccat_common2_send_with_status(usb_dev,
105 KONEPLUS_COMMAND_PROFILE_BUTTONS,
106 buttons, sizeof(struct koneplus_profile_buttons));
109 /* retval is 0-4 on success, < 0 on error */
110 static int koneplus_get_actual_profile(struct usb_device *usb_dev)
112 struct koneplus_actual_profile buf;
115 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
116 &buf, sizeof(struct koneplus_actual_profile));
118 return retval ? retval : buf.actual_profile;
121 static int koneplus_set_actual_profile(struct usb_device *usb_dev,
124 struct koneplus_actual_profile buf;
126 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
127 buf.size = sizeof(struct koneplus_actual_profile);
128 buf.actual_profile = new_profile;
130 return roccat_common2_send_with_status(usb_dev,
131 KONEPLUS_COMMAND_ACTUAL_PROFILE,
132 &buf, sizeof(struct koneplus_actual_profile));
135 static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
136 char *buf, loff_t off, size_t count,
137 size_t real_size, uint command)
140 container_of(kobj, struct device, kobj)->parent->parent;
141 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
142 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
145 if (off >= real_size)
148 if (off != 0 || count != real_size)
151 mutex_lock(&koneplus->koneplus_lock);
152 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
153 mutex_unlock(&koneplus->koneplus_lock);
161 static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
162 void const *buf, loff_t off, size_t count,
163 size_t real_size, uint command)
166 container_of(kobj, struct device, kobj)->parent->parent;
167 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
168 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
171 if (off != 0 || count != real_size)
174 mutex_lock(&koneplus->koneplus_lock);
175 retval = roccat_common2_send_with_status(usb_dev, command,
177 mutex_unlock(&koneplus->koneplus_lock);
185 static ssize_t koneplus_sysfs_write_talk(struct file *fp,
186 struct kobject *kobj, struct bin_attribute *attr, char *buf,
187 loff_t off, size_t count)
189 return koneplus_sysfs_write(fp, kobj, buf, off, count,
190 sizeof(struct koneplus_talk), KONEPLUS_COMMAND_TALK);
193 static ssize_t koneplus_sysfs_write_macro(struct file *fp,
194 struct kobject *kobj, struct bin_attribute *attr, char *buf,
195 loff_t off, size_t count)
197 return koneplus_sysfs_write(fp, kobj, buf, off, count,
198 sizeof(struct koneplus_macro), KONEPLUS_COMMAND_MACRO);
201 static ssize_t koneplus_sysfs_read_sensor(struct file *fp,
202 struct kobject *kobj, struct bin_attribute *attr, char *buf,
203 loff_t off, size_t count)
205 return koneplus_sysfs_read(fp, kobj, buf, off, count,
206 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
209 static ssize_t koneplus_sysfs_write_sensor(struct file *fp,
210 struct kobject *kobj, struct bin_attribute *attr, char *buf,
211 loff_t off, size_t count)
213 return koneplus_sysfs_write(fp, kobj, buf, off, count,
214 sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
217 static ssize_t koneplus_sysfs_write_tcu(struct file *fp,
218 struct kobject *kobj, struct bin_attribute *attr, char *buf,
219 loff_t off, size_t count)
221 return koneplus_sysfs_write(fp, kobj, buf, off, count,
222 sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
225 static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
226 struct kobject *kobj, struct bin_attribute *attr, char *buf,
227 loff_t off, size_t count)
229 return koneplus_sysfs_read(fp, kobj, buf, off, count,
230 sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
233 static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
234 struct kobject *kobj, struct bin_attribute *attr, char *buf,
235 loff_t off, size_t count)
238 container_of(kobj, struct device, kobj)->parent->parent;
239 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
241 if (off >= sizeof(struct koneplus_profile_settings))
244 if (off + count > sizeof(struct koneplus_profile_settings))
245 count = sizeof(struct koneplus_profile_settings) - off;
247 mutex_lock(&koneplus->koneplus_lock);
248 memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
250 mutex_unlock(&koneplus->koneplus_lock);
255 static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
256 struct kobject *kobj, struct bin_attribute *attr, char *buf,
257 loff_t off, size_t count)
260 container_of(kobj, struct device, kobj)->parent->parent;
261 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
262 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
266 struct koneplus_profile_settings *profile_settings;
268 if (off != 0 || count != sizeof(struct koneplus_profile_settings))
271 profile_number = ((struct koneplus_profile_settings const *)buf)->number;
272 profile_settings = &koneplus->profile_settings[profile_number];
274 mutex_lock(&koneplus->koneplus_lock);
275 difference = memcmp(buf, profile_settings,
276 sizeof(struct koneplus_profile_settings));
278 retval = koneplus_set_profile_settings(usb_dev,
279 (struct koneplus_profile_settings const *)buf);
281 memcpy(profile_settings, buf,
282 sizeof(struct koneplus_profile_settings));
284 mutex_unlock(&koneplus->koneplus_lock);
289 return sizeof(struct koneplus_profile_settings);
292 static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
293 struct kobject *kobj, struct bin_attribute *attr, char *buf,
294 loff_t off, size_t count)
297 container_of(kobj, struct device, kobj)->parent->parent;
298 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
300 if (off >= sizeof(struct koneplus_profile_buttons))
303 if (off + count > sizeof(struct koneplus_profile_buttons))
304 count = sizeof(struct koneplus_profile_buttons) - off;
306 mutex_lock(&koneplus->koneplus_lock);
307 memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
309 mutex_unlock(&koneplus->koneplus_lock);
314 static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
315 struct kobject *kobj, struct bin_attribute *attr, char *buf,
316 loff_t off, size_t count)
319 container_of(kobj, struct device, kobj)->parent->parent;
320 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
321 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
325 struct koneplus_profile_buttons *profile_buttons;
327 if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
330 profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
331 profile_buttons = &koneplus->profile_buttons[profile_number];
333 mutex_lock(&koneplus->koneplus_lock);
334 difference = memcmp(buf, profile_buttons,
335 sizeof(struct koneplus_profile_buttons));
337 retval = koneplus_set_profile_buttons(usb_dev,
338 (struct koneplus_profile_buttons const *)buf);
340 memcpy(profile_buttons, buf,
341 sizeof(struct koneplus_profile_buttons));
343 mutex_unlock(&koneplus->koneplus_lock);
348 return sizeof(struct koneplus_profile_buttons);
351 static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct koneplus_device *koneplus =
355 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
356 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
359 static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
360 struct device_attribute *attr, char const *buf, size_t size)
362 struct koneplus_device *koneplus;
363 struct usb_device *usb_dev;
364 unsigned long profile;
366 struct koneplus_roccat_report roccat_report;
368 dev = dev->parent->parent;
369 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
370 usb_dev = interface_to_usbdev(to_usb_interface(dev));
372 retval = strict_strtoul(buf, 10, &profile);
379 mutex_lock(&koneplus->koneplus_lock);
381 retval = koneplus_set_actual_profile(usb_dev, profile);
383 mutex_unlock(&koneplus->koneplus_lock);
387 koneplus_profile_activated(koneplus, profile);
389 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
390 roccat_report.data1 = profile + 1;
391 roccat_report.data2 = 0;
392 roccat_report.profile = profile + 1;
393 roccat_report_event(koneplus->chrdev_minor,
394 (uint8_t const *)&roccat_report);
396 mutex_unlock(&koneplus->koneplus_lock);
401 static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
402 struct device_attribute *attr, char *buf)
404 struct koneplus_device *koneplus =
405 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
406 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
409 static struct device_attribute koneplus_attributes[] = {
410 __ATTR(actual_profile, 0660,
411 koneplus_sysfs_show_actual_profile,
412 koneplus_sysfs_set_actual_profile),
413 __ATTR(startup_profile, 0660,
414 koneplus_sysfs_show_actual_profile,
415 koneplus_sysfs_set_actual_profile),
416 __ATTR(firmware_version, 0440,
417 koneplus_sysfs_show_firmware_version, NULL),
421 static struct bin_attribute koneplus_bin_attributes[] = {
423 .attr = { .name = "sensor", .mode = 0660 },
424 .size = sizeof(struct koneplus_sensor),
425 .read = koneplus_sysfs_read_sensor,
426 .write = koneplus_sysfs_write_sensor
429 .attr = { .name = "tcu", .mode = 0220 },
430 .size = sizeof(struct koneplus_tcu),
431 .write = koneplus_sysfs_write_tcu
434 .attr = { .name = "tcu_image", .mode = 0440 },
435 .size = sizeof(struct koneplus_tcu_image),
436 .read = koneplus_sysfs_read_tcu_image
439 .attr = { .name = "profile_settings", .mode = 0220 },
440 .size = sizeof(struct koneplus_profile_settings),
441 .write = koneplus_sysfs_write_profile_settings
444 .attr = { .name = "profile1_settings", .mode = 0440 },
445 .size = sizeof(struct koneplus_profile_settings),
446 .read = koneplus_sysfs_read_profilex_settings,
447 .private = &profile_numbers[0]
450 .attr = { .name = "profile2_settings", .mode = 0440 },
451 .size = sizeof(struct koneplus_profile_settings),
452 .read = koneplus_sysfs_read_profilex_settings,
453 .private = &profile_numbers[1]
456 .attr = { .name = "profile3_settings", .mode = 0440 },
457 .size = sizeof(struct koneplus_profile_settings),
458 .read = koneplus_sysfs_read_profilex_settings,
459 .private = &profile_numbers[2]
462 .attr = { .name = "profile4_settings", .mode = 0440 },
463 .size = sizeof(struct koneplus_profile_settings),
464 .read = koneplus_sysfs_read_profilex_settings,
465 .private = &profile_numbers[3]
468 .attr = { .name = "profile5_settings", .mode = 0440 },
469 .size = sizeof(struct koneplus_profile_settings),
470 .read = koneplus_sysfs_read_profilex_settings,
471 .private = &profile_numbers[4]
474 .attr = { .name = "profile_buttons", .mode = 0220 },
475 .size = sizeof(struct koneplus_profile_buttons),
476 .write = koneplus_sysfs_write_profile_buttons
479 .attr = { .name = "profile1_buttons", .mode = 0440 },
480 .size = sizeof(struct koneplus_profile_buttons),
481 .read = koneplus_sysfs_read_profilex_buttons,
482 .private = &profile_numbers[0]
485 .attr = { .name = "profile2_buttons", .mode = 0440 },
486 .size = sizeof(struct koneplus_profile_buttons),
487 .read = koneplus_sysfs_read_profilex_buttons,
488 .private = &profile_numbers[1]
491 .attr = { .name = "profile3_buttons", .mode = 0440 },
492 .size = sizeof(struct koneplus_profile_buttons),
493 .read = koneplus_sysfs_read_profilex_buttons,
494 .private = &profile_numbers[2]
497 .attr = { .name = "profile4_buttons", .mode = 0440 },
498 .size = sizeof(struct koneplus_profile_buttons),
499 .read = koneplus_sysfs_read_profilex_buttons,
500 .private = &profile_numbers[3]
503 .attr = { .name = "profile5_buttons", .mode = 0440 },
504 .size = sizeof(struct koneplus_profile_buttons),
505 .read = koneplus_sysfs_read_profilex_buttons,
506 .private = &profile_numbers[4]
509 .attr = { .name = "macro", .mode = 0220 },
510 .size = sizeof(struct koneplus_macro),
511 .write = koneplus_sysfs_write_macro
514 .attr = { .name = "talk", .mode = 0220 },
515 .size = sizeof(struct koneplus_talk),
516 .write = koneplus_sysfs_write_talk
521 static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
522 struct koneplus_device *koneplus)
525 static uint wait = 200;
527 mutex_init(&koneplus->koneplus_lock);
529 retval = koneplus_get_info(usb_dev, &koneplus->info);
533 for (i = 0; i < 5; ++i) {
535 retval = koneplus_get_profile_settings(usb_dev,
536 &koneplus->profile_settings[i], i);
541 retval = koneplus_get_profile_buttons(usb_dev,
542 &koneplus->profile_buttons[i], i);
548 retval = koneplus_get_actual_profile(usb_dev);
551 koneplus_profile_activated(koneplus, retval);
556 static int koneplus_init_specials(struct hid_device *hdev)
558 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
559 struct usb_device *usb_dev = interface_to_usbdev(intf);
560 struct koneplus_device *koneplus;
563 if (intf->cur_altsetting->desc.bInterfaceProtocol
564 == USB_INTERFACE_PROTOCOL_MOUSE) {
566 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
568 hid_err(hdev, "can't alloc device descriptor\n");
571 hid_set_drvdata(hdev, koneplus);
573 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
575 hid_err(hdev, "couldn't init struct koneplus_device\n");
579 retval = roccat_connect(koneplus_class, hdev,
580 sizeof(struct koneplus_roccat_report));
582 hid_err(hdev, "couldn't init char dev\n");
584 koneplus->chrdev_minor = retval;
585 koneplus->roccat_claimed = 1;
588 hid_set_drvdata(hdev, NULL);
597 static void koneplus_remove_specials(struct hid_device *hdev)
599 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
600 struct koneplus_device *koneplus;
602 if (intf->cur_altsetting->desc.bInterfaceProtocol
603 == USB_INTERFACE_PROTOCOL_MOUSE) {
604 koneplus = hid_get_drvdata(hdev);
605 if (koneplus->roccat_claimed)
606 roccat_disconnect(koneplus->chrdev_minor);
611 static int koneplus_probe(struct hid_device *hdev,
612 const struct hid_device_id *id)
616 retval = hid_parse(hdev);
618 hid_err(hdev, "parse failed\n");
622 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
624 hid_err(hdev, "hw start failed\n");
628 retval = koneplus_init_specials(hdev);
630 hid_err(hdev, "couldn't install mouse\n");
642 static void koneplus_remove(struct hid_device *hdev)
644 koneplus_remove_specials(hdev);
648 static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
651 struct koneplus_mouse_report_button const *button_report;
654 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
655 button_report = (struct koneplus_mouse_report_button const *)data;
656 switch (button_report->type) {
657 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
658 koneplus_profile_activated(koneplus, button_report->data1 - 1);
665 static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
668 struct koneplus_roccat_report roccat_report;
669 struct koneplus_mouse_report_button const *button_report;
671 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
674 button_report = (struct koneplus_mouse_report_button const *)data;
676 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
677 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
678 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
681 roccat_report.type = button_report->type;
682 roccat_report.data1 = button_report->data1;
683 roccat_report.data2 = button_report->data2;
684 roccat_report.profile = koneplus->actual_profile + 1;
685 roccat_report_event(koneplus->chrdev_minor,
686 (uint8_t const *)&roccat_report);
689 static int koneplus_raw_event(struct hid_device *hdev,
690 struct hid_report *report, u8 *data, int size)
692 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
693 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
695 if (intf->cur_altsetting->desc.bInterfaceProtocol
696 != USB_INTERFACE_PROTOCOL_MOUSE)
699 if (koneplus == NULL)
702 koneplus_keep_values_up_to_date(koneplus, data);
704 if (koneplus->roccat_claimed)
705 koneplus_report_to_chrdev(koneplus, data);
710 static const struct hid_device_id koneplus_devices[] = {
711 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
715 MODULE_DEVICE_TABLE(hid, koneplus_devices);
717 static struct hid_driver koneplus_driver = {
719 .id_table = koneplus_devices,
720 .probe = koneplus_probe,
721 .remove = koneplus_remove,
722 .raw_event = koneplus_raw_event
725 static int __init koneplus_init(void)
729 /* class name has to be same as driver name */
730 koneplus_class = class_create(THIS_MODULE, "koneplus");
731 if (IS_ERR(koneplus_class))
732 return PTR_ERR(koneplus_class);
733 koneplus_class->dev_attrs = koneplus_attributes;
734 koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
736 retval = hid_register_driver(&koneplus_driver);
738 class_destroy(koneplus_class);
742 static void __exit koneplus_exit(void)
744 hid_unregister_driver(&koneplus_driver);
745 class_destroy(koneplus_class);
748 module_init(koneplus_init);
749 module_exit(koneplus_exit);
751 MODULE_AUTHOR("Stefan Achatz");
752 MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
753 MODULE_LICENSE("GPL v2");