hid-roccat-konepure.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. KONEPURE_SYSFS_RW(thingy, THINGY); \
  81. static struct bin_attribute bin_attr_##thingy = { \
  82. .attr = { .name = #thingy, .mode = 0660 }, \
  83. .size = KONEPURE_SIZE_ ## THINGY, \
  84. .read = konepure_sysfs_read_ ## thingy, \
  85. .write = konepure_sysfs_write_ ## thingy \
  86. }
  87. #define KONEPURE_BIN_ATTRIBUTE_R(thingy, THINGY) \
  88. KONEPURE_SYSFS_R(thingy, THINGY); \
  89. static struct bin_attribute bin_attr_##thingy = { \
  90. .attr = { .name = #thingy, .mode = 0440 }, \
  91. .size = KONEPURE_SIZE_ ## THINGY, \
  92. .read = konepure_sysfs_read_ ## thingy, \
  93. }
  94. #define KONEPURE_BIN_ATTRIBUTE_W(thingy, THINGY) \
  95. KONEPURE_SYSFS_W(thingy, THINGY); \
  96. static struct bin_attribute bin_attr_##thingy = { \
  97. .attr = { .name = #thingy, .mode = 0220 }, \
  98. .size = KONEPURE_SIZE_ ## THINGY, \
  99. .write = konepure_sysfs_write_ ## thingy \
  100. }
  101. KONEPURE_BIN_ATTRIBUTE_RW(actual_profile, ACTUAL_PROFILE);
  102. KONEPURE_BIN_ATTRIBUTE_RW(info, INFO);
  103. KONEPURE_BIN_ATTRIBUTE_RW(sensor, SENSOR);
  104. KONEPURE_BIN_ATTRIBUTE_RW(tcu, TCU);
  105. KONEPURE_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
  106. KONEPURE_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
  107. KONEPURE_BIN_ATTRIBUTE_W(control, CONTROL);
  108. KONEPURE_BIN_ATTRIBUTE_W(talk, TALK);
  109. KONEPURE_BIN_ATTRIBUTE_W(macro, MACRO);
  110. KONEPURE_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
  111. static struct bin_attribute *konepure_bin_attributes[] = {
  112. &bin_attr_actual_profile,
  113. &bin_attr_info,
  114. &bin_attr_sensor,
  115. &bin_attr_tcu,
  116. &bin_attr_profile_settings,
  117. &bin_attr_profile_buttons,
  118. &bin_attr_control,
  119. &bin_attr_talk,
  120. &bin_attr_macro,
  121. &bin_attr_tcu_image,
  122. NULL,
  123. };
  124. static const struct attribute_group konepure_group = {
  125. .bin_attrs = konepure_bin_attributes,
  126. };
  127. static const struct attribute_group *konepure_groups[] = {
  128. &konepure_group,
  129. NULL,
  130. };
  131. static int konepure_init_konepure_device_struct(struct usb_device *usb_dev,
  132. struct konepure_device *konepure)
  133. {
  134. mutex_init(&konepure->konepure_lock);
  135. return 0;
  136. }
  137. static int konepure_init_specials(struct hid_device *hdev)
  138. {
  139. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  140. struct usb_device *usb_dev = interface_to_usbdev(intf);
  141. struct konepure_device *konepure;
  142. int retval;
  143. if (intf->cur_altsetting->desc.bInterfaceProtocol
  144. != USB_INTERFACE_PROTOCOL_MOUSE) {
  145. hid_set_drvdata(hdev, NULL);
  146. return 0;
  147. }
  148. konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
  149. if (!konepure) {
  150. hid_err(hdev, "can't alloc device descriptor\n");
  151. return -ENOMEM;
  152. }
  153. hid_set_drvdata(hdev, konepure);
  154. retval = konepure_init_konepure_device_struct(usb_dev, konepure);
  155. if (retval) {
  156. hid_err(hdev, "couldn't init struct konepure_device\n");
  157. goto exit_free;
  158. }
  159. retval = roccat_connect(konepure_class, hdev,
  160. sizeof(struct konepure_mouse_report_button));
  161. if (retval < 0) {
  162. hid_err(hdev, "couldn't init char dev\n");
  163. } else {
  164. konepure->chrdev_minor = retval;
  165. konepure->roccat_claimed = 1;
  166. }
  167. return 0;
  168. exit_free:
  169. kfree(konepure);
  170. return retval;
  171. }
  172. static void konepure_remove_specials(struct hid_device *hdev)
  173. {
  174. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  175. struct konepure_device *konepure;
  176. if (intf->cur_altsetting->desc.bInterfaceProtocol
  177. != USB_INTERFACE_PROTOCOL_MOUSE)
  178. return;
  179. konepure = hid_get_drvdata(hdev);
  180. if (konepure->roccat_claimed)
  181. roccat_disconnect(konepure->chrdev_minor);
  182. kfree(konepure);
  183. }
  184. static int konepure_probe(struct hid_device *hdev,
  185. const struct hid_device_id *id)
  186. {
  187. int retval;
  188. retval = hid_parse(hdev);
  189. if (retval) {
  190. hid_err(hdev, "parse failed\n");
  191. goto exit;
  192. }
  193. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  194. if (retval) {
  195. hid_err(hdev, "hw start failed\n");
  196. goto exit;
  197. }
  198. retval = konepure_init_specials(hdev);
  199. if (retval) {
  200. hid_err(hdev, "couldn't install mouse\n");
  201. goto exit_stop;
  202. }
  203. return 0;
  204. exit_stop:
  205. hid_hw_stop(hdev);
  206. exit:
  207. return retval;
  208. }
  209. static void konepure_remove(struct hid_device *hdev)
  210. {
  211. konepure_remove_specials(hdev);
  212. hid_hw_stop(hdev);
  213. }
  214. static int konepure_raw_event(struct hid_device *hdev,
  215. struct hid_report *report, u8 *data, int size)
  216. {
  217. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  218. struct konepure_device *konepure = hid_get_drvdata(hdev);
  219. if (intf->cur_altsetting->desc.bInterfaceProtocol
  220. != USB_INTERFACE_PROTOCOL_MOUSE)
  221. return 0;
  222. if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
  223. return 0;
  224. if (konepure != NULL && konepure->roccat_claimed)
  225. roccat_report_event(konepure->chrdev_minor, data);
  226. return 0;
  227. }
  228. static const struct hid_device_id konepure_devices[] = {
  229. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(hid, konepure_devices);
  233. static struct hid_driver konepure_driver = {
  234. .name = "konepure",
  235. .id_table = konepure_devices,
  236. .probe = konepure_probe,
  237. .remove = konepure_remove,
  238. .raw_event = konepure_raw_event
  239. };
  240. static int __init konepure_init(void)
  241. {
  242. int retval;
  243. konepure_class = class_create(THIS_MODULE, "konepure");
  244. if (IS_ERR(konepure_class))
  245. return PTR_ERR(konepure_class);
  246. konepure_class->dev_groups = konepure_groups;
  247. retval = hid_register_driver(&konepure_driver);
  248. if (retval)
  249. class_destroy(konepure_class);
  250. return retval;
  251. }
  252. static void __exit konepure_exit(void)
  253. {
  254. hid_unregister_driver(&konepure_driver);
  255. class_destroy(konepure_class);
  256. }
  257. module_init(konepure_init);
  258. module_exit(konepure_exit);
  259. MODULE_AUTHOR("Stefan Achatz");
  260. MODULE_DESCRIPTION("USB Roccat KonePure driver");
  261. MODULE_LICENSE("GPL v2");