hid-roccat-savu.c 5.0 KB

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