hid-roccat-arvo.c 11 KB

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