hid-gt683r.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * MSI GT683R led driver
  3. *
  4. * Copyright (c) 2014 Janne Kanniainen <janne.kanniainen@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/device.h>
  18. #include <linux/hid.h>
  19. #include <linux/kernel.h>
  20. #include <linux/leds.h>
  21. #include <linux/module.h>
  22. #include "hid-ids.h"
  23. #define GT683R_BUFFER_SIZE 8
  24. /*
  25. * GT683R_LED_OFF: all LEDs are off
  26. * GT683R_LED_AUDIO: LEDs brightness depends on sound level
  27. * GT683R_LED_BREATHING: LEDs brightness varies at human breathing rate
  28. * GT683R_LED_NORMAL: LEDs are fully on when enabled
  29. */
  30. enum gt683r_led_mode {
  31. GT683R_LED_OFF = 0,
  32. GT683R_LED_AUDIO = 2,
  33. GT683R_LED_BREATHING = 3,
  34. GT683R_LED_NORMAL = 5
  35. };
  36. enum gt683r_panels {
  37. GT683R_LED_BACK = 0,
  38. GT683R_LED_SIDE = 1,
  39. GT683R_LED_FRONT = 2,
  40. GT683R_LED_COUNT,
  41. };
  42. static const char * const gt683r_panel_names[] = {
  43. "back",
  44. "side",
  45. "front",
  46. };
  47. struct gt683r_led {
  48. struct hid_device *hdev;
  49. struct led_classdev led_devs[GT683R_LED_COUNT];
  50. struct mutex lock;
  51. struct work_struct work;
  52. enum led_brightness brightnesses[GT683R_LED_COUNT];
  53. enum gt683r_led_mode mode;
  54. };
  55. static const struct hid_device_id gt683r_led_id[] = {
  56. { HID_USB_DEVICE(USB_VENDOR_ID_MSI, USB_DEVICE_ID_MSI_GT683R_LED_PANEL) },
  57. { }
  58. };
  59. static void gt683r_brightness_set(struct led_classdev *led_cdev,
  60. enum led_brightness brightness)
  61. {
  62. int i;
  63. struct device *dev = led_cdev->dev->parent;
  64. struct hid_device *hdev = to_hid_device(dev);
  65. struct gt683r_led *led = hid_get_drvdata(hdev);
  66. for (i = 0; i < GT683R_LED_COUNT; i++) {
  67. if (led_cdev == &led->led_devs[i])
  68. break;
  69. }
  70. if (i < GT683R_LED_COUNT) {
  71. led->brightnesses[i] = brightness;
  72. schedule_work(&led->work);
  73. }
  74. }
  75. static ssize_t mode_show(struct device *dev,
  76. struct device_attribute *attr,
  77. char *buf)
  78. {
  79. u8 sysfs_mode;
  80. struct hid_device *hdev = to_hid_device(dev->parent);
  81. struct gt683r_led *led = hid_get_drvdata(hdev);
  82. if (led->mode == GT683R_LED_NORMAL)
  83. sysfs_mode = 0;
  84. else if (led->mode == GT683R_LED_AUDIO)
  85. sysfs_mode = 1;
  86. else
  87. sysfs_mode = 2;
  88. return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
  89. }
  90. static ssize_t mode_store(struct device *dev,
  91. struct device_attribute *attr,
  92. const char *buf, size_t count)
  93. {
  94. u8 sysfs_mode;
  95. struct hid_device *hdev = to_hid_device(dev->parent);
  96. struct gt683r_led *led = hid_get_drvdata(hdev);
  97. if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
  98. return -EINVAL;
  99. mutex_lock(&led->lock);
  100. if (sysfs_mode == 0)
  101. led->mode = GT683R_LED_NORMAL;
  102. else if (sysfs_mode == 1)
  103. led->mode = GT683R_LED_AUDIO;
  104. else
  105. led->mode = GT683R_LED_BREATHING;
  106. mutex_unlock(&led->lock);
  107. schedule_work(&led->work);
  108. return count;
  109. }
  110. static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
  111. {
  112. int ret;
  113. ret = hid_hw_raw_request(led->hdev, msg[0], msg, GT683R_BUFFER_SIZE,
  114. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  115. if (ret != GT683R_BUFFER_SIZE) {
  116. hid_err(led->hdev,
  117. "failed to send set report request: %i\n", ret);
  118. if (ret < 0)
  119. return ret;
  120. return -EIO;
  121. }
  122. return 0;
  123. }
  124. static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
  125. {
  126. int ret;
  127. u8 *buffer;
  128. buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
  129. if (!buffer)
  130. return -ENOMEM;
  131. buffer[0] = 0x01;
  132. buffer[1] = 0x02;
  133. buffer[2] = 0x30;
  134. buffer[3] = leds;
  135. ret = gt683r_led_snd_msg(led, buffer);
  136. kfree(buffer);
  137. return ret;
  138. }
  139. static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
  140. {
  141. int ret;
  142. u8 *buffer;
  143. buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
  144. if (!buffer)
  145. return -ENOMEM;
  146. buffer[0] = 0x01;
  147. buffer[1] = 0x02;
  148. buffer[2] = 0x20;
  149. buffer[3] = mode;
  150. buffer[4] = 0x01;
  151. ret = gt683r_led_snd_msg(led, buffer);
  152. kfree(buffer);
  153. return ret;
  154. }
  155. static void gt683r_led_work(struct work_struct *work)
  156. {
  157. int i;
  158. u8 leds = 0;
  159. u8 mode;
  160. struct gt683r_led *led = container_of(work, struct gt683r_led, work);
  161. mutex_lock(&led->lock);
  162. for (i = 0; i < GT683R_LED_COUNT; i++) {
  163. if (led->brightnesses[i])
  164. leds |= BIT(i);
  165. }
  166. if (gt683r_leds_set(led, leds))
  167. goto fail;
  168. if (leds)
  169. mode = led->mode;
  170. else
  171. mode = GT683R_LED_OFF;
  172. gt683r_mode_set(led, mode);
  173. fail:
  174. mutex_unlock(&led->lock);
  175. }
  176. static DEVICE_ATTR_RW(mode);
  177. static struct attribute *gt683r_led_attrs[] = {
  178. &dev_attr_mode.attr,
  179. NULL
  180. };
  181. static const struct attribute_group gt683r_led_group = {
  182. .name = "gt683r",
  183. .attrs = gt683r_led_attrs,
  184. };
  185. static const struct attribute_group *gt683r_led_groups[] = {
  186. &gt683r_led_group,
  187. NULL
  188. };
  189. static int gt683r_led_probe(struct hid_device *hdev,
  190. const struct hid_device_id *id)
  191. {
  192. int i;
  193. int ret;
  194. int name_sz;
  195. char *name;
  196. struct gt683r_led *led;
  197. led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
  198. if (!led)
  199. return -ENOMEM;
  200. mutex_init(&led->lock);
  201. INIT_WORK(&led->work, gt683r_led_work);
  202. led->mode = GT683R_LED_NORMAL;
  203. led->hdev = hdev;
  204. hid_set_drvdata(hdev, led);
  205. ret = hid_parse(hdev);
  206. if (ret) {
  207. hid_err(hdev, "hid parsing failed\n");
  208. return ret;
  209. }
  210. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  211. if (ret) {
  212. hid_err(hdev, "hw start failed\n");
  213. return ret;
  214. }
  215. for (i = 0; i < GT683R_LED_COUNT; i++) {
  216. name_sz = strlen(dev_name(&hdev->dev)) +
  217. strlen(gt683r_panel_names[i]) + 3;
  218. name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  219. if (!name) {
  220. ret = -ENOMEM;
  221. goto fail;
  222. }
  223. snprintf(name, name_sz, "%s::%s",
  224. dev_name(&hdev->dev), gt683r_panel_names[i]);
  225. led->led_devs[i].name = name;
  226. led->led_devs[i].max_brightness = 1;
  227. led->led_devs[i].brightness_set = gt683r_brightness_set;
  228. led->led_devs[i].groups = gt683r_led_groups;
  229. ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
  230. if (ret) {
  231. hid_err(hdev, "could not register led device\n");
  232. goto fail;
  233. }
  234. }
  235. return 0;
  236. fail:
  237. for (i = i - 1; i >= 0; i--)
  238. led_classdev_unregister(&led->led_devs[i]);
  239. hid_hw_stop(hdev);
  240. return ret;
  241. }
  242. static void gt683r_led_remove(struct hid_device *hdev)
  243. {
  244. int i;
  245. struct gt683r_led *led = hid_get_drvdata(hdev);
  246. for (i = 0; i < GT683R_LED_COUNT; i++)
  247. led_classdev_unregister(&led->led_devs[i]);
  248. flush_work(&led->work);
  249. hid_hw_stop(hdev);
  250. }
  251. static struct hid_driver gt683r_led_driver = {
  252. .probe = gt683r_led_probe,
  253. .remove = gt683r_led_remove,
  254. .name = "gt683r_led",
  255. .id_table = gt683r_led_id,
  256. };
  257. module_hid_driver(gt683r_led_driver);
  258. MODULE_AUTHOR("Janne Kanniainen");
  259. MODULE_DESCRIPTION("MSI GT683R led driver");
  260. MODULE_LICENSE("GPL");