hid-gt683r.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 = container_of(dev, struct 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 = container_of(dev->parent,
  81. struct hid_device, dev);
  82. struct gt683r_led *led = hid_get_drvdata(hdev);
  83. if (led->mode == GT683R_LED_NORMAL)
  84. sysfs_mode = 0;
  85. else if (led->mode == GT683R_LED_AUDIO)
  86. sysfs_mode = 1;
  87. else
  88. sysfs_mode = 2;
  89. return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
  90. }
  91. static ssize_t mode_store(struct device *dev,
  92. struct device_attribute *attr,
  93. const char *buf, size_t count)
  94. {
  95. u8 sysfs_mode;
  96. struct hid_device *hdev = container_of(dev->parent,
  97. struct hid_device, dev);
  98. struct gt683r_led *led = hid_get_drvdata(hdev);
  99. if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
  100. return -EINVAL;
  101. mutex_lock(&led->lock);
  102. if (sysfs_mode == 0)
  103. led->mode = GT683R_LED_NORMAL;
  104. else if (sysfs_mode == 1)
  105. led->mode = GT683R_LED_AUDIO;
  106. else
  107. led->mode = GT683R_LED_BREATHING;
  108. mutex_unlock(&led->lock);
  109. schedule_work(&led->work);
  110. return count;
  111. }
  112. static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
  113. {
  114. int ret;
  115. ret = hid_hw_raw_request(led->hdev, msg[0], msg, GT683R_BUFFER_SIZE,
  116. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  117. if (ret != GT683R_BUFFER_SIZE) {
  118. hid_err(led->hdev,
  119. "failed to send set report request: %i\n", ret);
  120. if (ret < 0)
  121. return ret;
  122. return -EIO;
  123. }
  124. return 0;
  125. }
  126. static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
  127. {
  128. int ret;
  129. u8 *buffer;
  130. buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
  131. if (!buffer)
  132. return -ENOMEM;
  133. buffer[0] = 0x01;
  134. buffer[1] = 0x02;
  135. buffer[2] = 0x30;
  136. buffer[3] = leds;
  137. ret = gt683r_led_snd_msg(led, buffer);
  138. kfree(buffer);
  139. return ret;
  140. }
  141. static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
  142. {
  143. int ret;
  144. u8 *buffer;
  145. buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
  146. if (!buffer)
  147. return -ENOMEM;
  148. buffer[0] = 0x01;
  149. buffer[1] = 0x02;
  150. buffer[2] = 0x20;
  151. buffer[3] = mode;
  152. buffer[4] = 0x01;
  153. ret = gt683r_led_snd_msg(led, buffer);
  154. kfree(buffer);
  155. return ret;
  156. }
  157. static void gt683r_led_work(struct work_struct *work)
  158. {
  159. int i;
  160. u8 leds = 0;
  161. u8 mode;
  162. struct gt683r_led *led = container_of(work, struct gt683r_led, work);
  163. mutex_lock(&led->lock);
  164. for (i = 0; i < GT683R_LED_COUNT; i++) {
  165. if (led->brightnesses[i])
  166. leds |= BIT(i);
  167. }
  168. if (gt683r_leds_set(led, leds))
  169. goto fail;
  170. if (leds)
  171. mode = led->mode;
  172. else
  173. mode = GT683R_LED_OFF;
  174. gt683r_mode_set(led, mode);
  175. fail:
  176. mutex_unlock(&led->lock);
  177. }
  178. static DEVICE_ATTR_RW(mode);
  179. static struct attribute *gt683r_led_attrs[] = {
  180. &dev_attr_mode.attr,
  181. NULL
  182. };
  183. static const struct attribute_group gt683r_led_group = {
  184. .name = "gt683r",
  185. .attrs = gt683r_led_attrs,
  186. };
  187. static const struct attribute_group *gt683r_led_groups[] = {
  188. &gt683r_led_group,
  189. NULL
  190. };
  191. static int gt683r_led_probe(struct hid_device *hdev,
  192. const struct hid_device_id *id)
  193. {
  194. int i;
  195. int ret;
  196. int name_sz;
  197. char *name;
  198. struct gt683r_led *led;
  199. led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
  200. if (!led)
  201. return -ENOMEM;
  202. mutex_init(&led->lock);
  203. INIT_WORK(&led->work, gt683r_led_work);
  204. led->mode = GT683R_LED_NORMAL;
  205. led->hdev = hdev;
  206. hid_set_drvdata(hdev, led);
  207. ret = hid_parse(hdev);
  208. if (ret) {
  209. hid_err(hdev, "hid parsing failed\n");
  210. return ret;
  211. }
  212. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  213. if (ret) {
  214. hid_err(hdev, "hw start failed\n");
  215. return ret;
  216. }
  217. for (i = 0; i < GT683R_LED_COUNT; i++) {
  218. name_sz = strlen(dev_name(&hdev->dev)) +
  219. strlen(gt683r_panel_names[i]) + 3;
  220. name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  221. if (!name) {
  222. ret = -ENOMEM;
  223. goto fail;
  224. }
  225. snprintf(name, name_sz, "%s::%s",
  226. dev_name(&hdev->dev), gt683r_panel_names[i]);
  227. led->led_devs[i].name = name;
  228. led->led_devs[i].max_brightness = 1;
  229. led->led_devs[i].brightness_set = gt683r_brightness_set;
  230. led->led_devs[i].groups = gt683r_led_groups;
  231. ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
  232. if (ret) {
  233. hid_err(hdev, "could not register led device\n");
  234. goto fail;
  235. }
  236. }
  237. return 0;
  238. fail:
  239. for (i = i - 1; i >= 0; i--)
  240. led_classdev_unregister(&led->led_devs[i]);
  241. hid_hw_stop(hdev);
  242. return ret;
  243. }
  244. static void gt683r_led_remove(struct hid_device *hdev)
  245. {
  246. int i;
  247. struct gt683r_led *led = hid_get_drvdata(hdev);
  248. for (i = 0; i < GT683R_LED_COUNT; i++)
  249. led_classdev_unregister(&led->led_devs[i]);
  250. flush_work(&led->work);
  251. hid_hw_stop(hdev);
  252. }
  253. static struct hid_driver gt683r_led_driver = {
  254. .probe = gt683r_led_probe,
  255. .remove = gt683r_led_remove,
  256. .name = "gt683r_led",
  257. .id_table = gt683r_led_id,
  258. };
  259. module_hid_driver(gt683r_led_driver);
  260. MODULE_AUTHOR("Janne Kanniainen");
  261. MODULE_DESCRIPTION("MSI GT683R led driver");
  262. MODULE_LICENSE("GPL");