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
) },
58 static void gt683r_brightness_set(struct led_classdev
*led_cdev
,
59 enum led_brightness brightness
)
62 struct device
*dev
= led_cdev
->dev
->parent
;
63 struct hid_device
*hdev
= to_hid_device(dev
);
64 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
])
71 if (i
< GT683R_LED_COUNT
) {
72 led
->brightnesses
[i
] = brightness
;
73 schedule_work(&led
->work
);
77 static ssize_t
mode_show(struct device
*dev
,
78 struct device_attribute
*attr
,
82 struct hid_device
*hdev
= to_hid_device(dev
->parent
);
83 struct gt683r_led
*led
= hid_get_drvdata(hdev
);
85 if (led
->mode
== GT683R_LED_NORMAL
)
87 else if (led
->mode
== GT683R_LED_AUDIO
)
92 return scnprintf(buf
, PAGE_SIZE
, "%u\n", sysfs_mode
);
95 static ssize_t
mode_store(struct device
*dev
,
96 struct device_attribute
*attr
,
97 const char *buf
, size_t count
)
100 struct hid_device
*hdev
= to_hid_device(dev
->parent
);
101 struct gt683r_led
*led
= hid_get_drvdata(hdev
);
104 if (kstrtou8(buf
, 10, &sysfs_mode
) || sysfs_mode
> 2)
107 mutex_lock(&led
->lock
);
110 led
->mode
= GT683R_LED_NORMAL
;
111 else if (sysfs_mode
== 1)
112 led
->mode
= GT683R_LED_AUDIO
;
114 led
->mode
= GT683R_LED_BREATHING
;
116 mutex_unlock(&led
->lock
);
117 schedule_work(&led
->work
);
122 static int gt683r_led_snd_msg(struct gt683r_led
*led
, u8
*msg
)
126 ret
= hid_hw_raw_request(led
->hdev
, msg
[0], msg
, GT683R_BUFFER_SIZE
,
127 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
128 if (ret
!= GT683R_BUFFER_SIZE
) {
130 "failed to send set report request: %i\n", ret
);
139 static int gt683r_leds_set(struct gt683r_led
*led
, u8 leds
)
144 buffer
= kzalloc(GT683R_BUFFER_SIZE
, GFP_KERNEL
);
152 ret
= gt683r_led_snd_msg(led
, buffer
);
158 static int gt683r_mode_set(struct gt683r_led
*led
, u8 mode
)
163 buffer
= kzalloc(GT683R_BUFFER_SIZE
, GFP_KERNEL
);
172 ret
= gt683r_led_snd_msg(led
, buffer
);
178 static void gt683r_led_work(struct work_struct
*work
)
183 struct gt683r_led
*led
= container_of(work
, struct gt683r_led
, work
);
185 mutex_lock(&led
->lock
);
187 for (i
= 0; i
< GT683R_LED_COUNT
; i
++) {
188 if (led
->brightnesses
[i
])
192 if (gt683r_leds_set(led
, leds
))
198 mode
= GT683R_LED_OFF
;
200 gt683r_mode_set(led
, mode
);
202 mutex_unlock(&led
->lock
);
205 static DEVICE_ATTR_RW(mode
);
207 static struct attribute
*gt683r_led_attrs
[] = {
212 static const struct attribute_group gt683r_led_group
= {
214 .attrs
= gt683r_led_attrs
,
217 static const struct attribute_group
*gt683r_led_groups
[] = {
222 static int gt683r_led_probe(struct hid_device
*hdev
,
223 const struct hid_device_id
*id
)
229 struct gt683r_led
*led
;
231 led
= devm_kzalloc(&hdev
->dev
, sizeof(*led
), GFP_KERNEL
);
235 mutex_init(&led
->lock
);
236 INIT_WORK(&led
->work
, gt683r_led_work
);
238 led
->mode
= GT683R_LED_NORMAL
;
240 hid_set_drvdata(hdev
, led
);
242 ret
= hid_parse(hdev
);
244 hid_err(hdev
, "hid parsing failed\n");
248 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
250 hid_err(hdev
, "hw start failed\n");
254 for (i
= 0; i
< GT683R_LED_COUNT
; i
++) {
255 name_sz
= strlen(dev_name(&hdev
->dev
)) +
256 strlen(gt683r_panel_names
[i
]) + 3;
258 name
= devm_kzalloc(&hdev
->dev
, name_sz
, GFP_KERNEL
);
264 snprintf(name
, name_sz
, "%s::%s",
265 dev_name(&hdev
->dev
), gt683r_panel_names
[i
]);
266 led
->led_devs
[i
].name
= name
;
267 led
->led_devs
[i
].max_brightness
= 1;
268 led
->led_devs
[i
].brightness_set
= gt683r_brightness_set
;
269 led
->led_devs
[i
].groups
= gt683r_led_groups
;
271 ret
= led_classdev_register(&hdev
->dev
, &led
->led_devs
[i
]);
273 hid_err(hdev
, "could not register led device\n");
281 for (i
= i
- 1; i
>= 0; i
--)
282 led_classdev_unregister(&led
->led_devs
[i
]);
287 static void gt683r_led_remove(struct hid_device
*hdev
)
290 struct gt683r_led
*led
= hid_get_drvdata(hdev
);
292 for (i
= 0; i
< GT683R_LED_COUNT
; i
++)
293 led_classdev_unregister(&led
->led_devs
[i
]);
294 flush_work(&led
->work
);
298 static struct hid_driver gt683r_led_driver
= {
299 .probe
= gt683r_led_probe
,
300 .remove
= gt683r_led_remove
,
301 .name
= "gt683r_led",
302 .id_table
= gt683r_led_id
,
305 module_hid_driver(gt683r_led_driver
);
307 MODULE_AUTHOR("Janne Kanniainen");
308 MODULE_DESCRIPTION("MSI GT683R led driver");
309 MODULE_LICENSE("GPL");