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 kovaplus->actual_profile = new_profile_index;
41 kovaplus->actual_cpi = kovaplus->profile_settings[new_profile_index].cpi_startup_level;
42 kovaplus->actual_x_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_x;
43 kovaplus->actual_y_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_y;
46 static int kovaplus_send_control(struct usb_device *usb_dev, uint value,
47 enum kovaplus_control_requests request)
50 struct roccat_common2_control control;
52 if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
53 request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
57 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
58 control.value = value;
59 control.request = request;
61 retval = roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
62 &control, sizeof(struct roccat_common2_control));
67 static int kovaplus_select_profile(struct usb_device *usb_dev, uint number,
68 enum kovaplus_control_requests request)
70 return kovaplus_send_control(usb_dev, number, request);
73 static int kovaplus_get_profile_settings(struct usb_device *usb_dev,
74 struct kovaplus_profile_settings *buf, uint number)
78 retval = kovaplus_select_profile(usb_dev, number,
79 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
83 return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_SETTINGS,
84 buf, KOVAPLUS_SIZE_PROFILE_SETTINGS);
87 static int kovaplus_get_profile_buttons(struct usb_device *usb_dev,
88 struct kovaplus_profile_buttons *buf, int number)
92 retval = kovaplus_select_profile(usb_dev, number,
93 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
97 return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_BUTTONS,
98 buf, KOVAPLUS_SIZE_PROFILE_BUTTONS);
101 /* retval is 0-4 on success, < 0 on error */
102 static int kovaplus_get_actual_profile(struct usb_device *usb_dev)
104 struct kovaplus_actual_profile buf;
107 retval = roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_ACTUAL_PROFILE,
108 &buf, sizeof(struct kovaplus_actual_profile));
110 return retval ? retval : buf.actual_profile;
113 static int kovaplus_set_actual_profile(struct usb_device *usb_dev,
116 struct kovaplus_actual_profile buf;
118 buf.command = KOVAPLUS_COMMAND_ACTUAL_PROFILE;
119 buf.size = sizeof(struct kovaplus_actual_profile);
120 buf.actual_profile = new_profile;
122 return roccat_common2_send_with_status(usb_dev,
123 KOVAPLUS_COMMAND_ACTUAL_PROFILE,
124 &buf, sizeof(struct kovaplus_actual_profile));
127 static ssize_t kovaplus_sysfs_read(struct file *fp, struct kobject *kobj,
128 char *buf, loff_t off, size_t count,
129 size_t real_size, uint command)
132 container_of(kobj, struct device, kobj)->parent->parent;
133 struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
134 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
137 if (off >= real_size)
140 if (off != 0 || count != real_size)
143 mutex_lock(&kovaplus->kovaplus_lock);
144 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
145 mutex_unlock(&kovaplus->kovaplus_lock);
153 static ssize_t kovaplus_sysfs_write(struct file *fp, struct kobject *kobj,
154 void const *buf, loff_t off, size_t count,
155 size_t real_size, uint command)
158 container_of(kobj, struct device, 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)
225 container_of(kobj, struct device, kobj)->parent->parent;
226 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
229 retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private),
230 KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
234 return kovaplus_sysfs_read(fp, kobj, buf, off, count,
235 KOVAPLUS_SIZE_PROFILE_SETTINGS,
236 KOVAPLUS_COMMAND_PROFILE_SETTINGS);
239 static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp,
240 struct kobject *kobj, struct bin_attribute *attr, char *buf,
241 loff_t off, size_t count)
244 container_of(kobj, struct device, kobj)->parent->parent;
245 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
248 retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private),
249 KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
253 return kovaplus_sysfs_read(fp, kobj, buf, off, count,
254 KOVAPLUS_SIZE_PROFILE_BUTTONS,
255 KOVAPLUS_COMMAND_PROFILE_BUTTONS);
258 #define PROFILE_ATTR(number) \
259 static struct bin_attribute bin_attr_profile##number##_settings = { \
260 .attr = { .name = "profile##number##_settings", .mode = 0440 }, \
261 .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \
262 .read = kovaplus_sysfs_read_profilex_settings, \
263 .private = &profile_numbers[number-1], \
265 static struct bin_attribute bin_attr_profile##number##_buttons = { \
266 .attr = { .name = "profile##number##_buttons", .mode = 0440 }, \
267 .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \
268 .read = kovaplus_sysfs_read_profilex_buttons, \
269 .private = &profile_numbers[number-1], \
277 static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct kovaplus_device *kovaplus =
281 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
282 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile);
285 static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev,
286 struct device_attribute *attr, char const *buf, size_t size)
288 struct kovaplus_device *kovaplus;
289 struct usb_device *usb_dev;
290 unsigned long profile;
292 struct kovaplus_roccat_report roccat_report;
294 dev = dev->parent->parent;
295 kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
296 usb_dev = interface_to_usbdev(to_usb_interface(dev));
298 retval = strict_strtoul(buf, 10, &profile);
305 mutex_lock(&kovaplus->kovaplus_lock);
306 retval = kovaplus_set_actual_profile(usb_dev, profile);
308 mutex_unlock(&kovaplus->kovaplus_lock);
312 kovaplus_profile_activated(kovaplus, profile);
314 roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1;
315 roccat_report.profile = profile + 1;
316 roccat_report.button = 0;
317 roccat_report.data1 = profile + 1;
318 roccat_report.data2 = 0;
319 roccat_report_event(kovaplus->chrdev_minor,
320 (uint8_t const *)&roccat_report);
322 mutex_unlock(&kovaplus->kovaplus_lock);
326 static DEVICE_ATTR(actual_profile, 0660,
327 kovaplus_sysfs_show_actual_profile,
328 kovaplus_sysfs_set_actual_profile);
330 static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct kovaplus_device *kovaplus =
334 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
335 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi);
337 static DEVICE_ATTR(actual_cpi, 0440, kovaplus_sysfs_show_actual_cpi, NULL);
339 static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev,
340 struct device_attribute *attr, char *buf)
342 struct kovaplus_device *kovaplus =
343 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
344 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity);
346 static DEVICE_ATTR(actual_sensitivity_x, 0440,
347 kovaplus_sysfs_show_actual_sensitivity_x, NULL);
349 static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev,
350 struct device_attribute *attr, char *buf)
352 struct kovaplus_device *kovaplus =
353 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
354 return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity);
356 static DEVICE_ATTR(actual_sensitivity_y, 0440,
357 kovaplus_sysfs_show_actual_sensitivity_y, NULL);
359 static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev,
360 struct device_attribute *attr, char *buf)
362 struct kovaplus_device *kovaplus;
363 struct usb_device *usb_dev;
364 struct kovaplus_info info;
366 dev = dev->parent->parent;
367 kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
368 usb_dev = interface_to_usbdev(to_usb_interface(dev));
370 mutex_lock(&kovaplus->kovaplus_lock);
371 roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_INFO,
372 &info, KOVAPLUS_SIZE_INFO);
373 mutex_unlock(&kovaplus->kovaplus_lock);
375 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
377 static DEVICE_ATTR(firmware_version, 0440,
378 kovaplus_sysfs_show_firmware_version, NULL);
380 static struct attribute *kovaplus_attrs[] = {
381 &dev_attr_actual_cpi.attr,
382 &dev_attr_firmware_version.attr,
383 &dev_attr_actual_profile.attr,
384 &dev_attr_actual_sensitivity_x.attr,
385 &dev_attr_actual_sensitivity_y.attr,
389 static struct bin_attribute *kovaplus_bin_attributes[] = {
392 &bin_attr_profile_settings,
393 &bin_attr_profile_buttons,
394 &bin_attr_profile1_settings,
395 &bin_attr_profile2_settings,
396 &bin_attr_profile3_settings,
397 &bin_attr_profile4_settings,
398 &bin_attr_profile5_settings,
399 &bin_attr_profile1_buttons,
400 &bin_attr_profile2_buttons,
401 &bin_attr_profile3_buttons,
402 &bin_attr_profile4_buttons,
403 &bin_attr_profile5_buttons,
407 static const struct attribute_group kovaplus_group = {
408 .attrs = kovaplus_attrs,
409 .bin_attrs = kovaplus_bin_attributes,
412 static const struct attribute_group *kovaplus_groups[] = {
417 static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev,
418 struct kovaplus_device *kovaplus)
421 static uint wait = 70; /* device will freeze with just 60 */
423 mutex_init(&kovaplus->kovaplus_lock);
425 for (i = 0; i < 5; ++i) {
427 retval = kovaplus_get_profile_settings(usb_dev,
428 &kovaplus->profile_settings[i], i);
433 retval = kovaplus_get_profile_buttons(usb_dev,
434 &kovaplus->profile_buttons[i], i);
440 retval = kovaplus_get_actual_profile(usb_dev);
443 kovaplus_profile_activated(kovaplus, retval);
448 static int kovaplus_init_specials(struct hid_device *hdev)
450 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
451 struct usb_device *usb_dev = interface_to_usbdev(intf);
452 struct kovaplus_device *kovaplus;
455 if (intf->cur_altsetting->desc.bInterfaceProtocol
456 == USB_INTERFACE_PROTOCOL_MOUSE) {
458 kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL);
460 hid_err(hdev, "can't alloc device descriptor\n");
463 hid_set_drvdata(hdev, kovaplus);
465 retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus);
467 hid_err(hdev, "couldn't init struct kovaplus_device\n");
471 retval = roccat_connect(kovaplus_class, hdev,
472 sizeof(struct kovaplus_roccat_report));
474 hid_err(hdev, "couldn't init char dev\n");
476 kovaplus->chrdev_minor = retval;
477 kovaplus->roccat_claimed = 1;
481 hid_set_drvdata(hdev, NULL);
490 static void kovaplus_remove_specials(struct hid_device *hdev)
492 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
493 struct kovaplus_device *kovaplus;
495 if (intf->cur_altsetting->desc.bInterfaceProtocol
496 == USB_INTERFACE_PROTOCOL_MOUSE) {
497 kovaplus = hid_get_drvdata(hdev);
498 if (kovaplus->roccat_claimed)
499 roccat_disconnect(kovaplus->chrdev_minor);
504 static int kovaplus_probe(struct hid_device *hdev,
505 const struct hid_device_id *id)
509 retval = hid_parse(hdev);
511 hid_err(hdev, "parse failed\n");
515 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
517 hid_err(hdev, "hw start failed\n");
521 retval = kovaplus_init_specials(hdev);
523 hid_err(hdev, "couldn't install mouse\n");
535 static void kovaplus_remove(struct hid_device *hdev)
537 kovaplus_remove_specials(hdev);
541 static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus,
544 struct kovaplus_mouse_report_button const *button_report;
546 if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
549 button_report = (struct kovaplus_mouse_report_button const *)data;
551 switch (button_report->type) {
552 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1:
553 kovaplus_profile_activated(kovaplus, button_report->data1 - 1);
555 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI:
556 kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1);
557 case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY:
558 kovaplus->actual_x_sensitivity = button_report->data1;
559 kovaplus->actual_y_sensitivity = button_report->data2;
563 static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus,
566 struct kovaplus_roccat_report roccat_report;
567 struct kovaplus_mouse_report_button const *button_report;
569 if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
572 button_report = (struct kovaplus_mouse_report_button const *)data;
574 if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2)
577 roccat_report.type = button_report->type;
578 roccat_report.profile = kovaplus->actual_profile + 1;
580 if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO ||
581 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT ||
582 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
583 roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER)
584 roccat_report.button = button_report->data1;
586 roccat_report.button = 0;
588 if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI)
589 roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1);
591 roccat_report.data1 = button_report->data1;
593 roccat_report.data2 = button_report->data2;
595 roccat_report_event(kovaplus->chrdev_minor,
596 (uint8_t const *)&roccat_report);
599 static int kovaplus_raw_event(struct hid_device *hdev,
600 struct hid_report *report, u8 *data, int size)
602 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
603 struct kovaplus_device *kovaplus = hid_get_drvdata(hdev);
605 if (intf->cur_altsetting->desc.bInterfaceProtocol
606 != USB_INTERFACE_PROTOCOL_MOUSE)
609 if (kovaplus == NULL)
612 kovaplus_keep_values_up_to_date(kovaplus, data);
614 if (kovaplus->roccat_claimed)
615 kovaplus_report_to_chrdev(kovaplus, data);
620 static const struct hid_device_id kovaplus_devices[] = {
621 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
625 MODULE_DEVICE_TABLE(hid, kovaplus_devices);
627 static struct hid_driver kovaplus_driver = {
629 .id_table = kovaplus_devices,
630 .probe = kovaplus_probe,
631 .remove = kovaplus_remove,
632 .raw_event = kovaplus_raw_event
635 static int __init kovaplus_init(void)
639 kovaplus_class = class_create(THIS_MODULE, "kovaplus");
640 if (IS_ERR(kovaplus_class))
641 return PTR_ERR(kovaplus_class);
642 kovaplus_class->dev_groups = kovaplus_groups;
644 retval = hid_register_driver(&kovaplus_driver);
646 class_destroy(kovaplus_class);
650 static void __exit kovaplus_exit(void)
652 hid_unregister_driver(&kovaplus_driver);
653 class_destroy(kovaplus_class);
656 module_init(kovaplus_init);
657 module_exit(kovaplus_exit);
659 MODULE_AUTHOR("Stefan Achatz");
660 MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
661 MODULE_LICENSE("GPL v2");