hid-roccat-lua.c 4.9 KB

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