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.
17 * KoneXTD has same capabilities but updated sensor.
20 #include <linux/device.h>
21 #include <linux/input.h>
22 #include <linux/hid.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/hid-roccat.h>
27 #include "hid-roccat-common.h"
28 #include "hid-roccat-koneplus.h"
30 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
32 static struct class *koneplus_class;
34 static void koneplus_profile_activated(struct koneplus_device *koneplus,
37 koneplus->actual_profile = new_profile;
40 static int koneplus_send_control(struct usb_device *usb_dev, uint value,
41 enum koneplus_control_requests request)
43 struct roccat_common2_control control;
45 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
46 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
50 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
51 control.value = value;
52 control.request = request;
54 return roccat_common2_send_with_status(usb_dev,
55 ROCCAT_COMMON_COMMAND_CONTROL,
56 &control, sizeof(struct roccat_common2_control));
60 /* retval is 0-4 on success, < 0 on error */
61 static int koneplus_get_actual_profile(struct usb_device *usb_dev)
63 struct koneplus_actual_profile buf;
66 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
67 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
69 return retval ? retval : buf.actual_profile;
72 static int koneplus_set_actual_profile(struct usb_device *usb_dev,
75 struct koneplus_actual_profile buf;
77 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
78 buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
79 buf.actual_profile = new_profile;
81 return roccat_common2_send_with_status(usb_dev,
82 KONEPLUS_COMMAND_ACTUAL_PROFILE,
83 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
86 static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
87 char *buf, loff_t off, size_t count,
88 size_t real_size, uint command)
90 struct device *dev = kobj_to_dev(kobj)->parent->parent;
91 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
92 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
98 if (off != 0 || count != real_size)
101 mutex_lock(&koneplus->koneplus_lock);
102 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
103 mutex_unlock(&koneplus->koneplus_lock);
111 static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
112 void const *buf, loff_t off, size_t count,
113 size_t real_size, uint command)
115 struct device *dev = kobj_to_dev(kobj)->parent->parent;
116 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
117 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
120 if (off != 0 || count != real_size)
123 mutex_lock(&koneplus->koneplus_lock);
124 retval = roccat_common2_send_with_status(usb_dev, command,
126 mutex_unlock(&koneplus->koneplus_lock);
134 #define KONEPLUS_SYSFS_W(thingy, THINGY) \
135 static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
136 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
137 loff_t off, size_t count) \
139 return koneplus_sysfs_write(fp, kobj, buf, off, count, \
140 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
143 #define KONEPLUS_SYSFS_R(thingy, THINGY) \
144 static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
145 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
146 loff_t off, size_t count) \
148 return koneplus_sysfs_read(fp, kobj, buf, off, count, \
149 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
152 #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
153 KONEPLUS_SYSFS_W(thingy, THINGY) \
154 KONEPLUS_SYSFS_R(thingy, THINGY)
156 #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
157 KONEPLUS_SYSFS_RW(thingy, THINGY); \
158 static struct bin_attribute bin_attr_##thingy = { \
159 .attr = { .name = #thingy, .mode = 0660 }, \
160 .size = KONEPLUS_SIZE_ ## THINGY, \
161 .read = koneplus_sysfs_read_ ## thingy, \
162 .write = koneplus_sysfs_write_ ## thingy \
165 #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
166 KONEPLUS_SYSFS_R(thingy, THINGY); \
167 static struct bin_attribute bin_attr_##thingy = { \
168 .attr = { .name = #thingy, .mode = 0440 }, \
169 .size = KONEPLUS_SIZE_ ## THINGY, \
170 .read = koneplus_sysfs_read_ ## thingy, \
173 #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
174 KONEPLUS_SYSFS_W(thingy, THINGY); \
175 static struct bin_attribute bin_attr_##thingy = { \
176 .attr = { .name = #thingy, .mode = 0220 }, \
177 .size = KONEPLUS_SIZE_ ## THINGY, \
178 .write = koneplus_sysfs_write_ ## thingy \
180 KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
181 KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
182 KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
183 KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
184 KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
185 KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
186 KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
187 KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
188 KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
190 static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
191 struct kobject *kobj, struct bin_attribute *attr, char *buf,
192 loff_t off, size_t count)
194 struct device *dev = kobj_to_dev(kobj)->parent->parent;
195 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
198 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
199 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
203 return koneplus_sysfs_read(fp, kobj, buf, off, count,
204 KONEPLUS_SIZE_PROFILE_SETTINGS,
205 KONEPLUS_COMMAND_PROFILE_SETTINGS);
208 static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
209 struct kobject *kobj, struct bin_attribute *attr, char *buf,
210 loff_t off, size_t count)
212 struct device *dev = kobj_to_dev(kobj)->parent->parent;
213 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
216 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
217 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
221 return koneplus_sysfs_read(fp, kobj, buf, off, count,
222 KONEPLUS_SIZE_PROFILE_BUTTONS,
223 KONEPLUS_COMMAND_PROFILE_BUTTONS);
226 #define PROFILE_ATTR(number) \
227 static struct bin_attribute bin_attr_profile##number##_settings = { \
228 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
229 .size = KONEPLUS_SIZE_PROFILE_SETTINGS, \
230 .read = koneplus_sysfs_read_profilex_settings, \
231 .private = &profile_numbers[number-1], \
233 static struct bin_attribute bin_attr_profile##number##_buttons = { \
234 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
235 .size = KONEPLUS_SIZE_PROFILE_BUTTONS, \
236 .read = koneplus_sysfs_read_profilex_buttons, \
237 .private = &profile_numbers[number-1], \
245 static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
246 struct device_attribute *attr, char *buf)
248 struct koneplus_device *koneplus =
249 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
250 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
253 static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
254 struct device_attribute *attr, char const *buf, size_t size)
256 struct koneplus_device *koneplus;
257 struct usb_device *usb_dev;
258 unsigned long profile;
260 struct koneplus_roccat_report roccat_report;
262 dev = dev->parent->parent;
263 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
264 usb_dev = interface_to_usbdev(to_usb_interface(dev));
266 retval = kstrtoul(buf, 10, &profile);
273 mutex_lock(&koneplus->koneplus_lock);
275 retval = koneplus_set_actual_profile(usb_dev, profile);
277 mutex_unlock(&koneplus->koneplus_lock);
281 koneplus_profile_activated(koneplus, profile);
283 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
284 roccat_report.data1 = profile + 1;
285 roccat_report.data2 = 0;
286 roccat_report.profile = profile + 1;
287 roccat_report_event(koneplus->chrdev_minor,
288 (uint8_t const *)&roccat_report);
290 mutex_unlock(&koneplus->koneplus_lock);
294 static DEVICE_ATTR(actual_profile, 0660,
295 koneplus_sysfs_show_actual_profile,
296 koneplus_sysfs_set_actual_profile);
297 static DEVICE_ATTR(startup_profile, 0660,
298 koneplus_sysfs_show_actual_profile,
299 koneplus_sysfs_set_actual_profile);
301 static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
302 struct device_attribute *attr, char *buf)
304 struct koneplus_device *koneplus;
305 struct usb_device *usb_dev;
306 struct koneplus_info info;
308 dev = dev->parent->parent;
309 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
310 usb_dev = interface_to_usbdev(to_usb_interface(dev));
312 mutex_lock(&koneplus->koneplus_lock);
313 roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
314 &info, KONEPLUS_SIZE_INFO);
315 mutex_unlock(&koneplus->koneplus_lock);
317 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
319 static DEVICE_ATTR(firmware_version, 0440,
320 koneplus_sysfs_show_firmware_version, NULL);
322 static struct attribute *koneplus_attrs[] = {
323 &dev_attr_actual_profile.attr,
324 &dev_attr_startup_profile.attr,
325 &dev_attr_firmware_version.attr,
329 static struct bin_attribute *koneplus_bin_attributes[] = {
337 &bin_attr_profile_settings,
338 &bin_attr_profile_buttons,
339 &bin_attr_profile1_settings,
340 &bin_attr_profile2_settings,
341 &bin_attr_profile3_settings,
342 &bin_attr_profile4_settings,
343 &bin_attr_profile5_settings,
344 &bin_attr_profile1_buttons,
345 &bin_attr_profile2_buttons,
346 &bin_attr_profile3_buttons,
347 &bin_attr_profile4_buttons,
348 &bin_attr_profile5_buttons,
352 static const struct attribute_group koneplus_group = {
353 .attrs = koneplus_attrs,
354 .bin_attrs = koneplus_bin_attributes,
357 static const struct attribute_group *koneplus_groups[] = {
362 static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
363 struct koneplus_device *koneplus)
367 mutex_init(&koneplus->koneplus_lock);
369 retval = koneplus_get_actual_profile(usb_dev);
372 koneplus_profile_activated(koneplus, retval);
377 static int koneplus_init_specials(struct hid_device *hdev)
379 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
380 struct usb_device *usb_dev = interface_to_usbdev(intf);
381 struct koneplus_device *koneplus;
384 if (intf->cur_altsetting->desc.bInterfaceProtocol
385 == USB_INTERFACE_PROTOCOL_MOUSE) {
387 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
389 hid_err(hdev, "can't alloc device descriptor\n");
392 hid_set_drvdata(hdev, koneplus);
394 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
396 hid_err(hdev, "couldn't init struct koneplus_device\n");
400 retval = roccat_connect(koneplus_class, hdev,
401 sizeof(struct koneplus_roccat_report));
403 hid_err(hdev, "couldn't init char dev\n");
405 koneplus->chrdev_minor = retval;
406 koneplus->roccat_claimed = 1;
409 hid_set_drvdata(hdev, NULL);
418 static void koneplus_remove_specials(struct hid_device *hdev)
420 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
421 struct koneplus_device *koneplus;
423 if (intf->cur_altsetting->desc.bInterfaceProtocol
424 == USB_INTERFACE_PROTOCOL_MOUSE) {
425 koneplus = hid_get_drvdata(hdev);
426 if (koneplus->roccat_claimed)
427 roccat_disconnect(koneplus->chrdev_minor);
432 static int koneplus_probe(struct hid_device *hdev,
433 const struct hid_device_id *id)
437 if (!hid_is_usb(hdev))
440 retval = hid_parse(hdev);
442 hid_err(hdev, "parse failed\n");
446 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
448 hid_err(hdev, "hw start failed\n");
452 retval = koneplus_init_specials(hdev);
454 hid_err(hdev, "couldn't install mouse\n");
466 static void koneplus_remove(struct hid_device *hdev)
468 koneplus_remove_specials(hdev);
472 static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
475 struct koneplus_mouse_report_button const *button_report;
478 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
479 button_report = (struct koneplus_mouse_report_button const *)data;
480 switch (button_report->type) {
481 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
482 koneplus_profile_activated(koneplus, button_report->data1 - 1);
489 static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
492 struct koneplus_roccat_report roccat_report;
493 struct koneplus_mouse_report_button const *button_report;
495 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
498 button_report = (struct koneplus_mouse_report_button const *)data;
500 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
501 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
502 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
505 roccat_report.type = button_report->type;
506 roccat_report.data1 = button_report->data1;
507 roccat_report.data2 = button_report->data2;
508 roccat_report.profile = koneplus->actual_profile + 1;
509 roccat_report_event(koneplus->chrdev_minor,
510 (uint8_t const *)&roccat_report);
513 static int koneplus_raw_event(struct hid_device *hdev,
514 struct hid_report *report, u8 *data, int size)
516 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
517 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
519 if (intf->cur_altsetting->desc.bInterfaceProtocol
520 != USB_INTERFACE_PROTOCOL_MOUSE)
523 if (koneplus == NULL)
526 koneplus_keep_values_up_to_date(koneplus, data);
528 if (koneplus->roccat_claimed)
529 koneplus_report_to_chrdev(koneplus, data);
534 static const struct hid_device_id koneplus_devices[] = {
535 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
536 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
540 MODULE_DEVICE_TABLE(hid, koneplus_devices);
542 static struct hid_driver koneplus_driver = {
544 .id_table = koneplus_devices,
545 .probe = koneplus_probe,
546 .remove = koneplus_remove,
547 .raw_event = koneplus_raw_event
550 static int __init koneplus_init(void)
554 /* class name has to be same as driver name */
555 koneplus_class = class_create(THIS_MODULE, "koneplus");
556 if (IS_ERR(koneplus_class))
557 return PTR_ERR(koneplus_class);
558 koneplus_class->dev_groups = koneplus_groups;
560 retval = hid_register_driver(&koneplus_driver);
562 class_destroy(koneplus_class);
566 static void __exit koneplus_exit(void)
568 hid_unregister_driver(&koneplus_driver);
569 class_destroy(koneplus_class);
572 module_init(koneplus_init);
573 module_exit(koneplus_exit);
575 MODULE_AUTHOR("Stefan Achatz");
576 MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
577 MODULE_LICENSE("GPL v2");