blob: 093bfad00b023d8a22a6bf17c382f4eef311b7ce [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 Achatz1edd5b42011-06-01 15:54:17 +020042 retval = roccat_common_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
Stefan Achatze68cc6032011-01-06 09:00:34 +010062 retval = strict_strtoul(buf, 10, &state);
63 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 Achatz1edd5b42011-06-01 15:54:17 +020070 retval = roccat_common_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}
78
79static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
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));
Stefan Achatz5772f632011-01-30 13:38:23 +010086 struct arvo_key_mask temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +010087 int retval;
88
Stefan Achatze68cc6032011-01-06 09:00:34 +010089 mutex_lock(&arvo->arvo_lock);
Stefan Achatz1edd5b42011-06-01 15:54:17 +020090 retval = roccat_common_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
Stefan Achatz5772f632011-01-30 13:38:23 +010091 &temp_buf, sizeof(struct arvo_key_mask));
Stefan Achatze68cc6032011-01-06 09:00:34 +010092 mutex_unlock(&arvo->arvo_lock);
93 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +010094 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +010095
Stefan Achatz5772f632011-01-30 13:38:23 +010096 return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask);
Stefan Achatze68cc6032011-01-06 09:00:34 +010097}
98
99static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
100 struct device_attribute *attr, char const *buf, size_t size)
101{
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));
Stefan Achatz5772f632011-01-30 13:38:23 +0100106 struct arvo_key_mask temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100107 unsigned long key_mask;
108 int retval;
109
Stefan Achatze68cc6032011-01-06 09:00:34 +0100110 retval = strict_strtoul(buf, 10, &key_mask);
111 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100112 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100113
Stefan Achatz5772f632011-01-30 13:38:23 +0100114 temp_buf.command = ARVO_COMMAND_KEY_MASK;
115 temp_buf.key_mask = key_mask;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100116
117 mutex_lock(&arvo->arvo_lock);
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200118 retval = roccat_common_send(usb_dev, ARVO_COMMAND_KEY_MASK,
Stefan Achatz5772f632011-01-30 13:38:23 +0100119 &temp_buf, sizeof(struct arvo_key_mask));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100120 mutex_unlock(&arvo->arvo_lock);
121 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100122 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100123
Stefan Achatz5772f632011-01-30 13:38:23 +0100124 return size;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100125}
126
127/* retval is 1-5 on success, < 0 on error */
128static int arvo_get_actual_profile(struct usb_device *usb_dev)
129{
Stefan Achatz5772f632011-01-30 13:38:23 +0100130 struct arvo_actual_profile temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100131 int retval;
132
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200133 retval = roccat_common_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
Stefan Achatz5772f632011-01-30 13:38:23 +0100134 &temp_buf, sizeof(struct arvo_actual_profile));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100135
Stefan Achatz5772f632011-01-30 13:38:23 +0100136 if (retval)
137 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100138
Stefan Achatz5772f632011-01-30 13:38:23 +0100139 return temp_buf.actual_profile;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100140}
141
142static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
145 struct arvo_device *arvo =
146 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
147
148 return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
149}
150
151static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
152 struct device_attribute *attr, char const *buf, size_t size)
153{
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));
Stefan Achatz5772f632011-01-30 13:38:23 +0100158 struct arvo_actual_profile temp_buf;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100159 unsigned long profile;
160 int retval;
161
Stefan Achatze68cc6032011-01-06 09:00:34 +0100162 retval = strict_strtoul(buf, 10, &profile);
163 if (retval)
Stefan Achatz5772f632011-01-30 13:38:23 +0100164 return retval;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100165
Stefan Achatz901e64d2011-06-12 10:02:44 +0200166 if (profile < 1 || profile > 5)
167 return -EINVAL;
168
Stefan Achatz5772f632011-01-30 13:38:23 +0100169 temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
170 temp_buf.actual_profile = profile;
Stefan Achatze68cc6032011-01-06 09:00:34 +0100171
172 mutex_lock(&arvo->arvo_lock);
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200173 retval = roccat_common_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
Stefan Achatz5772f632011-01-30 13:38:23 +0100174 &temp_buf, sizeof(struct arvo_actual_profile));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100175 if (!retval) {
176 arvo->actual_profile = profile;
177 retval = size;
178 }
179 mutex_unlock(&arvo->arvo_lock);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100180 return retval;
181}
182
183static 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)
186{
187 struct device *dev =
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));
191 int retval;
192
193 if (off != 0 || count != real_size)
194 return -EINVAL;
195
196 mutex_lock(&arvo->arvo_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100197 retval = roccat_common_send(usb_dev, command, buf, real_size);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100198 mutex_unlock(&arvo->arvo_lock);
199
200 return (retval ? retval : real_size);
201}
202
203static 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)
206{
207 struct device *dev =
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));
211 int retval;
212
213 if (off >= real_size)
214 return 0;
215
216 if (off != 0 || count != real_size)
217 return -EINVAL;
218
219 mutex_lock(&arvo->arvo_lock);
Stefan Achatz5772f632011-01-30 13:38:23 +0100220 retval = roccat_common_receive(usb_dev, command, buf, real_size);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100221 mutex_unlock(&arvo->arvo_lock);
222
223 return (retval ? retval : real_size);
224}
225
226static 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)
229{
230 return arvo_sysfs_write(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200231 sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100232}
233
234static 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)
237{
238 return arvo_sysfs_read(fp, kobj, buf, off, count,
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200239 sizeof(struct arvo_info), ARVO_COMMAND_INFO);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100240}
241
242
243static 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),
251 __ATTR_NULL
252};
253
254static struct bin_attribute arvo_bin_attributes[] = {
255 {
256 .attr = { .name = "button", .mode = 0220 },
257 .size = sizeof(struct arvo_button),
258 .write = arvo_sysfs_write_button
259 },
260 {
261 .attr = { .name = "info", .mode = 0440 },
262 .size = sizeof(struct arvo_info),
263 .read = arvo_sysfs_read_info
264 },
265 __ATTR_NULL
266};
267
268static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
269 struct arvo_device *arvo)
270{
271 int retval;
272
273 mutex_init(&arvo->arvo_lock);
274
275 retval = arvo_get_actual_profile(usb_dev);
276 if (retval < 0)
277 return retval;
278 arvo->actual_profile = retval;
279
280 return 0;
281}
282
283static int arvo_init_specials(struct hid_device *hdev)
284{
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;
288 int retval;
289
290 if (intf->cur_altsetting->desc.bInterfaceProtocol
291 == USB_INTERFACE_PROTOCOL_KEYBOARD) {
292 hid_set_drvdata(hdev, NULL);
293 return 0;
294 }
295
296 arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
297 if (!arvo) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100298 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100299 return -ENOMEM;
300 }
301 hid_set_drvdata(hdev, arvo);
302
303 retval = arvo_init_arvo_device_struct(usb_dev, arvo);
304 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100305 hid_err(hdev, "couldn't init struct arvo_device\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100306 goto exit_free;
307 }
308
Stefan Achatz8211e462011-01-30 13:38:25 +0100309 retval = roccat_connect(arvo_class, hdev,
310 sizeof(struct arvo_roccat_report));
Stefan Achatze68cc6032011-01-06 09:00:34 +0100311 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100312 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100313 } else {
314 arvo->chrdev_minor = retval;
315 arvo->roccat_claimed = 1;
316 }
317
318 return 0;
319exit_free:
320 kfree(arvo);
321 return retval;
322}
323
324static void arvo_remove_specials(struct hid_device *hdev)
325{
326 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
327 struct arvo_device *arvo;
328
329 if (intf->cur_altsetting->desc.bInterfaceProtocol
330 == USB_INTERFACE_PROTOCOL_KEYBOARD)
331 return;
332
333 arvo = hid_get_drvdata(hdev);
334 if (arvo->roccat_claimed)
335 roccat_disconnect(arvo->chrdev_minor);
336 kfree(arvo);
337}
338
339static int arvo_probe(struct hid_device *hdev,
340 const struct hid_device_id *id)
341{
342 int retval;
343
344 retval = hid_parse(hdev);
345 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100346 hid_err(hdev, "parse failed\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100347 goto exit;
348 }
349
350 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
351 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100352 hid_err(hdev, "hw start failed\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100353 goto exit;
354 }
355
356 retval = arvo_init_specials(hdev);
357 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100358 hid_err(hdev, "couldn't install keyboard\n");
Stefan Achatze68cc6032011-01-06 09:00:34 +0100359 goto exit_stop;
360 }
361
362 return 0;
363
364exit_stop:
365 hid_hw_stop(hdev);
366exit:
367 return retval;
368}
369
370static void arvo_remove(struct hid_device *hdev)
371{
372 arvo_remove_specials(hdev);
373 hid_hw_stop(hdev);
374}
375
376static void arvo_report_to_chrdev(struct arvo_device const *arvo,
377 u8 const *data)
378{
379 struct arvo_special_report const *special_report;
380 struct arvo_roccat_report roccat_report;
381
382 special_report = (struct arvo_special_report const *)data;
383
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;
390 else
391 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
392
Stefan Achatz8211e462011-01-30 13:38:25 +0100393 roccat_report_event(arvo->chrdev_minor,
394 (uint8_t const *)&roccat_report);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100395}
396
397static int arvo_raw_event(struct hid_device *hdev,
398 struct hid_report *report, u8 *data, int size)
399{
400 struct arvo_device *arvo = hid_get_drvdata(hdev);
401
402 if (size != 3)
403 return 0;
404
Stefan Achatz901e64d2011-06-12 10:02:44 +0200405 if (arvo && arvo->roccat_claimed)
Stefan Achatze68cc6032011-01-06 09:00:34 +0100406 arvo_report_to_chrdev(arvo, data);
407
408 return 0;
409}
410
411static const struct hid_device_id arvo_devices[] = {
412 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
413 { }
414};
415
416MODULE_DEVICE_TABLE(hid, arvo_devices);
417
418static struct hid_driver arvo_driver = {
419 .name = "arvo",
420 .id_table = arvo_devices,
421 .probe = arvo_probe,
422 .remove = arvo_remove,
423 .raw_event = arvo_raw_event
424};
425
426static int __init arvo_init(void)
427{
428 int retval;
429
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;
435
436 retval = hid_register_driver(&arvo_driver);
437 if (retval)
438 class_destroy(arvo_class);
439 return retval;
440}
441
442static void __exit arvo_exit(void)
443{
Stefan Achatze68cc6032011-01-06 09:00:34 +0100444 hid_unregister_driver(&arvo_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100445 class_destroy(arvo_class);
Stefan Achatze68cc6032011-01-06 09:00:34 +0100446}
447
448module_init(arvo_init);
449module_exit(arvo_exit);
450
451MODULE_AUTHOR("Stefan Achatz");
452MODULE_DESCRIPTION("USB Roccat Arvo driver");
453MODULE_LICENSE("GPL v2");