hid-roccat-savu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat Savu driver for Linux
  4. *
  5. * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
  6. */
  7. /*
  8. */
  9. /* Roccat Savu is a gamer mouse with macro keys that can be configured in
  10. * 5 profiles.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/input.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/hid-roccat.h>
  18. #include "hid-ids.h"
  19. #include "hid-roccat-common.h"
  20. #include "hid-roccat-savu.h"
  21. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03);
  22. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03);
  23. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10);
  24. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f);
  25. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823);
  26. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08);
  27. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04);
  28. static struct bin_attribute *savu_bin_attrs[] = {
  29. &bin_attr_control,
  30. &bin_attr_profile,
  31. &bin_attr_general,
  32. &bin_attr_buttons,
  33. &bin_attr_macro,
  34. &bin_attr_info,
  35. &bin_attr_sensor,
  36. NULL,
  37. };
  38. static const struct attribute_group savu_group = {
  39. .bin_attrs = savu_bin_attrs,
  40. };
  41. static const struct attribute_group *savu_groups[] = {
  42. &savu_group,
  43. NULL,
  44. };
  45. static const struct class savu_class = {
  46. .name = "savu",
  47. .dev_groups = savu_groups,
  48. };
  49. static int savu_init_specials(struct hid_device *hdev)
  50. {
  51. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  52. struct usb_device *usb_dev = interface_to_usbdev(intf);
  53. struct roccat_common2_device *savu;
  54. int retval;
  55. if (intf->cur_altsetting->desc.bInterfaceProtocol
  56. != USB_INTERFACE_PROTOCOL_MOUSE) {
  57. hid_set_drvdata(hdev, NULL);
  58. return 0;
  59. }
  60. savu = kzalloc(sizeof(*savu), GFP_KERNEL);
  61. if (!savu) {
  62. hid_err(hdev, "can't alloc device descriptor\n");
  63. return -ENOMEM;
  64. }
  65. hid_set_drvdata(hdev, savu);
  66. retval = roccat_common2_device_init_struct(usb_dev, savu);
  67. if (retval) {
  68. hid_err(hdev, "couldn't init Savu device\n");
  69. goto exit_free;
  70. }
  71. retval = roccat_connect(&savu_class, hdev,
  72. sizeof(struct savu_roccat_report));
  73. if (retval < 0) {
  74. hid_err(hdev, "couldn't init char dev\n");
  75. } else {
  76. savu->chrdev_minor = retval;
  77. savu->roccat_claimed = 1;
  78. }
  79. return 0;
  80. exit_free:
  81. kfree(savu);
  82. return retval;
  83. }
  84. static void savu_remove_specials(struct hid_device *hdev)
  85. {
  86. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  87. struct roccat_common2_device *savu;
  88. if (intf->cur_altsetting->desc.bInterfaceProtocol
  89. != USB_INTERFACE_PROTOCOL_MOUSE)
  90. return;
  91. savu = hid_get_drvdata(hdev);
  92. if (savu->roccat_claimed)
  93. roccat_disconnect(savu->chrdev_minor);
  94. kfree(savu);
  95. }
  96. static int savu_probe(struct hid_device *hdev,
  97. const struct hid_device_id *id)
  98. {
  99. int retval;
  100. if (!hid_is_usb(hdev))
  101. return -EINVAL;
  102. retval = hid_parse(hdev);
  103. if (retval) {
  104. hid_err(hdev, "parse failed\n");
  105. goto exit;
  106. }
  107. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  108. if (retval) {
  109. hid_err(hdev, "hw start failed\n");
  110. goto exit;
  111. }
  112. retval = savu_init_specials(hdev);
  113. if (retval) {
  114. hid_err(hdev, "couldn't install mouse\n");
  115. goto exit_stop;
  116. }
  117. return 0;
  118. exit_stop:
  119. hid_hw_stop(hdev);
  120. exit:
  121. return retval;
  122. }
  123. static void savu_remove(struct hid_device *hdev)
  124. {
  125. savu_remove_specials(hdev);
  126. hid_hw_stop(hdev);
  127. }
  128. static void savu_report_to_chrdev(struct roccat_common2_device const *savu,
  129. u8 const *data)
  130. {
  131. struct savu_roccat_report roccat_report;
  132. struct savu_mouse_report_special const *special_report;
  133. if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
  134. return;
  135. special_report = (struct savu_mouse_report_special const *)data;
  136. roccat_report.type = special_report->type;
  137. roccat_report.data[0] = special_report->data[0];
  138. roccat_report.data[1] = special_report->data[1];
  139. roccat_report_event(savu->chrdev_minor,
  140. (uint8_t const *)&roccat_report);
  141. }
  142. static int savu_raw_event(struct hid_device *hdev,
  143. struct hid_report *report, u8 *data, int size)
  144. {
  145. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  146. struct roccat_common2_device *savu = hid_get_drvdata(hdev);
  147. if (intf->cur_altsetting->desc.bInterfaceProtocol
  148. != USB_INTERFACE_PROTOCOL_MOUSE)
  149. return 0;
  150. if (savu == NULL)
  151. return 0;
  152. if (savu->roccat_claimed)
  153. savu_report_to_chrdev(savu, data);
  154. return 0;
  155. }
  156. static const struct hid_device_id savu_devices[] = {
  157. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
  158. { }
  159. };
  160. MODULE_DEVICE_TABLE(hid, savu_devices);
  161. static struct hid_driver savu_driver = {
  162. .name = "savu",
  163. .id_table = savu_devices,
  164. .probe = savu_probe,
  165. .remove = savu_remove,
  166. .raw_event = savu_raw_event
  167. };
  168. static int __init savu_init(void)
  169. {
  170. int retval;
  171. retval = class_register(&savu_class);
  172. if (retval)
  173. return retval;
  174. retval = hid_register_driver(&savu_driver);
  175. if (retval)
  176. class_unregister(&savu_class);
  177. return retval;
  178. }
  179. static void __exit savu_exit(void)
  180. {
  181. hid_unregister_driver(&savu_driver);
  182. class_unregister(&savu_class);
  183. }
  184. module_init(savu_init);
  185. module_exit(savu_exit);
  186. MODULE_AUTHOR("Stefan Achatz");
  187. MODULE_DESCRIPTION("USB Roccat Savu driver");
  188. MODULE_LICENSE("GPL v2");