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 
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>
23 
24 #include "hid-ids.h"
25 
26 #define GT683R_BUFFER_SIZE			8
27 
28 /*
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
33  */
34 enum gt683r_led_mode {
35 	GT683R_LED_OFF = 0,
36 	GT683R_LED_AUDIO = 2,
37 	GT683R_LED_BREATHING = 3,
38 	GT683R_LED_NORMAL = 5
39 };
40 
41 enum gt683r_panels {
42 	GT683R_LED_BACK = 0,
43 	GT683R_LED_SIDE = 1,
44 	GT683R_LED_FRONT = 2,
45 	GT683R_LED_COUNT,
46 };
47 
48 static const char * const gt683r_panel_names[] = {
49 	"back",
50 	"side",
51 	"front",
52 };
53 
54 struct gt683r_led {
55 	struct hid_device *hdev;
56 	struct led_classdev led_devs[GT683R_LED_COUNT];
57 	struct mutex lock;
58 	struct work_struct work;
59 	enum led_brightness brightnesses[GT683R_LED_COUNT];
60 	enum gt683r_led_mode mode;
61 };
62 
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) },
65 	{ }
66 };
67 MODULE_DEVICE_TABLE(hid, gt683r_led_id);
68 
gt683r_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)69 static void gt683r_brightness_set(struct led_classdev *led_cdev,
70 				enum led_brightness brightness)
71 {
72 	int i;
73 	struct device *dev = led_cdev->dev->parent;
74 	struct hid_device *hdev = to_hid_device(dev);
75 	struct gt683r_led *led = hid_get_drvdata(hdev);
76 
77 	for (i = 0; i < GT683R_LED_COUNT; i++) {
78 		if (led_cdev == &led->led_devs[i])
79 			break;
80 	}
81 
82 	if (i < GT683R_LED_COUNT) {
83 		led->brightnesses[i] = brightness;
84 		schedule_work(&led->work);
85 	}
86 }
87 
mode_show(struct device * dev,struct device_attribute * attr,char * buf)88 static ssize_t mode_show(struct device *dev,
89 				struct device_attribute *attr,
90 				char *buf)
91 {
92 	u8 sysfs_mode;
93 	struct hid_device *hdev = to_hid_device(dev->parent);
94 	struct gt683r_led *led = hid_get_drvdata(hdev);
95 
96 	if (led->mode == GT683R_LED_NORMAL)
97 		sysfs_mode = 0;
98 	else if (led->mode == GT683R_LED_AUDIO)
99 		sysfs_mode = 1;
100 	else
101 		sysfs_mode = 2;
102 
103 	return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
104 }
105 
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)106 static ssize_t mode_store(struct device *dev,
107 				struct device_attribute *attr,
108 				const char *buf, size_t count)
109 {
110 	u8 sysfs_mode;
111 	struct hid_device *hdev = to_hid_device(dev->parent);
112 	struct gt683r_led *led = hid_get_drvdata(hdev);
113 
114 
115 	if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
116 		return -EINVAL;
117 
118 	mutex_lock(&led->lock);
119 
120 	if (sysfs_mode == 0)
121 		led->mode = GT683R_LED_NORMAL;
122 	else if (sysfs_mode == 1)
123 		led->mode = GT683R_LED_AUDIO;
124 	else
125 		led->mode = GT683R_LED_BREATHING;
126 
127 	mutex_unlock(&led->lock);
128 	schedule_work(&led->work);
129 
130 	return count;
131 }
132 
gt683r_led_snd_msg(struct gt683r_led * led,u8 * msg)133 static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
134 {
135 	int ret;
136 
137 	ret = hid_hw_raw_request(led->hdev, msg[0], msg, GT683R_BUFFER_SIZE,
138 				HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
139 	if (ret != GT683R_BUFFER_SIZE) {
140 		hid_err(led->hdev,
141 			"failed to send set report request: %i\n", ret);
142 		if (ret < 0)
143 			return ret;
144 		return -EIO;
145 	}
146 
147 	return 0;
148 }
149 
gt683r_leds_set(struct gt683r_led * led,u8 leds)150 static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
151 {
152 	int ret;
153 	u8 *buffer;
154 
155 	buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
156 	if (!buffer)
157 		return -ENOMEM;
158 
159 	buffer[0] = 0x01;
160 	buffer[1] = 0x02;
161 	buffer[2] = 0x30;
162 	buffer[3] = leds;
163 	ret = gt683r_led_snd_msg(led, buffer);
164 
165 	kfree(buffer);
166 	return ret;
167 }
168 
gt683r_mode_set(struct gt683r_led * led,u8 mode)169 static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
170 {
171 	int ret;
172 	u8 *buffer;
173 
174 	buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
175 	if (!buffer)
176 		return -ENOMEM;
177 
178 	buffer[0] = 0x01;
179 	buffer[1] = 0x02;
180 	buffer[2] = 0x20;
181 	buffer[3] = mode;
182 	buffer[4] = 0x01;
183 	ret = gt683r_led_snd_msg(led, buffer);
184 
185 	kfree(buffer);
186 	return ret;
187 }
188 
gt683r_led_work(struct work_struct * work)189 static void gt683r_led_work(struct work_struct *work)
190 {
191 	int i;
192 	u8 leds = 0;
193 	u8 mode;
194 	struct gt683r_led *led = container_of(work, struct gt683r_led, work);
195 
196 	mutex_lock(&led->lock);
197 
198 	for (i = 0; i < GT683R_LED_COUNT; i++) {
199 		if (led->brightnesses[i])
200 			leds |= BIT(i);
201 	}
202 
203 	if (gt683r_leds_set(led, leds))
204 		goto fail;
205 
206 	if (leds)
207 		mode = led->mode;
208 	else
209 		mode = GT683R_LED_OFF;
210 
211 	gt683r_mode_set(led, mode);
212 fail:
213 	mutex_unlock(&led->lock);
214 }
215 
216 static DEVICE_ATTR_RW(mode);
217 
218 static struct attribute *gt683r_led_attrs[] = {
219 	&dev_attr_mode.attr,
220 	NULL
221 };
222 
223 static const struct attribute_group gt683r_led_group = {
224 	.name = "gt683r",
225 	.attrs = gt683r_led_attrs,
226 };
227 
228 static const struct attribute_group *gt683r_led_groups[] = {
229 	&gt683r_led_group,
230 	NULL
231 };
232 
gt683r_led_probe(struct hid_device * hdev,const struct hid_device_id * id)233 static int gt683r_led_probe(struct hid_device *hdev,
234 			const struct hid_device_id *id)
235 {
236 	int i;
237 	int ret;
238 	int name_sz;
239 	char *name;
240 	struct gt683r_led *led;
241 
242 	led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
243 	if (!led)
244 		return -ENOMEM;
245 
246 	mutex_init(&led->lock);
247 	INIT_WORK(&led->work, gt683r_led_work);
248 
249 	led->mode = GT683R_LED_NORMAL;
250 	led->hdev = hdev;
251 	hid_set_drvdata(hdev, led);
252 
253 	ret = hid_parse(hdev);
254 	if (ret) {
255 		hid_err(hdev, "hid parsing failed\n");
256 		return ret;
257 	}
258 
259 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
260 	if (ret) {
261 		hid_err(hdev, "hw start failed\n");
262 		return ret;
263 	}
264 
265 	for (i = 0; i < GT683R_LED_COUNT; i++) {
266 		name_sz = strlen(dev_name(&hdev->dev)) +
267 				strlen(gt683r_panel_names[i]) + 3;
268 
269 		name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
270 		if (!name) {
271 			ret = -ENOMEM;
272 			goto fail;
273 		}
274 
275 		snprintf(name, name_sz, "%s::%s",
276 				dev_name(&hdev->dev), gt683r_panel_names[i]);
277 		led->led_devs[i].name = name;
278 		led->led_devs[i].max_brightness = 1;
279 		led->led_devs[i].brightness_set = gt683r_brightness_set;
280 		led->led_devs[i].groups = gt683r_led_groups;
281 
282 		ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
283 		if (ret) {
284 			hid_err(hdev, "could not register led device\n");
285 			goto fail;
286 		}
287 	}
288 
289 	return 0;
290 
291 fail:
292 	for (i = i - 1; i >= 0; i--)
293 		led_classdev_unregister(&led->led_devs[i]);
294 	hid_hw_stop(hdev);
295 	return ret;
296 }
297 
gt683r_led_remove(struct hid_device * hdev)298 static void gt683r_led_remove(struct hid_device *hdev)
299 {
300 	int i;
301 	struct gt683r_led *led = hid_get_drvdata(hdev);
302 
303 	for (i = 0; i < GT683R_LED_COUNT; i++)
304 		led_classdev_unregister(&led->led_devs[i]);
305 	flush_work(&led->work);
306 	hid_hw_stop(hdev);
307 }
308 
309 static struct hid_driver gt683r_led_driver = {
310 	.probe = gt683r_led_probe,
311 	.remove = gt683r_led_remove,
312 	.name = "gt683r_led",
313 	.id_table = gt683r_led_id,
314 };
315 
316 module_hid_driver(gt683r_led_driver);
317 
318 MODULE_AUTHOR("Janne Kanniainen");
319 MODULE_DESCRIPTION("MSI GT683R led driver");
320 MODULE_LICENSE("GPL");
321