1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Roccat Arvo driver for Linux
5 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
12 * Roccat Arvo is a gamer keyboard with 5 macro keys that can be configured in
16 #include <linux/device.h>
17 #include <linux/input.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/hid-roccat.h>
23 #include "hid-roccat-common.h"
24 #include "hid-roccat-arvo.h"
26 static ssize_t arvo_sysfs_show_mode_key(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct arvo_device *arvo =
30 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
31 struct usb_device *usb_dev =
32 interface_to_usbdev(to_usb_interface(dev->parent->parent));
33 struct arvo_mode_key temp_buf;
36 mutex_lock(&arvo->arvo_lock);
37 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY,
38 &temp_buf, sizeof(struct arvo_mode_key));
39 mutex_unlock(&arvo->arvo_lock);
43 return sysfs_emit(buf, "%d\n", temp_buf.state);
46 static ssize_t arvo_sysfs_set_mode_key(struct device *dev,
47 struct device_attribute *attr, char const *buf, size_t size)
49 struct arvo_device *arvo =
50 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
51 struct usb_device *usb_dev =
52 interface_to_usbdev(to_usb_interface(dev->parent->parent));
53 struct arvo_mode_key temp_buf;
57 retval = kstrtoul(buf, 10, &state);
61 temp_buf.command = ARVO_COMMAND_MODE_KEY;
62 temp_buf.state = state;
64 mutex_lock(&arvo->arvo_lock);
65 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY,
66 &temp_buf, sizeof(struct arvo_mode_key));
67 mutex_unlock(&arvo->arvo_lock);
73 static DEVICE_ATTR(mode_key, 0660,
74 arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key);
76 static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
77 struct device_attribute *attr, char *buf)
79 struct arvo_device *arvo =
80 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
81 struct usb_device *usb_dev =
82 interface_to_usbdev(to_usb_interface(dev->parent->parent));
83 struct arvo_key_mask temp_buf;
86 mutex_lock(&arvo->arvo_lock);
87 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
88 &temp_buf, sizeof(struct arvo_key_mask));
89 mutex_unlock(&arvo->arvo_lock);
93 return sysfs_emit(buf, "%d\n", temp_buf.key_mask);
96 static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
97 struct device_attribute *attr, char const *buf, size_t size)
99 struct arvo_device *arvo =
100 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
101 struct usb_device *usb_dev =
102 interface_to_usbdev(to_usb_interface(dev->parent->parent));
103 struct arvo_key_mask temp_buf;
104 unsigned long key_mask;
107 retval = kstrtoul(buf, 10, &key_mask);
111 temp_buf.command = ARVO_COMMAND_KEY_MASK;
112 temp_buf.key_mask = key_mask;
114 mutex_lock(&arvo->arvo_lock);
115 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK,
116 &temp_buf, sizeof(struct arvo_key_mask));
117 mutex_unlock(&arvo->arvo_lock);
123 static DEVICE_ATTR(key_mask, 0660,
124 arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask);
126 /* retval is 1-5 on success, < 0 on error */
127 static int arvo_get_actual_profile(struct usb_device *usb_dev)
129 struct arvo_actual_profile temp_buf;
132 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
133 &temp_buf, sizeof(struct arvo_actual_profile));
138 return temp_buf.actual_profile;
141 static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
142 struct device_attribute *attr, char *buf)
144 struct arvo_device *arvo =
145 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
147 return sysfs_emit(buf, "%d\n", arvo->actual_profile);
150 static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
151 struct device_attribute *attr, char const *buf, size_t size)
153 struct arvo_device *arvo =
154 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
155 struct usb_device *usb_dev =
156 interface_to_usbdev(to_usb_interface(dev->parent->parent));
157 struct arvo_actual_profile temp_buf;
158 unsigned long profile;
161 retval = kstrtoul(buf, 10, &profile);
165 if (profile < 1 || profile > 5)
168 temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
169 temp_buf.actual_profile = profile;
171 mutex_lock(&arvo->arvo_lock);
172 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
173 &temp_buf, sizeof(struct arvo_actual_profile));
175 arvo->actual_profile = profile;
178 mutex_unlock(&arvo->arvo_lock);
181 static DEVICE_ATTR(actual_profile, 0660,
182 arvo_sysfs_show_actual_profile,
183 arvo_sysfs_set_actual_profile);
185 static ssize_t arvo_sysfs_write(struct file *fp,
186 struct kobject *kobj, void const *buf,
187 loff_t off, size_t count, size_t real_size, uint command)
189 struct device *dev = kobj_to_dev(kobj)->parent->parent;
190 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
191 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
194 if (off != 0 || count != real_size)
197 mutex_lock(&arvo->arvo_lock);
198 retval = roccat_common2_send(usb_dev, command, buf, real_size);
199 mutex_unlock(&arvo->arvo_lock);
201 return (retval ? retval : real_size);
204 static ssize_t arvo_sysfs_read(struct file *fp,
205 struct kobject *kobj, void *buf, loff_t off,
206 size_t count, size_t real_size, uint command)
208 struct device *dev = kobj_to_dev(kobj)->parent->parent;
209 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
210 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
213 if (off >= real_size)
216 if (off != 0 || count != real_size)
219 mutex_lock(&arvo->arvo_lock);
220 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
221 mutex_unlock(&arvo->arvo_lock);
223 return (retval ? retval : real_size);
226 static ssize_t arvo_sysfs_write_button(struct file *fp,
227 struct kobject *kobj, struct bin_attribute *attr, char *buf,
228 loff_t off, size_t count)
230 return arvo_sysfs_write(fp, kobj, buf, off, count,
231 sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
233 static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button,
234 sizeof(struct arvo_button));
236 static ssize_t arvo_sysfs_read_info(struct file *fp,
237 struct kobject *kobj, struct bin_attribute *attr, char *buf,
238 loff_t off, size_t count)
240 return arvo_sysfs_read(fp, kobj, buf, off, count,
241 sizeof(struct arvo_info), ARVO_COMMAND_INFO);
243 static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL,
244 sizeof(struct arvo_info));
246 static struct attribute *arvo_attrs[] = {
247 &dev_attr_mode_key.attr,
248 &dev_attr_key_mask.attr,
249 &dev_attr_actual_profile.attr,
253 static struct bin_attribute *arvo_bin_attributes[] = {
259 static const struct attribute_group arvo_group = {
261 .bin_attrs = arvo_bin_attributes,
264 static const struct attribute_group *arvo_groups[] = {
269 static const struct class arvo_class = {
271 .dev_groups = arvo_groups,
274 static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
275 struct arvo_device *arvo)
279 mutex_init(&arvo->arvo_lock);
281 retval = arvo_get_actual_profile(usb_dev);
284 arvo->actual_profile = retval;
289 static int arvo_init_specials(struct hid_device *hdev)
291 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
292 struct usb_device *usb_dev = interface_to_usbdev(intf);
293 struct arvo_device *arvo;
296 if (intf->cur_altsetting->desc.bInterfaceProtocol
297 == USB_INTERFACE_PROTOCOL_KEYBOARD) {
298 hid_set_drvdata(hdev, NULL);
302 arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
304 hid_err(hdev, "can't alloc device descriptor\n");
307 hid_set_drvdata(hdev, arvo);
309 retval = arvo_init_arvo_device_struct(usb_dev, arvo);
311 hid_err(hdev, "couldn't init struct arvo_device\n");
315 retval = roccat_connect(&arvo_class, hdev,
316 sizeof(struct arvo_roccat_report));
318 hid_err(hdev, "couldn't init char dev\n");
320 arvo->chrdev_minor = retval;
321 arvo->roccat_claimed = 1;
330 static void arvo_remove_specials(struct hid_device *hdev)
332 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
333 struct arvo_device *arvo;
335 if (intf->cur_altsetting->desc.bInterfaceProtocol
336 == USB_INTERFACE_PROTOCOL_KEYBOARD)
339 arvo = hid_get_drvdata(hdev);
340 if (arvo->roccat_claimed)
341 roccat_disconnect(arvo->chrdev_minor);
345 static int arvo_probe(struct hid_device *hdev,
346 const struct hid_device_id *id)
350 if (!hid_is_usb(hdev))
353 retval = hid_parse(hdev);
355 hid_err(hdev, "parse failed\n");
359 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
361 hid_err(hdev, "hw start failed\n");
365 retval = arvo_init_specials(hdev);
367 hid_err(hdev, "couldn't install keyboard\n");
379 static void arvo_remove(struct hid_device *hdev)
381 arvo_remove_specials(hdev);
385 static void arvo_report_to_chrdev(struct arvo_device const *arvo,
388 struct arvo_special_report const *special_report;
389 struct arvo_roccat_report roccat_report;
391 special_report = (struct arvo_special_report const *)data;
393 roccat_report.profile = arvo->actual_profile;
394 roccat_report.button = special_report->event &
395 ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
396 if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
397 ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
398 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
400 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
402 roccat_report_event(arvo->chrdev_minor,
403 (uint8_t const *)&roccat_report);
406 static int arvo_raw_event(struct hid_device *hdev,
407 struct hid_report *report, u8 *data, int size)
409 struct arvo_device *arvo = hid_get_drvdata(hdev);
414 if (arvo && arvo->roccat_claimed)
415 arvo_report_to_chrdev(arvo, data);
420 static const struct hid_device_id arvo_devices[] = {
421 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
425 MODULE_DEVICE_TABLE(hid, arvo_devices);
427 static struct hid_driver arvo_driver = {
429 .id_table = arvo_devices,
431 .remove = arvo_remove,
432 .raw_event = arvo_raw_event
435 static int __init arvo_init(void)
439 retval = class_register(&arvo_class);
443 retval = hid_register_driver(&arvo_driver);
445 class_unregister(&arvo_class);
449 static void __exit arvo_exit(void)
451 hid_unregister_driver(&arvo_driver);
452 class_unregister(&arvo_class);
455 module_init(arvo_init);
456 module_exit(arvo_exit);
458 MODULE_AUTHOR("Stefan Achatz");
459 MODULE_DESCRIPTION("USB Roccat Arvo driver");
460 MODULE_LICENSE("GPL v2");