hid-roccat-ryos.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat Ryos driver for Linux
  4. *
  5. * Copyright (c) 2013 Stefan Achatz <[email protected]>
  6. */
  7. /*
  8. */
  9. #include <linux/types.h>
  10. #include <linux/device.h>
  11. #include <linux/input.h>
  12. #include <linux/hid.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/hid-roccat.h>
  16. #include "hid-ids.h"
  17. #include "hid-roccat-common.h"
  18. enum {
  19. RYOS_REPORT_NUMBER_SPECIAL = 3,
  20. RYOS_USB_INTERFACE_PROTOCOL = 0,
  21. };
  22. struct ryos_report_special {
  23. uint8_t number; /* RYOS_REPORT_NUMBER_SPECIAL */
  24. uint8_t data[4];
  25. } __packed;
  26. static struct class *ryos_class;
  27. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
  28. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x05, 0x03);
  29. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_primary, 0x06, 0x7d);
  30. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_function, 0x07, 0x5f);
  31. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_macro, 0x08, 0x23);
  32. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_thumbster, 0x09, 0x17);
  33. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_extra, 0x0a, 0x08);
  34. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_easyzone, 0x0b, 0x126);
  35. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(key_mask, 0x0c, 0x06);
  36. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light, 0x0d, 0x10);
  37. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x0e, 0x7d2);
  38. ROCCAT_COMMON2_BIN_ATTRIBUTE_R(info, 0x0f, 0x08);
  39. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(reset, 0x11, 0x03);
  40. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(light_control, 0x13, 0x08);
  41. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x16, 0x10);
  42. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(stored_lights, 0x17, 0x0566);
  43. ROCCAT_COMMON2_BIN_ATTRIBUTE_W(custom_lights, 0x18, 0x14);
  44. ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light_macro, 0x19, 0x07d2);
  45. static struct bin_attribute *ryos_bin_attrs[] = {
  46. &bin_attr_control,
  47. &bin_attr_profile,
  48. &bin_attr_keys_primary,
  49. &bin_attr_keys_function,
  50. &bin_attr_keys_macro,
  51. &bin_attr_keys_thumbster,
  52. &bin_attr_keys_extra,
  53. &bin_attr_keys_easyzone,
  54. &bin_attr_key_mask,
  55. &bin_attr_light,
  56. &bin_attr_macro,
  57. &bin_attr_info,
  58. &bin_attr_reset,
  59. &bin_attr_light_control,
  60. &bin_attr_talk,
  61. &bin_attr_stored_lights,
  62. &bin_attr_custom_lights,
  63. &bin_attr_light_macro,
  64. NULL,
  65. };
  66. static const struct attribute_group ryos_group = {
  67. .bin_attrs = ryos_bin_attrs,
  68. };
  69. static const struct attribute_group *ryos_groups[] = {
  70. &ryos_group,
  71. NULL,
  72. };
  73. static int ryos_init_specials(struct hid_device *hdev)
  74. {
  75. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  76. struct usb_device *usb_dev = interface_to_usbdev(intf);
  77. struct roccat_common2_device *ryos;
  78. int retval;
  79. if (intf->cur_altsetting->desc.bInterfaceProtocol
  80. != RYOS_USB_INTERFACE_PROTOCOL) {
  81. hid_set_drvdata(hdev, NULL);
  82. return 0;
  83. }
  84. ryos = kzalloc(sizeof(*ryos), GFP_KERNEL);
  85. if (!ryos) {
  86. hid_err(hdev, "can't alloc device descriptor\n");
  87. return -ENOMEM;
  88. }
  89. hid_set_drvdata(hdev, ryos);
  90. retval = roccat_common2_device_init_struct(usb_dev, ryos);
  91. if (retval) {
  92. hid_err(hdev, "couldn't init Ryos device\n");
  93. goto exit_free;
  94. }
  95. retval = roccat_connect(ryos_class, hdev,
  96. sizeof(struct ryos_report_special));
  97. if (retval < 0) {
  98. hid_err(hdev, "couldn't init char dev\n");
  99. } else {
  100. ryos->chrdev_minor = retval;
  101. ryos->roccat_claimed = 1;
  102. }
  103. return 0;
  104. exit_free:
  105. kfree(ryos);
  106. return retval;
  107. }
  108. static void ryos_remove_specials(struct hid_device *hdev)
  109. {
  110. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  111. struct roccat_common2_device *ryos;
  112. if (intf->cur_altsetting->desc.bInterfaceProtocol
  113. != RYOS_USB_INTERFACE_PROTOCOL)
  114. return;
  115. ryos = hid_get_drvdata(hdev);
  116. if (ryos->roccat_claimed)
  117. roccat_disconnect(ryos->chrdev_minor);
  118. kfree(ryos);
  119. }
  120. static int ryos_probe(struct hid_device *hdev,
  121. const struct hid_device_id *id)
  122. {
  123. int retval;
  124. if (!hid_is_usb(hdev))
  125. return -EINVAL;
  126. retval = hid_parse(hdev);
  127. if (retval) {
  128. hid_err(hdev, "parse failed\n");
  129. goto exit;
  130. }
  131. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  132. if (retval) {
  133. hid_err(hdev, "hw start failed\n");
  134. goto exit;
  135. }
  136. retval = ryos_init_specials(hdev);
  137. if (retval) {
  138. hid_err(hdev, "couldn't install mouse\n");
  139. goto exit_stop;
  140. }
  141. return 0;
  142. exit_stop:
  143. hid_hw_stop(hdev);
  144. exit:
  145. return retval;
  146. }
  147. static void ryos_remove(struct hid_device *hdev)
  148. {
  149. ryos_remove_specials(hdev);
  150. hid_hw_stop(hdev);
  151. }
  152. static int ryos_raw_event(struct hid_device *hdev,
  153. struct hid_report *report, u8 *data, int size)
  154. {
  155. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  156. struct roccat_common2_device *ryos = hid_get_drvdata(hdev);
  157. if (intf->cur_altsetting->desc.bInterfaceProtocol
  158. != RYOS_USB_INTERFACE_PROTOCOL)
  159. return 0;
  160. if (data[0] != RYOS_REPORT_NUMBER_SPECIAL)
  161. return 0;
  162. if (ryos != NULL && ryos->roccat_claimed)
  163. roccat_report_event(ryos->chrdev_minor, data);
  164. return 0;
  165. }
  166. static const struct hid_device_id ryos_devices[] = {
  167. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK) },
  168. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_GLOW) },
  169. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_PRO) },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(hid, ryos_devices);
  173. static struct hid_driver ryos_driver = {
  174. .name = "ryos",
  175. .id_table = ryos_devices,
  176. .probe = ryos_probe,
  177. .remove = ryos_remove,
  178. .raw_event = ryos_raw_event
  179. };
  180. static int __init ryos_init(void)
  181. {
  182. int retval;
  183. ryos_class = class_create(THIS_MODULE, "ryos");
  184. if (IS_ERR(ryos_class))
  185. return PTR_ERR(ryos_class);
  186. ryos_class->dev_groups = ryos_groups;
  187. retval = hid_register_driver(&ryos_driver);
  188. if (retval)
  189. class_destroy(ryos_class);
  190. return retval;
  191. }
  192. static void __exit ryos_exit(void)
  193. {
  194. hid_unregister_driver(&ryos_driver);
  195. class_destroy(ryos_class);
  196. }
  197. module_init(ryos_init);
  198. module_exit(ryos_exit);
  199. MODULE_AUTHOR("Stefan Achatz");
  200. MODULE_DESCRIPTION("USB Roccat Ryos MK/Glow/Pro driver");
  201. MODULE_LICENSE("GPL v2");