1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MSI GT683R led driver
5 * Copyright (c) 2014 Janne Kanniainen <janne.kanniainen@gmail.com>
8 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
16 #define GT683R_BUFFER_SIZE 8
19 * GT683R_LED_OFF: all LEDs are off
20 * GT683R_LED_AUDIO: LEDs brightness depends on sound level
21 * GT683R_LED_BREATHING: LEDs brightness varies at human breathing rate
22 * GT683R_LED_NORMAL: LEDs are fully on when enabled
24 enum gt683r_led_mode {
27 GT683R_LED_BREATHING = 3,
38 static const char * const gt683r_panel_names[] = {
45 struct hid_device *hdev;
46 struct led_classdev led_devs[GT683R_LED_COUNT];
48 struct work_struct work;
49 enum led_brightness brightnesses[GT683R_LED_COUNT];
50 enum gt683r_led_mode mode;
53 static const struct hid_device_id gt683r_led_id[] = {
54 { HID_USB_DEVICE(USB_VENDOR_ID_MSI, USB_DEVICE_ID_MSI_GT683R_LED_PANEL) },
57 MODULE_DEVICE_TABLE(hid, gt683r_led_id);
59 static void gt683r_brightness_set(struct led_classdev *led_cdev,
60 enum led_brightness brightness)
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);
67 for (i = 0; i < GT683R_LED_COUNT; i++) {
68 if (led_cdev == &led->led_devs[i])
72 if (i < GT683R_LED_COUNT) {
73 led->brightnesses[i] = brightness;
74 schedule_work(&led->work);
78 static ssize_t mode_show(struct device *dev,
79 struct device_attribute *attr,
83 struct hid_device *hdev = to_hid_device(dev->parent);
84 struct gt683r_led *led = hid_get_drvdata(hdev);
86 if (led->mode == GT683R_LED_NORMAL)
88 else if (led->mode == GT683R_LED_AUDIO)
93 return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
96 static ssize_t mode_store(struct device *dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
101 struct hid_device *hdev = to_hid_device(dev->parent);
102 struct gt683r_led *led = hid_get_drvdata(hdev);
105 if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
108 mutex_lock(&led->lock);
111 led->mode = GT683R_LED_NORMAL;
112 else if (sysfs_mode == 1)
113 led->mode = GT683R_LED_AUDIO;
115 led->mode = GT683R_LED_BREATHING;
117 mutex_unlock(&led->lock);
118 schedule_work(&led->work);
123 static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
127 ret = hid_hw_raw_request(led->hdev, msg[0], msg, GT683R_BUFFER_SIZE,
128 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
129 if (ret != GT683R_BUFFER_SIZE) {
131 "failed to send set report request: %i\n", ret);
140 static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
145 buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
153 ret = gt683r_led_snd_msg(led, buffer);
159 static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
164 buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
173 ret = gt683r_led_snd_msg(led, buffer);
179 static void gt683r_led_work(struct work_struct *work)
184 struct gt683r_led *led = container_of(work, struct gt683r_led, work);
186 mutex_lock(&led->lock);
188 for (i = 0; i < GT683R_LED_COUNT; i++) {
189 if (led->brightnesses[i])
193 if (gt683r_leds_set(led, leds))
199 mode = GT683R_LED_OFF;
201 gt683r_mode_set(led, mode);
203 mutex_unlock(&led->lock);
206 static DEVICE_ATTR_RW(mode);
208 static struct attribute *gt683r_led_attrs[] = {
213 static const struct attribute_group gt683r_led_group = {
215 .attrs = gt683r_led_attrs,
218 static const struct attribute_group *gt683r_led_groups[] = {
223 static int gt683r_led_probe(struct hid_device *hdev,
224 const struct hid_device_id *id)
230 struct gt683r_led *led;
232 led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
236 mutex_init(&led->lock);
237 INIT_WORK(&led->work, gt683r_led_work);
239 led->mode = GT683R_LED_NORMAL;
241 hid_set_drvdata(hdev, led);
243 ret = hid_parse(hdev);
245 hid_err(hdev, "hid parsing failed\n");
249 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
251 hid_err(hdev, "hw start failed\n");
255 for (i = 0; i < GT683R_LED_COUNT; i++) {
256 name_sz = strlen(dev_name(&hdev->dev)) +
257 strlen(gt683r_panel_names[i]) + 3;
259 name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
265 snprintf(name, name_sz, "%s::%s",
266 dev_name(&hdev->dev), gt683r_panel_names[i]);
267 led->led_devs[i].name = name;
268 led->led_devs[i].max_brightness = 1;
269 led->led_devs[i].brightness_set = gt683r_brightness_set;
270 led->led_devs[i].groups = gt683r_led_groups;
272 ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
274 hid_err(hdev, "could not register led device\n");
282 for (i = i - 1; i >= 0; i--)
283 led_classdev_unregister(&led->led_devs[i]);
288 static void gt683r_led_remove(struct hid_device *hdev)
291 struct gt683r_led *led = hid_get_drvdata(hdev);
293 for (i = 0; i < GT683R_LED_COUNT; i++)
294 led_classdev_unregister(&led->led_devs[i]);
295 flush_work(&led->work);
299 static struct hid_driver gt683r_led_driver = {
300 .probe = gt683r_led_probe,
301 .remove = gt683r_led_remove,
302 .name = "gt683r_led",
303 .id_table = gt683r_led_id,
306 module_hid_driver(gt683r_led_driver);
308 MODULE_AUTHOR("Janne Kanniainen");
309 MODULE_DESCRIPTION("MSI GT683R led driver");
310 MODULE_LICENSE("GPL");