hid-roccat-savu.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. SAVU_SYSFS_RW(thingy, THINGY); \
  81. static struct bin_attribute bin_attr_##thingy = { \
  82. .attr = { .name = #thingy, .mode = 0660 }, \
  83. .size = SAVU_SIZE_ ## THINGY, \
  84. .read = savu_sysfs_read_ ## thingy, \
  85. .write = savu_sysfs_write_ ## thingy \
  86. }
  87. #define SAVU_BIN_ATTRIBUTE_W(thingy, THINGY) \
  88. SAVU_SYSFS_W(thingy, THINGY); \
  89. static struct bin_attribute bin_attr_##thingy = { \
  90. .attr = { .name = #thingy, .mode = 0220 }, \
  91. .size = SAVU_SIZE_ ## THINGY, \
  92. .write = savu_sysfs_write_ ## thingy \
  93. }
  94. SAVU_BIN_ATTRIBUTE_W(control, CONTROL);
  95. SAVU_BIN_ATTRIBUTE_RW(profile, PROFILE);
  96. SAVU_BIN_ATTRIBUTE_RW(general, GENERAL);
  97. SAVU_BIN_ATTRIBUTE_RW(buttons, BUTTONS);
  98. SAVU_BIN_ATTRIBUTE_RW(macro, MACRO);
  99. SAVU_BIN_ATTRIBUTE_RW(info, INFO);
  100. SAVU_BIN_ATTRIBUTE_RW(sensor, SENSOR);
  101. static struct bin_attribute *savu_bin_attributes[] = {
  102. &bin_attr_control,
  103. &bin_attr_profile,
  104. &bin_attr_general,
  105. &bin_attr_buttons,
  106. &bin_attr_macro,
  107. &bin_attr_info,
  108. &bin_attr_sensor,
  109. NULL,
  110. };
  111. static const struct attribute_group savu_group = {
  112. .bin_attrs = savu_bin_attributes,
  113. };
  114. static const struct attribute_group *savu_groups[] = {
  115. &savu_group,
  116. NULL,
  117. };
  118. static int savu_init_savu_device_struct(struct usb_device *usb_dev,
  119. struct savu_device *savu)
  120. {
  121. mutex_init(&savu->savu_lock);
  122. return 0;
  123. }
  124. static int savu_init_specials(struct hid_device *hdev)
  125. {
  126. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  127. struct usb_device *usb_dev = interface_to_usbdev(intf);
  128. struct savu_device *savu;
  129. int retval;
  130. if (intf->cur_altsetting->desc.bInterfaceProtocol
  131. != USB_INTERFACE_PROTOCOL_MOUSE) {
  132. hid_set_drvdata(hdev, NULL);
  133. return 0;
  134. }
  135. savu = kzalloc(sizeof(*savu), GFP_KERNEL);
  136. if (!savu) {
  137. hid_err(hdev, "can't alloc device descriptor\n");
  138. return -ENOMEM;
  139. }
  140. hid_set_drvdata(hdev, savu);
  141. retval = savu_init_savu_device_struct(usb_dev, savu);
  142. if (retval) {
  143. hid_err(hdev, "couldn't init struct savu_device\n");
  144. goto exit_free;
  145. }
  146. retval = roccat_connect(savu_class, hdev,
  147. sizeof(struct savu_roccat_report));
  148. if (retval < 0) {
  149. hid_err(hdev, "couldn't init char dev\n");
  150. } else {
  151. savu->chrdev_minor = retval;
  152. savu->roccat_claimed = 1;
  153. }
  154. return 0;
  155. exit_free:
  156. kfree(savu);
  157. return retval;
  158. }
  159. static void savu_remove_specials(struct hid_device *hdev)
  160. {
  161. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  162. struct savu_device *savu;
  163. if (intf->cur_altsetting->desc.bInterfaceProtocol
  164. != USB_INTERFACE_PROTOCOL_MOUSE)
  165. return;
  166. savu = hid_get_drvdata(hdev);
  167. if (savu->roccat_claimed)
  168. roccat_disconnect(savu->chrdev_minor);
  169. kfree(savu);
  170. }
  171. static int savu_probe(struct hid_device *hdev,
  172. const struct hid_device_id *id)
  173. {
  174. int retval;
  175. retval = hid_parse(hdev);
  176. if (retval) {
  177. hid_err(hdev, "parse failed\n");
  178. goto exit;
  179. }
  180. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  181. if (retval) {
  182. hid_err(hdev, "hw start failed\n");
  183. goto exit;
  184. }
  185. retval = savu_init_specials(hdev);
  186. if (retval) {
  187. hid_err(hdev, "couldn't install mouse\n");
  188. goto exit_stop;
  189. }
  190. return 0;
  191. exit_stop:
  192. hid_hw_stop(hdev);
  193. exit:
  194. return retval;
  195. }
  196. static void savu_remove(struct hid_device *hdev)
  197. {
  198. savu_remove_specials(hdev);
  199. hid_hw_stop(hdev);
  200. }
  201. static void savu_report_to_chrdev(struct savu_device const *savu,
  202. u8 const *data)
  203. {
  204. struct savu_roccat_report roccat_report;
  205. struct savu_mouse_report_special const *special_report;
  206. if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
  207. return;
  208. special_report = (struct savu_mouse_report_special const *)data;
  209. roccat_report.type = special_report->type;
  210. roccat_report.data[0] = special_report->data[0];
  211. roccat_report.data[1] = special_report->data[1];
  212. roccat_report_event(savu->chrdev_minor,
  213. (uint8_t const *)&roccat_report);
  214. }
  215. static int savu_raw_event(struct hid_device *hdev,
  216. struct hid_report *report, u8 *data, int size)
  217. {
  218. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  219. struct savu_device *savu = hid_get_drvdata(hdev);
  220. if (intf->cur_altsetting->desc.bInterfaceProtocol
  221. != USB_INTERFACE_PROTOCOL_MOUSE)
  222. return 0;
  223. if (savu == NULL)
  224. return 0;
  225. if (savu->roccat_claimed)
  226. savu_report_to_chrdev(savu, data);
  227. return 0;
  228. }
  229. static const struct hid_device_id savu_devices[] = {
  230. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(hid, savu_devices);
  234. static struct hid_driver savu_driver = {
  235. .name = "savu",
  236. .id_table = savu_devices,
  237. .probe = savu_probe,
  238. .remove = savu_remove,
  239. .raw_event = savu_raw_event
  240. };
  241. static int __init savu_init(void)
  242. {
  243. int retval;
  244. savu_class = class_create(THIS_MODULE, "savu");
  245. if (IS_ERR(savu_class))
  246. return PTR_ERR(savu_class);
  247. savu_class->dev_groups = savu_groups;
  248. retval = hid_register_driver(&savu_driver);
  249. if (retval)
  250. class_destroy(savu_class);
  251. return retval;
  252. }
  253. static void __exit savu_exit(void)
  254. {
  255. hid_unregister_driver(&savu_driver);
  256. class_destroy(savu_class);
  257. }
  258. module_init(savu_init);
  259. module_exit(savu_exit);
  260. MODULE_AUTHOR("Stefan Achatz");
  261. MODULE_DESCRIPTION("USB Roccat Savu driver");
  262. MODULE_LICENSE("GPL v2");