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 uint
kovaplus_convert_event_cpi(uint value
)
29 return (value
== 7 ? 4 : (value
== 4 ? 3 : value
));
32 static void kovaplus_profile_activated(struct kovaplus_device
*kovaplus
,
33 uint new_profile_index
)
35 if (new_profile_index
>= ARRAY_SIZE(kovaplus
->profile_settings
))
37 kovaplus
->actual_profile
= new_profile_index
;
38 kovaplus
->actual_cpi
= kovaplus
->profile_settings
[new_profile_index
].cpi_startup_level
;
39 kovaplus
->actual_x_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_x
;
40 kovaplus
->actual_y_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_y
;
43 static int kovaplus_send_control(struct usb_device
*usb_dev
, uint value
,
44 enum kovaplus_control_requests request
)
47 struct roccat_common2_control control
;
49 if ((request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
||
50 request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
54 control
.command
= ROCCAT_COMMON_COMMAND_CONTROL
;
55 control
.value
= value
;
56 control
.request
= request
;
58 retval
= roccat_common2_send(usb_dev
, ROCCAT_COMMON_COMMAND_CONTROL
,
59 &control
, sizeof(struct roccat_common2_control
));
64 static int kovaplus_select_profile(struct usb_device
*usb_dev
, uint number
,
65 enum kovaplus_control_requests request
)
67 return kovaplus_send_control(usb_dev
, number
, request
);
70 static int kovaplus_get_profile_settings(struct usb_device
*usb_dev
,
71 struct kovaplus_profile_settings
*buf
, uint number
)
75 retval
= kovaplus_select_profile(usb_dev
, number
,
76 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
80 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_SETTINGS
,
81 buf
, KOVAPLUS_SIZE_PROFILE_SETTINGS
);
84 static int kovaplus_get_profile_buttons(struct usb_device
*usb_dev
,
85 struct kovaplus_profile_buttons
*buf
, int number
)
89 retval
= kovaplus_select_profile(usb_dev
, number
,
90 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
94 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_BUTTONS
,
95 buf
, KOVAPLUS_SIZE_PROFILE_BUTTONS
);
98 /* retval is 0-4 on success, < 0 on error */
99 static int kovaplus_get_actual_profile(struct usb_device
*usb_dev
)
101 struct kovaplus_actual_profile buf
;
104 retval
= roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
105 &buf
, sizeof(struct kovaplus_actual_profile
));
107 return retval
? retval
: buf
.actual_profile
;
110 static int kovaplus_set_actual_profile(struct usb_device
*usb_dev
,
113 struct kovaplus_actual_profile buf
;
115 buf
.command
= KOVAPLUS_COMMAND_ACTUAL_PROFILE
;
116 buf
.size
= sizeof(struct kovaplus_actual_profile
);
117 buf
.actual_profile
= new_profile
;
119 return roccat_common2_send_with_status(usb_dev
,
120 KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
121 &buf
, sizeof(struct kovaplus_actual_profile
));
124 static ssize_t
kovaplus_sysfs_read(struct file
*fp
, struct kobject
*kobj
,
125 char *buf
, loff_t off
, size_t count
,
126 size_t real_size
, uint command
)
128 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
129 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
130 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
133 if (off
>= real_size
)
136 if (off
!= 0 || count
!= real_size
)
139 mutex_lock(&kovaplus
->kovaplus_lock
);
140 retval
= roccat_common2_receive(usb_dev
, command
, buf
, real_size
);
141 mutex_unlock(&kovaplus
->kovaplus_lock
);
149 static ssize_t
kovaplus_sysfs_write(struct file
*fp
, struct kobject
*kobj
,
150 void const *buf
, loff_t off
, size_t count
,
151 size_t real_size
, uint command
)
153 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
154 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
155 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
158 if (off
!= 0 || count
!= real_size
)
161 mutex_lock(&kovaplus
->kovaplus_lock
);
162 retval
= roccat_common2_send_with_status(usb_dev
, command
,
164 mutex_unlock(&kovaplus
->kovaplus_lock
);
172 #define KOVAPLUS_SYSFS_W(thingy, THINGY) \
173 static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \
174 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
175 loff_t off, size_t count) \
177 return kovaplus_sysfs_write(fp, kobj, buf, off, count, \
178 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
181 #define KOVAPLUS_SYSFS_R(thingy, THINGY) \
182 static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \
183 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
184 loff_t off, size_t count) \
186 return kovaplus_sysfs_read(fp, kobj, buf, off, count, \
187 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
190 #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \
191 KOVAPLUS_SYSFS_W(thingy, THINGY) \
192 KOVAPLUS_SYSFS_R(thingy, THINGY)
194 #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
195 KOVAPLUS_SYSFS_RW(thingy, THINGY); \
196 static struct bin_attribute bin_attr_##thingy = { \
197 .attr = { .name = #thingy, .mode = 0660 }, \
198 .size = KOVAPLUS_SIZE_ ## THINGY, \
199 .read = kovaplus_sysfs_read_ ## thingy, \
200 .write = kovaplus_sysfs_write_ ## thingy \
203 #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
204 KOVAPLUS_SYSFS_W(thingy, THINGY); \
205 static struct bin_attribute bin_attr_##thingy = { \
206 .attr = { .name = #thingy, .mode = 0220 }, \
207 .size = KOVAPLUS_SIZE_ ## THINGY, \
208 .write = kovaplus_sysfs_write_ ## thingy \
210 KOVAPLUS_BIN_ATTRIBUTE_W(control
, CONTROL
);
211 KOVAPLUS_BIN_ATTRIBUTE_RW(info
, INFO
);
212 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings
, PROFILE_SETTINGS
);
213 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons
, PROFILE_BUTTONS
);
215 static ssize_t
kovaplus_sysfs_read_profilex_settings(struct file
*fp
,
216 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
217 loff_t off
, size_t count
)
219 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
220 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
223 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
224 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
228 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
229 KOVAPLUS_SIZE_PROFILE_SETTINGS
,
230 KOVAPLUS_COMMAND_PROFILE_SETTINGS
);
233 static ssize_t
kovaplus_sysfs_read_profilex_buttons(struct file
*fp
,
234 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
235 loff_t off
, size_t count
)
237 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
238 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
241 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
242 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
246 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
247 KOVAPLUS_SIZE_PROFILE_BUTTONS
,
248 KOVAPLUS_COMMAND_PROFILE_BUTTONS
);
251 #define PROFILE_ATTR(number) \
252 static struct bin_attribute bin_attr_profile##number##_settings = { \
253 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
254 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \
255 .read = kovaplus_sysfs_read_profilex_settings, \
256 .private = &profile_numbers[number-1], \
258 static struct bin_attribute bin_attr_profile##number##_buttons = { \
259 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
260 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \
261 .read = kovaplus_sysfs_read_profilex_buttons, \
262 .private = &profile_numbers[number-1], \
270 static ssize_t
kovaplus_sysfs_show_actual_profile(struct device
*dev
,
271 struct device_attribute
*attr
, char *buf
)
273 struct kovaplus_device
*kovaplus
=
274 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
275 return sysfs_emit(buf
, "%d\n", kovaplus
->actual_profile
);
278 static ssize_t
kovaplus_sysfs_set_actual_profile(struct device
*dev
,
279 struct device_attribute
*attr
, char const *buf
, size_t size
)
281 struct kovaplus_device
*kovaplus
;
282 struct usb_device
*usb_dev
;
283 unsigned long profile
;
285 struct kovaplus_roccat_report roccat_report
;
287 dev
= dev
->parent
->parent
;
288 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
289 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
291 retval
= kstrtoul(buf
, 10, &profile
);
298 mutex_lock(&kovaplus
->kovaplus_lock
);
299 retval
= kovaplus_set_actual_profile(usb_dev
, profile
);
301 mutex_unlock(&kovaplus
->kovaplus_lock
);
305 kovaplus_profile_activated(kovaplus
, profile
);
307 roccat_report
.type
= KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
;
308 roccat_report
.profile
= profile
+ 1;
309 roccat_report
.button
= 0;
310 roccat_report
.data1
= profile
+ 1;
311 roccat_report
.data2
= 0;
312 roccat_report_event(kovaplus
->chrdev_minor
,
313 (uint8_t const *)&roccat_report
);
315 mutex_unlock(&kovaplus
->kovaplus_lock
);
319 static DEVICE_ATTR(actual_profile
, 0660,
320 kovaplus_sysfs_show_actual_profile
,
321 kovaplus_sysfs_set_actual_profile
);
323 static ssize_t
kovaplus_sysfs_show_actual_cpi(struct device
*dev
,
324 struct device_attribute
*attr
, char *buf
)
326 struct kovaplus_device
*kovaplus
=
327 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
328 return sysfs_emit(buf
, "%d\n", kovaplus
->actual_cpi
);
330 static DEVICE_ATTR(actual_cpi
, 0440, kovaplus_sysfs_show_actual_cpi
, NULL
);
332 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_x(struct device
*dev
,
333 struct device_attribute
*attr
, char *buf
)
335 struct kovaplus_device
*kovaplus
=
336 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
337 return sysfs_emit(buf
, "%d\n", kovaplus
->actual_x_sensitivity
);
339 static DEVICE_ATTR(actual_sensitivity_x
, 0440,
340 kovaplus_sysfs_show_actual_sensitivity_x
, NULL
);
342 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_y(struct device
*dev
,
343 struct device_attribute
*attr
, char *buf
)
345 struct kovaplus_device
*kovaplus
=
346 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
347 return sysfs_emit(buf
, "%d\n", kovaplus
->actual_y_sensitivity
);
349 static DEVICE_ATTR(actual_sensitivity_y
, 0440,
350 kovaplus_sysfs_show_actual_sensitivity_y
, NULL
);
352 static ssize_t
kovaplus_sysfs_show_firmware_version(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
355 struct kovaplus_device
*kovaplus
;
356 struct usb_device
*usb_dev
;
357 struct kovaplus_info info
;
359 dev
= dev
->parent
->parent
;
360 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
361 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
363 mutex_lock(&kovaplus
->kovaplus_lock
);
364 roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_INFO
,
365 &info
, KOVAPLUS_SIZE_INFO
);
366 mutex_unlock(&kovaplus
->kovaplus_lock
);
368 return sysfs_emit(buf
, "%d\n", info
.firmware_version
);
370 static DEVICE_ATTR(firmware_version
, 0440,
371 kovaplus_sysfs_show_firmware_version
, NULL
);
373 static struct attribute
*kovaplus_attrs
[] = {
374 &dev_attr_actual_cpi
.attr
,
375 &dev_attr_firmware_version
.attr
,
376 &dev_attr_actual_profile
.attr
,
377 &dev_attr_actual_sensitivity_x
.attr
,
378 &dev_attr_actual_sensitivity_y
.attr
,
382 static struct bin_attribute
*kovaplus_bin_attributes
[] = {
385 &bin_attr_profile_settings
,
386 &bin_attr_profile_buttons
,
387 &bin_attr_profile1_settings
,
388 &bin_attr_profile2_settings
,
389 &bin_attr_profile3_settings
,
390 &bin_attr_profile4_settings
,
391 &bin_attr_profile5_settings
,
392 &bin_attr_profile1_buttons
,
393 &bin_attr_profile2_buttons
,
394 &bin_attr_profile3_buttons
,
395 &bin_attr_profile4_buttons
,
396 &bin_attr_profile5_buttons
,
400 static const struct attribute_group kovaplus_group
= {
401 .attrs
= kovaplus_attrs
,
402 .bin_attrs
= kovaplus_bin_attributes
,
405 static const struct attribute_group
*kovaplus_groups
[] = {
410 static const struct class kovaplus_class
= {
412 .dev_groups
= kovaplus_groups
,
415 static int kovaplus_init_kovaplus_device_struct(struct usb_device
*usb_dev
,
416 struct kovaplus_device
*kovaplus
)
419 static uint wait
= 70; /* device will freeze with just 60 */
421 mutex_init(&kovaplus
->kovaplus_lock
);
423 for (i
= 0; i
< 5; ++i
) {
425 retval
= kovaplus_get_profile_settings(usb_dev
,
426 &kovaplus
->profile_settings
[i
], i
);
431 retval
= kovaplus_get_profile_buttons(usb_dev
,
432 &kovaplus
->profile_buttons
[i
], i
);
438 retval
= kovaplus_get_actual_profile(usb_dev
);
441 kovaplus_profile_activated(kovaplus
, retval
);
446 static int kovaplus_init_specials(struct hid_device
*hdev
)
448 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
449 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
450 struct kovaplus_device
*kovaplus
;
453 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
454 == USB_INTERFACE_PROTOCOL_MOUSE
) {
456 kovaplus
= kzalloc(sizeof(*kovaplus
), GFP_KERNEL
);
458 hid_err(hdev
, "can't alloc device descriptor\n");
461 hid_set_drvdata(hdev
, kovaplus
);
463 retval
= kovaplus_init_kovaplus_device_struct(usb_dev
, kovaplus
);
465 hid_err(hdev
, "couldn't init struct kovaplus_device\n");
469 retval
= roccat_connect(&kovaplus_class
, hdev
,
470 sizeof(struct kovaplus_roccat_report
));
472 hid_err(hdev
, "couldn't init char dev\n");
474 kovaplus
->chrdev_minor
= retval
;
475 kovaplus
->roccat_claimed
= 1;
479 hid_set_drvdata(hdev
, NULL
);
488 static void kovaplus_remove_specials(struct hid_device
*hdev
)
490 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
491 struct kovaplus_device
*kovaplus
;
493 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
494 == USB_INTERFACE_PROTOCOL_MOUSE
) {
495 kovaplus
= hid_get_drvdata(hdev
);
496 if (kovaplus
->roccat_claimed
)
497 roccat_disconnect(kovaplus
->chrdev_minor
);
502 static int kovaplus_probe(struct hid_device
*hdev
,
503 const struct hid_device_id
*id
)
507 if (!hid_is_usb(hdev
))
510 retval
= hid_parse(hdev
);
512 hid_err(hdev
, "parse failed\n");
516 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
518 hid_err(hdev
, "hw start failed\n");
522 retval
= kovaplus_init_specials(hdev
);
524 hid_err(hdev
, "couldn't install mouse\n");
536 static void kovaplus_remove(struct hid_device
*hdev
)
538 kovaplus_remove_specials(hdev
);
542 static void kovaplus_keep_values_up_to_date(struct kovaplus_device
*kovaplus
,
545 struct kovaplus_mouse_report_button
const *button_report
;
547 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
550 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
552 switch (button_report
->type
) {
553 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
:
554 kovaplus_profile_activated(kovaplus
, button_report
->data1
- 1);
556 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
:
557 kovaplus
->actual_cpi
= kovaplus_convert_event_cpi(button_report
->data1
);
559 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY
:
560 kovaplus
->actual_x_sensitivity
= button_report
->data1
;
561 kovaplus
->actual_y_sensitivity
= button_report
->data2
;
568 static void kovaplus_report_to_chrdev(struct kovaplus_device
const *kovaplus
,
571 struct kovaplus_roccat_report roccat_report
;
572 struct kovaplus_mouse_report_button
const *button_report
;
574 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
577 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
579 if (button_report
->type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2
)
582 roccat_report
.type
= button_report
->type
;
583 roccat_report
.profile
= kovaplus
->actual_profile
+ 1;
585 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO
||
586 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT
||
587 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH
||
588 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER
)
589 roccat_report
.button
= button_report
->data1
;
591 roccat_report
.button
= 0;
593 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
)
594 roccat_report
.data1
= kovaplus_convert_event_cpi(button_report
->data1
);
596 roccat_report
.data1
= button_report
->data1
;
598 roccat_report
.data2
= button_report
->data2
;
600 roccat_report_event(kovaplus
->chrdev_minor
,
601 (uint8_t const *)&roccat_report
);
604 static int kovaplus_raw_event(struct hid_device
*hdev
,
605 struct hid_report
*report
, u8
*data
, int size
)
607 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
608 struct kovaplus_device
*kovaplus
= hid_get_drvdata(hdev
);
610 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
611 != USB_INTERFACE_PROTOCOL_MOUSE
)
614 if (kovaplus
== NULL
)
617 kovaplus_keep_values_up_to_date(kovaplus
, data
);
619 if (kovaplus
->roccat_claimed
)
620 kovaplus_report_to_chrdev(kovaplus
, data
);
625 static const struct hid_device_id kovaplus_devices
[] = {
626 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
, USB_DEVICE_ID_ROCCAT_KOVAPLUS
) },
630 MODULE_DEVICE_TABLE(hid
, kovaplus_devices
);
632 static struct hid_driver kovaplus_driver
= {
634 .id_table
= kovaplus_devices
,
635 .probe
= kovaplus_probe
,
636 .remove
= kovaplus_remove
,
637 .raw_event
= kovaplus_raw_event
640 static int __init
kovaplus_init(void)
644 retval
= class_register(&kovaplus_class
);
648 retval
= hid_register_driver(&kovaplus_driver
);
650 class_unregister(&kovaplus_class
);
654 static void __exit
kovaplus_exit(void)
656 hid_unregister_driver(&kovaplus_driver
);
657 class_unregister(&kovaplus_class
);
660 module_init(kovaplus_init
);
661 module_exit(kovaplus_exit
);
663 MODULE_AUTHOR("Stefan Achatz");
664 MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
665 MODULE_LICENSE("GPL v2");