hid-roccat-ryos.c 5.8 KB

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