hid-roccat-konepure.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/device.h>
  16. #include <linux/input.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/hid-roccat.h>
  21. #include "hid-ids.h"
  22. #include "hid-roccat-common.h"
  23. #include "hid-roccat-konepure.h"
  24. static struct class *konepure_class;
  25. static ssize_t konepure_sysfs_read(struct file *fp, struct kobject *kobj,
  26. char *buf, loff_t off, size_t count,
  27. size_t real_size, uint command)
  28. {
  29. struct device *dev =
  30. container_of(kobj, struct device, kobj)->parent->parent;
  31. struct konepure_device *konepure = hid_get_drvdata(dev_get_drvdata(dev));
  32. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  33. int retval;
  34. if (off >= real_size)
  35. return 0;
  36. if (off != 0 || count != real_size)
  37. return -EINVAL;
  38. mutex_lock(&konepure->konepure_lock);
  39. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  40. mutex_unlock(&konepure->konepure_lock);
  41. return retval ? retval : real_size;
  42. }
  43. static ssize_t konepure_sysfs_write(struct file *fp, struct kobject *kobj,
  44. void const *buf, loff_t off, size_t count,
  45. size_t real_size, uint command)
  46. {
  47. struct device *dev =
  48. container_of(kobj, struct device, kobj)->parent->parent;
  49. struct konepure_device *konepure = hid_get_drvdata(dev_get_drvdata(dev));
  50. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  51. int retval;
  52. if (off != 0 || count != real_size)
  53. return -EINVAL;
  54. mutex_lock(&konepure->konepure_lock);
  55. retval = roccat_common2_send_with_status(usb_dev, command,
  56. (void *)buf, real_size);
  57. mutex_unlock(&konepure->konepure_lock);
  58. return retval ? retval : real_size;
  59. }
  60. #define KONEPURE_SYSFS_W(thingy, THINGY) \
  61. static ssize_t konepure_sysfs_write_ ## thingy(struct file *fp, \
  62. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  63. loff_t off, size_t count) \
  64. { \
  65. return konepure_sysfs_write(fp, kobj, buf, off, count, \
  66. KONEPURE_SIZE_ ## THINGY, KONEPURE_COMMAND_ ## THINGY); \
  67. }
  68. #define KONEPURE_SYSFS_R(thingy, THINGY) \
  69. static ssize_t konepure_sysfs_read_ ## thingy(struct file *fp, \
  70. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  71. loff_t off, size_t count) \
  72. { \
  73. return konepure_sysfs_read(fp, kobj, buf, off, count, \
  74. KONEPURE_SIZE_ ## THINGY, KONEPURE_COMMAND_ ## THINGY); \
  75. }
  76. #define KONEPURE_SYSFS_RW(thingy, THINGY) \
  77. KONEPURE_SYSFS_W(thingy, THINGY) \
  78. KONEPURE_SYSFS_R(thingy, THINGY)
  79. #define KONEPURE_BIN_ATTRIBUTE_RW(thingy, THINGY) \
  80. { \
  81. .attr = { .name = #thingy, .mode = 0660 }, \
  82. .size = KONEPURE_SIZE_ ## THINGY, \
  83. .read = konepure_sysfs_read_ ## thingy, \
  84. .write = konepure_sysfs_write_ ## thingy \
  85. }
  86. #define KONEPURE_BIN_ATTRIBUTE_R(thingy, THINGY) \
  87. { \
  88. .attr = { .name = #thingy, .mode = 0440 }, \
  89. .size = KONEPURE_SIZE_ ## THINGY, \
  90. .read = konepure_sysfs_read_ ## thingy, \
  91. }
  92. #define KONEPURE_BIN_ATTRIBUTE_W(thingy, THINGY) \
  93. { \
  94. .attr = { .name = #thingy, .mode = 0220 }, \
  95. .size = KONEPURE_SIZE_ ## THINGY, \
  96. .write = konepure_sysfs_write_ ## thingy \
  97. }
  98. KONEPURE_SYSFS_RW(actual_profile, ACTUAL_PROFILE)
  99. KONEPURE_SYSFS_W(control, CONTROL)
  100. KONEPURE_SYSFS_RW(info, INFO)
  101. KONEPURE_SYSFS_W(talk, TALK)
  102. KONEPURE_SYSFS_W(macro, MACRO)
  103. KONEPURE_SYSFS_RW(sensor, SENSOR)
  104. KONEPURE_SYSFS_RW(tcu, TCU)
  105. KONEPURE_SYSFS_R(tcu_image, TCU_IMAGE)
  106. KONEPURE_SYSFS_RW(profile_settings, PROFILE_SETTINGS)
  107. KONEPURE_SYSFS_RW(profile_buttons, PROFILE_BUTTONS)
  108. static struct bin_attribute konepure_bin_attributes[] = {
  109. KONEPURE_BIN_ATTRIBUTE_RW(actual_profile, ACTUAL_PROFILE),
  110. KONEPURE_BIN_ATTRIBUTE_W(control, CONTROL),
  111. KONEPURE_BIN_ATTRIBUTE_RW(info, INFO),
  112. KONEPURE_BIN_ATTRIBUTE_W(talk, TALK),
  113. KONEPURE_BIN_ATTRIBUTE_W(macro, MACRO),
  114. KONEPURE_BIN_ATTRIBUTE_RW(sensor, SENSOR),
  115. KONEPURE_BIN_ATTRIBUTE_RW(tcu, TCU),
  116. KONEPURE_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE),
  117. KONEPURE_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS),
  118. KONEPURE_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS),
  119. __ATTR_NULL
  120. };
  121. static int konepure_init_konepure_device_struct(struct usb_device *usb_dev,
  122. struct konepure_device *konepure)
  123. {
  124. mutex_init(&konepure->konepure_lock);
  125. return 0;
  126. }
  127. static int konepure_init_specials(struct hid_device *hdev)
  128. {
  129. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  130. struct usb_device *usb_dev = interface_to_usbdev(intf);
  131. struct konepure_device *konepure;
  132. int retval;
  133. if (intf->cur_altsetting->desc.bInterfaceProtocol
  134. != USB_INTERFACE_PROTOCOL_MOUSE) {
  135. hid_set_drvdata(hdev, NULL);
  136. return 0;
  137. }
  138. konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
  139. if (!konepure) {
  140. hid_err(hdev, "can't alloc device descriptor\n");
  141. return -ENOMEM;
  142. }
  143. hid_set_drvdata(hdev, konepure);
  144. retval = konepure_init_konepure_device_struct(usb_dev, konepure);
  145. if (retval) {
  146. hid_err(hdev, "couldn't init struct konepure_device\n");
  147. goto exit_free;
  148. }
  149. retval = roccat_connect(konepure_class, hdev,
  150. sizeof(struct konepure_mouse_report_button));
  151. if (retval < 0) {
  152. hid_err(hdev, "couldn't init char dev\n");
  153. } else {
  154. konepure->chrdev_minor = retval;
  155. konepure->roccat_claimed = 1;
  156. }
  157. return 0;
  158. exit_free:
  159. kfree(konepure);
  160. return retval;
  161. }
  162. static void konepure_remove_specials(struct hid_device *hdev)
  163. {
  164. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  165. struct konepure_device *konepure;
  166. if (intf->cur_altsetting->desc.bInterfaceProtocol
  167. != USB_INTERFACE_PROTOCOL_MOUSE)
  168. return;
  169. konepure = hid_get_drvdata(hdev);
  170. if (konepure->roccat_claimed)
  171. roccat_disconnect(konepure->chrdev_minor);
  172. kfree(konepure);
  173. }
  174. static int konepure_probe(struct hid_device *hdev,
  175. const struct hid_device_id *id)
  176. {
  177. int retval;
  178. retval = hid_parse(hdev);
  179. if (retval) {
  180. hid_err(hdev, "parse failed\n");
  181. goto exit;
  182. }
  183. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  184. if (retval) {
  185. hid_err(hdev, "hw start failed\n");
  186. goto exit;
  187. }
  188. retval = konepure_init_specials(hdev);
  189. if (retval) {
  190. hid_err(hdev, "couldn't install mouse\n");
  191. goto exit_stop;
  192. }
  193. return 0;
  194. exit_stop:
  195. hid_hw_stop(hdev);
  196. exit:
  197. return retval;
  198. }
  199. static void konepure_remove(struct hid_device *hdev)
  200. {
  201. konepure_remove_specials(hdev);
  202. hid_hw_stop(hdev);
  203. }
  204. static int konepure_raw_event(struct hid_device *hdev,
  205. struct hid_report *report, u8 *data, int size)
  206. {
  207. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  208. struct konepure_device *konepure = hid_get_drvdata(hdev);
  209. if (intf->cur_altsetting->desc.bInterfaceProtocol
  210. != USB_INTERFACE_PROTOCOL_MOUSE)
  211. return 0;
  212. if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
  213. return 0;
  214. if (konepure != NULL && konepure->roccat_claimed)
  215. roccat_report_event(konepure->chrdev_minor, data);
  216. return 0;
  217. }
  218. static const struct hid_device_id konepure_devices[] = {
  219. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
  220. { }
  221. };
  222. MODULE_DEVICE_TABLE(hid, konepure_devices);
  223. static struct hid_driver konepure_driver = {
  224. .name = "konepure",
  225. .id_table = konepure_devices,
  226. .probe = konepure_probe,
  227. .remove = konepure_remove,
  228. .raw_event = konepure_raw_event
  229. };
  230. static int __init konepure_init(void)
  231. {
  232. int retval;
  233. konepure_class = class_create(THIS_MODULE, "konepure");
  234. if (IS_ERR(konepure_class))
  235. return PTR_ERR(konepure_class);
  236. konepure_class->dev_bin_attrs = konepure_bin_attributes;
  237. retval = hid_register_driver(&konepure_driver);
  238. if (retval)
  239. class_destroy(konepure_class);
  240. return retval;
  241. }
  242. static void __exit konepure_exit(void)
  243. {
  244. hid_unregister_driver(&konepure_driver);
  245. class_destroy(konepure_class);
  246. }
  247. module_init(konepure_init);
  248. module_exit(konepure_exit);
  249. MODULE_AUTHOR("Stefan Achatz");
  250. MODULE_DESCRIPTION("USB Roccat KonePure driver");
  251. MODULE_LICENSE("GPL v2");