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
)
134 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
135 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
136 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
139 if (off
>= real_size
)
142 if (off
!= 0 || count
!= real_size
)
145 mutex_lock(&kovaplus
->kovaplus_lock
);
146 retval
= roccat_common2_receive(usb_dev
, command
, buf
, real_size
);
147 mutex_unlock(&kovaplus
->kovaplus_lock
);
155 static ssize_t
kovaplus_sysfs_write(struct file
*fp
, struct kobject
*kobj
,
156 void const *buf
, loff_t off
, size_t count
,
157 size_t real_size
, uint command
)
160 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
161 struct kovaplus_device
*kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
162 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
165 if (off
!= 0 || count
!= real_size
)
168 mutex_lock(&kovaplus
->kovaplus_lock
);
169 retval
= roccat_common2_send_with_status(usb_dev
, command
,
171 mutex_unlock(&kovaplus
->kovaplus_lock
);
179 #define KOVAPLUS_SYSFS_W(thingy, THINGY) \
180 static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \
181 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
182 loff_t off, size_t count) \
184 return kovaplus_sysfs_write(fp, kobj, buf, off, count, \
185 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
188 #define KOVAPLUS_SYSFS_R(thingy, THINGY) \
189 static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \
190 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
191 loff_t off, size_t count) \
193 return kovaplus_sysfs_read(fp, kobj, buf, off, count, \
194 KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \
197 #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \
198 KOVAPLUS_SYSFS_W(thingy, THINGY) \
199 KOVAPLUS_SYSFS_R(thingy, THINGY)
201 #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
202 KOVAPLUS_SYSFS_RW(thingy, THINGY); \
203 static struct bin_attribute bin_attr_##thingy = { \
204 .attr = { .name = #thingy, .mode = 0660 }, \
205 .size = KOVAPLUS_SIZE_ ## THINGY, \
206 .read = kovaplus_sysfs_read_ ## thingy, \
207 .write = kovaplus_sysfs_write_ ## thingy \
210 #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
211 KOVAPLUS_SYSFS_W(thingy, THINGY); \
212 static struct bin_attribute bin_attr_##thingy = { \
213 .attr = { .name = #thingy, .mode = 0220 }, \
214 .size = KOVAPLUS_SIZE_ ## THINGY, \
215 .write = kovaplus_sysfs_write_ ## thingy \
217 KOVAPLUS_BIN_ATTRIBUTE_W(control
, CONTROL
);
218 KOVAPLUS_BIN_ATTRIBUTE_RW(info
, INFO
);
219 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings
, PROFILE_SETTINGS
);
220 KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons
, PROFILE_BUTTONS
);
222 static ssize_t
kovaplus_sysfs_read_profilex_settings(struct file
*fp
,
223 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
224 loff_t off
, size_t count
)
227 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
228 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
231 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
232 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS
);
236 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
237 KOVAPLUS_SIZE_PROFILE_SETTINGS
,
238 KOVAPLUS_COMMAND_PROFILE_SETTINGS
);
241 static ssize_t
kovaplus_sysfs_read_profilex_buttons(struct file
*fp
,
242 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
243 loff_t off
, size_t count
)
246 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
247 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
250 retval
= kovaplus_select_profile(usb_dev
, *(uint
*)(attr
->private),
251 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS
);
255 return kovaplus_sysfs_read(fp
, kobj
, buf
, off
, count
,
256 KOVAPLUS_SIZE_PROFILE_BUTTONS
,
257 KOVAPLUS_COMMAND_PROFILE_BUTTONS
);
260 #define PROFILE_ATTR(number) \
261 static struct bin_attribute bin_attr_profile##number##_settings = { \
262 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
263 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \
264 .read = kovaplus_sysfs_read_profilex_settings, \
265 .private = &profile_numbers[number-1], \
267 static struct bin_attribute bin_attr_profile##number##_buttons = { \
268 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
269 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \
270 .read = kovaplus_sysfs_read_profilex_buttons, \
271 .private = &profile_numbers[number-1], \
279 static ssize_t
kovaplus_sysfs_show_actual_profile(struct device
*dev
,
280 struct device_attribute
*attr
, char *buf
)
282 struct kovaplus_device
*kovaplus
=
283 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
284 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_profile
);
287 static ssize_t
kovaplus_sysfs_set_actual_profile(struct device
*dev
,
288 struct device_attribute
*attr
, char const *buf
, size_t size
)
290 struct kovaplus_device
*kovaplus
;
291 struct usb_device
*usb_dev
;
292 unsigned long profile
;
294 struct kovaplus_roccat_report roccat_report
;
296 dev
= dev
->parent
->parent
;
297 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
298 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
300 retval
= kstrtoul(buf
, 10, &profile
);
307 mutex_lock(&kovaplus
->kovaplus_lock
);
308 retval
= kovaplus_set_actual_profile(usb_dev
, profile
);
310 mutex_unlock(&kovaplus
->kovaplus_lock
);
314 kovaplus_profile_activated(kovaplus
, profile
);
316 roccat_report
.type
= KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
;
317 roccat_report
.profile
= profile
+ 1;
318 roccat_report
.button
= 0;
319 roccat_report
.data1
= profile
+ 1;
320 roccat_report
.data2
= 0;
321 roccat_report_event(kovaplus
->chrdev_minor
,
322 (uint8_t const *)&roccat_report
);
324 mutex_unlock(&kovaplus
->kovaplus_lock
);
328 static DEVICE_ATTR(actual_profile
, 0660,
329 kovaplus_sysfs_show_actual_profile
,
330 kovaplus_sysfs_set_actual_profile
);
332 static ssize_t
kovaplus_sysfs_show_actual_cpi(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 snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_cpi
);
339 static DEVICE_ATTR(actual_cpi
, 0440, kovaplus_sysfs_show_actual_cpi
, NULL
);
341 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_x(struct device
*dev
,
342 struct device_attribute
*attr
, char *buf
)
344 struct kovaplus_device
*kovaplus
=
345 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
346 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_x_sensitivity
);
348 static DEVICE_ATTR(actual_sensitivity_x
, 0440,
349 kovaplus_sysfs_show_actual_sensitivity_x
, NULL
);
351 static ssize_t
kovaplus_sysfs_show_actual_sensitivity_y(struct device
*dev
,
352 struct device_attribute
*attr
, char *buf
)
354 struct kovaplus_device
*kovaplus
=
355 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
356 return snprintf(buf
, PAGE_SIZE
, "%d\n", kovaplus
->actual_y_sensitivity
);
358 static DEVICE_ATTR(actual_sensitivity_y
, 0440,
359 kovaplus_sysfs_show_actual_sensitivity_y
, NULL
);
361 static ssize_t
kovaplus_sysfs_show_firmware_version(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 struct kovaplus_device
*kovaplus
;
365 struct usb_device
*usb_dev
;
366 struct kovaplus_info info
;
368 dev
= dev
->parent
->parent
;
369 kovaplus
= hid_get_drvdata(dev_get_drvdata(dev
));
370 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
372 mutex_lock(&kovaplus
->kovaplus_lock
);
373 roccat_common2_receive(usb_dev
, KOVAPLUS_COMMAND_INFO
,
374 &info
, KOVAPLUS_SIZE_INFO
);
375 mutex_unlock(&kovaplus
->kovaplus_lock
);
377 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.firmware_version
);
379 static DEVICE_ATTR(firmware_version
, 0440,
380 kovaplus_sysfs_show_firmware_version
, NULL
);
382 static struct attribute
*kovaplus_attrs
[] = {
383 &dev_attr_actual_cpi
.attr
,
384 &dev_attr_firmware_version
.attr
,
385 &dev_attr_actual_profile
.attr
,
386 &dev_attr_actual_sensitivity_x
.attr
,
387 &dev_attr_actual_sensitivity_y
.attr
,
391 static struct bin_attribute
*kovaplus_bin_attributes
[] = {
394 &bin_attr_profile_settings
,
395 &bin_attr_profile_buttons
,
396 &bin_attr_profile1_settings
,
397 &bin_attr_profile2_settings
,
398 &bin_attr_profile3_settings
,
399 &bin_attr_profile4_settings
,
400 &bin_attr_profile5_settings
,
401 &bin_attr_profile1_buttons
,
402 &bin_attr_profile2_buttons
,
403 &bin_attr_profile3_buttons
,
404 &bin_attr_profile4_buttons
,
405 &bin_attr_profile5_buttons
,
409 static const struct attribute_group kovaplus_group
= {
410 .attrs
= kovaplus_attrs
,
411 .bin_attrs
= kovaplus_bin_attributes
,
414 static const struct attribute_group
*kovaplus_groups
[] = {
419 static int kovaplus_init_kovaplus_device_struct(struct usb_device
*usb_dev
,
420 struct kovaplus_device
*kovaplus
)
423 static uint wait
= 70; /* device will freeze with just 60 */
425 mutex_init(&kovaplus
->kovaplus_lock
);
427 for (i
= 0; i
< 5; ++i
) {
429 retval
= kovaplus_get_profile_settings(usb_dev
,
430 &kovaplus
->profile_settings
[i
], i
);
435 retval
= kovaplus_get_profile_buttons(usb_dev
,
436 &kovaplus
->profile_buttons
[i
], i
);
442 retval
= kovaplus_get_actual_profile(usb_dev
);
445 kovaplus_profile_activated(kovaplus
, retval
);
450 static int kovaplus_init_specials(struct hid_device
*hdev
)
452 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
453 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
454 struct kovaplus_device
*kovaplus
;
457 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
458 == USB_INTERFACE_PROTOCOL_MOUSE
) {
460 kovaplus
= kzalloc(sizeof(*kovaplus
), GFP_KERNEL
);
462 hid_err(hdev
, "can't alloc device descriptor\n");
465 hid_set_drvdata(hdev
, kovaplus
);
467 retval
= kovaplus_init_kovaplus_device_struct(usb_dev
, kovaplus
);
469 hid_err(hdev
, "couldn't init struct kovaplus_device\n");
473 retval
= roccat_connect(kovaplus_class
, hdev
,
474 sizeof(struct kovaplus_roccat_report
));
476 hid_err(hdev
, "couldn't init char dev\n");
478 kovaplus
->chrdev_minor
= retval
;
479 kovaplus
->roccat_claimed
= 1;
483 hid_set_drvdata(hdev
, NULL
);
492 static void kovaplus_remove_specials(struct hid_device
*hdev
)
494 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
495 struct kovaplus_device
*kovaplus
;
497 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
498 == USB_INTERFACE_PROTOCOL_MOUSE
) {
499 kovaplus
= hid_get_drvdata(hdev
);
500 if (kovaplus
->roccat_claimed
)
501 roccat_disconnect(kovaplus
->chrdev_minor
);
506 static int kovaplus_probe(struct hid_device
*hdev
,
507 const struct hid_device_id
*id
)
511 retval
= hid_parse(hdev
);
513 hid_err(hdev
, "parse failed\n");
517 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
519 hid_err(hdev
, "hw start failed\n");
523 retval
= kovaplus_init_specials(hdev
);
525 hid_err(hdev
, "couldn't install mouse\n");
537 static void kovaplus_remove(struct hid_device
*hdev
)
539 kovaplus_remove_specials(hdev
);
543 static void kovaplus_keep_values_up_to_date(struct kovaplus_device
*kovaplus
,
546 struct kovaplus_mouse_report_button
const *button_report
;
548 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
551 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
553 switch (button_report
->type
) {
554 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1
:
555 kovaplus_profile_activated(kovaplus
, button_report
->data1
- 1);
557 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
:
558 kovaplus
->actual_cpi
= kovaplus_convert_event_cpi(button_report
->data1
);
560 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY
:
561 kovaplus
->actual_x_sensitivity
= button_report
->data1
;
562 kovaplus
->actual_y_sensitivity
= button_report
->data2
;
569 static void kovaplus_report_to_chrdev(struct kovaplus_device
const *kovaplus
,
572 struct kovaplus_roccat_report roccat_report
;
573 struct kovaplus_mouse_report_button
const *button_report
;
575 if (data
[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON
)
578 button_report
= (struct kovaplus_mouse_report_button
const *)data
;
580 if (button_report
->type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2
)
583 roccat_report
.type
= button_report
->type
;
584 roccat_report
.profile
= kovaplus
->actual_profile
+ 1;
586 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO
||
587 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT
||
588 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH
||
589 roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER
)
590 roccat_report
.button
= button_report
->data1
;
592 roccat_report
.button
= 0;
594 if (roccat_report
.type
== KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI
)
595 roccat_report
.data1
= kovaplus_convert_event_cpi(button_report
->data1
);
597 roccat_report
.data1
= button_report
->data1
;
599 roccat_report
.data2
= button_report
->data2
;
601 roccat_report_event(kovaplus
->chrdev_minor
,
602 (uint8_t const *)&roccat_report
);
605 static int kovaplus_raw_event(struct hid_device
*hdev
,
606 struct hid_report
*report
, u8
*data
, int size
)
608 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
609 struct kovaplus_device
*kovaplus
= hid_get_drvdata(hdev
);
611 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
612 != USB_INTERFACE_PROTOCOL_MOUSE
)
615 if (kovaplus
== NULL
)
618 kovaplus_keep_values_up_to_date(kovaplus
, data
);
620 if (kovaplus
->roccat_claimed
)
621 kovaplus_report_to_chrdev(kovaplus
, data
);
626 static const struct hid_device_id kovaplus_devices
[] = {
627 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
, USB_DEVICE_ID_ROCCAT_KOVAPLUS
) },
631 MODULE_DEVICE_TABLE(hid
, kovaplus_devices
);
633 static struct hid_driver kovaplus_driver
= {
635 .id_table
= kovaplus_devices
,
636 .probe
= kovaplus_probe
,
637 .remove
= kovaplus_remove
,
638 .raw_event
= kovaplus_raw_event
641 static int __init
kovaplus_init(void)
645 kovaplus_class
= class_create(THIS_MODULE
, "kovaplus");
646 if (IS_ERR(kovaplus_class
))
647 return PTR_ERR(kovaplus_class
);
648 kovaplus_class
->dev_groups
= kovaplus_groups
;
650 retval
= hid_register_driver(&kovaplus_driver
);
652 class_destroy(kovaplus_class
);
656 static void __exit
kovaplus_exit(void)
658 hid_unregister_driver(&kovaplus_driver
);
659 class_destroy(kovaplus_class
);
662 module_init(kovaplus_init
);
663 module_exit(kovaplus_exit
);
665 MODULE_AUTHOR("Stefan Achatz");
666 MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
667 MODULE_LICENSE("GPL v2");