hid-roccat-arvo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_common_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 = strict_strtoul(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_common_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 ssize_t arvo_sysfs_show_key_mask(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct arvo_device *arvo =
  70. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  71. struct usb_device *usb_dev =
  72. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  73. struct arvo_key_mask temp_buf;
  74. int retval;
  75. mutex_lock(&arvo->arvo_lock);
  76. retval = roccat_common_receive(usb_dev, ARVO_COMMAND_KEY_MASK,
  77. &temp_buf, sizeof(struct arvo_key_mask));
  78. mutex_unlock(&arvo->arvo_lock);
  79. if (retval)
  80. return retval;
  81. return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask);
  82. }
  83. static ssize_t arvo_sysfs_set_key_mask(struct device *dev,
  84. struct device_attribute *attr, char const *buf, size_t size)
  85. {
  86. struct arvo_device *arvo =
  87. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  88. struct usb_device *usb_dev =
  89. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  90. struct arvo_key_mask temp_buf;
  91. unsigned long key_mask;
  92. int retval;
  93. retval = strict_strtoul(buf, 10, &key_mask);
  94. if (retval)
  95. return retval;
  96. temp_buf.command = ARVO_COMMAND_KEY_MASK;
  97. temp_buf.key_mask = key_mask;
  98. mutex_lock(&arvo->arvo_lock);
  99. retval = roccat_common_send(usb_dev, ARVO_COMMAND_KEY_MASK,
  100. &temp_buf, sizeof(struct arvo_key_mask));
  101. mutex_unlock(&arvo->arvo_lock);
  102. if (retval)
  103. return retval;
  104. return size;
  105. }
  106. /* retval is 1-5 on success, < 0 on error */
  107. static int arvo_get_actual_profile(struct usb_device *usb_dev)
  108. {
  109. struct arvo_actual_profile temp_buf;
  110. int retval;
  111. retval = roccat_common_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
  112. &temp_buf, sizeof(struct arvo_actual_profile));
  113. if (retval)
  114. return retval;
  115. return temp_buf.actual_profile;
  116. }
  117. static ssize_t arvo_sysfs_show_actual_profile(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct arvo_device *arvo =
  121. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  122. return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile);
  123. }
  124. static ssize_t arvo_sysfs_set_actual_profile(struct device *dev,
  125. struct device_attribute *attr, char const *buf, size_t size)
  126. {
  127. struct arvo_device *arvo =
  128. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  129. struct usb_device *usb_dev =
  130. interface_to_usbdev(to_usb_interface(dev->parent->parent));
  131. struct arvo_actual_profile temp_buf;
  132. unsigned long profile;
  133. int retval;
  134. retval = strict_strtoul(buf, 10, &profile);
  135. if (retval)
  136. return retval;
  137. if (profile < 1 || profile > 5)
  138. return -EINVAL;
  139. temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE;
  140. temp_buf.actual_profile = profile;
  141. mutex_lock(&arvo->arvo_lock);
  142. retval = roccat_common_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE,
  143. &temp_buf, sizeof(struct arvo_actual_profile));
  144. if (!retval) {
  145. arvo->actual_profile = profile;
  146. retval = size;
  147. }
  148. mutex_unlock(&arvo->arvo_lock);
  149. return retval;
  150. }
  151. static ssize_t arvo_sysfs_write(struct file *fp,
  152. struct kobject *kobj, void const *buf,
  153. loff_t off, size_t count, size_t real_size, uint command)
  154. {
  155. struct device *dev =
  156. container_of(kobj, struct device, kobj)->parent->parent;
  157. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  158. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  159. int retval;
  160. if (off != 0 || count != real_size)
  161. return -EINVAL;
  162. mutex_lock(&arvo->arvo_lock);
  163. retval = roccat_common_send(usb_dev, command, buf, real_size);
  164. mutex_unlock(&arvo->arvo_lock);
  165. return (retval ? retval : real_size);
  166. }
  167. static ssize_t arvo_sysfs_read(struct file *fp,
  168. struct kobject *kobj, void *buf, loff_t off,
  169. size_t count, size_t real_size, uint command)
  170. {
  171. struct device *dev =
  172. container_of(kobj, struct device, kobj)->parent->parent;
  173. struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev));
  174. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  175. int retval;
  176. if (off >= real_size)
  177. return 0;
  178. if (off != 0 || count != real_size)
  179. return -EINVAL;
  180. mutex_lock(&arvo->arvo_lock);
  181. retval = roccat_common_receive(usb_dev, command, buf, real_size);
  182. mutex_unlock(&arvo->arvo_lock);
  183. return (retval ? retval : real_size);
  184. }
  185. static ssize_t arvo_sysfs_write_button(struct file *fp,
  186. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  187. loff_t off, size_t count)
  188. {
  189. return arvo_sysfs_write(fp, kobj, buf, off, count,
  190. sizeof(struct arvo_button), ARVO_COMMAND_BUTTON);
  191. }
  192. static ssize_t arvo_sysfs_read_info(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_read(fp, kobj, buf, off, count,
  197. sizeof(struct arvo_info), ARVO_COMMAND_INFO);
  198. }
  199. static struct device_attribute arvo_attributes[] = {
  200. __ATTR(mode_key, 0660,
  201. arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key),
  202. __ATTR(key_mask, 0660,
  203. arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask),
  204. __ATTR(actual_profile, 0660,
  205. arvo_sysfs_show_actual_profile,
  206. arvo_sysfs_set_actual_profile),
  207. __ATTR_NULL
  208. };
  209. static struct bin_attribute arvo_bin_attributes[] = {
  210. {
  211. .attr = { .name = "button", .mode = 0220 },
  212. .size = sizeof(struct arvo_button),
  213. .write = arvo_sysfs_write_button
  214. },
  215. {
  216. .attr = { .name = "info", .mode = 0440 },
  217. .size = sizeof(struct arvo_info),
  218. .read = arvo_sysfs_read_info
  219. },
  220. __ATTR_NULL
  221. };
  222. static int arvo_init_arvo_device_struct(struct usb_device *usb_dev,
  223. struct arvo_device *arvo)
  224. {
  225. int retval;
  226. mutex_init(&arvo->arvo_lock);
  227. retval = arvo_get_actual_profile(usb_dev);
  228. if (retval < 0)
  229. return retval;
  230. arvo->actual_profile = retval;
  231. return 0;
  232. }
  233. static int arvo_init_specials(struct hid_device *hdev)
  234. {
  235. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  236. struct usb_device *usb_dev = interface_to_usbdev(intf);
  237. struct arvo_device *arvo;
  238. int retval;
  239. if (intf->cur_altsetting->desc.bInterfaceProtocol
  240. == USB_INTERFACE_PROTOCOL_KEYBOARD) {
  241. hid_set_drvdata(hdev, NULL);
  242. return 0;
  243. }
  244. arvo = kzalloc(sizeof(*arvo), GFP_KERNEL);
  245. if (!arvo) {
  246. hid_err(hdev, "can't alloc device descriptor\n");
  247. return -ENOMEM;
  248. }
  249. hid_set_drvdata(hdev, arvo);
  250. retval = arvo_init_arvo_device_struct(usb_dev, arvo);
  251. if (retval) {
  252. hid_err(hdev, "couldn't init struct arvo_device\n");
  253. goto exit_free;
  254. }
  255. retval = roccat_connect(arvo_class, hdev,
  256. sizeof(struct arvo_roccat_report));
  257. if (retval < 0) {
  258. hid_err(hdev, "couldn't init char dev\n");
  259. } else {
  260. arvo->chrdev_minor = retval;
  261. arvo->roccat_claimed = 1;
  262. }
  263. return 0;
  264. exit_free:
  265. kfree(arvo);
  266. return retval;
  267. }
  268. static void arvo_remove_specials(struct hid_device *hdev)
  269. {
  270. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  271. struct arvo_device *arvo;
  272. if (intf->cur_altsetting->desc.bInterfaceProtocol
  273. == USB_INTERFACE_PROTOCOL_KEYBOARD)
  274. return;
  275. arvo = hid_get_drvdata(hdev);
  276. if (arvo->roccat_claimed)
  277. roccat_disconnect(arvo->chrdev_minor);
  278. kfree(arvo);
  279. }
  280. static int arvo_probe(struct hid_device *hdev,
  281. const struct hid_device_id *id)
  282. {
  283. int retval;
  284. retval = hid_parse(hdev);
  285. if (retval) {
  286. hid_err(hdev, "parse failed\n");
  287. goto exit;
  288. }
  289. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  290. if (retval) {
  291. hid_err(hdev, "hw start failed\n");
  292. goto exit;
  293. }
  294. retval = arvo_init_specials(hdev);
  295. if (retval) {
  296. hid_err(hdev, "couldn't install keyboard\n");
  297. goto exit_stop;
  298. }
  299. return 0;
  300. exit_stop:
  301. hid_hw_stop(hdev);
  302. exit:
  303. return retval;
  304. }
  305. static void arvo_remove(struct hid_device *hdev)
  306. {
  307. arvo_remove_specials(hdev);
  308. hid_hw_stop(hdev);
  309. }
  310. static void arvo_report_to_chrdev(struct arvo_device const *arvo,
  311. u8 const *data)
  312. {
  313. struct arvo_special_report const *special_report;
  314. struct arvo_roccat_report roccat_report;
  315. special_report = (struct arvo_special_report const *)data;
  316. roccat_report.profile = arvo->actual_profile;
  317. roccat_report.button = special_report->event &
  318. ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON;
  319. if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) ==
  320. ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS)
  321. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS;
  322. else
  323. roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE;
  324. roccat_report_event(arvo->chrdev_minor,
  325. (uint8_t const *)&roccat_report);
  326. }
  327. static int arvo_raw_event(struct hid_device *hdev,
  328. struct hid_report *report, u8 *data, int size)
  329. {
  330. struct arvo_device *arvo = hid_get_drvdata(hdev);
  331. if (size != 3)
  332. return 0;
  333. if (arvo && arvo->roccat_claimed)
  334. arvo_report_to_chrdev(arvo, data);
  335. return 0;
  336. }
  337. static const struct hid_device_id arvo_devices[] = {
  338. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) },
  339. { }
  340. };
  341. MODULE_DEVICE_TABLE(hid, arvo_devices);
  342. static struct hid_driver arvo_driver = {
  343. .name = "arvo",
  344. .id_table = arvo_devices,
  345. .probe = arvo_probe,
  346. .remove = arvo_remove,
  347. .raw_event = arvo_raw_event
  348. };
  349. static int __init arvo_init(void)
  350. {
  351. int retval;
  352. arvo_class = class_create(THIS_MODULE, "arvo");
  353. if (IS_ERR(arvo_class))
  354. return PTR_ERR(arvo_class);
  355. arvo_class->dev_attrs = arvo_attributes;
  356. arvo_class->dev_bin_attrs = arvo_bin_attributes;
  357. retval = hid_register_driver(&arvo_driver);
  358. if (retval)
  359. class_destroy(arvo_class);
  360. return retval;
  361. }
  362. static void __exit arvo_exit(void)
  363. {
  364. hid_unregister_driver(&arvo_driver);
  365. class_destroy(arvo_class);
  366. }
  367. module_init(arvo_init);
  368. module_exit(arvo_exit);
  369. MODULE_AUTHOR("Stefan Achatz");
  370. MODULE_DESCRIPTION("USB Roccat Arvo driver");
  371. MODULE_LICENSE("GPL v2");