2
* Roccat Arvo 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 Arvo is a gamer keyboard with 5 macro keys that can be configured in
19
#include <linux/device.h>
20
#include <linux/input.h>
21
#include <linux/hid.h>
22
#include <linux/module.h>
23
#include <linux/slab.h>
24
#include <linux/hid-roccat.h>
26
#include "hid-roccat-common.h"
27
#include "hid-roccat-arvo.h"
29
static struct class *arvo_class;
31
static ssize_t arvo_sysfs_show_mode_key(struct device *dev,
32
struct device_attribute *attr, char *buf)
34
struct arvo_device *arvo =
35
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
36
struct usb_device *usb_dev =
37
interface_to_usbdev(to_usb_interface(dev->parent->parent));
38
struct arvo_mode_key temp_buf;
41
mutex_lock(&arvo->arvo_lock);
42
retval = roccat_common_receive(usb_dev, ARVO_COMMAND_MODE_KEY,
43
&temp_buf, sizeof(struct arvo_mode_key));
44
mutex_unlock(&arvo->arvo_lock);
48
return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.state);
51
static ssize_t arvo_sysfs_set_mode_key(struct device *dev,
52
struct device_attribute *attr, char const *buf, size_t size)
54
struct arvo_device *arvo =
55
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
56
struct usb_device *usb_dev =
57
interface_to_usbdev(to_usb_interface(dev->parent->parent));
58
struct arvo_mode_key temp_buf;
62
retval = strict_strtoul(buf, 10, &state);
66
temp_buf.command = ARVO_COMMAND_MODE_KEY;
67
temp_buf.state = state;
69
mutex_lock(&arvo->arvo_lock);
70
retval = roccat_common_send(usb_dev, ARVO_COMMAND_MODE_KEY,
71
&temp_buf, sizeof(struct arvo_mode_key));
72
mutex_unlock(&arvo->arvo_lock);
79
static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
80
struct device_attribute *attr, char *buf)
82
struct arvo_device *arvo =
83
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
84
struct usb_device *usb_dev =
85
interface_to_usbdev(to_usb_interface(dev->parent->parent));
86
struct arvo_key_mask temp_buf;
89
mutex_lock(&arvo->arvo_lock);
90
retval = roccat_common_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
91
&temp_buf, sizeof(struct arvo_key_mask));
92
mutex_unlock(&arvo->arvo_lock);
96
return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask);
99
static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
100
struct device_attribute *attr, char const *buf, size_t size)
102
struct arvo_device *arvo =
103
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
104
struct usb_device *usb_dev =
105
interface_to_usbdev(to_usb_interface(dev->parent->parent));
106
struct arvo_key_mask temp_buf;
107
unsigned long key_mask;
110
retval = strict_strtoul(buf, 10, &key_mask);
114
temp_buf.command = ARVO_COMMAND_KEY_MASK;
115
temp_buf.key_mask = key_mask;
117
mutex_lock(&arvo->arvo_lock);
118
retval = roccat_common_send(usb_dev, ARVO_COMMAND_KEY_MASK,
119
&temp_buf, sizeof(struct arvo_key_mask));
120
mutex_unlock(&arvo->arvo_lock);
127
/* retval is 1-5 on success, < 0 on error */
128
static int arvo_get_actual_profile(struct usb_device *usb_dev)
130
struct arvo_actual_profile temp_buf;
133
retval = roccat_common_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
134
&temp_buf, sizeof(struct arvo_actual_profile));
139
return temp_buf.actual_profile;
142
static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
143
struct device_attribute *attr, char *buf)
145
struct arvo_device *arvo =
146
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
148
return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
151
static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
152
struct device_attribute *attr, char const *buf, size_t size)
154
struct arvo_device *arvo =
155
hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
156
struct usb_device *usb_dev =
157
interface_to_usbdev(to_usb_interface(dev->parent->parent));
158
struct arvo_actual_profile temp_buf;
159
unsigned long profile;
162
retval = strict_strtoul(buf, 10, &profile);
166
if (profile < 1 || profile > 5)
169
temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
170
temp_buf.actual_profile = profile;
172
mutex_lock(&arvo->arvo_lock);
173
retval = roccat_common_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
174
&temp_buf, sizeof(struct arvo_actual_profile));
176
arvo->actual_profile = profile;
179
mutex_unlock(&arvo->arvo_lock);
183
static ssize_t arvo_sysfs_write(struct file *fp,
184
struct kobject *kobj, void const *buf,
185
loff_t off, size_t count, size_t real_size, uint command)
188
container_of(kobj, struct device, kobj)->parent->parent;
189
struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
190
struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
193
if (off != 0 || count != real_size)
196
mutex_lock(&arvo->arvo_lock);
197
retval = roccat_common_send(usb_dev, command, buf, real_size);
198
mutex_unlock(&arvo->arvo_lock);
200
return (retval ? retval : real_size);
203
static ssize_t arvo_sysfs_read(struct file *fp,
204
struct kobject *kobj, void *buf, loff_t off,
205
size_t count, size_t real_size, uint command)
208
container_of(kobj, struct device, 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_common_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);
234
static ssize_t arvo_sysfs_read_info(struct file *fp,
235
struct kobject *kobj, struct bin_attribute *attr, char *buf,
236
loff_t off, size_t count)
238
return arvo_sysfs_read(fp, kobj, buf, off, count,
239
sizeof(struct arvo_info), ARVO_COMMAND_INFO);
243
static struct device_attribute arvo_attributes[] = {
244
__ATTR(mode_key, 0660,
245
arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key),
246
__ATTR(key_mask, 0660,
247
arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask),
248
__ATTR(actual_profile, 0660,
249
arvo_sysfs_show_actual_profile,
250
arvo_sysfs_set_actual_profile),
254
static struct bin_attribute arvo_bin_attributes[] = {
256
.attr = { .name = "button", .mode = 0220 },
257
.size = sizeof(struct arvo_button),
258
.write = arvo_sysfs_write_button
261
.attr = { .name = "info", .mode = 0440 },
262
.size = sizeof(struct arvo_info),
263
.read = arvo_sysfs_read_info
268
static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
269
struct arvo_device *arvo)
273
mutex_init(&arvo->arvo_lock);
275
retval = arvo_get_actual_profile(usb_dev);
278
arvo->actual_profile = retval;
283
static int arvo_init_specials(struct hid_device *hdev)
285
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
286
struct usb_device *usb_dev = interface_to_usbdev(intf);
287
struct arvo_device *arvo;
290
if (intf->cur_altsetting->desc.bInterfaceProtocol
291
== USB_INTERFACE_PROTOCOL_KEYBOARD) {
292
hid_set_drvdata(hdev, NULL);
296
arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
298
hid_err(hdev, "can't alloc device descriptor\n");
301
hid_set_drvdata(hdev, arvo);
303
retval = arvo_init_arvo_device_struct(usb_dev, arvo);
305
hid_err(hdev, "couldn't init struct arvo_device\n");
309
retval = roccat_connect(arvo_class, hdev,
310
sizeof(struct arvo_roccat_report));
312
hid_err(hdev, "couldn't init char dev\n");
314
arvo->chrdev_minor = retval;
315
arvo->roccat_claimed = 1;
324
static void arvo_remove_specials(struct hid_device *hdev)
326
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
327
struct arvo_device *arvo;
329
if (intf->cur_altsetting->desc.bInterfaceProtocol
330
== USB_INTERFACE_PROTOCOL_KEYBOARD)
333
arvo = hid_get_drvdata(hdev);
334
if (arvo->roccat_claimed)
335
roccat_disconnect(arvo->chrdev_minor);
339
static int arvo_probe(struct hid_device *hdev,
340
const struct hid_device_id *id)
344
retval = hid_parse(hdev);
346
hid_err(hdev, "parse failed\n");
350
retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
352
hid_err(hdev, "hw start failed\n");
356
retval = arvo_init_specials(hdev);
358
hid_err(hdev, "couldn't install keyboard\n");
370
static void arvo_remove(struct hid_device *hdev)
372
arvo_remove_specials(hdev);
376
static void arvo_report_to_chrdev(struct arvo_device const *arvo,
379
struct arvo_special_report const *special_report;
380
struct arvo_roccat_report roccat_report;
382
special_report = (struct arvo_special_report const *)data;
384
roccat_report.profile = arvo->actual_profile;
385
roccat_report.button = special_report->event &
386
ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
387
if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
388
ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
389
roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
391
roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
393
roccat_report_event(arvo->chrdev_minor,
394
(uint8_t const *)&roccat_report);
397
static int arvo_raw_event(struct hid_device *hdev,
398
struct hid_report *report, u8 *data, int size)
400
struct arvo_device *arvo = hid_get_drvdata(hdev);
405
if (arvo && arvo->roccat_claimed)
406
arvo_report_to_chrdev(arvo, data);
411
static const struct hid_device_id arvo_devices[] = {
412
{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
416
MODULE_DEVICE_TABLE(hid, arvo_devices);
418
static struct hid_driver arvo_driver = {
420
.id_table = arvo_devices,
422
.remove = arvo_remove,
423
.raw_event = arvo_raw_event
426
static int __init arvo_init(void)
430
arvo_class = class_create(THIS_MODULE, "arvo");
431
if (IS_ERR(arvo_class))
432
return PTR_ERR(arvo_class);
433
arvo_class->dev_attrs = arvo_attributes;
434
arvo_class->dev_bin_attrs = arvo_bin_attributes;
436
retval = hid_register_driver(&arvo_driver);
438
class_destroy(arvo_class);
442
static void __exit arvo_exit(void)
444
hid_unregister_driver(&arvo_driver);
445
class_destroy(arvo_class);
448
module_init(arvo_init);
449
module_exit(arvo_exit);
451
MODULE_AUTHOR("Stefan Achatz");
452
MODULE_DESCRIPTION("USB Roccat Arvo driver");
453
MODULE_LICENSE("GPL v2");