hid-roccat-savu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Roccat Savu 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. /* Roccat Savu is a gamer mouse with macro keys that can be configured in
  13. * 5 profiles.
  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-savu.h"
  24. static struct class *savu_class;
  25. static int savu_receive_control_status(struct usb_device *usb_dev)
  26. {
  27. int retval;
  28. struct savu_control control;
  29. do {
  30. msleep(50);
  31. retval = roccat_common_receive(usb_dev, SAVU_COMMAND_CONTROL,
  32. &control, sizeof(struct savu_control));
  33. if (retval)
  34. return retval;
  35. switch (control.value) {
  36. case SAVU_CONTROL_REQUEST_WRITE_CHECK_OK:
  37. return 0;
  38. case SAVU_CONTROL_REQUEST_WRITE_CHECK_WAIT:
  39. continue;
  40. case SAVU_CONTROL_REQUEST_WRITE_CHECK_INVALID:
  41. /* seems to be critical - replug necessary */
  42. case SAVU_CONTROL_REQUEST_WRITE_CHECK_OVERLOAD:
  43. return -EINVAL;
  44. default:
  45. hid_err(usb_dev, "savu_receive_control_status: "
  46. "unknown response value 0x%x\n",
  47. control.value);
  48. return -EINVAL;
  49. }
  50. } while (1);
  51. }
  52. static int savu_send(struct usb_device *usb_dev, uint command,
  53. void const *buf, uint size)
  54. {
  55. int retval;
  56. retval = roccat_common_send(usb_dev, command, buf, size);
  57. if (retval)
  58. return retval;
  59. return savu_receive_control_status(usb_dev);
  60. }
  61. static ssize_t savu_sysfs_read(struct file *fp, struct kobject *kobj,
  62. char *buf, loff_t off, size_t count,
  63. size_t real_size, uint command)
  64. {
  65. struct device *dev =
  66. container_of(kobj, struct device, kobj)->parent->parent;
  67. struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
  68. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  69. int retval;
  70. if (off >= real_size)
  71. return 0;
  72. if (off != 0 || count != real_size)
  73. return -EINVAL;
  74. mutex_lock(&savu->savu_lock);
  75. retval = roccat_common_receive(usb_dev, command, buf, real_size);
  76. mutex_unlock(&savu->savu_lock);
  77. return retval ? retval : real_size;
  78. }
  79. static ssize_t savu_sysfs_write(struct file *fp, struct kobject *kobj,
  80. void const *buf, loff_t off, size_t count,
  81. size_t real_size, uint command)
  82. {
  83. struct device *dev =
  84. container_of(kobj, struct device, kobj)->parent->parent;
  85. struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
  86. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  87. int retval;
  88. if (off != 0 || count != real_size)
  89. return -EINVAL;
  90. mutex_lock(&savu->savu_lock);
  91. retval = savu_send(usb_dev, command, (void *)buf, real_size);
  92. mutex_unlock(&savu->savu_lock);
  93. return retval ? retval : real_size;
  94. }
  95. #define SAVU_SYSFS_W(thingy, THINGY) \
  96. static ssize_t savu_sysfs_write_ ## thingy(struct file *fp, \
  97. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  98. loff_t off, size_t count) \
  99. { \
  100. return savu_sysfs_write(fp, kobj, buf, off, count, \
  101. SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
  102. }
  103. #define SAVU_SYSFS_R(thingy, THINGY) \
  104. static ssize_t savu_sysfs_read_ ## thingy(struct file *fp, \
  105. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  106. loff_t off, size_t count) \
  107. { \
  108. return savu_sysfs_read(fp, kobj, buf, off, count, \
  109. SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
  110. }
  111. #define SAVU_SYSFS_RW(thingy, THINGY) \
  112. SAVU_SYSFS_W(thingy, THINGY) \
  113. SAVU_SYSFS_R(thingy, THINGY)
  114. #define SAVU_BIN_ATTRIBUTE_RW(thingy, THINGY) \
  115. { \
  116. .attr = { .name = #thingy, .mode = 0660 }, \
  117. .size = SAVU_SIZE_ ## THINGY, \
  118. .read = savu_sysfs_read_ ## thingy, \
  119. .write = savu_sysfs_write_ ## thingy \
  120. }
  121. #define SAVU_BIN_ATTRIBUTE_R(thingy, THINGY) \
  122. { \
  123. .attr = { .name = #thingy, .mode = 0440 }, \
  124. .size = SAVU_SIZE_ ## THINGY, \
  125. .read = savu_sysfs_read_ ## thingy, \
  126. }
  127. #define SAVU_BIN_ATTRIBUTE_W(thingy, THINGY) \
  128. { \
  129. .attr = { .name = #thingy, .mode = 0220 }, \
  130. .size = SAVU_SIZE_ ## THINGY, \
  131. .write = savu_sysfs_write_ ## thingy \
  132. }
  133. SAVU_SYSFS_W(control, CONTROL)
  134. SAVU_SYSFS_RW(profile, PROFILE)
  135. SAVU_SYSFS_RW(general, GENERAL)
  136. SAVU_SYSFS_RW(buttons, BUTTONS)
  137. SAVU_SYSFS_RW(macro, MACRO)
  138. SAVU_SYSFS_R(info, INFO)
  139. static struct bin_attribute savu_bin_attributes[] = {
  140. SAVU_BIN_ATTRIBUTE_W(control, CONTROL),
  141. SAVU_BIN_ATTRIBUTE_RW(profile, PROFILE),
  142. SAVU_BIN_ATTRIBUTE_RW(general, GENERAL),
  143. SAVU_BIN_ATTRIBUTE_RW(buttons, BUTTONS),
  144. SAVU_BIN_ATTRIBUTE_RW(macro, MACRO),
  145. SAVU_BIN_ATTRIBUTE_R(info, INFO),
  146. __ATTR_NULL
  147. };
  148. static int savu_init_savu_device_struct(struct usb_device *usb_dev,
  149. struct savu_device *savu)
  150. {
  151. mutex_init(&savu->savu_lock);
  152. return 0;
  153. }
  154. static int savu_init_specials(struct hid_device *hdev)
  155. {
  156. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  157. struct usb_device *usb_dev = interface_to_usbdev(intf);
  158. struct savu_device *savu;
  159. int retval;
  160. if (intf->cur_altsetting->desc.bInterfaceProtocol
  161. != USB_INTERFACE_PROTOCOL_MOUSE) {
  162. hid_set_drvdata(hdev, NULL);
  163. return 0;
  164. }
  165. savu = kzalloc(sizeof(*savu), GFP_KERNEL);
  166. if (!savu) {
  167. hid_err(hdev, "can't alloc device descriptor\n");
  168. return -ENOMEM;
  169. }
  170. hid_set_drvdata(hdev, savu);
  171. retval = savu_init_savu_device_struct(usb_dev, savu);
  172. if (retval) {
  173. hid_err(hdev, "couldn't init struct savu_device\n");
  174. goto exit_free;
  175. }
  176. retval = roccat_connect(savu_class, hdev,
  177. sizeof(struct savu_roccat_report));
  178. if (retval < 0) {
  179. hid_err(hdev, "couldn't init char dev\n");
  180. } else {
  181. savu->chrdev_minor = retval;
  182. savu->roccat_claimed = 1;
  183. }
  184. return 0;
  185. exit_free:
  186. kfree(savu);
  187. return retval;
  188. }
  189. static void savu_remove_specials(struct hid_device *hdev)
  190. {
  191. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  192. struct savu_device *savu;
  193. if (intf->cur_altsetting->desc.bInterfaceProtocol
  194. != USB_INTERFACE_PROTOCOL_MOUSE)
  195. return;
  196. savu = hid_get_drvdata(hdev);
  197. if (savu->roccat_claimed)
  198. roccat_disconnect(savu->chrdev_minor);
  199. kfree(savu);
  200. }
  201. static int savu_probe(struct hid_device *hdev,
  202. const struct hid_device_id *id)
  203. {
  204. int retval;
  205. retval = hid_parse(hdev);
  206. if (retval) {
  207. hid_err(hdev, "parse failed\n");
  208. goto exit;
  209. }
  210. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  211. if (retval) {
  212. hid_err(hdev, "hw start failed\n");
  213. goto exit;
  214. }
  215. retval = savu_init_specials(hdev);
  216. if (retval) {
  217. hid_err(hdev, "couldn't install mouse\n");
  218. goto exit_stop;
  219. }
  220. return 0;
  221. exit_stop:
  222. hid_hw_stop(hdev);
  223. exit:
  224. return retval;
  225. }
  226. static void savu_remove(struct hid_device *hdev)
  227. {
  228. savu_remove_specials(hdev);
  229. hid_hw_stop(hdev);
  230. }
  231. static void savu_report_to_chrdev(struct savu_device const *savu,
  232. u8 const *data)
  233. {
  234. struct savu_roccat_report roccat_report;
  235. struct savu_mouse_report_special const *special_report;
  236. if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
  237. return;
  238. special_report = (struct savu_mouse_report_special const *)data;
  239. roccat_report.type = special_report->type;
  240. roccat_report.data[0] = special_report->data[0];
  241. roccat_report.data[1] = special_report->data[1];
  242. roccat_report_event(savu->chrdev_minor,
  243. (uint8_t const *)&roccat_report);
  244. }
  245. static int savu_raw_event(struct hid_device *hdev,
  246. struct hid_report *report, u8 *data, int size)
  247. {
  248. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  249. struct savu_device *savu = hid_get_drvdata(hdev);
  250. if (intf->cur_altsetting->desc.bInterfaceProtocol
  251. != USB_INTERFACE_PROTOCOL_MOUSE)
  252. return 0;
  253. if (savu == NULL)
  254. return 0;
  255. if (savu->roccat_claimed)
  256. savu_report_to_chrdev(savu, data);
  257. return 0;
  258. }
  259. static const struct hid_device_id savu_devices[] = {
  260. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(hid, savu_devices);
  264. static struct hid_driver savu_driver = {
  265. .name = "savu",
  266. .id_table = savu_devices,
  267. .probe = savu_probe,
  268. .remove = savu_remove,
  269. .raw_event = savu_raw_event
  270. };
  271. static int __init savu_init(void)
  272. {
  273. int retval;
  274. savu_class = class_create(THIS_MODULE, "savu");
  275. if (IS_ERR(savu_class))
  276. return PTR_ERR(savu_class);
  277. savu_class->dev_bin_attrs = savu_bin_attributes;
  278. retval = hid_register_driver(&savu_driver);
  279. if (retval)
  280. class_destroy(savu_class);
  281. return retval;
  282. }
  283. static void __exit savu_exit(void)
  284. {
  285. hid_unregister_driver(&savu_driver);
  286. class_destroy(savu_class);
  287. }
  288. module_init(savu_init);
  289. module_exit(savu_exit);
  290. MODULE_AUTHOR("Stefan Achatz");
  291. MODULE_DESCRIPTION("USB Roccat Savu driver");
  292. MODULE_LICENSE("GPL v2");