hid-roccat-arvo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Roccat Arvo driver for Linux
  3. *
  4. * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. /*
  13. * Roccat Arvo is a gamer keyboard with 5 macro keys that can be configured in
  14. * 5 profiles.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. #include "hid-roccat-arvo.h"
  25. static struct class *arvo_class;
  26. static ssize_t arvo_sysfs_show_mode_key(struct device *dev,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. struct arvo_device *arvo =
  30. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  31. struct usb_device *usb_dev =
  32. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  33. struct arvo_mode_key temp_buf;
  34. int retval;
  35. mutex_lock(&arvo->arvo_lock);
  36. retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY,
  37. &temp_buf, sizeof(struct arvo_mode_key));
  38. mutex_unlock(&arvo->arvo_lock);
  39. if (retval)
  40. return retval;
  41. return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.state);
  42. }
  43. static ssize_t arvo_sysfs_set_mode_key(struct device *dev,
  44. struct device_attribute *attr, char const *buf, size_t size)
  45. {
  46. struct arvo_device *arvo =
  47. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  48. struct usb_device *usb_dev =
  49. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  50. struct arvo_mode_key temp_buf;
  51. unsigned long state;
  52. int retval;
  53. retval = kstrtoul(buf, 10, &state);
  54. if (retval)
  55. return retval;
  56. temp_buf.command = ARVO_COMMAND_MODE_KEY;
  57. temp_buf.state = state;
  58. mutex_lock(&arvo->arvo_lock);
  59. retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY,
  60. &temp_buf, sizeof(struct arvo_mode_key));
  61. mutex_unlock(&arvo->arvo_lock);
  62. if (retval)
  63. return retval;
  64. return size;
  65. }
  66. static DEVICE_ATTR(mode_key, 0660,
  67. arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key);
  68. static ssize_t arvo_sysfs_show_key_mask(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct arvo_device *arvo =
  72. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  73. struct usb_device *usb_dev =
  74. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  75. struct arvo_key_mask temp_buf;
  76. int retval;
  77. mutex_lock(&arvo->arvo_lock);
  78. retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
  79. &temp_buf, sizeof(struct arvo_key_mask));
  80. mutex_unlock(&arvo->arvo_lock);
  81. if (retval)
  82. return retval;
  83. return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask);
  84. }
  85. static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
  86. struct device_attribute *attr, char const *buf, size_t size)
  87. {
  88. struct arvo_device *arvo =
  89. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  90. struct usb_device *usb_dev =
  91. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  92. struct arvo_key_mask temp_buf;
  93. unsigned long key_mask;
  94. int retval;
  95. retval = kstrtoul(buf, 10, &key_mask);
  96. if (retval)
  97. return retval;
  98. temp_buf.command = ARVO_COMMAND_KEY_MASK;
  99. temp_buf.key_mask = key_mask;
  100. mutex_lock(&arvo->arvo_lock);
  101. retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK,
  102. &temp_buf, sizeof(struct arvo_key_mask));
  103. mutex_unlock(&arvo->arvo_lock);
  104. if (retval)
  105. return retval;
  106. return size;
  107. }
  108. static DEVICE_ATTR(key_mask, 0660,
  109. arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask);
  110. /* retval is 1-5 on success, < 0 on error */
  111. static int arvo_get_actual_profile(struct usb_device *usb_dev)
  112. {
  113. struct arvo_actual_profile temp_buf;
  114. int retval;
  115. retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
  116. &temp_buf, sizeof(struct arvo_actual_profile));
  117. if (retval)
  118. return retval;
  119. return temp_buf.actual_profile;
  120. }
  121. static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct arvo_device *arvo =
  125. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  126. return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
  127. }
  128. static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
  129. struct device_attribute *attr, char const *buf, size_t size)
  130. {
  131. struct arvo_device *arvo =
  132. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  133. struct usb_device *usb_dev =
  134. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  135. struct arvo_actual_profile temp_buf;
  136. unsigned long profile;
  137. int retval;
  138. retval = kstrtoul(buf, 10, &profile);
  139. if (retval)
  140. return retval;
  141. if (profile < 1 || profile > 5)
  142. return -EINVAL;
  143. temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
  144. temp_buf.actual_profile = profile;
  145. mutex_lock(&arvo->arvo_lock);
  146. retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
  147. &temp_buf, sizeof(struct arvo_actual_profile));
  148. if (!retval) {
  149. arvo->actual_profile = profile;
  150. retval = size;
  151. }
  152. mutex_unlock(&arvo->arvo_lock);
  153. return retval;
  154. }
  155. static DEVICE_ATTR(actual_profile, 0660,
  156. arvo_sysfs_show_actual_profile,
  157. arvo_sysfs_set_actual_profile);
  158. static ssize_t arvo_sysfs_write(struct file *fp,
  159. struct kobject *kobj, void const *buf,
  160. loff_t off, size_t count, size_t real_size, uint command)
  161. {
  162. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  163. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  164. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  165. int retval;
  166. if (off != 0 || count != real_size)
  167. return -EINVAL;
  168. mutex_lock(&arvo->arvo_lock);
  169. retval = roccat_common2_send(usb_dev, command, buf, real_size);
  170. mutex_unlock(&arvo->arvo_lock);
  171. return (retval ? retval : real_size);
  172. }
  173. static ssize_t arvo_sysfs_read(struct file *fp,
  174. struct kobject *kobj, void *buf, loff_t off,
  175. size_t count, size_t real_size, uint command)
  176. {
  177. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  178. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  179. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  180. int retval;
  181. if (off >= real_size)
  182. return 0;
  183. if (off != 0 || count != real_size)
  184. return -EINVAL;
  185. mutex_lock(&arvo->arvo_lock);
  186. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  187. mutex_unlock(&arvo->arvo_lock);
  188. return (retval ? retval : real_size);
  189. }
  190. static ssize_t arvo_sysfs_write_button(struct file *fp,
  191. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  192. loff_t off, size_t count)
  193. {
  194. return arvo_sysfs_write(fp, kobj, buf, off, count,
  195. sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
  196. }
  197. static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button,
  198. sizeof(struct arvo_button));
  199. static ssize_t arvo_sysfs_read_info(struct file *fp,
  200. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  201. loff_t off, size_t count)
  202. {
  203. return arvo_sysfs_read(fp, kobj, buf, off, count,
  204. sizeof(struct arvo_info), ARVO_COMMAND_INFO);
  205. }
  206. static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL,
  207. sizeof(struct arvo_info));
  208. static struct attribute *arvo_attrs[] = {
  209. &dev_attr_mode_key.attr,
  210. &dev_attr_key_mask.attr,
  211. &dev_attr_actual_profile.attr,
  212. NULL,
  213. };
  214. static struct bin_attribute *arvo_bin_attributes[] = {
  215. &bin_attr_button,
  216. &bin_attr_info,
  217. NULL,
  218. };
  219. static const struct attribute_group arvo_group = {
  220. .attrs = arvo_attrs,
  221. .bin_attrs = arvo_bin_attributes,
  222. };
  223. static const struct attribute_group *arvo_groups[] = {
  224. &arvo_group,
  225. NULL,
  226. };
  227. static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
  228. struct arvo_device *arvo)
  229. {
  230. int retval;
  231. mutex_init(&arvo->arvo_lock);
  232. retval = arvo_get_actual_profile(usb_dev);
  233. if (retval < 0)
  234. return retval;
  235. arvo->actual_profile = retval;
  236. return 0;
  237. }
  238. static int arvo_init_specials(struct hid_device *hdev)
  239. {
  240. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  241. struct usb_device *usb_dev = interface_to_usbdev(intf);
  242. struct arvo_device *arvo;
  243. int retval;
  244. if (intf->cur_altsetting->desc.bInterfaceProtocol
  245. == USB_INTERFACE_PROTOCOL_KEYBOARD) {
  246. hid_set_drvdata(hdev, NULL);
  247. return 0;
  248. }
  249. arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
  250. if (!arvo) {
  251. hid_err(hdev, "can't alloc device descriptor\n");
  252. return -ENOMEM;
  253. }
  254. hid_set_drvdata(hdev, arvo);
  255. retval = arvo_init_arvo_device_struct(usb_dev, arvo);
  256. if (retval) {
  257. hid_err(hdev, "couldn't init struct arvo_device\n");
  258. goto exit_free;
  259. }
  260. retval = roccat_connect(arvo_class, hdev,
  261. sizeof(struct arvo_roccat_report));
  262. if (retval < 0) {
  263. hid_err(hdev, "couldn't init char dev\n");
  264. } else {
  265. arvo->chrdev_minor = retval;
  266. arvo->roccat_claimed = 1;
  267. }
  268. return 0;
  269. exit_free:
  270. kfree(arvo);
  271. return retval;
  272. }
  273. static void arvo_remove_specials(struct hid_device *hdev)
  274. {
  275. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  276. struct arvo_device *arvo;
  277. if (intf->cur_altsetting->desc.bInterfaceProtocol
  278. == USB_INTERFACE_PROTOCOL_KEYBOARD)
  279. return;
  280. arvo = hid_get_drvdata(hdev);
  281. if (arvo->roccat_claimed)
  282. roccat_disconnect(arvo->chrdev_minor);
  283. kfree(arvo);
  284. }
  285. static int arvo_probe(struct hid_device *hdev,
  286. const struct hid_device_id *id)
  287. {
  288. int retval;
  289. retval = hid_parse(hdev);
  290. if (retval) {
  291. hid_err(hdev, "parse failed\n");
  292. goto exit;
  293. }
  294. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  295. if (retval) {
  296. hid_err(hdev, "hw start failed\n");
  297. goto exit;
  298. }
  299. retval = arvo_init_specials(hdev);
  300. if (retval) {
  301. hid_err(hdev, "couldn't install keyboard\n");
  302. goto exit_stop;
  303. }
  304. return 0;
  305. exit_stop:
  306. hid_hw_stop(hdev);
  307. exit:
  308. return retval;
  309. }
  310. static void arvo_remove(struct hid_device *hdev)
  311. {
  312. arvo_remove_specials(hdev);
  313. hid_hw_stop(hdev);
  314. }
  315. static void arvo_report_to_chrdev(struct arvo_device const *arvo,
  316. u8 const *data)
  317. {
  318. struct arvo_special_report const *special_report;
  319. struct arvo_roccat_report roccat_report;
  320. special_report = (struct arvo_special_report const *)data;
  321. roccat_report.profile = arvo->actual_profile;
  322. roccat_report.button = special_report->event &
  323. ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
  324. if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
  325. ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
  326. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
  327. else
  328. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
  329. roccat_report_event(arvo->chrdev_minor,
  330. (uint8_t const *)&roccat_report);
  331. }
  332. static int arvo_raw_event(struct hid_device *hdev,
  333. struct hid_report *report, u8 *data, int size)
  334. {
  335. struct arvo_device *arvo = hid_get_drvdata(hdev);
  336. if (size != 3)
  337. return 0;
  338. if (arvo && arvo->roccat_claimed)
  339. arvo_report_to_chrdev(arvo, data);
  340. return 0;
  341. }
  342. static const struct hid_device_id arvo_devices[] = {
  343. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(hid, arvo_devices);
  347. static struct hid_driver arvo_driver = {
  348. .name = "arvo",
  349. .id_table = arvo_devices,
  350. .probe = arvo_probe,
  351. .remove = arvo_remove,
  352. .raw_event = arvo_raw_event
  353. };
  354. static int __init arvo_init(void)
  355. {
  356. int retval;
  357. arvo_class = class_create(THIS_MODULE, "arvo");
  358. if (IS_ERR(arvo_class))
  359. return PTR_ERR(arvo_class);
  360. arvo_class->dev_groups = arvo_groups;
  361. retval = hid_register_driver(&arvo_driver);
  362. if (retval)
  363. class_destroy(arvo_class);
  364. return retval;
  365. }
  366. static void __exit arvo_exit(void)
  367. {
  368. hid_unregister_driver(&arvo_driver);
  369. class_destroy(arvo_class);
  370. }
  371. module_init(arvo_init);
  372. module_exit(arvo_exit);
  373. MODULE_AUTHOR("Stefan Achatz");
  374. MODULE_DESCRIPTION("USB Roccat Arvo driver");
  375. MODULE_LICENSE("GPL v2");