1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Roccat Kone[+] driver for Linux
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
12 * Roccat Kone[+] is an updated/improved version of the Kone with more memory
13 * and functionality and without the non-standard behaviours the Kone had.
14 * KoneXTD has same capabilities but updated sensor.
17 #include <linux/device.h>
18 #include <linux/input.h>
19 #include <linux/hid.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/hid-roccat.h>
24 #include "hid-roccat-common.h"
25 #include "hid-roccat-koneplus.h"
27 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
29 static struct class *koneplus_class;
31 static void koneplus_profile_activated(struct koneplus_device *koneplus,
34 koneplus->actual_profile = new_profile;
37 static int koneplus_send_control(struct usb_device *usb_dev, uint value,
38 enum koneplus_control_requests request)
40 struct roccat_common2_control control;
42 if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
43 request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
47 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
48 control.value = value;
49 control.request = request;
51 return roccat_common2_send_with_status(usb_dev,
52 ROCCAT_COMMON_COMMAND_CONTROL,
53 &control, sizeof(struct roccat_common2_control));
57 /* retval is 0-4 on success, < 0 on error */
58 static int koneplus_get_actual_profile(struct usb_device *usb_dev)
60 struct koneplus_actual_profile buf;
63 retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
64 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
66 return retval ? retval : buf.actual_profile;
69 static int koneplus_set_actual_profile(struct usb_device *usb_dev,
72 struct koneplus_actual_profile buf;
74 buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
75 buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
76 buf.actual_profile = new_profile;
78 return roccat_common2_send_with_status(usb_dev,
79 KONEPLUS_COMMAND_ACTUAL_PROFILE,
80 &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
83 static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
84 char *buf, loff_t off, size_t count,
85 size_t real_size, uint command)
87 struct device *dev = kobj_to_dev(kobj)->parent->parent;
88 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
89 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
95 if (off != 0 || count != real_size)
98 mutex_lock(&koneplus->koneplus_lock);
99 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
100 mutex_unlock(&koneplus->koneplus_lock);
108 static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
109 void const *buf, loff_t off, size_t count,
110 size_t real_size, uint command)
112 struct device *dev = kobj_to_dev(kobj)->parent->parent;
113 struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
114 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
117 if (off != 0 || count != real_size)
120 mutex_lock(&koneplus->koneplus_lock);
121 retval = roccat_common2_send_with_status(usb_dev, command,
123 mutex_unlock(&koneplus->koneplus_lock);
131 #define KONEPLUS_SYSFS_W(thingy, THINGY) \
132 static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
133 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
134 loff_t off, size_t count) \
136 return koneplus_sysfs_write(fp, kobj, buf, off, count, \
137 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
140 #define KONEPLUS_SYSFS_R(thingy, THINGY) \
141 static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
142 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
143 loff_t off, size_t count) \
145 return koneplus_sysfs_read(fp, kobj, buf, off, count, \
146 KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
149 #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
150 KONEPLUS_SYSFS_W(thingy, THINGY) \
151 KONEPLUS_SYSFS_R(thingy, THINGY)
153 #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
154 KONEPLUS_SYSFS_RW(thingy, THINGY); \
155 static struct bin_attribute bin_attr_##thingy = { \
156 .attr = { .name = #thingy, .mode = 0660 }, \
157 .size = KONEPLUS_SIZE_ ## THINGY, \
158 .read = koneplus_sysfs_read_ ## thingy, \
159 .write = koneplus_sysfs_write_ ## thingy \
162 #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
163 KONEPLUS_SYSFS_R(thingy, THINGY); \
164 static struct bin_attribute bin_attr_##thingy = { \
165 .attr = { .name = #thingy, .mode = 0440 }, \
166 .size = KONEPLUS_SIZE_ ## THINGY, \
167 .read = koneplus_sysfs_read_ ## thingy, \
170 #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
171 KONEPLUS_SYSFS_W(thingy, THINGY); \
172 static struct bin_attribute bin_attr_##thingy = { \
173 .attr = { .name = #thingy, .mode = 0220 }, \
174 .size = KONEPLUS_SIZE_ ## THINGY, \
175 .write = koneplus_sysfs_write_ ## thingy \
177 KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
178 KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
179 KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
180 KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
181 KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
182 KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
183 KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
184 KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
185 KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
187 static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
188 struct kobject *kobj, struct bin_attribute *attr, char *buf,
189 loff_t off, size_t count)
191 struct device *dev = kobj_to_dev(kobj)->parent->parent;
192 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
195 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
196 KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
200 return koneplus_sysfs_read(fp, kobj, buf, off, count,
201 KONEPLUS_SIZE_PROFILE_SETTINGS,
202 KONEPLUS_COMMAND_PROFILE_SETTINGS);
205 static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
206 struct kobject *kobj, struct bin_attribute *attr, char *buf,
207 loff_t off, size_t count)
209 struct device *dev = kobj_to_dev(kobj)->parent->parent;
210 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
213 retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
214 KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
218 return koneplus_sysfs_read(fp, kobj, buf, off, count,
219 KONEPLUS_SIZE_PROFILE_BUTTONS,
220 KONEPLUS_COMMAND_PROFILE_BUTTONS);
223 #define PROFILE_ATTR(number) \
224 static struct bin_attribute bin_attr_profile##number##_settings = { \
225 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
226 .size = KONEPLUS_SIZE_PROFILE_SETTINGS, \
227 .read = koneplus_sysfs_read_profilex_settings, \
228 .private = &profile_numbers[number-1], \
230 static struct bin_attribute bin_attr_profile##number##_buttons = { \
231 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
232 .size = KONEPLUS_SIZE_PROFILE_BUTTONS, \
233 .read = koneplus_sysfs_read_profilex_buttons, \
234 .private = &profile_numbers[number-1], \
242 static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
243 struct device_attribute *attr, char *buf)
245 struct koneplus_device *koneplus =
246 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
247 return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
250 static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
251 struct device_attribute *attr, char const *buf, size_t size)
253 struct koneplus_device *koneplus;
254 struct usb_device *usb_dev;
255 unsigned long profile;
257 struct koneplus_roccat_report roccat_report;
259 dev = dev->parent->parent;
260 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
261 usb_dev = interface_to_usbdev(to_usb_interface(dev));
263 retval = kstrtoul(buf, 10, &profile);
270 mutex_lock(&koneplus->koneplus_lock);
272 retval = koneplus_set_actual_profile(usb_dev, profile);
274 mutex_unlock(&koneplus->koneplus_lock);
278 koneplus_profile_activated(koneplus, profile);
280 roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
281 roccat_report.data1 = profile + 1;
282 roccat_report.data2 = 0;
283 roccat_report.profile = profile + 1;
284 roccat_report_event(koneplus->chrdev_minor,
285 (uint8_t const *)&roccat_report);
287 mutex_unlock(&koneplus->koneplus_lock);
291 static DEVICE_ATTR(actual_profile, 0660,
292 koneplus_sysfs_show_actual_profile,
293 koneplus_sysfs_set_actual_profile);
294 static DEVICE_ATTR(startup_profile, 0660,
295 koneplus_sysfs_show_actual_profile,
296 koneplus_sysfs_set_actual_profile);
298 static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
299 struct device_attribute *attr, char *buf)
301 struct koneplus_device *koneplus;
302 struct usb_device *usb_dev;
303 struct koneplus_info info;
305 dev = dev->parent->parent;
306 koneplus = hid_get_drvdata(dev_get_drvdata(dev));
307 usb_dev = interface_to_usbdev(to_usb_interface(dev));
309 mutex_lock(&koneplus->koneplus_lock);
310 roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
311 &info, KONEPLUS_SIZE_INFO);
312 mutex_unlock(&koneplus->koneplus_lock);
314 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
316 static DEVICE_ATTR(firmware_version, 0440,
317 koneplus_sysfs_show_firmware_version, NULL);
319 static struct attribute *koneplus_attrs[] = {
320 &dev_attr_actual_profile.attr,
321 &dev_attr_startup_profile.attr,
322 &dev_attr_firmware_version.attr,
326 static struct bin_attribute *koneplus_bin_attributes[] = {
334 &bin_attr_profile_settings,
335 &bin_attr_profile_buttons,
336 &bin_attr_profile1_settings,
337 &bin_attr_profile2_settings,
338 &bin_attr_profile3_settings,
339 &bin_attr_profile4_settings,
340 &bin_attr_profile5_settings,
341 &bin_attr_profile1_buttons,
342 &bin_attr_profile2_buttons,
343 &bin_attr_profile3_buttons,
344 &bin_attr_profile4_buttons,
345 &bin_attr_profile5_buttons,
349 static const struct attribute_group koneplus_group = {
350 .attrs = koneplus_attrs,
351 .bin_attrs = koneplus_bin_attributes,
354 static const struct attribute_group *koneplus_groups[] = {
359 static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
360 struct koneplus_device *koneplus)
364 mutex_init(&koneplus->koneplus_lock);
366 retval = koneplus_get_actual_profile(usb_dev);
369 koneplus_profile_activated(koneplus, retval);
374 static int koneplus_init_specials(struct hid_device *hdev)
376 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
377 struct usb_device *usb_dev = interface_to_usbdev(intf);
378 struct koneplus_device *koneplus;
381 if (intf->cur_altsetting->desc.bInterfaceProtocol
382 == USB_INTERFACE_PROTOCOL_MOUSE) {
384 koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
386 hid_err(hdev, "can't alloc device descriptor\n");
389 hid_set_drvdata(hdev, koneplus);
391 retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
393 hid_err(hdev, "couldn't init struct koneplus_device\n");
397 retval = roccat_connect(koneplus_class, hdev,
398 sizeof(struct koneplus_roccat_report));
400 hid_err(hdev, "couldn't init char dev\n");
402 koneplus->chrdev_minor = retval;
403 koneplus->roccat_claimed = 1;
406 hid_set_drvdata(hdev, NULL);
415 static void koneplus_remove_specials(struct hid_device *hdev)
417 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
418 struct koneplus_device *koneplus;
420 if (intf->cur_altsetting->desc.bInterfaceProtocol
421 == USB_INTERFACE_PROTOCOL_MOUSE) {
422 koneplus = hid_get_drvdata(hdev);
423 if (koneplus->roccat_claimed)
424 roccat_disconnect(koneplus->chrdev_minor);
429 static int koneplus_probe(struct hid_device *hdev,
430 const struct hid_device_id *id)
434 if (!hid_is_usb(hdev))
437 retval = hid_parse(hdev);
439 hid_err(hdev, "parse failed\n");
443 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
445 hid_err(hdev, "hw start failed\n");
449 retval = koneplus_init_specials(hdev);
451 hid_err(hdev, "couldn't install mouse\n");
463 static void koneplus_remove(struct hid_device *hdev)
465 koneplus_remove_specials(hdev);
469 static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
472 struct koneplus_mouse_report_button const *button_report;
475 case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
476 button_report = (struct koneplus_mouse_report_button const *)data;
477 switch (button_report->type) {
478 case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
479 koneplus_profile_activated(koneplus, button_report->data1 - 1);
486 static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
489 struct koneplus_roccat_report roccat_report;
490 struct koneplus_mouse_report_button const *button_report;
492 if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
495 button_report = (struct koneplus_mouse_report_button const *)data;
497 if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
498 button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
499 button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
502 roccat_report.type = button_report->type;
503 roccat_report.data1 = button_report->data1;
504 roccat_report.data2 = button_report->data2;
505 roccat_report.profile = koneplus->actual_profile + 1;
506 roccat_report_event(koneplus->chrdev_minor,
507 (uint8_t const *)&roccat_report);
510 static int koneplus_raw_event(struct hid_device *hdev,
511 struct hid_report *report, u8 *data, int size)
513 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
514 struct koneplus_device *koneplus = hid_get_drvdata(hdev);
516 if (intf->cur_altsetting->desc.bInterfaceProtocol
517 != USB_INTERFACE_PROTOCOL_MOUSE)
520 if (koneplus == NULL)
523 koneplus_keep_values_up_to_date(koneplus, data);
525 if (koneplus->roccat_claimed)
526 koneplus_report_to_chrdev(koneplus, data);
531 static const struct hid_device_id koneplus_devices[] = {
532 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
533 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
537 MODULE_DEVICE_TABLE(hid, koneplus_devices);
539 static struct hid_driver koneplus_driver = {
541 .id_table = koneplus_devices,
542 .probe = koneplus_probe,
543 .remove = koneplus_remove,
544 .raw_event = koneplus_raw_event
547 static int __init koneplus_init(void)
551 /* class name has to be same as driver name */
552 koneplus_class = class_create(THIS_MODULE, "koneplus");
553 if (IS_ERR(koneplus_class))
554 return PTR_ERR(koneplus_class);
555 koneplus_class->dev_groups = koneplus_groups;
557 retval = hid_register_driver(&koneplus_driver);
559 class_destroy(koneplus_class);
563 static void __exit koneplus_exit(void)
565 hid_unregister_driver(&koneplus_driver);
566 class_destroy(koneplus_class);
569 module_init(koneplus_init);
570 module_exit(koneplus_exit);
572 MODULE_AUTHOR("Stefan Achatz");
573 MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
574 MODULE_LICENSE("GPL v2");