hid-roccat-konepure.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Roccat KonePure 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. /*
  13. * Roccat KonePure is a smaller version of KoneXTD with less buttons and lights.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. enum {
  25. KONEPURE_MOUSE_REPORT_NUMBER_BUTTON = 3,
  26. };
  27. struct konepure_mouse_report_button {
  28. uint8_t report_number; /* always KONEPURE_MOUSE_REPORT_NUMBER_BUTTON */
  29. uint8_t zero;
  30. uint8_t type;
  31. uint8_t data1;
  32. uint8_t data2;
  33. uint8_t zero2;
  34. uint8_t unknown[2];
  35. } __packed;
  36. static struct class *konepure_class;
  37. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
  38. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(actual_profile, 0x05, 0x03);
  39. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_settings, 0x06, 0x1f);
  40. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile_buttons, 0x07, 0x3b);
  41. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(macro, 0x08, 0x0822);
  42. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x09, 0x06);
  43. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(tcu, 0x0c, 0x04);
  44. ROCCAT_COMMON2_BIN_ATTRIBUTE_R(tcu_image, 0x0c, 0x0404);
  45. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0x0f, 0x06);
  46. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x10, 0x10);
  47. static struct bin_attribute *konepure_bin_attrs[] = {
  48. &bin_attr_actual_profile,
  49. &bin_attr_control,
  50. &bin_attr_info,
  51. &bin_attr_talk,
  52. &bin_attr_macro,
  53. &bin_attr_sensor,
  54. &bin_attr_tcu,
  55. &bin_attr_tcu_image,
  56. &bin_attr_profile_settings,
  57. &bin_attr_profile_buttons,
  58. NULL,
  59. };
  60. static const struct attribute_group konepure_group = {
  61. .bin_attrs = konepure_bin_attrs,
  62. };
  63. static const struct attribute_group *konepure_groups[] = {
  64. &konepure_group,
  65. NULL,
  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. retval = hid_parse(hdev);
  119. if (retval) {
  120. hid_err(hdev, "parse failed\n");
  121. goto exit;
  122. }
  123. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  124. if (retval) {
  125. hid_err(hdev, "hw start failed\n");
  126. goto exit;
  127. }
  128. retval = konepure_init_specials(hdev);
  129. if (retval) {
  130. hid_err(hdev, "couldn't install mouse\n");
  131. goto exit_stop;
  132. }
  133. return 0;
  134. exit_stop:
  135. hid_hw_stop(hdev);
  136. exit:
  137. return retval;
  138. }
  139. static void konepure_remove(struct hid_device *hdev)
  140. {
  141. konepure_remove_specials(hdev);
  142. hid_hw_stop(hdev);
  143. }
  144. static int konepure_raw_event(struct hid_device *hdev,
  145. struct hid_report *report, u8 *data, int size)
  146. {
  147. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  148. struct roccat_common2_device *konepure = hid_get_drvdata(hdev);
  149. if (intf->cur_altsetting->desc.bInterfaceProtocol
  150. != USB_INTERFACE_PROTOCOL_MOUSE)
  151. return 0;
  152. if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
  153. return 0;
  154. if (konepure != NULL && konepure->roccat_claimed)
  155. roccat_report_event(konepure->chrdev_minor, data);
  156. return 0;
  157. }
  158. static const struct hid_device_id konepure_devices[] = {
  159. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
  160. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE_OPTICAL) },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(hid, konepure_devices);
  164. static struct hid_driver konepure_driver = {
  165. .name = "konepure",
  166. .id_table = konepure_devices,
  167. .probe = konepure_probe,
  168. .remove = konepure_remove,
  169. .raw_event = konepure_raw_event
  170. };
  171. static int __init konepure_init(void)
  172. {
  173. int retval;
  174. konepure_class = class_create(THIS_MODULE, "konepure");
  175. if (IS_ERR(konepure_class))
  176. return PTR_ERR(konepure_class);
  177. konepure_class->dev_groups = konepure_groups;
  178. retval = hid_register_driver(&konepure_driver);
  179. if (retval)
  180. class_destroy(konepure_class);
  181. return retval;
  182. }
  183. static void __exit konepure_exit(void)
  184. {
  185. hid_unregister_driver(&konepure_driver);
  186. class_destroy(konepure_class);
  187. }
  188. module_init(konepure_init);
  189. module_exit(konepure_exit);
  190. MODULE_AUTHOR("Stefan Achatz");
  191. MODULE_DESCRIPTION("USB Roccat KonePure/Optical driver");
  192. MODULE_LICENSE("GPL v2");