hid-roccat-lua.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Roccat Lua 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 Lua is a gamer mouse which cpi, button and light settings can be
  14. * configured.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. #include "hid-roccat-lua.h"
  25. static ssize_t lua_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 = container_of(kobj, struct device, kobj);
  30. struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
  31. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  32. int retval;
  33. if (off >= real_size)
  34. return 0;
  35. if (off != 0 || count != real_size)
  36. return -EINVAL;
  37. mutex_lock(&lua->lua_lock);
  38. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  39. mutex_unlock(&lua->lua_lock);
  40. return retval ? retval : real_size;
  41. }
  42. static ssize_t lua_sysfs_write(struct file *fp, struct kobject *kobj,
  43. void const *buf, loff_t off, size_t count,
  44. size_t real_size, uint command)
  45. {
  46. struct device *dev = container_of(kobj, struct device, kobj);
  47. struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
  48. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  49. int retval;
  50. if (off != 0 || count != real_size)
  51. return -EINVAL;
  52. mutex_lock(&lua->lua_lock);
  53. retval = roccat_common2_send(usb_dev, command, buf, real_size);
  54. mutex_unlock(&lua->lua_lock);
  55. return retval ? retval : real_size;
  56. }
  57. #define LUA_SYSFS_W(thingy, THINGY) \
  58. static ssize_t lua_sysfs_write_ ## thingy(struct file *fp, \
  59. struct kobject *kobj, struct bin_attribute *attr, \
  60. char *buf, loff_t off, size_t count) \
  61. { \
  62. return lua_sysfs_write(fp, kobj, buf, off, count, \
  63. LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
  64. }
  65. #define LUA_SYSFS_R(thingy, THINGY) \
  66. static ssize_t lua_sysfs_read_ ## thingy(struct file *fp, \
  67. struct kobject *kobj, struct bin_attribute *attr, \
  68. char *buf, loff_t off, size_t count) \
  69. { \
  70. return lua_sysfs_read(fp, kobj, buf, off, count, \
  71. LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
  72. }
  73. #define LUA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
  74. LUA_SYSFS_W(thingy, THINGY) \
  75. LUA_SYSFS_R(thingy, THINGY) \
  76. static struct bin_attribute lua_ ## thingy ## _attr = { \
  77. .attr = { .name = #thingy, .mode = 0660 }, \
  78. .size = LUA_SIZE_ ## THINGY, \
  79. .read = lua_sysfs_read_ ## thingy, \
  80. .write = lua_sysfs_write_ ## thingy \
  81. };
  82. LUA_BIN_ATTRIBUTE_RW(control, CONTROL)
  83. static int lua_create_sysfs_attributes(struct usb_interface *intf)
  84. {
  85. return sysfs_create_bin_file(&intf->dev.kobj, &lua_control_attr);
  86. }
  87. static void lua_remove_sysfs_attributes(struct usb_interface *intf)
  88. {
  89. sysfs_remove_bin_file(&intf->dev.kobj, &lua_control_attr);
  90. }
  91. static int lua_init_lua_device_struct(struct usb_device *usb_dev,
  92. struct lua_device *lua)
  93. {
  94. mutex_init(&lua->lua_lock);
  95. return 0;
  96. }
  97. static int lua_init_specials(struct hid_device *hdev)
  98. {
  99. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  100. struct usb_device *usb_dev = interface_to_usbdev(intf);
  101. struct lua_device *lua;
  102. int retval;
  103. lua = kzalloc(sizeof(*lua), GFP_KERNEL);
  104. if (!lua) {
  105. hid_err(hdev, "can't alloc device descriptor\n");
  106. return -ENOMEM;
  107. }
  108. hid_set_drvdata(hdev, lua);
  109. retval = lua_init_lua_device_struct(usb_dev, lua);
  110. if (retval) {
  111. hid_err(hdev, "couldn't init struct lua_device\n");
  112. goto exit;
  113. }
  114. retval = lua_create_sysfs_attributes(intf);
  115. if (retval) {
  116. hid_err(hdev, "cannot create sysfs files\n");
  117. goto exit;
  118. }
  119. return 0;
  120. exit:
  121. kfree(lua);
  122. return retval;
  123. }
  124. static void lua_remove_specials(struct hid_device *hdev)
  125. {
  126. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  127. struct lua_device *lua;
  128. lua_remove_sysfs_attributes(intf);
  129. lua = hid_get_drvdata(hdev);
  130. kfree(lua);
  131. }
  132. static int lua_probe(struct hid_device *hdev,
  133. const struct hid_device_id *id)
  134. {
  135. int retval;
  136. retval = hid_parse(hdev);
  137. if (retval) {
  138. hid_err(hdev, "parse failed\n");
  139. goto exit;
  140. }
  141. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  142. if (retval) {
  143. hid_err(hdev, "hw start failed\n");
  144. goto exit;
  145. }
  146. retval = lua_init_specials(hdev);
  147. if (retval) {
  148. hid_err(hdev, "couldn't install mouse\n");
  149. goto exit_stop;
  150. }
  151. return 0;
  152. exit_stop:
  153. hid_hw_stop(hdev);
  154. exit:
  155. return retval;
  156. }
  157. static void lua_remove(struct hid_device *hdev)
  158. {
  159. lua_remove_specials(hdev);
  160. hid_hw_stop(hdev);
  161. }
  162. static const struct hid_device_id lua_devices[] = {
  163. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_LUA) },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(hid, lua_devices);
  167. static struct hid_driver lua_driver = {
  168. .name = "lua",
  169. .id_table = lua_devices,
  170. .probe = lua_probe,
  171. .remove = lua_remove
  172. };
  173. module_hid_driver(lua_driver);
  174. MODULE_AUTHOR("Stefan Achatz");
  175. MODULE_DESCRIPTION("USB Roccat Lua driver");
  176. MODULE_LICENSE("GPL v2");