]>
Commit | Line | Data |
---|---|---|
e68cc603 SA |
1 | /* |
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> | |
e68cc603 SA |
22 | #include <linux/module.h> |
23 | #include <linux/slab.h> | |
5dc0c983 | 24 | #include <linux/hid-roccat.h> |
e68cc603 | 25 | #include "hid-ids.h" |
5772f636 | 26 | #include "hid-roccat-common.h" |
e68cc603 SA |
27 | #include "hid-roccat-arvo.h" |
28 | ||
29 | static struct class *arvo_class; | |
30 | ||
e68cc603 SA |
31 | static 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)); | |
5772f636 | 38 | struct arvo_mode_key temp_buf; |
e68cc603 SA |
39 | int retval; |
40 | ||
e68cc603 | 41 | mutex_lock(&arvo->arvo_lock); |
7392d73b | 42 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY, |
5772f636 | 43 | &temp_buf, sizeof(struct arvo_mode_key)); |
e68cc603 SA |
44 | mutex_unlock(&arvo->arvo_lock); |
45 | if (retval) | |
5772f636 | 46 | return retval; |
e68cc603 | 47 | |
5772f636 | 48 | return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.state); |
e68cc603 SA |
49 | } |
50 | ||
51 | static 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)); | |
5772f636 | 58 | struct arvo_mode_key temp_buf; |
e68cc603 SA |
59 | unsigned long state; |
60 | int retval; | |
61 | ||
e68cc603 SA |
62 | retval = strict_strtoul(buf, 10, &state); |
63 | if (retval) | |
5772f636 | 64 | return retval; |
e68cc603 | 65 | |
5772f636 SA |
66 | temp_buf.command = ARVO_COMMAND_MODE_KEY; |
67 | temp_buf.state = state; | |
e68cc603 SA |
68 | |
69 | mutex_lock(&arvo->arvo_lock); | |
7392d73b | 70 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY, |
5772f636 | 71 | &temp_buf, sizeof(struct arvo_mode_key)); |
e68cc603 SA |
72 | mutex_unlock(&arvo->arvo_lock); |
73 | if (retval) | |
5772f636 | 74 | return retval; |
e68cc603 | 75 | |
5772f636 | 76 | return size; |
e68cc603 | 77 | } |
46a58c44 GKH |
78 | static DEVICE_ATTR(mode_key, 0660, |
79 | arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key); | |
e68cc603 SA |
80 | |
81 | static 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)); | |
5772f636 | 88 | struct arvo_key_mask temp_buf; |
e68cc603 SA |
89 | int retval; |
90 | ||
e68cc603 | 91 | mutex_lock(&arvo->arvo_lock); |
7392d73b | 92 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK, |
5772f636 | 93 | &temp_buf, sizeof(struct arvo_key_mask)); |
e68cc603 SA |
94 | mutex_unlock(&arvo->arvo_lock); |
95 | if (retval) | |
5772f636 | 96 | return retval; |
e68cc603 | 97 | |
5772f636 | 98 | return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask); |
e68cc603 SA |
99 | } |
100 | ||
101 | static 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)); | |
5772f636 | 108 | struct arvo_key_mask temp_buf; |
e68cc603 SA |
109 | unsigned long key_mask; |
110 | int retval; | |
111 | ||
e68cc603 SA |
112 | retval = strict_strtoul(buf, 10, &key_mask); |
113 | if (retval) | |
5772f636 | 114 | return retval; |
e68cc603 | 115 | |
5772f636 SA |
116 | temp_buf.command = ARVO_COMMAND_KEY_MASK; |
117 | temp_buf.key_mask = key_mask; | |
e68cc603 SA |
118 | |
119 | mutex_lock(&arvo->arvo_lock); | |
7392d73b | 120 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK, |
5772f636 | 121 | &temp_buf, sizeof(struct arvo_key_mask)); |
e68cc603 SA |
122 | mutex_unlock(&arvo->arvo_lock); |
123 | if (retval) | |
5772f636 | 124 | return retval; |
e68cc603 | 125 | |
5772f636 | 126 | return size; |
e68cc603 | 127 | } |
46a58c44 GKH |
128 | static DEVICE_ATTR(key_mask, 0660, |
129 | arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask); | |
e68cc603 SA |
130 | |
131 | /* retval is 1-5 on success, < 0 on error */ | |
132 | static int arvo_get_actual_profile(struct usb_device *usb_dev) | |
133 | { | |
5772f636 | 134 | struct arvo_actual_profile temp_buf; |
e68cc603 SA |
135 | int retval; |
136 | ||
7392d73b | 137 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
5772f636 | 138 | &temp_buf, sizeof(struct arvo_actual_profile)); |
e68cc603 | 139 | |
5772f636 SA |
140 | if (retval) |
141 | return retval; | |
e68cc603 | 142 | |
5772f636 | 143 | return temp_buf.actual_profile; |
e68cc603 SA |
144 | } |
145 | ||
146 | static 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 | ||
155 | static 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)); | |
5772f636 | 162 | struct arvo_actual_profile temp_buf; |
e68cc603 SA |
163 | unsigned long profile; |
164 | int retval; | |
165 | ||
e68cc603 SA |
166 | retval = strict_strtoul(buf, 10, &profile); |
167 | if (retval) | |
5772f636 | 168 | return retval; |
e68cc603 | 169 | |
901e64db SA |
170 | if (profile < 1 || profile > 5) |
171 | return -EINVAL; | |
172 | ||
5772f636 SA |
173 | temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE; |
174 | temp_buf.actual_profile = profile; | |
e68cc603 SA |
175 | |
176 | mutex_lock(&arvo->arvo_lock); | |
7392d73b | 177 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
5772f636 | 178 | &temp_buf, sizeof(struct arvo_actual_profile)); |
e68cc603 SA |
179 | if (!retval) { |
180 | arvo->actual_profile = profile; | |
181 | retval = size; | |
182 | } | |
183 | mutex_unlock(&arvo->arvo_lock); | |
e68cc603 SA |
184 | return retval; |
185 | } | |
46a58c44 GKH |
186 | static DEVICE_ATTR(actual_profile, 0660, |
187 | arvo_sysfs_show_actual_profile, | |
188 | arvo_sysfs_set_actual_profile); | |
e68cc603 SA |
189 | |
190 | static 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); | |
7392d73b | 204 | retval = roccat_common2_send(usb_dev, command, buf, real_size); |
e68cc603 SA |
205 | mutex_unlock(&arvo->arvo_lock); |
206 | ||
207 | return (retval ? retval : real_size); | |
208 | } | |
209 | ||
210 | static 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); | |
7392d73b | 227 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
e68cc603 SA |
228 | mutex_unlock(&arvo->arvo_lock); |
229 | ||
230 | return (retval ? retval : real_size); | |
231 | } | |
232 | ||
233 | static 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, | |
1edd5b42 | 238 | sizeof(struct arvo_button), ARVO_COMMAND_BUTTON); |
e68cc603 SA |
239 | } |
240 | ||
241 | static ssize_t arvo_sysfs_read_info(struct file *fp, | |
242 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
243 | loff_t off, size_t count) | |
244 | { | |
245 | return arvo_sysfs_read(fp, kobj, buf, off, count, | |
1edd5b42 | 246 | sizeof(struct arvo_info), ARVO_COMMAND_INFO); |
e68cc603 SA |
247 | } |
248 | ||
46a58c44 GKH |
249 | static struct attribute *arvo_attrs[] = { |
250 | &dev_attr_mode_key.attr, | |
251 | &dev_attr_key_mask.attr, | |
252 | &dev_attr_actual_profile.attr, | |
253 | NULL, | |
e68cc603 | 254 | }; |
46a58c44 | 255 | ATTRIBUTE_GROUPS(arvo); |
e68cc603 SA |
256 | |
257 | static struct bin_attribute arvo_bin_attributes[] = { | |
258 | { | |
259 | .attr = { .name = "button", .mode = 0220 }, | |
260 | .size = sizeof(struct arvo_button), | |
261 | .write = arvo_sysfs_write_button | |
262 | }, | |
263 | { | |
264 | .attr = { .name = "info", .mode = 0440 }, | |
265 | .size = sizeof(struct arvo_info), | |
266 | .read = arvo_sysfs_read_info | |
267 | }, | |
268 | __ATTR_NULL | |
269 | }; | |
270 | ||
271 | static int arvo_init_arvo_device_struct(struct usb_device *usb_dev, | |
272 | struct arvo_device *arvo) | |
273 | { | |
274 | int retval; | |
275 | ||
276 | mutex_init(&arvo->arvo_lock); | |
277 | ||
278 | retval = arvo_get_actual_profile(usb_dev); | |
279 | if (retval < 0) | |
280 | return retval; | |
281 | arvo->actual_profile = retval; | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
286 | static int arvo_init_specials(struct hid_device *hdev) | |
287 | { | |
288 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
289 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
290 | struct arvo_device *arvo; | |
291 | int retval; | |
292 | ||
293 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
294 | == USB_INTERFACE_PROTOCOL_KEYBOARD) { | |
295 | hid_set_drvdata(hdev, NULL); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | arvo = kzalloc(sizeof(*arvo), GFP_KERNEL); | |
300 | if (!arvo) { | |
a28764ef | 301 | hid_err(hdev, "can't alloc device descriptor\n"); |
e68cc603 SA |
302 | return -ENOMEM; |
303 | } | |
304 | hid_set_drvdata(hdev, arvo); | |
305 | ||
306 | retval = arvo_init_arvo_device_struct(usb_dev, arvo); | |
307 | if (retval) { | |
a28764ef | 308 | hid_err(hdev, "couldn't init struct arvo_device\n"); |
e68cc603 SA |
309 | goto exit_free; |
310 | } | |
311 | ||
8211e460 SA |
312 | retval = roccat_connect(arvo_class, hdev, |
313 | sizeof(struct arvo_roccat_report)); | |
e68cc603 | 314 | if (retval < 0) { |
a28764ef | 315 | hid_err(hdev, "couldn't init char dev\n"); |
e68cc603 SA |
316 | } else { |
317 | arvo->chrdev_minor = retval; | |
318 | arvo->roccat_claimed = 1; | |
319 | } | |
320 | ||
321 | return 0; | |
322 | exit_free: | |
323 | kfree(arvo); | |
324 | return retval; | |
325 | } | |
326 | ||
327 | static void arvo_remove_specials(struct hid_device *hdev) | |
328 | { | |
329 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
330 | struct arvo_device *arvo; | |
331 | ||
332 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
333 | == USB_INTERFACE_PROTOCOL_KEYBOARD) | |
334 | return; | |
335 | ||
336 | arvo = hid_get_drvdata(hdev); | |
337 | if (arvo->roccat_claimed) | |
338 | roccat_disconnect(arvo->chrdev_minor); | |
339 | kfree(arvo); | |
340 | } | |
341 | ||
342 | static int arvo_probe(struct hid_device *hdev, | |
343 | const struct hid_device_id *id) | |
344 | { | |
345 | int retval; | |
346 | ||
347 | retval = hid_parse(hdev); | |
348 | if (retval) { | |
a28764ef | 349 | hid_err(hdev, "parse failed\n"); |
e68cc603 SA |
350 | goto exit; |
351 | } | |
352 | ||
353 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
354 | if (retval) { | |
a28764ef | 355 | hid_err(hdev, "hw start failed\n"); |
e68cc603 SA |
356 | goto exit; |
357 | } | |
358 | ||
359 | retval = arvo_init_specials(hdev); | |
360 | if (retval) { | |
a28764ef | 361 | hid_err(hdev, "couldn't install keyboard\n"); |
e68cc603 SA |
362 | goto exit_stop; |
363 | } | |
364 | ||
365 | return 0; | |
366 | ||
367 | exit_stop: | |
368 | hid_hw_stop(hdev); | |
369 | exit: | |
370 | return retval; | |
371 | } | |
372 | ||
373 | static void arvo_remove(struct hid_device *hdev) | |
374 | { | |
375 | arvo_remove_specials(hdev); | |
376 | hid_hw_stop(hdev); | |
377 | } | |
378 | ||
379 | static void arvo_report_to_chrdev(struct arvo_device const *arvo, | |
380 | u8 const *data) | |
381 | { | |
382 | struct arvo_special_report const *special_report; | |
383 | struct arvo_roccat_report roccat_report; | |
384 | ||
385 | special_report = (struct arvo_special_report const *)data; | |
386 | ||
387 | roccat_report.profile = arvo->actual_profile; | |
388 | roccat_report.button = special_report->event & | |
389 | ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON; | |
390 | if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) == | |
391 | ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS) | |
392 | roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS; | |
393 | else | |
394 | roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE; | |
395 | ||
8211e460 SA |
396 | roccat_report_event(arvo->chrdev_minor, |
397 | (uint8_t const *)&roccat_report); | |
e68cc603 SA |
398 | } |
399 | ||
400 | static int arvo_raw_event(struct hid_device *hdev, | |
401 | struct hid_report *report, u8 *data, int size) | |
402 | { | |
403 | struct arvo_device *arvo = hid_get_drvdata(hdev); | |
404 | ||
405 | if (size != 3) | |
406 | return 0; | |
407 | ||
901e64db | 408 | if (arvo && arvo->roccat_claimed) |
e68cc603 SA |
409 | arvo_report_to_chrdev(arvo, data); |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | static const struct hid_device_id arvo_devices[] = { | |
415 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) }, | |
416 | { } | |
417 | }; | |
418 | ||
419 | MODULE_DEVICE_TABLE(hid, arvo_devices); | |
420 | ||
421 | static struct hid_driver arvo_driver = { | |
422 | .name = "arvo", | |
423 | .id_table = arvo_devices, | |
424 | .probe = arvo_probe, | |
425 | .remove = arvo_remove, | |
426 | .raw_event = arvo_raw_event | |
427 | }; | |
428 | ||
429 | static int __init arvo_init(void) | |
430 | { | |
431 | int retval; | |
432 | ||
433 | arvo_class = class_create(THIS_MODULE, "arvo"); | |
434 | if (IS_ERR(arvo_class)) | |
435 | return PTR_ERR(arvo_class); | |
46a58c44 | 436 | arvo_class->dev_groups = arvo_groups; |
e68cc603 SA |
437 | arvo_class->dev_bin_attrs = arvo_bin_attributes; |
438 | ||
439 | retval = hid_register_driver(&arvo_driver); | |
440 | if (retval) | |
441 | class_destroy(arvo_class); | |
442 | return retval; | |
443 | } | |
444 | ||
445 | static void __exit arvo_exit(void) | |
446 | { | |
e68cc603 | 447 | hid_unregister_driver(&arvo_driver); |
74b643da | 448 | class_destroy(arvo_class); |
e68cc603 SA |
449 | } |
450 | ||
451 | module_init(arvo_init); | |
452 | module_exit(arvo_exit); | |
453 | ||
454 | MODULE_AUTHOR("Stefan Achatz"); | |
455 | MODULE_DESCRIPTION("USB Roccat Arvo driver"); | |
456 | MODULE_LICENSE("GPL v2"); |