hid-roccat-savu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Roccat Savu driver for Linux
  3. *
  4. * Copyright (c) 2012 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. /* Roccat Savu is a gamer mouse with macro keys that can be configured in
  13. * 5 profiles.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/input.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/hid-roccat.h>
  21. #include "hid-ids.h"
  22. #include "hid-roccat-common.h"
  23. #include "hid-roccat-savu.h"
  24. static struct class *savu_class;
  25. static ssize_t savu_sysfs_read(struct file *fp, struct kobject *kobj,
  26. char *buf, loff_t off, size_t count,
  27. size_t real_size, uint command)
  28. {
  29. struct device *dev =
  30. container_of(kobj, struct device, kobj)->parent->parent;
  31. struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
  32. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  33. int retval;
  34. if (off >= real_size)
  35. return 0;
  36. if (off != 0 || count != real_size)
  37. return -EINVAL;
  38. mutex_lock(&savu->savu_lock);
  39. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  40. mutex_unlock(&savu->savu_lock);
  41. return retval ? retval : real_size;
  42. }
  43. static ssize_t savu_sysfs_write(struct file *fp, struct kobject *kobj,
  44. void const *buf, loff_t off, size_t count,
  45. size_t real_size, uint command)
  46. {
  47. struct device *dev =
  48. container_of(kobj, struct device, kobj)->parent->parent;
  49. struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
  50. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  51. int retval;
  52. if (off != 0 || count != real_size)
  53. return -EINVAL;
  54. mutex_lock(&savu->savu_lock);
  55. retval = roccat_common2_send_with_status(usb_dev, command,
  56. (void *)buf, real_size);
  57. mutex_unlock(&savu->savu_lock);
  58. return retval ? retval : real_size;
  59. }
  60. #define SAVU_SYSFS_W(thingy, THINGY) \
  61. static ssize_t savu_sysfs_write_ ## thingy(struct file *fp, \
  62. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  63. loff_t off, size_t count) \
  64. { \
  65. return savu_sysfs_write(fp, kobj, buf, off, count, \
  66. SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
  67. }
  68. #define SAVU_SYSFS_R(thingy, THINGY) \
  69. static ssize_t savu_sysfs_read_ ## thingy(struct file *fp, \
  70. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  71. loff_t off, size_t count) \
  72. { \
  73. return savu_sysfs_read(fp, kobj, buf, off, count, \
  74. SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
  75. }
  76. #define SAVU_SYSFS_RW(thingy, THINGY) \
  77. SAVU_SYSFS_W(thingy, THINGY) \
  78. SAVU_SYSFS_R(thingy, THINGY)
  79. #define SAVU_BIN_ATTRIBUTE_RW(thingy, THINGY) \
  80. { \
  81. .attr = { .name = #thingy, .mode = 0660 }, \
  82. .size = SAVU_SIZE_ ## THINGY, \
  83. .read = savu_sysfs_read_ ## thingy, \
  84. .write = savu_sysfs_write_ ## thingy \
  85. }
  86. #define SAVU_BIN_ATTRIBUTE_R(thingy, THINGY) \
  87. { \
  88. .attr = { .name = #thingy, .mode = 0440 }, \
  89. .size = SAVU_SIZE_ ## THINGY, \
  90. .read = savu_sysfs_read_ ## thingy, \
  91. }
  92. #define SAVU_BIN_ATTRIBUTE_W(thingy, THINGY) \
  93. { \
  94. .attr = { .name = #thingy, .mode = 0220 }, \
  95. .size = SAVU_SIZE_ ## THINGY, \
  96. .write = savu_sysfs_write_ ## thingy \
  97. }
  98. SAVU_SYSFS_W(control, CONTROL)
  99. SAVU_SYSFS_RW(profile, PROFILE)
  100. SAVU_SYSFS_RW(general, GENERAL)
  101. SAVU_SYSFS_RW(buttons, BUTTONS)
  102. SAVU_SYSFS_RW(macro, MACRO)
  103. SAVU_SYSFS_RW(info, INFO)
  104. SAVU_SYSFS_RW(sensor, SENSOR)
  105. static struct bin_attribute savu_bin_attributes[] = {
  106. SAVU_BIN_ATTRIBUTE_W(control, CONTROL),
  107. SAVU_BIN_ATTRIBUTE_RW(profile, PROFILE),
  108. SAVU_BIN_ATTRIBUTE_RW(general, GENERAL),
  109. SAVU_BIN_ATTRIBUTE_RW(buttons, BUTTONS),
  110. SAVU_BIN_ATTRIBUTE_RW(macro, MACRO),
  111. SAVU_BIN_ATTRIBUTE_RW(info, INFO),
  112. SAVU_BIN_ATTRIBUTE_RW(sensor, SENSOR),
  113. __ATTR_NULL
  114. };
  115. static int savu_init_savu_device_struct(struct usb_device *usb_dev,
  116. struct savu_device *savu)
  117. {
  118. mutex_init(&savu->savu_lock);
  119. return 0;
  120. }
  121. static int savu_init_specials(struct hid_device *hdev)
  122. {
  123. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  124. struct usb_device *usb_dev = interface_to_usbdev(intf);
  125. struct savu_device *savu;
  126. int retval;
  127. if (intf->cur_altsetting->desc.bInterfaceProtocol
  128. != USB_INTERFACE_PROTOCOL_MOUSE) {
  129. hid_set_drvdata(hdev, NULL);
  130. return 0;
  131. }
  132. savu = kzalloc(sizeof(*savu), GFP_KERNEL);
  133. if (!savu) {
  134. hid_err(hdev, "can't alloc device descriptor\n");
  135. return -ENOMEM;
  136. }
  137. hid_set_drvdata(hdev, savu);
  138. retval = savu_init_savu_device_struct(usb_dev, savu);
  139. if (retval) {
  140. hid_err(hdev, "couldn't init struct savu_device\n");
  141. goto exit_free;
  142. }
  143. retval = roccat_connect(savu_class, hdev,
  144. sizeof(struct savu_roccat_report));
  145. if (retval < 0) {
  146. hid_err(hdev, "couldn't init char dev\n");
  147. } else {
  148. savu->chrdev_minor = retval;
  149. savu->roccat_claimed = 1;
  150. }
  151. return 0;
  152. exit_free:
  153. kfree(savu);
  154. return retval;
  155. }
  156. static void savu_remove_specials(struct hid_device *hdev)
  157. {
  158. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  159. struct savu_device *savu;
  160. if (intf->cur_altsetting->desc.bInterfaceProtocol
  161. != USB_INTERFACE_PROTOCOL_MOUSE)
  162. return;
  163. savu = hid_get_drvdata(hdev);
  164. if (savu->roccat_claimed)
  165. roccat_disconnect(savu->chrdev_minor);
  166. kfree(savu);
  167. }
  168. static int savu_probe(struct hid_device *hdev,
  169. const struct hid_device_id *id)
  170. {
  171. int retval;
  172. retval = hid_parse(hdev);
  173. if (retval) {
  174. hid_err(hdev, "parse failed\n");
  175. goto exit;
  176. }
  177. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  178. if (retval) {
  179. hid_err(hdev, "hw start failed\n");
  180. goto exit;
  181. }
  182. retval = savu_init_specials(hdev);
  183. if (retval) {
  184. hid_err(hdev, "couldn't install mouse\n");
  185. goto exit_stop;
  186. }
  187. return 0;
  188. exit_stop:
  189. hid_hw_stop(hdev);
  190. exit:
  191. return retval;
  192. }
  193. static void savu_remove(struct hid_device *hdev)
  194. {
  195. savu_remove_specials(hdev);
  196. hid_hw_stop(hdev);
  197. }
  198. static void savu_report_to_chrdev(struct savu_device const *savu,
  199. u8 const *data)
  200. {
  201. struct savu_roccat_report roccat_report;
  202. struct savu_mouse_report_special const *special_report;
  203. if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
  204. return;
  205. special_report = (struct savu_mouse_report_special const *)data;
  206. roccat_report.type = special_report->type;
  207. roccat_report.data[0] = special_report->data[0];
  208. roccat_report.data[1] = special_report->data[1];
  209. roccat_report_event(savu->chrdev_minor,
  210. (uint8_t const *)&roccat_report);
  211. }
  212. static int savu_raw_event(struct hid_device *hdev,
  213. struct hid_report *report, u8 *data, int size)
  214. {
  215. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  216. struct savu_device *savu = hid_get_drvdata(hdev);
  217. if (intf->cur_altsetting->desc.bInterfaceProtocol
  218. != USB_INTERFACE_PROTOCOL_MOUSE)
  219. return 0;
  220. if (savu == NULL)
  221. return 0;
  222. if (savu->roccat_claimed)
  223. savu_report_to_chrdev(savu, data);
  224. return 0;
  225. }
  226. static const struct hid_device_id savu_devices[] = {
  227. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
  228. { }
  229. };
  230. MODULE_DEVICE_TABLE(hid, savu_devices);
  231. static struct hid_driver savu_driver = {
  232. .name = "savu",
  233. .id_table = savu_devices,
  234. .probe = savu_probe,
  235. .remove = savu_remove,
  236. .raw_event = savu_raw_event
  237. };
  238. static int __init savu_init(void)
  239. {
  240. int retval;
  241. savu_class = class_create(THIS_MODULE, "savu");
  242. if (IS_ERR(savu_class))
  243. return PTR_ERR(savu_class);
  244. savu_class->dev_bin_attrs = savu_bin_attributes;
  245. retval = hid_register_driver(&savu_driver);
  246. if (retval)
  247. class_destroy(savu_class);
  248. return retval;
  249. }
  250. static void __exit savu_exit(void)
  251. {
  252. hid_unregister_driver(&savu_driver);
  253. class_destroy(savu_class);
  254. }
  255. module_init(savu_init);
  256. module_exit(savu_exit);
  257. MODULE_AUTHOR("Stefan Achatz");
  258. MODULE_DESCRIPTION("USB Roccat Savu driver");
  259. MODULE_LICENSE("GPL v2");