blob: 1948208fe0380ad2b091038d369d6502de5d2a11 [file] [log] [blame]
Stefan Achatze68cc6032011-01-06 09:00:34 +01001/*
2 * Roccat Arvo driver for Linux
3 *
4 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
6
7/*
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)
11 * any later version.
12 */
13
14/*
15 * Roccat Arvo is a gamer keyboard with 5 macro keys that can be configured in
16 * 5 profiles.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
Stefan Achatze68cc6032011-01-06 09:00:34 +010022#include <linux/module.h>
23#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010024#include <linux/hid-roccat.h>
Stefan Achatze68cc6032011-01-06 09:00:34 +010025#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010026#include "hid-roccat-common.h"
Stefan Achatze68cc6032011-01-06 09:00:34 +010027#include "hid-roccat-arvo.h"
28
29static struct class *arvo_class;
30
Stefan Achatze68cc6032011-01-06 09:00:34 +010031static ssize_t arvo_sysfs_show_mode_key(struct device *dev,
32 struct device_attribute *attr, char *buf)
33{
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));
Stefan Achatz5772f632011-01-30 13:38:23 +010038 struct arvo_mode_key temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +010039 int retval;
40
Stefan Achatze68cc6032011-01-06 09:00:34 +010041 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020042 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY,
Stefan Achatz5772f632011-01-30 13:38:23 +010043 &temp_buf, sizeof(struct arvo_mode_key));
Stefan Achatze68cc6032011-01-06 09:00:34 +010044 mutex_unlock(&arvo->arvo_lock);
45 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +010046 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +010047
Stefan Achatz5772f632011-01-30 13:38:23 +010048 return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.state);
Stefan Achatze68cc6032011-01-06 09:00:34 +010049}
50
51static ssize_t arvo_sysfs_set_mode_key(struct device *dev,
52 struct device_attribute *attr, char const *buf, size_t size)
53{
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));
Stefan Achatz5772f632011-01-30 13:38:23 +010058 struct arvo_mode_key temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +010059 unsigned long state;
60 int retval;
61
Jingoo Handfc450b2013-07-19 15:53:16 +090062 retval = kstrtoul(buf, 10, &state);
Stefan Achatze68cc6032011-01-06 09:00:34 +010063 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +010064 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +010065
Stefan Achatz5772f632011-01-30 13:38:23 +010066 temp_buf.command = ARVO_COMMAND_MODE_KEY;
67 temp_buf.state = state;
Stefan Achatze68cc6032011-01-06 09:00:34 +010068
69 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020070 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY,
Stefan Achatz5772f632011-01-30 13:38:23 +010071 &temp_buf, sizeof(struct arvo_mode_key));
Stefan Achatze68cc6032011-01-06 09:00:34 +010072 mutex_unlock(&arvo->arvo_lock);
73 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +010074 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +010075
Stefan Achatz5772f632011-01-30 13:38:23 +010076 return size;
Stefan Achatze68cc6032011-01-06 09:00:34 +010077}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -070078static DEVICE_ATTR(mode_key, 0660,
79 arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key);
Stefan Achatze68cc6032011-01-06 09:00:34 +010080
81static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
82 struct device_attribute *attr, char *buf)
83{
84 struct arvo_device *arvo =
85 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
86 struct usb_device *usb_dev =
87 interface_to_usbdev(to_usb_interface(dev->parent->parent));
Stefan Achatz5772f632011-01-30 13:38:23 +010088 struct arvo_key_mask temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +010089 int retval;
90
Stefan Achatze68cc6032011-01-06 09:00:34 +010091 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020092 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
Stefan Achatz5772f632011-01-30 13:38:23 +010093 &temp_buf, sizeof(struct arvo_key_mask));
Stefan Achatze68cc6032011-01-06 09:00:34 +010094 mutex_unlock(&arvo->arvo_lock);
95 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +010096 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +010097
Stefan Achatz5772f632011-01-30 13:38:23 +010098 return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask);
Stefan Achatze68cc6032011-01-06 09:00:34 +010099}
100
101static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
102 struct device_attribute *attr, char const *buf, size_t size)
103{
104 struct arvo_device *arvo =
105 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
106 struct usb_device *usb_dev =
107 interface_to_usbdev(to_usb_interface(dev->parent->parent));
Stefan Achatz5772f632011-01-30 13:38:23 +0100108 struct arvo_key_mask temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100109 unsigned long key_mask;
110 int retval;
111
Jingoo Handfc450b2013-07-19 15:53:16 +0900112 retval = kstrtoul(buf, 10, &key_mask);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100113 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100114 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100115
Stefan Achatz5772f632011-01-30 13:38:23 +0100116 temp_buf.command = ARVO_COMMAND_KEY_MASK;
117 temp_buf.key_mask = key_mask;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100118
119 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200120 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK,
Stefan Achatz5772f632011-01-30 13:38:23 +0100121 &temp_buf, sizeof(struct arvo_key_mask));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100122 mutex_unlock(&arvo->arvo_lock);
123 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100124 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100125
Stefan Achatz5772f632011-01-30 13:38:23 +0100126 return size;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100127}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700128static DEVICE_ATTR(key_mask, 0660,
129 arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100130
131/* retval is 1-5 on success, < 0 on error */
132static int arvo_get_actual_profile(struct usb_device *usb_dev)
133{
Stefan Achatz5772f632011-01-30 13:38:23 +0100134 struct arvo_actual_profile temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100135 int retval;
136
Stefan Achatz7392d732012-05-20 22:45:04 +0200137 retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
Stefan Achatz5772f632011-01-30 13:38:23 +0100138 &temp_buf, sizeof(struct arvo_actual_profile));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100139
Stefan Achatz5772f632011-01-30 13:38:23 +0100140 if (retval)
141 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100142
Stefan Achatz5772f632011-01-30 13:38:23 +0100143 return temp_buf.actual_profile;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100144}
145
146static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct arvo_device *arvo =
150 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
151
152 return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
153}
154
155static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
156 struct device_attribute *attr, char const *buf, size_t size)
157{
158 struct arvo_device *arvo =
159 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
160 struct usb_device *usb_dev =
161 interface_to_usbdev(to_usb_interface(dev->parent->parent));
Stefan Achatz5772f632011-01-30 13:38:23 +0100162 struct arvo_actual_profile temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100163 unsigned long profile;
164 int retval;
165
Jingoo Handfc450b2013-07-19 15:53:16 +0900166 retval = kstrtoul(buf, 10, &profile);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100167 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100168 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100169
Stefan Achatz901e64d2011-06-12 10:02:44 +0200170 if (profile < 1 || profile > 5)
171 return -EINVAL;
172
Stefan Achatz5772f632011-01-30 13:38:23 +0100173 temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
174 temp_buf.actual_profile = profile;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100175
176 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200177 retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
Stefan Achatz5772f632011-01-30 13:38:23 +0100178 &temp_buf, sizeof(struct arvo_actual_profile));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100179 if (!retval) {
180 arvo->actual_profile = profile;
181 retval = size;
182 }
183 mutex_unlock(&arvo->arvo_lock);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100184 return retval;
185}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700186static DEVICE_ATTR(actual_profile, 0660,
187 arvo_sysfs_show_actual_profile,
188 arvo_sysfs_set_actual_profile);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100189
190static ssize_t arvo_sysfs_write(struct file *fp,
191 struct kobject *kobj, void const *buf,
192 loff_t off, size_t count, size_t real_size, uint command)
193{
194 struct device *dev =
195 container_of(kobj, struct device, kobj)->parent->parent;
196 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
197 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
198 int retval;
199
200 if (off != 0 || count != real_size)
201 return -EINVAL;
202
203 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200204 retval = roccat_common2_send(usb_dev, command, buf, real_size);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100205 mutex_unlock(&arvo->arvo_lock);
206
207 return (retval ? retval : real_size);
208}
209
210static ssize_t arvo_sysfs_read(struct file *fp,
211 struct kobject *kobj, void *buf, loff_t off,
212 size_t count, size_t real_size, uint command)
213{
214 struct device *dev =
215 container_of(kobj, struct device, kobj)->parent->parent;
216 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
217 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
218 int retval;
219
220 if (off >= real_size)
221 return 0;
222
223 if (off != 0 || count != real_size)
224 return -EINVAL;
225
226 mutex_lock(&arvo->arvo_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200227 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100228 mutex_unlock(&arvo->arvo_lock);
229
230 return (retval ? retval : real_size);
231}
232
233static ssize_t arvo_sysfs_write_button(struct file *fp,
234 struct kobject *kobj, struct bin_attribute *attr, char *buf,
235 loff_t off, size_t count)
236{
237 return arvo_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200238 sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100239}
Greg Kroah-Hartmane0a00d82013-08-19 21:40:26 -0700240static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button,
241 sizeof(struct arvo_button));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100242
243static ssize_t arvo_sysfs_read_info(struct file *fp,
244 struct kobject *kobj, struct bin_attribute *attr, char *buf,
245 loff_t off, size_t count)
246{
247 return arvo_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200248 sizeof(struct arvo_info), ARVO_COMMAND_INFO);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100249}
Greg Kroah-Hartmane0a00d82013-08-19 21:40:26 -0700250static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL,
251 sizeof(struct arvo_info));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100252
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700253static struct attribute *arvo_attrs[] = {
254 &dev_attr_mode_key.attr,
255 &dev_attr_key_mask.attr,
256 &dev_attr_actual_profile.attr,
257 NULL,
Stefan Achatze68cc6032011-01-06 09:00:34 +0100258};
259
Greg Kroah-Hartmane0a00d82013-08-19 21:40:26 -0700260static struct bin_attribute *arvo_bin_attributes[] = {
261 &bin_attr_button,
262 &bin_attr_info,
263 NULL,
264};
265
266static const struct attribute_group arvo_group = {
267 .attrs = arvo_attrs,
268 .bin_attrs = arvo_bin_attributes,
269};
270
271static const struct attribute_group *arvo_groups[] = {
272 &arvo_group,
273 NULL,
Stefan Achatze68cc6032011-01-06 09:00:34 +0100274};
275
276static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
277 struct arvo_device *arvo)
278{
279 int retval;
280
281 mutex_init(&arvo->arvo_lock);
282
283 retval = arvo_get_actual_profile(usb_dev);
284 if (retval < 0)
285 return retval;
286 arvo->actual_profile = retval;
287
288 return 0;
289}
290
291static int arvo_init_specials(struct hid_device *hdev)
292{
293 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
294 struct usb_device *usb_dev = interface_to_usbdev(intf);
295 struct arvo_device *arvo;
296 int retval;
297
298 if (intf->cur_altsetting->desc.bInterfaceProtocol
299 == USB_INTERFACE_PROTOCOL_KEYBOARD) {
300 hid_set_drvdata(hdev, NULL);
301 return 0;
302 }
303
304 arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
305 if (!arvo) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100306 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100307 return -ENOMEM;
308 }
309 hid_set_drvdata(hdev, arvo);
310
311 retval = arvo_init_arvo_device_struct(usb_dev, arvo);
312 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100313 hid_err(hdev, "couldn't init struct arvo_device\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100314 goto exit_free;
315 }
316
Stefan Achatz8211e462011-01-30 13:38:25 +0100317 retval = roccat_connect(arvo_class, hdev,
318 sizeof(struct arvo_roccat_report));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100319 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100320 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100321 } else {
322 arvo->chrdev_minor = retval;
323 arvo->roccat_claimed = 1;
324 }
325
326 return 0;
327exit_free:
328 kfree(arvo);
329 return retval;
330}
331
332static void arvo_remove_specials(struct hid_device *hdev)
333{
334 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
335 struct arvo_device *arvo;
336
337 if (intf->cur_altsetting->desc.bInterfaceProtocol
338 == USB_INTERFACE_PROTOCOL_KEYBOARD)
339 return;
340
341 arvo = hid_get_drvdata(hdev);
342 if (arvo->roccat_claimed)
343 roccat_disconnect(arvo->chrdev_minor);
344 kfree(arvo);
345}
346
347static int arvo_probe(struct hid_device *hdev,
348 const struct hid_device_id *id)
349{
350 int retval;
351
352 retval = hid_parse(hdev);
353 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100354 hid_err(hdev, "parse failed\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100355 goto exit;
356 }
357
358 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
359 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100360 hid_err(hdev, "hw start failed\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100361 goto exit;
362 }
363
364 retval = arvo_init_specials(hdev);
365 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100366 hid_err(hdev, "couldn't install keyboard\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100367 goto exit_stop;
368 }
369
370 return 0;
371
372exit_stop:
373 hid_hw_stop(hdev);
374exit:
375 return retval;
376}
377
378static void arvo_remove(struct hid_device *hdev)
379{
380 arvo_remove_specials(hdev);
381 hid_hw_stop(hdev);
382}
383
384static void arvo_report_to_chrdev(struct arvo_device const *arvo,
385 u8 const *data)
386{
387 struct arvo_special_report const *special_report;
388 struct arvo_roccat_report roccat_report;
389
390 special_report = (struct arvo_special_report const *)data;
391
392 roccat_report.profile = arvo->actual_profile;
393 roccat_report.button = special_report->event &
394 ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
395 if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
396 ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
397 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
398 else
399 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
400
Stefan Achatz8211e462011-01-30 13:38:25 +0100401 roccat_report_event(arvo->chrdev_minor,
402 (uint8_t const *)&roccat_report);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100403}
404
405static int arvo_raw_event(struct hid_device *hdev,
406 struct hid_report *report, u8 *data, int size)
407{
408 struct arvo_device *arvo = hid_get_drvdata(hdev);
409
410 if (size != 3)
411 return 0;
412
Stefan Achatz901e64d2011-06-12 10:02:44 +0200413 if (arvo && arvo->roccat_claimed)
Stefan Achatze68cc6032011-01-06 09:00:34 +0100414 arvo_report_to_chrdev(arvo, data);
415
416 return 0;
417}
418
419static const struct hid_device_id arvo_devices[] = {
420 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
421 { }
422};
423
424MODULE_DEVICE_TABLE(hid, arvo_devices);
425
426static struct hid_driver arvo_driver = {
427 .name = "arvo",
428 .id_table = arvo_devices,
429 .probe = arvo_probe,
430 .remove = arvo_remove,
431 .raw_event = arvo_raw_event
432};
433
434static int __init arvo_init(void)
435{
436 int retval;
437
438 arvo_class = class_create(THIS_MODULE, "arvo");
439 if (IS_ERR(arvo_class))
440 return PTR_ERR(arvo_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700441 arvo_class->dev_groups = arvo_groups;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100442
443 retval = hid_register_driver(&arvo_driver);
444 if (retval)
445 class_destroy(arvo_class);
446 return retval;
447}
448
449static void __exit arvo_exit(void)
450{
Stefan Achatze68cc6032011-01-06 09:00:34 +0100451 hid_unregister_driver(&arvo_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100452 class_destroy(arvo_class);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100453}
454
455module_init(arvo_init);
456module_exit(arvo_exit);
457
458MODULE_AUTHOR("Stefan Achatz");
459MODULE_DESCRIPTION("USB Roccat Arvo driver");
460MODULE_LICENSE("GPL v2");