hid-roccat-arvo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 =
  163. container_of(kobj, struct device, kobj)->parent->parent;
  164. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  165. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  166. int retval;
  167. if (off != 0 || count != real_size)
  168. return -EINVAL;
  169. mutex_lock(&arvo->arvo_lock);
  170. retval = roccat_common2_send(usb_dev, command, buf, real_size);
  171. mutex_unlock(&arvo->arvo_lock);
  172. return (retval ? retval : real_size);
  173. }
  174. static ssize_t arvo_sysfs_read(struct file *fp,
  175. struct kobject *kobj, void *buf, loff_t off,
  176. size_t count, size_t real_size, uint command)
  177. {
  178. struct device *dev =
  179. container_of(kobj, struct device, kobj)->parent->parent;
  180. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  181. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  182. int retval;
  183. if (off >= real_size)
  184. return 0;
  185. if (off != 0 || count != real_size)
  186. return -EINVAL;
  187. mutex_lock(&arvo->arvo_lock);
  188. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  189. mutex_unlock(&arvo->arvo_lock);
  190. return (retval ? retval : real_size);
  191. }
  192. static ssize_t arvo_sysfs_write_button(struct file *fp,
  193. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  194. loff_t off, size_t count)
  195. {
  196. return arvo_sysfs_write(fp, kobj, buf, off, count,
  197. sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
  198. }
  199. static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button,
  200. sizeof(struct arvo_button));
  201. static ssize_t arvo_sysfs_read_info(struct file *fp,
  202. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  203. loff_t off, size_t count)
  204. {
  205. return arvo_sysfs_read(fp, kobj, buf, off, count,
  206. sizeof(struct arvo_info), ARVO_COMMAND_INFO);
  207. }
  208. static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL,
  209. sizeof(struct arvo_info));
  210. static struct attribute *arvo_attrs[] = {
  211. &dev_attr_mode_key.attr,
  212. &dev_attr_key_mask.attr,
  213. &dev_attr_actual_profile.attr,
  214. NULL,
  215. };
  216. static struct bin_attribute *arvo_bin_attributes[] = {
  217. &bin_attr_button,
  218. &bin_attr_info,
  219. NULL,
  220. };
  221. static const struct attribute_group arvo_group = {
  222. .attrs = arvo_attrs,
  223. .bin_attrs = arvo_bin_attributes,
  224. };
  225. static const struct attribute_group *arvo_groups[] = {
  226. &arvo_group,
  227. NULL,
  228. };
  229. static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
  230. struct arvo_device *arvo)
  231. {
  232. int retval;
  233. mutex_init(&arvo->arvo_lock);
  234. retval = arvo_get_actual_profile(usb_dev);
  235. if (retval < 0)
  236. return retval;
  237. arvo->actual_profile = retval;
  238. return 0;
  239. }
  240. static int arvo_init_specials(struct hid_device *hdev)
  241. {
  242. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  243. struct usb_device *usb_dev = interface_to_usbdev(intf);
  244. struct arvo_device *arvo;
  245. int retval;
  246. if (intf->cur_altsetting->desc.bInterfaceProtocol
  247. == USB_INTERFACE_PROTOCOL_KEYBOARD) {
  248. hid_set_drvdata(hdev, NULL);
  249. return 0;
  250. }
  251. arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
  252. if (!arvo) {
  253. hid_err(hdev, "can't alloc device descriptor\n");
  254. return -ENOMEM;
  255. }
  256. hid_set_drvdata(hdev, arvo);
  257. retval = arvo_init_arvo_device_struct(usb_dev, arvo);
  258. if (retval) {
  259. hid_err(hdev, "couldn't init struct arvo_device\n");
  260. goto exit_free;
  261. }
  262. retval = roccat_connect(arvo_class, hdev,
  263. sizeof(struct arvo_roccat_report));
  264. if (retval < 0) {
  265. hid_err(hdev, "couldn't init char dev\n");
  266. } else {
  267. arvo->chrdev_minor = retval;
  268. arvo->roccat_claimed = 1;
  269. }
  270. return 0;
  271. exit_free:
  272. kfree(arvo);
  273. return retval;
  274. }
  275. static void arvo_remove_specials(struct hid_device *hdev)
  276. {
  277. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  278. struct arvo_device *arvo;
  279. if (intf->cur_altsetting->desc.bInterfaceProtocol
  280. == USB_INTERFACE_PROTOCOL_KEYBOARD)
  281. return;
  282. arvo = hid_get_drvdata(hdev);
  283. if (arvo->roccat_claimed)
  284. roccat_disconnect(arvo->chrdev_minor);
  285. kfree(arvo);
  286. }
  287. static int arvo_probe(struct hid_device *hdev,
  288. const struct hid_device_id *id)
  289. {
  290. int retval;
  291. retval = hid_parse(hdev);
  292. if (retval) {
  293. hid_err(hdev, "parse failed\n");
  294. goto exit;
  295. }
  296. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  297. if (retval) {
  298. hid_err(hdev, "hw start failed\n");
  299. goto exit;
  300. }
  301. retval = arvo_init_specials(hdev);
  302. if (retval) {
  303. hid_err(hdev, "couldn't install keyboard\n");
  304. goto exit_stop;
  305. }
  306. return 0;
  307. exit_stop:
  308. hid_hw_stop(hdev);
  309. exit:
  310. return retval;
  311. }
  312. static void arvo_remove(struct hid_device *hdev)
  313. {
  314. arvo_remove_specials(hdev);
  315. hid_hw_stop(hdev);
  316. }
  317. static void arvo_report_to_chrdev(struct arvo_device const *arvo,
  318. u8 const *data)
  319. {
  320. struct arvo_special_report const *special_report;
  321. struct arvo_roccat_report roccat_report;
  322. special_report = (struct arvo_special_report const *)data;
  323. roccat_report.profile = arvo->actual_profile;
  324. roccat_report.button = special_report->event &
  325. ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
  326. if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
  327. ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
  328. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
  329. else
  330. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
  331. roccat_report_event(arvo->chrdev_minor,
  332. (uint8_t const *)&roccat_report);
  333. }
  334. static int arvo_raw_event(struct hid_device *hdev,
  335. struct hid_report *report, u8 *data, int size)
  336. {
  337. struct arvo_device *arvo = hid_get_drvdata(hdev);
  338. if (size != 3)
  339. return 0;
  340. if (arvo && arvo->roccat_claimed)
  341. arvo_report_to_chrdev(arvo, data);
  342. return 0;
  343. }
  344. static const struct hid_device_id arvo_devices[] = {
  345. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
  346. { }
  347. };
  348. MODULE_DEVICE_TABLE(hid, arvo_devices);
  349. static struct hid_driver arvo_driver = {
  350. .name = "arvo",
  351. .id_table = arvo_devices,
  352. .probe = arvo_probe,
  353. .remove = arvo_remove,
  354. .raw_event = arvo_raw_event
  355. };
  356. static int __init arvo_init(void)
  357. {
  358. int retval;
  359. arvo_class = class_create(THIS_MODULE, "arvo");
  360. if (IS_ERR(arvo_class))
  361. return PTR_ERR(arvo_class);
  362. arvo_class->dev_groups = arvo_groups;
  363. retval = hid_register_driver(&arvo_driver);
  364. if (retval)
  365. class_destroy(arvo_class);
  366. return retval;
  367. }
  368. static void __exit arvo_exit(void)
  369. {
  370. hid_unregister_driver(&arvo_driver);
  371. class_destroy(arvo_class);
  372. }
  373. module_init(arvo_init);
  374. module_exit(arvo_exit);
  375. MODULE_AUTHOR("Stefan Achatz");
  376. MODULE_DESCRIPTION("USB Roccat Arvo driver");
  377. MODULE_LICENSE("GPL v2");