GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / hid / hid-gt683r.c
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
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 = container_of(dev, struct 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
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 = container_of(dev->parent,
94                                         struct hid_device, dev);
95         struct gt683r_led *led = hid_get_drvdata(hdev);
96
97         if (led->mode == GT683R_LED_NORMAL)
98                 sysfs_mode = 0;
99         else if (led->mode == GT683R_LED_AUDIO)
100                 sysfs_mode = 1;
101         else
102                 sysfs_mode = 2;
103
104         return scnprintf(buf, PAGE_SIZE, "%u\n", sysfs_mode);
105 }
106
107 static ssize_t mode_store(struct device *dev,
108                                 struct device_attribute *attr,
109                                 const char *buf, size_t count)
110 {
111         u8 sysfs_mode;
112         struct hid_device *hdev = container_of(dev->parent,
113                                         struct hid_device, dev);
114         struct gt683r_led *led = hid_get_drvdata(hdev);
115
116
117         if (kstrtou8(buf, 10, &sysfs_mode) || sysfs_mode > 2)
118                 return -EINVAL;
119
120         mutex_lock(&led->lock);
121
122         if (sysfs_mode == 0)
123                 led->mode = GT683R_LED_NORMAL;
124         else if (sysfs_mode == 1)
125                 led->mode = GT683R_LED_AUDIO;
126         else
127                 led->mode = GT683R_LED_BREATHING;
128
129         mutex_unlock(&led->lock);
130         schedule_work(&led->work);
131
132         return count;
133 }
134
135 static int gt683r_led_snd_msg(struct gt683r_led *led, u8 *msg)
136 {
137         int ret;
138
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) {
142                 hid_err(led->hdev,
143                         "failed to send set report request: %i\n", ret);
144                 if (ret < 0)
145                         return ret;
146                 return -EIO;
147         }
148
149         return 0;
150 }
151
152 static int gt683r_leds_set(struct gt683r_led *led, u8 leds)
153 {
154         int ret;
155         u8 *buffer;
156
157         buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
158         if (!buffer)
159                 return -ENOMEM;
160
161         buffer[0] = 0x01;
162         buffer[1] = 0x02;
163         buffer[2] = 0x30;
164         buffer[3] = leds;
165         ret = gt683r_led_snd_msg(led, buffer);
166
167         kfree(buffer);
168         return ret;
169 }
170
171 static int gt683r_mode_set(struct gt683r_led *led, u8 mode)
172 {
173         int ret;
174         u8 *buffer;
175
176         buffer = kzalloc(GT683R_BUFFER_SIZE, GFP_KERNEL);
177         if (!buffer)
178                 return -ENOMEM;
179
180         buffer[0] = 0x01;
181         buffer[1] = 0x02;
182         buffer[2] = 0x20;
183         buffer[3] = mode;
184         buffer[4] = 0x01;
185         ret = gt683r_led_snd_msg(led, buffer);
186
187         kfree(buffer);
188         return ret;
189 }
190
191 static void gt683r_led_work(struct work_struct *work)
192 {
193         int i;
194         u8 leds = 0;
195         u8 mode;
196         struct gt683r_led *led = container_of(work, struct gt683r_led, work);
197
198         mutex_lock(&led->lock);
199
200         for (i = 0; i < GT683R_LED_COUNT; i++) {
201                 if (led->brightnesses[i])
202                         leds |= BIT(i);
203         }
204
205         if (gt683r_leds_set(led, leds))
206                 goto fail;
207
208         if (leds)
209                 mode = led->mode;
210         else
211                 mode = GT683R_LED_OFF;
212
213         gt683r_mode_set(led, mode);
214 fail:
215         mutex_unlock(&led->lock);
216 }
217
218 static DEVICE_ATTR_RW(mode);
219
220 static struct attribute *gt683r_led_attrs[] = {
221         &dev_attr_mode.attr,
222         NULL
223 };
224
225 static const struct attribute_group gt683r_led_group = {
226         .name = "gt683r",
227         .attrs = gt683r_led_attrs,
228 };
229
230 static const struct attribute_group *gt683r_led_groups[] = {
231         &gt683r_led_group,
232         NULL
233 };
234
235 static int gt683r_led_probe(struct hid_device *hdev,
236                         const struct hid_device_id *id)
237 {
238         int i;
239         int ret;
240         int name_sz;
241         char *name;
242         struct gt683r_led *led;
243
244         led = devm_kzalloc(&hdev->dev, sizeof(*led), GFP_KERNEL);
245         if (!led)
246                 return -ENOMEM;
247
248         mutex_init(&led->lock);
249         INIT_WORK(&led->work, gt683r_led_work);
250
251         led->mode = GT683R_LED_NORMAL;
252         led->hdev = hdev;
253         hid_set_drvdata(hdev, led);
254
255         ret = hid_parse(hdev);
256         if (ret) {
257                 hid_err(hdev, "hid parsing failed\n");
258                 return ret;
259         }
260
261         ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
262         if (ret) {
263                 hid_err(hdev, "hw start failed\n");
264                 return ret;
265         }
266
267         for (i = 0; i < GT683R_LED_COUNT; i++) {
268                 name_sz = strlen(dev_name(&hdev->dev)) +
269                                 strlen(gt683r_panel_names[i]) + 3;
270
271                 name = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
272                 if (!name) {
273                         ret = -ENOMEM;
274                         goto fail;
275                 }
276
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;
283
284                 ret = led_classdev_register(&hdev->dev, &led->led_devs[i]);
285                 if (ret) {
286                         hid_err(hdev, "could not register led device\n");
287                         goto fail;
288                 }
289         }
290
291         return 0;
292
293 fail:
294         for (i = i - 1; i >= 0; i--)
295                 led_classdev_unregister(&led->led_devs[i]);
296         hid_hw_stop(hdev);
297         return ret;
298 }
299
300 static void gt683r_led_remove(struct hid_device *hdev)
301 {
302         int i;
303         struct gt683r_led *led = hid_get_drvdata(hdev);
304
305         for (i = 0; i < GT683R_LED_COUNT; i++)
306                 led_classdev_unregister(&led->led_devs[i]);
307         flush_work(&led->work);
308         hid_hw_stop(hdev);
309 }
310
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,
316 };
317
318 module_hid_driver(gt683r_led_driver);
319
320 MODULE_AUTHOR("Janne Kanniainen");
321 MODULE_DESCRIPTION("MSI GT683R led driver");
322 MODULE_LICENSE("GPL");