hid-roccat-konepure.c 5.4 KB

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