hid-roccat-konepure.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat KonePure driver for Linux
  4. *
  5. * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
  6. */
  7. /*
  8. */
  9. /*
  10. * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/device.h>
  14. #include <linux/input.h>
  15. #include <linux/hid.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/hid-roccat.h>
  19. #include "hid-ids.h"
  20. #include "hid-roccat-common.h"
  21. enum {
  22. KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3,
  23. };
  24. struct konepure_mouse_report_button {
  25. uint8_t report_number; /* always KONEPURE_MOUSE_REPORT_NUMBER_BUTTON */
  26. uint8_t zero;
  27. uint8_t type;
  28. uint8_t data1;
  29. uint8_t data2;
  30. uint8_t zero2;
  31. uint8_t unknown[2];
  32. } __packed;
  33. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
  34. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03);
  35. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f);
  36. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b);
  37. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822);
  38. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06);
  39. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04);
  40. ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404);
  41. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06);
  42. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10);
  43. static struct bin_attribute *konepure_bin_attrs[] = {
  44. &bin_attr_actual_profile,
  45. &bin_attr_control,
  46. &bin_attr_info,
  47. &bin_attr_talk,
  48. &bin_attr_macro,
  49. &bin_attr_sensor,
  50. &bin_attr_tcu,
  51. &bin_attr_tcu_image,
  52. &bin_attr_profile_settings,
  53. &bin_attr_profile_buttons,
  54. NULL,
  55. };
  56. static const struct attribute_group konepure_group = {
  57. .bin_attrs = konepure_bin_attrs,
  58. };
  59. static const struct attribute_group *konepure_groups[] = {
  60. &konepure_group,
  61. NULL,
  62. };
  63. static const struct class konepure_class = {
  64. .name = "konepure",
  65. .dev_groups = konepure_groups,
  66. };
  67. static int konepure_init_specials(struct hid_device *hdev)
  68. {
  69. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  70. struct usb_device *usb_dev = interface_to_usbdev(intf);
  71. struct roccat_common2_device *konepure;
  72. int retval;
  73. if (intf->cur_altsetting->desc.bInterfaceProtocol
  74. != USB_INTERFACE_PROTOCOL_MOUSE) {
  75. hid_set_drvdata(hdev, NULL);
  76. return 0;
  77. }
  78. konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
  79. if (!konepure) {
  80. hid_err(hdev, "can't alloc device descriptor\n");
  81. return -ENOMEM;
  82. }
  83. hid_set_drvdata(hdev, konepure);
  84. retval = roccat_common2_device_init_struct(usb_dev, konepure);
  85. if (retval) {
  86. hid_err(hdev, "couldn't init KonePure device\n");
  87. goto exit_free;
  88. }
  89. retval = roccat_connect(&konepure_class, hdev,
  90. sizeof(struct konepure_mouse_report_button));
  91. if (retval < 0) {
  92. hid_err(hdev, "couldn't init char dev\n");
  93. } else {
  94. konepure->chrdev_minor = retval;
  95. konepure->roccat_claimed = 1;
  96. }
  97. return 0;
  98. exit_free:
  99. kfree(konepure);
  100. return retval;
  101. }
  102. static void konepure_remove_specials(struct hid_device *hdev)
  103. {
  104. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  105. struct roccat_common2_device *konepure;
  106. if (intf->cur_altsetting->desc.bInterfaceProtocol
  107. != USB_INTERFACE_PROTOCOL_MOUSE)
  108. return;
  109. konepure = hid_get_drvdata(hdev);
  110. if (konepure->roccat_claimed)
  111. roccat_disconnect(konepure->chrdev_minor);
  112. kfree(konepure);
  113. }
  114. static int konepure_probe(struct hid_device *hdev,
  115. const struct hid_device_id *id)
  116. {
  117. int retval;
  118. if (!hid_is_usb(hdev))
  119. return -EINVAL;
  120. retval = hid_parse(hdev);
  121. if (retval) {
  122. hid_err(hdev, "parse failed\n");
  123. goto exit;
  124. }
  125. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  126. if (retval) {
  127. hid_err(hdev, "hw start failed\n");
  128. goto exit;
  129. }
  130. retval = konepure_init_specials(hdev);
  131. if (retval) {
  132. hid_err(hdev, "couldn't install mouse\n");
  133. goto exit_stop;
  134. }
  135. return 0;
  136. exit_stop:
  137. hid_hw_stop(hdev);
  138. exit:
  139. return retval;
  140. }
  141. static void konepure_remove(struct hid_device *hdev)
  142. {
  143. konepure_remove_specials(hdev);
  144. hid_hw_stop(hdev);
  145. }
  146. static int konepure_raw_event(struct hid_device *hdev,
  147. struct hid_report *report, u8 *data, int size)
  148. {
  149. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  150. struct roccat_common2_device *konepure = hid_get_drvdata(hdev);
  151. if (intf->cur_altsetting->desc.bInterfaceProtocol
  152. != USB_INTERFACE_PROTOCOL_MOUSE)
  153. return 0;
  154. if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
  155. return 0;
  156. if (konepure != NULL && konepure->roccat_claimed)
  157. roccat_report_event(konepure->chrdev_minor, data);
  158. return 0;
  159. }
  160. static const struct hid_device_id konepure_devices[] = {
  161. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
  162. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
  163. { }
  164. };
  165. MODULE_DEVICE_TABLE(hid, konepure_devices);
  166. static struct hid_driver konepure_driver = {
  167. .name = "konepure",
  168. .id_table = konepure_devices,
  169. .probe = konepure_probe,
  170. .remove = konepure_remove,
  171. .raw_event = konepure_raw_event
  172. };
  173. static int __init konepure_init(void)
  174. {
  175. int retval;
  176. retval = class_register(&konepure_class);
  177. if (retval)
  178. return retval;
  179. retval = hid_register_driver(&konepure_driver);
  180. if (retval)
  181. class_unregister(&konepure_class);
  182. return retval;
  183. }
  184. static void __exit konepure_exit(void)
  185. {
  186. hid_unregister_driver(&konepure_driver);
  187. class_unregister(&konepure_class);
  188. }
  189. module_init(konepure_init);
  190. module_exit(konepure_exit);
  191. MODULE_AUTHOR("Stefan Achatz");
  192. MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver");
  193. MODULE_LICENSE("GPL v2");