blob: ebf3c15f1a7e50e1168363b552635bb2a6fc430b [file] [log] [blame]
Stefan Achatze68cc602011-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>
22#include <linux/usb.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include "hid-ids.h"
26#include "hid-roccat.h"
27#include "hid-roccat-arvo.h"
28
29static struct class *arvo_class;
30
31static int arvo_receive(struct usb_device *usb_dev, uint usb_command,
32 void *buf, uint size)
33{
34 int len;
35
36 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
37 USB_REQ_CLEAR_FEATURE,
38 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
39 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
40
41 return (len != size) ? -EIO : 0;
42}
43
44static int arvo_send(struct usb_device *usb_dev, uint usb_command,
45 void const *buf, uint size)
46{
47 int len;
48
49 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
50 USB_REQ_SET_CONFIGURATION,
51 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
52 usb_command, 0, (void *)buf, size, USB_CTRL_SET_TIMEOUT);
53
54 return (len != size) ? -EIO : 0;
55}
56
57static ssize_t arvo_sysfs_show_mode_key(struct device *dev,
58 struct device_attribute *attr, char *buf)
59{
60 struct arvo_device *arvo =
61 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
62 struct usb_device *usb_dev =
63 interface_to_usbdev(to_usb_interface(dev->parent->parent));
64 struct arvo_mode_key *temp_buf;
65 int retval;
66
67 temp_buf = kmalloc(sizeof(struct arvo_mode_key), GFP_KERNEL);
68 if (!temp_buf)
69 return -ENOMEM;
70
71 mutex_lock(&arvo->arvo_lock);
72 retval = arvo_receive(usb_dev, ARVO_USB_COMMAND_MODE_KEY,
73 temp_buf, sizeof(struct arvo_mode_key));
74 mutex_unlock(&arvo->arvo_lock);
75 if (retval)
76 goto out;
77
78 retval = snprintf(buf, PAGE_SIZE, "%d\n", temp_buf->state);
79out:
80 kfree(temp_buf);
81 return retval;
82}
83
84static ssize_t arvo_sysfs_set_mode_key(struct device *dev,
85 struct device_attribute *attr, char const *buf, size_t size)
86{
87 struct arvo_device *arvo =
88 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
89 struct usb_device *usb_dev =
90 interface_to_usbdev(to_usb_interface(dev->parent->parent));
91 struct arvo_mode_key *temp_buf;
92 unsigned long state;
93 int retval;
94
95 temp_buf = kmalloc(sizeof(struct arvo_mode_key), GFP_KERNEL);
96 if (!temp_buf)
97 return -ENOMEM;
98
99 retval = strict_strtoul(buf, 10, &state);
100 if (retval)
101 goto out;
102
103 temp_buf->command = ARVO_COMMAND_MODE_KEY;
104 temp_buf->state = state;
105
106 mutex_lock(&arvo->arvo_lock);
107 retval = arvo_send(usb_dev, ARVO_USB_COMMAND_MODE_KEY,
108 temp_buf, sizeof(struct arvo_mode_key));
109 mutex_unlock(&arvo->arvo_lock);
110 if (retval)
111 goto out;
112
113 retval = size;
114out:
115 kfree(temp_buf);
116 return retval;
117}
118
119static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
120 struct device_attribute *attr, char *buf)
121{
122 struct arvo_device *arvo =
123 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
124 struct usb_device *usb_dev =
125 interface_to_usbdev(to_usb_interface(dev->parent->parent));
126 struct arvo_key_mask *temp_buf;
127 int retval;
128
129 temp_buf = kmalloc(sizeof(struct arvo_key_mask), GFP_KERNEL);
130 if (!temp_buf)
131 return -ENOMEM;
132
133 mutex_lock(&arvo->arvo_lock);
134 retval = arvo_receive(usb_dev, ARVO_USB_COMMAND_KEY_MASK,
135 temp_buf, sizeof(struct arvo_key_mask));
136 mutex_unlock(&arvo->arvo_lock);
137 if (retval)
138 goto out;
139
140 retval = snprintf(buf, PAGE_SIZE, "%d\n", temp_buf->key_mask);
141out:
142 kfree(temp_buf);
143 return retval;
144}
145
146static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
147 struct device_attribute *attr, char const *buf, size_t size)
148{
149 struct arvo_device *arvo =
150 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
151 struct usb_device *usb_dev =
152 interface_to_usbdev(to_usb_interface(dev->parent->parent));
153 struct arvo_key_mask *temp_buf;
154 unsigned long key_mask;
155 int retval;
156
157 temp_buf = kmalloc(sizeof(struct arvo_key_mask), GFP_KERNEL);
158 if (!temp_buf)
159 return -ENOMEM;
160
161 retval = strict_strtoul(buf, 10, &key_mask);
162 if (retval)
163 goto out;
164
165 temp_buf->command = ARVO_COMMAND_KEY_MASK;
166 temp_buf->key_mask = key_mask;
167
168 mutex_lock(&arvo->arvo_lock);
169 retval = arvo_send(usb_dev, ARVO_USB_COMMAND_KEY_MASK,
170 temp_buf, sizeof(struct arvo_key_mask));
171 mutex_unlock(&arvo->arvo_lock);
172 if (retval)
173 goto out;
174
175 retval = size;
176out:
177 kfree(temp_buf);
178 return retval;
179}
180
181/* retval is 1-5 on success, < 0 on error */
182static int arvo_get_actual_profile(struct usb_device *usb_dev)
183{
184 struct arvo_actual_profile *temp_buf;
185 int retval;
186
187 temp_buf = kmalloc(sizeof(struct arvo_actual_profile), GFP_KERNEL);
188 if (!temp_buf)
189 return -ENOMEM;
190
191 retval = arvo_receive(usb_dev, ARVO_USB_COMMAND_ACTUAL_PROFILE,
192 temp_buf, sizeof(struct arvo_actual_profile));
193
194 if (!retval)
195 retval = temp_buf->actual_profile;
196
197 kfree(temp_buf);
198 return retval;
199}
200
201static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
204 struct arvo_device *arvo =
205 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
206
207 return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
208}
209
210static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
211 struct device_attribute *attr, char const *buf, size_t size)
212{
213 struct arvo_device *arvo =
214 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
215 struct usb_device *usb_dev =
216 interface_to_usbdev(to_usb_interface(dev->parent->parent));
217 struct arvo_actual_profile *temp_buf;
218 unsigned long profile;
219 int retval;
220
221 temp_buf = kmalloc(sizeof(struct arvo_actual_profile), GFP_KERNEL);
222 if (!temp_buf)
223 return -ENOMEM;
224
225 retval = strict_strtoul(buf, 10, &profile);
226 if (retval)
227 goto out;
228
229 temp_buf->command = ARVO_COMMAND_ACTUAL_PROFILE;
230 temp_buf->actual_profile = profile;
231
232 mutex_lock(&arvo->arvo_lock);
233 retval = arvo_send(usb_dev, ARVO_USB_COMMAND_ACTUAL_PROFILE,
234 temp_buf, sizeof(struct arvo_actual_profile));
235 if (!retval) {
236 arvo->actual_profile = profile;
237 retval = size;
238 }
239 mutex_unlock(&arvo->arvo_lock);
240
241out:
242 kfree(temp_buf);
243 return retval;
244}
245
246static ssize_t arvo_sysfs_write(struct file *fp,
247 struct kobject *kobj, void const *buf,
248 loff_t off, size_t count, size_t real_size, uint command)
249{
250 struct device *dev =
251 container_of(kobj, struct device, kobj)->parent->parent;
252 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
253 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
254 int retval;
255
256 if (off != 0 || count != real_size)
257 return -EINVAL;
258
259 mutex_lock(&arvo->arvo_lock);
260 retval = arvo_send(usb_dev, command, buf, real_size);
261 mutex_unlock(&arvo->arvo_lock);
262
263 return (retval ? retval : real_size);
264}
265
266static ssize_t arvo_sysfs_read(struct file *fp,
267 struct kobject *kobj, void *buf, loff_t off,
268 size_t count, size_t real_size, uint command)
269{
270 struct device *dev =
271 container_of(kobj, struct device, kobj)->parent->parent;
272 struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
273 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
274 int retval;
275
276 if (off >= real_size)
277 return 0;
278
279 if (off != 0 || count != real_size)
280 return -EINVAL;
281
282 mutex_lock(&arvo->arvo_lock);
283 retval = arvo_receive(usb_dev, command, buf, real_size);
284 mutex_unlock(&arvo->arvo_lock);
285
286 return (retval ? retval : real_size);
287}
288
289static ssize_t arvo_sysfs_write_button(struct file *fp,
290 struct kobject *kobj, struct bin_attribute *attr, char *buf,
291 loff_t off, size_t count)
292{
293 return arvo_sysfs_write(fp, kobj, buf, off, count,
294 sizeof(struct arvo_button), ARVO_USB_COMMAND_BUTTON);
295}
296
297static ssize_t arvo_sysfs_read_info(struct file *fp,
298 struct kobject *kobj, struct bin_attribute *attr, char *buf,
299 loff_t off, size_t count)
300{
301 return arvo_sysfs_read(fp, kobj, buf, off, count,
302 sizeof(struct arvo_info), ARVO_USB_COMMAND_INFO);
303}
304
305
306static struct device_attribute arvo_attributes[] = {
307 __ATTR(mode_key, 0660,
308 arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key),
309 __ATTR(key_mask, 0660,
310 arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask),
311 __ATTR(actual_profile, 0660,
312 arvo_sysfs_show_actual_profile,
313 arvo_sysfs_set_actual_profile),
314 __ATTR_NULL
315};
316
317static struct bin_attribute arvo_bin_attributes[] = {
318 {
319 .attr = { .name = "button", .mode = 0220 },
320 .size = sizeof(struct arvo_button),
321 .write = arvo_sysfs_write_button
322 },
323 {
324 .attr = { .name = "info", .mode = 0440 },
325 .size = sizeof(struct arvo_info),
326 .read = arvo_sysfs_read_info
327 },
328 __ATTR_NULL
329};
330
331static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
332 struct arvo_device *arvo)
333{
334 int retval;
335
336 mutex_init(&arvo->arvo_lock);
337
338 retval = arvo_get_actual_profile(usb_dev);
339 if (retval < 0)
340 return retval;
341 arvo->actual_profile = retval;
342
343 return 0;
344}
345
346static int arvo_init_specials(struct hid_device *hdev)
347{
348 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
349 struct usb_device *usb_dev = interface_to_usbdev(intf);
350 struct arvo_device *arvo;
351 int retval;
352
353 if (intf->cur_altsetting->desc.bInterfaceProtocol
354 == USB_INTERFACE_PROTOCOL_KEYBOARD) {
355 hid_set_drvdata(hdev, NULL);
356 return 0;
357 }
358
359 arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
360 if (!arvo) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100361 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100362 return -ENOMEM;
363 }
364 hid_set_drvdata(hdev, arvo);
365
366 retval = arvo_init_arvo_device_struct(usb_dev, arvo);
367 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100368 hid_err(hdev, "couldn't init struct arvo_device\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100369 goto exit_free;
370 }
371
372 retval = roccat_connect(arvo_class, hdev);
373 if (retval < 0) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100374 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100375 } else {
376 arvo->chrdev_minor = retval;
377 arvo->roccat_claimed = 1;
378 }
379
380 return 0;
381exit_free:
382 kfree(arvo);
383 return retval;
384}
385
386static void arvo_remove_specials(struct hid_device *hdev)
387{
388 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
389 struct arvo_device *arvo;
390
391 if (intf->cur_altsetting->desc.bInterfaceProtocol
392 == USB_INTERFACE_PROTOCOL_KEYBOARD)
393 return;
394
395 arvo = hid_get_drvdata(hdev);
396 if (arvo->roccat_claimed)
397 roccat_disconnect(arvo->chrdev_minor);
398 kfree(arvo);
399}
400
401static int arvo_probe(struct hid_device *hdev,
402 const struct hid_device_id *id)
403{
404 int retval;
405
406 retval = hid_parse(hdev);
407 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100408 hid_err(hdev, "parse failed\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100409 goto exit;
410 }
411
412 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
413 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100414 hid_err(hdev, "hw start failed\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100415 goto exit;
416 }
417
418 retval = arvo_init_specials(hdev);
419 if (retval) {
Stefan Achatza28764e2011-01-30 13:38:21 +0100420 hid_err(hdev, "couldn't install keyboard\n");
Stefan Achatze68cc602011-01-06 09:00:34 +0100421 goto exit_stop;
422 }
423
424 return 0;
425
426exit_stop:
427 hid_hw_stop(hdev);
428exit:
429 return retval;
430}
431
432static void arvo_remove(struct hid_device *hdev)
433{
434 arvo_remove_specials(hdev);
435 hid_hw_stop(hdev);
436}
437
438static void arvo_report_to_chrdev(struct arvo_device const *arvo,
439 u8 const *data)
440{
441 struct arvo_special_report const *special_report;
442 struct arvo_roccat_report roccat_report;
443
444 special_report = (struct arvo_special_report const *)data;
445
446 roccat_report.profile = arvo->actual_profile;
447 roccat_report.button = special_report->event &
448 ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
449 if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
450 ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
451 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
452 else
453 roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
454
455 roccat_report_event(arvo->chrdev_minor, (uint8_t const *)&roccat_report,
456 sizeof(struct arvo_roccat_report));
457}
458
459static int arvo_raw_event(struct hid_device *hdev,
460 struct hid_report *report, u8 *data, int size)
461{
462 struct arvo_device *arvo = hid_get_drvdata(hdev);
463
464 if (size != 3)
465 return 0;
466
467 if (arvo->roccat_claimed)
468 arvo_report_to_chrdev(arvo, data);
469
470 return 0;
471}
472
473static const struct hid_device_id arvo_devices[] = {
474 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
475 { }
476};
477
478MODULE_DEVICE_TABLE(hid, arvo_devices);
479
480static struct hid_driver arvo_driver = {
481 .name = "arvo",
482 .id_table = arvo_devices,
483 .probe = arvo_probe,
484 .remove = arvo_remove,
485 .raw_event = arvo_raw_event
486};
487
488static int __init arvo_init(void)
489{
490 int retval;
491
492 arvo_class = class_create(THIS_MODULE, "arvo");
493 if (IS_ERR(arvo_class))
494 return PTR_ERR(arvo_class);
495 arvo_class->dev_attrs = arvo_attributes;
496 arvo_class->dev_bin_attrs = arvo_bin_attributes;
497
498 retval = hid_register_driver(&arvo_driver);
499 if (retval)
500 class_destroy(arvo_class);
501 return retval;
502}
503
504static void __exit arvo_exit(void)
505{
506 class_destroy(arvo_class);
507 hid_unregister_driver(&arvo_driver);
508}
509
510module_init(arvo_init);
511module_exit(arvo_exit);
512
513MODULE_AUTHOR("Stefan Achatz");
514MODULE_DESCRIPTION("USB Roccat Arvo driver");
515MODULE_LICENSE("GPL v2");