2 * MSI GT683R led driver
4 * Copyright (c) 2014 Janne Kanniainen <janne.kanniainen@gmail.com>
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.
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.
18 #include <linux/device.h>
19 #include <linux/hid.h>
20 #include <linux/kernel.h>
21 #include <linux/leds.h>
22 #include <linux/module.h>
26 #define GT683R_BUFFER_SIZE 8
29 * GT683R_LED_OFF: all LEDs are off
30 * GT683R_LED_AUDIO: LEDs brightness depends on sound level
31 * GT683R_LED_BREATHING: LEDs brightness varies at human breathing rate
32 * GT683R_LED_NORMAL: LEDs are fully on when enabled
34 enum gt683r_led_mode {
37 GT683R_LED_BREATHING = 3,
48 static const char * const gt683r_panel_names[] = {
55 struct hid_device *hdev;
56 struct led_classdev led_devs[GT683R_LED_COUNT];
58 struct work_struct work;
59 enum led_brightness brightnesses[GT683R_LED_COUNT];
60 enum gt683r_led_mode mode;
63 static const struct hid_device_id gt683r_led_id[] = {
64 { HID_USB_DEVICE(USB_VENDOR_ID_MSI, USB_DEVICE_ID_MSI_GT683R_LED_PANEL) },
67 MODULE_DEVICE_TABLE(hid, gt683r_led_id);
69 static void gt683r_brightness_set(struct led_classdev *led_cdev,
70 enum led_brightness brightness)
73 struct device *dev = led_cdev->dev->parent;
74 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
75 struct gt683r_led *led = hid_get_drvdata(hdev);
77 for (i = 0; i < GT683R_LED_COUNT; i++) {
78 if (led_cdev == &led->led_devs[i])
82 if (i < GT683R_LED_COUNT) {
83 led->brightnesses[i] = brightness;
84 schedule_work(&led->work);
88 static ssize_t mode_show(struct device *dev,
89 struct device_attribute *attr,
93 struct hid_device *hdev = container_of(dev->parent,
94 struct hid_device, dev);
95 struct gt683r_led *led = hid_get_drvdata(hdev);
97 if (led->mode == GT683R_LED_NORMAL)
99 else if (led->mode == GT683R_LED_AUDIO)
104 return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
107 static ssize_t mode_store(struct device *dev,
108 struct device_attribute *attr,
109 const char *buf, size_t count)
112 struct hid_device *hdev = container_of(dev->parent,
113 struct hid_device, dev);
114 struct gt683r_led *led = hid_get_drvdata(hdev);
117 if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
120 mutex_lock(&led->lock);
123 led->mode = GT683R_LED_NORMAL;
124 else if (sysfs_mode == 1)
125 led->mode = GT683R_LED_AUDIO;
127 led->mode = GT683R_LED_BREATHING;
129 mutex_unlock(&led->lock);
130 schedule_work(&led->work);
135 static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
139 ret = hid_hw_raw_request(led->hdev, msg[0], msg, GT683R_BUFFER_SIZE,
140 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
141 if (ret != GT683R_BUFFER_SIZE) {
143 "failed to send set report request: %i\n", ret);
152 static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
157 buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
165 ret = gt683r_led_snd_msg(led, buffer);
171 static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
176 buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
185 ret = gt683r_led_snd_msg(led, buffer);
191 static void gt683r_led_work(struct work_struct *work)
196 struct gt683r_led *led = container_of(work, struct gt683r_led, work);
198 mutex_lock(&led->lock);
200 for (i = 0; i < GT683R_LED_COUNT; i++) {
201 if (led->brightnesses[i])
205 if (gt683r_leds_set(led, leds))
211 mode = GT683R_LED_OFF;
213 gt683r_mode_set(led, mode);
215 mutex_unlock(&led->lock);
218 static DEVICE_ATTR_RW(mode);
220 static struct attribute *gt683r_led_attrs[] = {
225 static const struct attribute_group gt683r_led_group = {
227 .attrs = gt683r_led_attrs,
230 static const struct attribute_group *gt683r_led_groups[] = {
235 static int gt683r_led_probe(struct hid_device *hdev,
236 const struct hid_device_id *id)
242 struct gt683r_led *led;
244 led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
248 mutex_init(&led->lock);
249 INIT_WORK(&led->work, gt683r_led_work);
251 led->mode = GT683R_LED_NORMAL;
253 hid_set_drvdata(hdev, led);
255 ret = hid_parse(hdev);
257 hid_err(hdev, "hid parsing failed\n");
261 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
263 hid_err(hdev, "hw start failed\n");
267 for (i = 0; i < GT683R_LED_COUNT; i++) {
268 name_sz = strlen(dev_name(&hdev->dev)) +
269 strlen(gt683r_panel_names[i]) + 3;
271 name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
277 snprintf(name, name_sz, "%s::%s",
278 dev_name(&hdev->dev), gt683r_panel_names[i]);
279 led->led_devs[i].name = name;
280 led->led_devs[i].max_brightness = 1;
281 led->led_devs[i].brightness_set = gt683r_brightness_set;
282 led->led_devs[i].groups = gt683r_led_groups;
284 ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
286 hid_err(hdev, "could not register led device\n");
294 for (i = i - 1; i >= 0; i--)
295 led_classdev_unregister(&led->led_devs[i]);
300 static void gt683r_led_remove(struct hid_device *hdev)
303 struct gt683r_led *led = hid_get_drvdata(hdev);
305 for (i = 0; i < GT683R_LED_COUNT; i++)
306 led_classdev_unregister(&led->led_devs[i]);
307 flush_work(&led->work);
311 static struct hid_driver gt683r_led_driver = {
312 .probe = gt683r_led_probe,
313 .remove = gt683r_led_remove,
314 .name = "gt683r_led",
315 .id_table = gt683r_led_id,
318 module_hid_driver(gt683r_led_driver);
320 MODULE_AUTHOR("Janne Kanniainen");
321 MODULE_DESCRIPTION("MSI GT683R led driver");
322 MODULE_LICENSE("GPL");