hid-roccat-savu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03);
  26. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03);
  27. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10);
  28. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f);
  29. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823);
  30. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08);
  31. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04);
  32. static struct bin_attribute *savu_bin_attrs[] = {
  33. &bin_attr_control,
  34. &bin_attr_profile,
  35. &bin_attr_general,
  36. &bin_attr_buttons,
  37. &bin_attr_macro,
  38. &bin_attr_info,
  39. &bin_attr_sensor,
  40. NULL,
  41. };
  42. static const struct attribute_group savu_group = {
  43. .bin_attrs = savu_bin_attrs,
  44. };
  45. static const struct attribute_group *savu_groups[] = {
  46. &savu_group,
  47. NULL,
  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. retval = hid_parse(hdev);
  101. if (retval) {
  102. hid_err(hdev, "parse failed\n");
  103. goto exit;
  104. }
  105. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  106. if (retval) {
  107. hid_err(hdev, "hw start failed\n");
  108. goto exit;
  109. }
  110. retval = savu_init_specials(hdev);
  111. if (retval) {
  112. hid_err(hdev, "couldn't install mouse\n");
  113. goto exit_stop;
  114. }
  115. return 0;
  116. exit_stop:
  117. hid_hw_stop(hdev);
  118. exit:
  119. return retval;
  120. }
  121. static void savu_remove(struct hid_device *hdev)
  122. {
  123. savu_remove_specials(hdev);
  124. hid_hw_stop(hdev);
  125. }
  126. static void savu_report_to_chrdev(struct roccat_common2_device const *savu,
  127. u8 const *data)
  128. {
  129. struct savu_roccat_report roccat_report;
  130. struct savu_mouse_report_special const *special_report;
  131. if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
  132. return;
  133. special_report = (struct savu_mouse_report_special const *)data;
  134. roccat_report.type = special_report->type;
  135. roccat_report.data[0] = special_report->data[0];
  136. roccat_report.data[1] = special_report->data[1];
  137. roccat_report_event(savu->chrdev_minor,
  138. (uint8_t const *)&roccat_report);
  139. }
  140. static int savu_raw_event(struct hid_device *hdev,
  141. struct hid_report *report, u8 *data, int size)
  142. {
  143. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  144. struct roccat_common2_device *savu = hid_get_drvdata(hdev);
  145. if (intf->cur_altsetting->desc.bInterfaceProtocol
  146. != USB_INTERFACE_PROTOCOL_MOUSE)
  147. return 0;
  148. if (savu == NULL)
  149. return 0;
  150. if (savu->roccat_claimed)
  151. savu_report_to_chrdev(savu, data);
  152. return 0;
  153. }
  154. static const struct hid_device_id savu_devices[] = {
  155. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
  156. { }
  157. };
  158. MODULE_DEVICE_TABLE(hid, savu_devices);
  159. static struct hid_driver savu_driver = {
  160. .name = "savu",
  161. .id_table = savu_devices,
  162. .probe = savu_probe,
  163. .remove = savu_remove,
  164. .raw_event = savu_raw_event
  165. };
  166. static int __init savu_init(void)
  167. {
  168. int retval;
  169. savu_class = class_create(THIS_MODULE, "savu");
  170. if (IS_ERR(savu_class))
  171. return PTR_ERR(savu_class);
  172. savu_class->dev_groups = savu_groups;
  173. retval = hid_register_driver(&savu_driver);
  174. if (retval)
  175. class_destroy(savu_class);
  176. return retval;
  177. }
  178. static void __exit savu_exit(void)
  179. {
  180. hid_unregister_driver(&savu_driver);
  181. class_destroy(savu_class);
  182. }
  183. module_init(savu_init);
  184. module_exit(savu_exit);
  185. MODULE_AUTHOR("Stefan Achatz");
  186. MODULE_DESCRIPTION("USB Roccat Savu driver");
  187. MODULE_LICENSE("GPL v2");