1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Roccat Kova[+] driver for Linux
5 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
12 * Roccat Kova[+] is a bigger version of the Pyra with two more side buttons.
15 #include <linux/device.h>
16 #include <linux/input.h>
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/hid-roccat.h>
22 #include "hid-roccat-common.h"
23 #include "hid-roccat-kovaplus.h"
25 static uint profile_numbers
[5] = {0, 1, 2, 3, 4};
27 static struct class *kovaplus_class
;
29 static uint
kovaplus_convert_event_cpi(uint value
)
31 return (value
== 7 ? 4 : (value
== 4 ? 3 : value
));
34 static void kovaplus_profile_activated(struct kovaplus_device
*kovaplus
,
35 uint new_profile_index
)
37 if (new_profile_index
>= ARRAY_SIZE(kovaplus
->profile_settings
))
39 kovaplus
->actual_profile
= new_profile_index
;
40 kovaplus
->actual_cpi
= kovaplus
->profile_settings
[new_profile_index
].cpi_startup_level
;
41 kovaplus
->actual_x_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_x
;
42 kovaplus
->actual_y_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_y
;
45 static int kovaplus_send_control(struct usb_device
*usb_dev
, uint value
,
46 enum kovaplus_control_requests request
)
49 struct roccat_common2_control control
;
51 if ((request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
||
52 request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
56 control
.command
= ROCCAT_COMMON_COMMAND_CONTROL
;
57 control
.value
= value
;
58 control
.request
= request
;
60 retval
= roccat_common2_send(usb_dev
, ROCCAT_COMMON_COMMAND_CONTROL
,
61 &control
, sizeof(struct roccat_common2_control
));
66 static int kovaplus_select_profile(struct usb_device
*usb_dev
, uint number
,
67 enum kovaplus_control_requests request
)
69 return kovaplus_send_control(usb_dev
, number
, request
);
72 static int kovaplus_get_profile_settings(struct usb_device
*usb_dev
,
73 struct kovaplus_profile_settings
*buf
, uint number
)
77 retval
= kovaplus_select_profile(usb_dev
, number
,
78 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
82 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_SETTINGS
,
83 buf
, KOVAPLUS_SIZE_PROFILE_SETTINGS
);
86 static int kovaplus_get_profile_buttons(struct usb_device
*usb_dev
,
87 struct kovaplus_profile_buttons
*buf
, int number
)
91 retval
= kovaplus_select_profile(usb_dev
, number
,
92 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
96 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_BUTTONS
,
97 buf
, KOVAPLUS_SIZE_PROFILE_BUTTONS
);
100 /* retval is 0-4 on success, < 0 on error */
101 static int kovaplus_get_actual_profile(struct usb_device
*usb_dev
)
103 struct kovaplus_actual_profile buf
;
106 retval
= roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
107 &buf
, sizeof(struct kovaplus_actual_profile
));
109 return retval
? retval
: buf
.actual_profile
;
112 static int kovaplus_set_actual_profile(struct usb_device
*usb_dev
,
115 struct kovaplus_actual_profile buf
;
117 buf
.command
= KOVAPLUS_COMMAND_ACTUAL_PROFILE
;
118 buf
.size
= sizeof(struct kovaplus_actual_profile
);
119 buf
.actual_profile
= new_profile
;
121 return roccat_common2_send_with_status(usb_dev
,
122 KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
123 &buf
, sizeof(struct kovaplus_actual_profile
));
126 static ssize_t
kovaplus_sysfs_read(struct file
*fp
, struct kobject
*kobj
,
127 char *buf
, loff_t off
, size_t count
,
128 size_t real_size
, uint command
)
130 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
131 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
132 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
135 if (off
>= real_size
)
138 if (off
!= 0 || count
!= real_size
)
141 mutex_lock(&kovaplus
->kovaplus_lock
);
142 retval
= roccat_common2_receive(usb_dev
, command
, buf
, real_size
);
143 mutex_unlock(&kovaplus
->kovaplus_lock
);
151 static ssize_t
kovaplus_sysfs_write(struct file
*fp
, struct kobject
*kobj
,
152 void const *buf
, loff_t off
, size_t count
,
153 size_t real_size
, uint command
)
155 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
156 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
157 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
160 if (off
!= 0 || count
!= real_size
)
163 mutex_lock(&kovaplus
->kovaplus_lock
);
164 retval
= roccat_common2_send_with_status(usb_dev
, command
,
166 mutex_unlock(&kovaplus
->kovaplus_lock
);
174 #define KOVAPLUS_SYSFS_W(thingy, THINGY) \
175 static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \
176 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
177 loff_t off, size_t count) \
179 return kovaplus_sysfs_write(fp, kobj, buf, off, count, \
180 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
183 #define KOVAPLUS_SYSFS_R(thingy, THINGY) \
184 static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \
185 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
186 loff_t off, size_t count) \
188 return kovaplus_sysfs_read(fp, kobj, buf, off, count, \
189 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
192 #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \
193 KOVAPLUS_SYSFS_W(thingy, THINGY) \
194 KOVAPLUS_SYSFS_R(thingy, THINGY)
196 #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
197 KOVAPLUS_SYSFS_RW(thingy, THINGY); \
198 static struct bin_attribute bin_attr_##thingy = { \
199 .attr = { .name = #thingy, .mode = 0660 }, \
200 .size = KOVAPLUS_SIZE_ ## THINGY, \
201 .read = kovaplus_sysfs_read_ ## thingy, \
202 .write = kovaplus_sysfs_write_ ## thingy \
205 #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
206 KOVAPLUS_SYSFS_W(thingy, THINGY); \
207 static struct bin_attribute bin_attr_##thingy = { \
208 .attr = { .name = #thingy, .mode = 0220 }, \
209 .size = KOVAPLUS_SIZE_ ## THINGY, \
210 .write = kovaplus_sysfs_write_ ## thingy \
212 KOVAPLUS_BIN_ATTRIBUTE_W(control
, CONTROL
);
213 KOVAPLUS_BIN_ATTRIBUTE_RW(info
, INFO
);
214 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings
, PROFILE_SETTINGS
);
215 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons
, PROFILE_BUTTONS
);
217 static ssize_t
kovaplus_sysfs_read_profilex_settings(struct file
*fp
,
218 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
219 loff_t off
, size_t count
)
221 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
222 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
225 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
226 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
230 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
231 KOVAPLUS_SIZE_PROFILE_SETTINGS
,
232 KOVAPLUS_COMMAND_PROFILE_SETTINGS
);
235 static ssize_t
kovaplus_sysfs_read_profilex_buttons(struct file
*fp
,
236 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
237 loff_t off
, size_t count
)
239 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
240 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
243 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
244 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
248 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
249 KOVAPLUS_SIZE_PROFILE_BUTTONS
,
250 KOVAPLUS_COMMAND_PROFILE_BUTTONS
);
253 #define PROFILE_ATTR(number) \
254 static struct bin_attribute bin_attr_profile##number##_settings = { \
255 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
256 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \
257 .read = kovaplus_sysfs_read_profilex_settings, \
258 .private = &profile_numbers[number-1], \
260 static struct bin_attribute bin_attr_profile##number##_buttons = { \
261 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
262 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \
263 .read = kovaplus_sysfs_read_profilex_buttons, \
264 .private = &profile_numbers[number-1], \
272 static ssize_t
kovaplus_sysfs_show_actual_profile(struct device
*dev
,
273 struct device_attribute
*attr
, char *buf
)
275 struct kovaplus_device
*kovaplus
=
276 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
277 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_profile
);
280 static ssize_t
kovaplus_sysfs_set_actual_profile(struct device
*dev
,
281 struct device_attribute
*attr
, char const *buf
, size_t size
)
283 struct kovaplus_device
*kovaplus
;
284 struct usb_device
*usb_dev
;
285 unsigned long profile
;
287 struct kovaplus_roccat_report roccat_report
;
289 dev
= dev
->parent
->parent
;
290 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
291 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
293 retval
= kstrtoul(buf
, 10, &profile
);
300 mutex_lock(&kovaplus
->kovaplus_lock
);
301 retval
= kovaplus_set_actual_profile(usb_dev
, profile
);
303 mutex_unlock(&kovaplus
->kovaplus_lock
);
307 kovaplus_profile_activated(kovaplus
, profile
);
309 roccat_report
.type
= KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
;
310 roccat_report
.profile
= profile
+ 1;
311 roccat_report
.button
= 0;
312 roccat_report
.data1
= profile
+ 1;
313 roccat_report
.data2
= 0;
314 roccat_report_event(kovaplus
->chrdev_minor
,
315 (uint8_t const *)&roccat_report
);
317 mutex_unlock(&kovaplus
->kovaplus_lock
);
321 static DEVICE_ATTR(actual_profile
, 0660,
322 kovaplus_sysfs_show_actual_profile
,
323 kovaplus_sysfs_set_actual_profile
);
325 static ssize_t
kovaplus_sysfs_show_actual_cpi(struct device
*dev
,
326 struct device_attribute
*attr
, char *buf
)
328 struct kovaplus_device
*kovaplus
=
329 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
330 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_cpi
);
332 static DEVICE_ATTR(actual_cpi
, 0440, kovaplus_sysfs_show_actual_cpi
, NULL
);
334 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_x(struct device
*dev
,
335 struct device_attribute
*attr
, char *buf
)
337 struct kovaplus_device
*kovaplus
=
338 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
339 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_x_sensitivity
);
341 static DEVICE_ATTR(actual_sensitivity_x
, 0440,
342 kovaplus_sysfs_show_actual_sensitivity_x
, NULL
);
344 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_y(struct device
*dev
,
345 struct device_attribute
*attr
, char *buf
)
347 struct kovaplus_device
*kovaplus
=
348 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
349 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_y_sensitivity
);
351 static DEVICE_ATTR(actual_sensitivity_y
, 0440,
352 kovaplus_sysfs_show_actual_sensitivity_y
, NULL
);
354 static ssize_t
kovaplus_sysfs_show_firmware_version(struct device
*dev
,
355 struct device_attribute
*attr
, char *buf
)
357 struct kovaplus_device
*kovaplus
;
358 struct usb_device
*usb_dev
;
359 struct kovaplus_info info
;
361 dev
= dev
->parent
->parent
;
362 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
363 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
365 mutex_lock(&kovaplus
->kovaplus_lock
);
366 roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_INFO
,
367 &info
, KOVAPLUS_SIZE_INFO
);
368 mutex_unlock(&kovaplus
->kovaplus_lock
);
370 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.firmware_version
);
372 static DEVICE_ATTR(firmware_version
, 0440,
373 kovaplus_sysfs_show_firmware_version
, NULL
);
375 static struct attribute
*kovaplus_attrs
[] = {
376 &dev_attr_actual_cpi
.attr
,
377 &dev_attr_firmware_version
.attr
,
378 &dev_attr_actual_profile
.attr
,
379 &dev_attr_actual_sensitivity_x
.attr
,
380 &dev_attr_actual_sensitivity_y
.attr
,
384 static struct bin_attribute
*kovaplus_bin_attributes
[] = {
387 &bin_attr_profile_settings
,
388 &bin_attr_profile_buttons
,
389 &bin_attr_profile1_settings
,
390 &bin_attr_profile2_settings
,
391 &bin_attr_profile3_settings
,
392 &bin_attr_profile4_settings
,
393 &bin_attr_profile5_settings
,
394 &bin_attr_profile1_buttons
,
395 &bin_attr_profile2_buttons
,
396 &bin_attr_profile3_buttons
,
397 &bin_attr_profile4_buttons
,
398 &bin_attr_profile5_buttons
,
402 static const struct attribute_group kovaplus_group
= {
403 .attrs
= kovaplus_attrs
,
404 .bin_attrs
= kovaplus_bin_attributes
,
407 static const struct attribute_group
*kovaplus_groups
[] = {
412 static int kovaplus_init_kovaplus_device_struct(struct usb_device
*usb_dev
,
413 struct kovaplus_device
*kovaplus
)
416 static uint wait
= 70; /* device will freeze with just 60 */
418 mutex_init(&kovaplus
->kovaplus_lock
);
420 for (i
= 0; i
< 5; ++i
) {
422 retval
= kovaplus_get_profile_settings(usb_dev
,
423 &kovaplus
->profile_settings
[i
], i
);
428 retval
= kovaplus_get_profile_buttons(usb_dev
,
429 &kovaplus
->profile_buttons
[i
], i
);
435 retval
= kovaplus_get_actual_profile(usb_dev
);
438 kovaplus_profile_activated(kovaplus
, retval
);
443 static int kovaplus_init_specials(struct hid_device
*hdev
)
445 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
446 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
447 struct kovaplus_device
*kovaplus
;
450 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
451 == USB_INTERFACE_PROTOCOL_MOUSE
) {
453 kovaplus
= kzalloc(sizeof(*kovaplus
), GFP_KERNEL
);
455 hid_err(hdev
, "can't alloc device descriptor\n");
458 hid_set_drvdata(hdev
, kovaplus
);
460 retval
= kovaplus_init_kovaplus_device_struct(usb_dev
, kovaplus
);
462 hid_err(hdev
, "couldn't init struct kovaplus_device\n");
466 retval
= roccat_connect(kovaplus_class
, hdev
,
467 sizeof(struct kovaplus_roccat_report
));
469 hid_err(hdev
, "couldn't init char dev\n");
471 kovaplus
->chrdev_minor
= retval
;
472 kovaplus
->roccat_claimed
= 1;
476 hid_set_drvdata(hdev
, NULL
);
485 static void kovaplus_remove_specials(struct hid_device
*hdev
)
487 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
488 struct kovaplus_device
*kovaplus
;
490 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
491 == USB_INTERFACE_PROTOCOL_MOUSE
) {
492 kovaplus
= hid_get_drvdata(hdev
);
493 if (kovaplus
->roccat_claimed
)
494 roccat_disconnect(kovaplus
->chrdev_minor
);
499 static int kovaplus_probe(struct hid_device
*hdev
,
500 const struct hid_device_id
*id
)
504 retval
= hid_parse(hdev
);
506 hid_err(hdev
, "parse failed\n");
510 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
512 hid_err(hdev
, "hw start failed\n");
516 retval
= kovaplus_init_specials(hdev
);
518 hid_err(hdev
, "couldn't install mouse\n");
530 static void kovaplus_remove(struct hid_device
*hdev
)
532 kovaplus_remove_specials(hdev
);
536 static void kovaplus_keep_values_up_to_date(struct kovaplus_device
*kovaplus
,
539 struct kovaplus_mouse_report_button
const *button_report
;
541 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
544 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
546 switch (button_report
->type
) {
547 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
:
548 kovaplus_profile_activated(kovaplus
, button_report
->data1
- 1);
550 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
:
551 kovaplus
->actual_cpi
= kovaplus_convert_event_cpi(button_report
->data1
);
553 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY
:
554 kovaplus
->actual_x_sensitivity
= button_report
->data1
;
555 kovaplus
->actual_y_sensitivity
= button_report
->data2
;
562 static void kovaplus_report_to_chrdev(struct kovaplus_device
const *kovaplus
,
565 struct kovaplus_roccat_report roccat_report
;
566 struct kovaplus_mouse_report_button
const *button_report
;
568 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
571 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
573 if (button_report
->type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2
)
576 roccat_report
.type
= button_report
->type
;
577 roccat_report
.profile
= kovaplus
->actual_profile
+ 1;
579 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO
||
580 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT
||
581 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH
||
582 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER
)
583 roccat_report
.button
= button_report
->data1
;
585 roccat_report
.button
= 0;
587 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
)
588 roccat_report
.data1
= kovaplus_convert_event_cpi(button_report
->data1
);
590 roccat_report
.data1
= button_report
->data1
;
592 roccat_report
.data2
= button_report
->data2
;
594 roccat_report_event(kovaplus
->chrdev_minor
,
595 (uint8_t const *)&roccat_report
);
598 static int kovaplus_raw_event(struct hid_device
*hdev
,
599 struct hid_report
*report
, u8
*data
, int size
)
601 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
602 struct kovaplus_device
*kovaplus
= hid_get_drvdata(hdev
);
604 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
605 != USB_INTERFACE_PROTOCOL_MOUSE
)
608 if (kovaplus
== NULL
)
611 kovaplus_keep_values_up_to_date(kovaplus
, data
);
613 if (kovaplus
->roccat_claimed
)
614 kovaplus_report_to_chrdev(kovaplus
, data
);
619 static const struct hid_device_id kovaplus_devices
[] = {
620 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
, USB_DEVICE_ID_ROCCAT_KOVAPLUS
) },
624 MODULE_DEVICE_TABLE(hid
, kovaplus_devices
);
626 static struct hid_driver kovaplus_driver
= {
628 .id_table
= kovaplus_devices
,
629 .probe
= kovaplus_probe
,
630 .remove
= kovaplus_remove
,
631 .raw_event
= kovaplus_raw_event
634 static int __init
kovaplus_init(void)
638 kovaplus_class
= class_create(THIS_MODULE
, "kovaplus");
639 if (IS_ERR(kovaplus_class
))
640 return PTR_ERR(kovaplus_class
);
641 kovaplus_class
->dev_groups
= kovaplus_groups
;
643 retval
= hid_register_driver(&kovaplus_driver
);
645 class_destroy(kovaplus_class
);
649 static void __exit
kovaplus_exit(void)
651 hid_unregister_driver(&kovaplus_driver
);
652 class_destroy(kovaplus_class
);
655 module_init(kovaplus_init
);
656 module_exit(kovaplus_exit
);
658 MODULE_AUTHOR("Stefan Achatz");
659 MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
660 MODULE_LICENSE("GPL v2");