2 * Roccat Kova[+] driver for Linux
4 * Copyright (c) 2011 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 Kova[+] is a bigger version of the Pyra with two more side buttons.
18 #include <linux/device.h>
19 #include <linux/input.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/hid-roccat.h>
25 #include "hid-roccat-common.h"
26 #include "hid-roccat-kovaplus.h"
28 static uint profile_numbers
[5] = {0, 1, 2, 3, 4};
30 static struct class *kovaplus_class
;
32 static uint
kovaplus_convert_event_cpi(uint value
)
34 return (value
== 7 ? 4 : (value
== 4 ? 3 : value
));
37 static void kovaplus_profile_activated(struct kovaplus_device
*kovaplus
,
38 uint new_profile_index
)
40 if (new_profile_index
>= ARRAY_SIZE(kovaplus
->profile_settings
))
42 kovaplus
->actual_profile
= new_profile_index
;
43 kovaplus
->actual_cpi
= kovaplus
->profile_settings
[new_profile_index
].cpi_startup_level
;
44 kovaplus
->actual_x_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_x
;
45 kovaplus
->actual_y_sensitivity
= kovaplus
->profile_settings
[new_profile_index
].sensitivity_y
;
48 static int kovaplus_send_control(struct usb_device
*usb_dev
, uint value
,
49 enum kovaplus_control_requests request
)
52 struct roccat_common2_control control
;
54 if ((request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
||
55 request
== KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
59 control
.command
= ROCCAT_COMMON_COMMAND_CONTROL
;
60 control
.value
= value
;
61 control
.request
= request
;
63 retval
= roccat_common2_send(usb_dev
, ROCCAT_COMMON_COMMAND_CONTROL
,
64 &control
, sizeof(struct roccat_common2_control
));
69 static int kovaplus_select_profile(struct usb_device
*usb_dev
, uint number
,
70 enum kovaplus_control_requests request
)
72 return kovaplus_send_control(usb_dev
, number
, request
);
75 static int kovaplus_get_profile_settings(struct usb_device
*usb_dev
,
76 struct kovaplus_profile_settings
*buf
, uint number
)
80 retval
= kovaplus_select_profile(usb_dev
, number
,
81 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
85 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_SETTINGS
,
86 buf
, KOVAPLUS_SIZE_PROFILE_SETTINGS
);
89 static int kovaplus_get_profile_buttons(struct usb_device
*usb_dev
,
90 struct kovaplus_profile_buttons
*buf
, int number
)
94 retval
= kovaplus_select_profile(usb_dev
, number
,
95 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
99 return roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_PROFILE_BUTTONS
,
100 buf
, KOVAPLUS_SIZE_PROFILE_BUTTONS
);
103 /* retval is 0-4 on success, < 0 on error */
104 static int kovaplus_get_actual_profile(struct usb_device
*usb_dev
)
106 struct kovaplus_actual_profile buf
;
109 retval
= roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
110 &buf
, sizeof(struct kovaplus_actual_profile
));
112 return retval
? retval
: buf
.actual_profile
;
115 static int kovaplus_set_actual_profile(struct usb_device
*usb_dev
,
118 struct kovaplus_actual_profile buf
;
120 buf
.command
= KOVAPLUS_COMMAND_ACTUAL_PROFILE
;
121 buf
.size
= sizeof(struct kovaplus_actual_profile
);
122 buf
.actual_profile
= new_profile
;
124 return roccat_common2_send_with_status(usb_dev
,
125 KOVAPLUS_COMMAND_ACTUAL_PROFILE
,
126 &buf
, sizeof(struct kovaplus_actual_profile
));
129 static ssize_t
kovaplus_sysfs_read(struct file
*fp
, struct kobject
*kobj
,
130 char *buf
, loff_t off
, size_t count
,
131 size_t real_size
, uint command
)
133 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
134 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
135 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
138 if (off
>= real_size
)
141 if (off
!= 0 || count
!= real_size
)
144 mutex_lock(&kovaplus
->kovaplus_lock
);
145 retval
= roccat_common2_receive(usb_dev
, command
, buf
, real_size
);
146 mutex_unlock(&kovaplus
->kovaplus_lock
);
154 static ssize_t
kovaplus_sysfs_write(struct file
*fp
, struct kobject
*kobj
,
155 void const *buf
, loff_t off
, size_t count
,
156 size_t real_size
, uint command
)
158 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
159 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
160 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
163 if (off
!= 0 || count
!= real_size
)
166 mutex_lock(&kovaplus
->kovaplus_lock
);
167 retval
= roccat_common2_send_with_status(usb_dev
, command
,
169 mutex_unlock(&kovaplus
->kovaplus_lock
);
177 #define KOVAPLUS_SYSFS_W(thingy, THINGY) \
178 static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \
179 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
180 loff_t off, size_t count) \
182 return kovaplus_sysfs_write(fp, kobj, buf, off, count, \
183 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
186 #define KOVAPLUS_SYSFS_R(thingy, THINGY) \
187 static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \
188 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
189 loff_t off, size_t count) \
191 return kovaplus_sysfs_read(fp, kobj, buf, off, count, \
192 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
195 #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \
196 KOVAPLUS_SYSFS_W(thingy, THINGY) \
197 KOVAPLUS_SYSFS_R(thingy, THINGY)
199 #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
200 KOVAPLUS_SYSFS_RW(thingy, THINGY); \
201 static struct bin_attribute bin_attr_##thingy = { \
202 .attr = { .name = #thingy, .mode = 0660 }, \
203 .size = KOVAPLUS_SIZE_ ## THINGY, \
204 .read = kovaplus_sysfs_read_ ## thingy, \
205 .write = kovaplus_sysfs_write_ ## thingy \
208 #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
209 KOVAPLUS_SYSFS_W(thingy, THINGY); \
210 static struct bin_attribute bin_attr_##thingy = { \
211 .attr = { .name = #thingy, .mode = 0220 }, \
212 .size = KOVAPLUS_SIZE_ ## THINGY, \
213 .write = kovaplus_sysfs_write_ ## thingy \
215 KOVAPLUS_BIN_ATTRIBUTE_W(control
, CONTROL
);
216 KOVAPLUS_BIN_ATTRIBUTE_RW(info
, INFO
);
217 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings
, PROFILE_SETTINGS
);
218 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons
, PROFILE_BUTTONS
);
220 static ssize_t
kovaplus_sysfs_read_profilex_settings(struct file
*fp
,
221 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
222 loff_t off
, size_t count
)
224 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
225 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
228 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
229 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
233 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
234 KOVAPLUS_SIZE_PROFILE_SETTINGS
,
235 KOVAPLUS_COMMAND_PROFILE_SETTINGS
);
238 static ssize_t
kovaplus_sysfs_read_profilex_buttons(struct file
*fp
,
239 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
240 loff_t off
, size_t count
)
242 struct device
*dev
= kobj_to_dev(kobj
)->parent
->parent
;
243 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
246 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
247 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
251 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
252 KOVAPLUS_SIZE_PROFILE_BUTTONS
,
253 KOVAPLUS_COMMAND_PROFILE_BUTTONS
);
256 #define PROFILE_ATTR(number) \
257 static struct bin_attribute bin_attr_profile##number##_settings = { \
258 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
259 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \
260 .read = kovaplus_sysfs_read_profilex_settings, \
261 .private = &profile_numbers[number-1], \
263 static struct bin_attribute bin_attr_profile##number##_buttons = { \
264 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
265 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \
266 .read = kovaplus_sysfs_read_profilex_buttons, \
267 .private = &profile_numbers[number-1], \
275 static ssize_t
kovaplus_sysfs_show_actual_profile(struct device
*dev
,
276 struct device_attribute
*attr
, char *buf
)
278 struct kovaplus_device
*kovaplus
=
279 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
280 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_profile
);
283 static ssize_t
kovaplus_sysfs_set_actual_profile(struct device
*dev
,
284 struct device_attribute
*attr
, char const *buf
, size_t size
)
286 struct kovaplus_device
*kovaplus
;
287 struct usb_device
*usb_dev
;
288 unsigned long profile
;
290 struct kovaplus_roccat_report roccat_report
;
292 dev
= dev
->parent
->parent
;
293 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
294 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
296 retval
= kstrtoul(buf
, 10, &profile
);
303 mutex_lock(&kovaplus
->kovaplus_lock
);
304 retval
= kovaplus_set_actual_profile(usb_dev
, profile
);
306 mutex_unlock(&kovaplus
->kovaplus_lock
);
310 kovaplus_profile_activated(kovaplus
, profile
);
312 roccat_report
.type
= KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
;
313 roccat_report
.profile
= profile
+ 1;
314 roccat_report
.button
= 0;
315 roccat_report
.data1
= profile
+ 1;
316 roccat_report
.data2
= 0;
317 roccat_report_event(kovaplus
->chrdev_minor
,
318 (uint8_t const *)&roccat_report
);
320 mutex_unlock(&kovaplus
->kovaplus_lock
);
324 static DEVICE_ATTR(actual_profile
, 0660,
325 kovaplus_sysfs_show_actual_profile
,
326 kovaplus_sysfs_set_actual_profile
);
328 static ssize_t
kovaplus_sysfs_show_actual_cpi(struct device
*dev
,
329 struct device_attribute
*attr
, char *buf
)
331 struct kovaplus_device
*kovaplus
=
332 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
333 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_cpi
);
335 static DEVICE_ATTR(actual_cpi
, 0440, kovaplus_sysfs_show_actual_cpi
, NULL
);
337 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_x(struct device
*dev
,
338 struct device_attribute
*attr
, char *buf
)
340 struct kovaplus_device
*kovaplus
=
341 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
342 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_x_sensitivity
);
344 static DEVICE_ATTR(actual_sensitivity_x
, 0440,
345 kovaplus_sysfs_show_actual_sensitivity_x
, NULL
);
347 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_y(struct device
*dev
,
348 struct device_attribute
*attr
, char *buf
)
350 struct kovaplus_device
*kovaplus
=
351 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
352 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_y_sensitivity
);
354 static DEVICE_ATTR(actual_sensitivity_y
, 0440,
355 kovaplus_sysfs_show_actual_sensitivity_y
, NULL
);
357 static ssize_t
kovaplus_sysfs_show_firmware_version(struct device
*dev
,
358 struct device_attribute
*attr
, char *buf
)
360 struct kovaplus_device
*kovaplus
;
361 struct usb_device
*usb_dev
;
362 struct kovaplus_info info
;
364 dev
= dev
->parent
->parent
;
365 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
366 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
368 mutex_lock(&kovaplus
->kovaplus_lock
);
369 roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_INFO
,
370 &info
, KOVAPLUS_SIZE_INFO
);
371 mutex_unlock(&kovaplus
->kovaplus_lock
);
373 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.firmware_version
);
375 static DEVICE_ATTR(firmware_version
, 0440,
376 kovaplus_sysfs_show_firmware_version
, NULL
);
378 static struct attribute
*kovaplus_attrs
[] = {
379 &dev_attr_actual_cpi
.attr
,
380 &dev_attr_firmware_version
.attr
,
381 &dev_attr_actual_profile
.attr
,
382 &dev_attr_actual_sensitivity_x
.attr
,
383 &dev_attr_actual_sensitivity_y
.attr
,
387 static struct bin_attribute
*kovaplus_bin_attributes
[] = {
390 &bin_attr_profile_settings
,
391 &bin_attr_profile_buttons
,
392 &bin_attr_profile1_settings
,
393 &bin_attr_profile2_settings
,
394 &bin_attr_profile3_settings
,
395 &bin_attr_profile4_settings
,
396 &bin_attr_profile5_settings
,
397 &bin_attr_profile1_buttons
,
398 &bin_attr_profile2_buttons
,
399 &bin_attr_profile3_buttons
,
400 &bin_attr_profile4_buttons
,
401 &bin_attr_profile5_buttons
,
405 static const struct attribute_group kovaplus_group
= {
406 .attrs
= kovaplus_attrs
,
407 .bin_attrs
= kovaplus_bin_attributes
,
410 static const struct attribute_group
*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 retval
= hid_parse(hdev
);
509 hid_err(hdev
, "parse failed\n");
513 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
515 hid_err(hdev
, "hw start failed\n");
519 retval
= kovaplus_init_specials(hdev
);
521 hid_err(hdev
, "couldn't install mouse\n");
533 static void kovaplus_remove(struct hid_device
*hdev
)
535 kovaplus_remove_specials(hdev
);
539 static void kovaplus_keep_values_up_to_date(struct kovaplus_device
*kovaplus
,
542 struct kovaplus_mouse_report_button
const *button_report
;
544 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
547 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
549 switch (button_report
->type
) {
550 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
:
551 kovaplus_profile_activated(kovaplus
, button_report
->data1
- 1);
553 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
:
554 kovaplus
->actual_cpi
= kovaplus_convert_event_cpi(button_report
->data1
);
556 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY
:
557 kovaplus
->actual_x_sensitivity
= button_report
->data1
;
558 kovaplus
->actual_y_sensitivity
= button_report
->data2
;
565 static void kovaplus_report_to_chrdev(struct kovaplus_device
const *kovaplus
,
568 struct kovaplus_roccat_report roccat_report
;
569 struct kovaplus_mouse_report_button
const *button_report
;
571 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
574 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
576 if (button_report
->type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2
)
579 roccat_report
.type
= button_report
->type
;
580 roccat_report
.profile
= kovaplus
->actual_profile
+ 1;
582 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO
||
583 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT
||
584 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH
||
585 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER
)
586 roccat_report
.button
= button_report
->data1
;
588 roccat_report
.button
= 0;
590 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
)
591 roccat_report
.data1
= kovaplus_convert_event_cpi(button_report
->data1
);
593 roccat_report
.data1
= button_report
->data1
;
595 roccat_report
.data2
= button_report
->data2
;
597 roccat_report_event(kovaplus
->chrdev_minor
,
598 (uint8_t const *)&roccat_report
);
601 static int kovaplus_raw_event(struct hid_device
*hdev
,
602 struct hid_report
*report
, u8
*data
, int size
)
604 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
605 struct kovaplus_device
*kovaplus
= hid_get_drvdata(hdev
);
607 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
608 != USB_INTERFACE_PROTOCOL_MOUSE
)
611 if (kovaplus
== NULL
)
614 kovaplus_keep_values_up_to_date(kovaplus
, data
);
616 if (kovaplus
->roccat_claimed
)
617 kovaplus_report_to_chrdev(kovaplus
, data
);
622 static const struct hid_device_id kovaplus_devices
[] = {
623 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
, USB_DEVICE_ID_ROCCAT_KOVAPLUS
) },
627 MODULE_DEVICE_TABLE(hid
, kovaplus_devices
);
629 static struct hid_driver kovaplus_driver
= {
631 .id_table
= kovaplus_devices
,
632 .probe
= kovaplus_probe
,
633 .remove
= kovaplus_remove
,
634 .raw_event
= kovaplus_raw_event
637 static int __init
kovaplus_init(void)
641 kovaplus_class
= class_create(THIS_MODULE
, "kovaplus");
642 if (IS_ERR(kovaplus_class
))
643 return PTR_ERR(kovaplus_class
);
644 kovaplus_class
->dev_groups
= kovaplus_groups
;
646 retval
= hid_register_driver(&kovaplus_driver
);
648 class_destroy(kovaplus_class
);
652 static void __exit
kovaplus_exit(void)
654 hid_unregister_driver(&kovaplus_driver
);
655 class_destroy(kovaplus_class
);
658 module_init(kovaplus_init
);
659 module_exit(kovaplus_exit
);
661 MODULE_AUTHOR("Stefan Achatz");
662 MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
663 MODULE_LICENSE("GPL v2");