fce2a03cb6471fa525d914857cebab5d38ca8f8f
[deliverable/linux.git] / drivers / hid / hid-led.c
1 /*
2 * Simple USB RGB LED driver
3 *
4 * Copyright 2016 Heiner Kallweit <hkallweit1@gmail.com>
5 * Based on drivers/hid/hid-thingm.c and
6 * drivers/usb/misc/usbled.c
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2.
11 */
12
13 #include <linux/hid.h>
14 #include <linux/hidraw.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18
19 #include "hid-ids.h"
20
21 enum hidled_report_type {
22 RAW_REQUEST,
23 OUTPUT_REPORT
24 };
25
26 enum hidled_type {
27 RISO_KAGAKU,
28 DREAM_CHEEKY,
29 THINGM,
30 };
31
32 static unsigned const char riso_kagaku_tbl[] = {
33 /* R+2G+4B -> riso kagaku color index */
34 [0] = 0, /* black */
35 [1] = 2, /* red */
36 [2] = 1, /* green */
37 [3] = 5, /* yellow */
38 [4] = 3, /* blue */
39 [5] = 6, /* magenta */
40 [6] = 4, /* cyan */
41 [7] = 7 /* white */
42 };
43
44 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
45
46 struct hidled_device;
47 struct hidled_rgb;
48
49 struct hidled_config {
50 enum hidled_type type;
51 const char *name;
52 const char *short_name;
53 enum led_brightness max_brightness;
54 int num_leds;
55 size_t report_size;
56 enum hidled_report_type report_type;
57 int (*init)(struct hidled_device *ldev);
58 int (*write)(struct led_classdev *cdev, enum led_brightness br);
59 };
60
61 struct hidled_led {
62 struct led_classdev cdev;
63 struct hidled_rgb *rgb;
64 char name[32];
65 };
66
67 struct hidled_rgb {
68 struct hidled_device *ldev;
69 struct hidled_led red;
70 struct hidled_led green;
71 struct hidled_led blue;
72 u8 num;
73 };
74
75 struct hidled_device {
76 const struct hidled_config *config;
77 struct hid_device *hdev;
78 struct hidled_rgb *rgb;
79 struct mutex lock;
80 };
81
82 #define MAX_REPORT_SIZE 16
83
84 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
85
86 static bool riso_kagaku_switch_green_blue;
87 module_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(riso_kagaku_switch_green_blue,
89 "switch green and blue RGB component for Riso Kagaku devices");
90
91 static int hidled_send(struct hidled_device *ldev, __u8 *buf)
92 {
93 int ret;
94
95 mutex_lock(&ldev->lock);
96
97 if (ldev->config->report_type == RAW_REQUEST)
98 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
99 ldev->config->report_size,
100 HID_FEATURE_REPORT,
101 HID_REQ_SET_REPORT);
102 else if (ldev->config->report_type == OUTPUT_REPORT)
103 ret = hid_hw_output_report(ldev->hdev, buf,
104 ldev->config->report_size);
105 else
106 ret = -EINVAL;
107
108 mutex_unlock(&ldev->lock);
109
110 if (ret < 0)
111 return ret;
112
113 return ret == ldev->config->report_size ? 0 : -EMSGSIZE;
114 }
115
116 /* reading data is supported for report type RAW_REQUEST only */
117 static int hidled_recv(struct hidled_device *ldev, __u8 *buf)
118 {
119 int ret;
120
121 if (ldev->config->report_type != RAW_REQUEST)
122 return -EINVAL;
123
124 mutex_lock(&ldev->lock);
125
126 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
127 ldev->config->report_size,
128 HID_FEATURE_REPORT,
129 HID_REQ_SET_REPORT);
130 if (ret < 0)
131 goto err;
132
133 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
134 ldev->config->report_size,
135 HID_FEATURE_REPORT,
136 HID_REQ_GET_REPORT);
137 err:
138 mutex_unlock(&ldev->lock);
139
140 return ret < 0 ? ret : 0;
141 }
142
143 static u8 riso_kagaku_index(struct hidled_rgb *rgb)
144 {
145 enum led_brightness r, g, b;
146
147 r = rgb->red.cdev.brightness;
148 g = rgb->green.cdev.brightness;
149 b = rgb->blue.cdev.brightness;
150
151 if (riso_kagaku_switch_green_blue)
152 return RISO_KAGAKU_IX(r, b, g);
153 else
154 return RISO_KAGAKU_IX(r, g, b);
155 }
156
157 static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br)
158 {
159 struct hidled_led *led = to_hidled_led(cdev);
160 struct hidled_rgb *rgb = led->rgb;
161 __u8 buf[MAX_REPORT_SIZE] = {};
162
163 buf[1] = riso_kagaku_index(rgb);
164
165 return hidled_send(rgb->ldev, buf);
166 }
167
168 static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br)
169 {
170 struct hidled_led *led = to_hidled_led(cdev);
171 struct hidled_rgb *rgb = led->rgb;
172 __u8 buf[MAX_REPORT_SIZE] = {};
173
174 buf[1] = rgb->red.cdev.brightness;
175 buf[2] = rgb->green.cdev.brightness;
176 buf[3] = rgb->blue.cdev.brightness;
177 buf[7] = 0x1a;
178 buf[8] = 0x05;
179
180 return hidled_send(rgb->ldev, buf);
181 }
182
183 static int dream_cheeky_init(struct hidled_device *ldev)
184 {
185 __u8 buf[MAX_REPORT_SIZE] = {};
186
187 /* Dream Cheeky magic */
188 buf[1] = 0x1f;
189 buf[2] = 0x02;
190 buf[4] = 0x5f;
191 buf[7] = 0x1a;
192 buf[8] = 0x03;
193
194 return hidled_send(ldev, buf);
195 }
196
197 static int _thingm_write(struct led_classdev *cdev, enum led_brightness br,
198 u8 offset)
199 {
200 struct hidled_led *led = to_hidled_led(cdev);
201 __u8 buf[MAX_REPORT_SIZE] = { 1, 'c' };
202
203 buf[2] = led->rgb->red.cdev.brightness;
204 buf[3] = led->rgb->green.cdev.brightness;
205 buf[4] = led->rgb->blue.cdev.brightness;
206 buf[7] = led->rgb->num + offset;
207
208 return hidled_send(led->rgb->ldev, buf);
209 }
210
211 static int thingm_write_v1(struct led_classdev *cdev, enum led_brightness br)
212 {
213 return _thingm_write(cdev, br, 0);
214 }
215
216 static int thingm_write(struct led_classdev *cdev, enum led_brightness br)
217 {
218 return _thingm_write(cdev, br, 1);
219 }
220
221 static const struct hidled_config hidled_config_thingm_v1 = {
222 .name = "ThingM blink(1) v1",
223 .short_name = "thingm",
224 .max_brightness = 255,
225 .num_leds = 1,
226 .report_size = 9,
227 .report_type = RAW_REQUEST,
228 .write = thingm_write_v1,
229 };
230
231 static int thingm_init(struct hidled_device *ldev)
232 {
233 __u8 buf[MAX_REPORT_SIZE] = { 1, 'v' };
234 int ret;
235
236 ret = hidled_recv(ldev, buf);
237 if (ret)
238 return ret;
239
240 /* Check for firmware major version 1 */
241 if (buf[3] == '1')
242 ldev->config = &hidled_config_thingm_v1;
243
244 return 0;
245 }
246
247 static const struct hidled_config hidled_configs[] = {
248 {
249 .type = RISO_KAGAKU,
250 .name = "Riso Kagaku Webmail Notifier",
251 .short_name = "riso_kagaku",
252 .max_brightness = 1,
253 .num_leds = 1,
254 .report_size = 6,
255 .report_type = OUTPUT_REPORT,
256 .write = riso_kagaku_write,
257 },
258 {
259 .type = DREAM_CHEEKY,
260 .name = "Dream Cheeky Webmail Notifier",
261 .short_name = "dream_cheeky",
262 .max_brightness = 31,
263 .num_leds = 1,
264 .report_size = 9,
265 .report_type = RAW_REQUEST,
266 .init = dream_cheeky_init,
267 .write = dream_cheeky_write,
268 },
269 {
270 .type = THINGM,
271 .name = "ThingM blink(1)",
272 .short_name = "thingm",
273 .max_brightness = 255,
274 .num_leds = 2,
275 .report_size = 9,
276 .report_type = RAW_REQUEST,
277 .init = thingm_init,
278 .write = thingm_write,
279 },
280 };
281
282 static int hidled_init_led(struct hidled_led *led, const char *color_name,
283 struct hidled_rgb *rgb, unsigned int minor)
284 {
285 const struct hidled_config *config = rgb->ldev->config;
286
287 if (config->num_leds > 1)
288 snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u",
289 config->short_name, minor, color_name, rgb->num);
290 else
291 snprintf(led->name, sizeof(led->name), "%s%u:%s",
292 config->short_name, minor, color_name);
293 led->cdev.name = led->name;
294 led->cdev.max_brightness = config->max_brightness;
295 led->cdev.brightness_set_blocking = config->write;
296 led->cdev.flags = LED_HW_PLUGGABLE;
297 led->rgb = rgb;
298
299 return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev);
300 }
301
302 static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
303 {
304 int ret;
305
306 /* Register the red diode */
307 ret = hidled_init_led(&rgb->red, "red", rgb, minor);
308 if (ret)
309 return ret;
310
311 /* Register the green diode */
312 ret = hidled_init_led(&rgb->green, "green", rgb, minor);
313 if (ret)
314 return ret;
315
316 /* Register the blue diode */
317 return hidled_init_led(&rgb->blue, "blue", rgb, minor);
318 }
319
320 static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
321 {
322 struct hidled_device *ldev;
323 unsigned int minor;
324 int ret, i;
325
326 ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
327 if (!ldev)
328 return -ENOMEM;
329
330 ret = hid_parse(hdev);
331 if (ret)
332 return ret;
333
334 ldev->hdev = hdev;
335 mutex_init(&ldev->lock);
336
337 for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++)
338 if (hidled_configs[i].type == id->driver_data)
339 ldev->config = &hidled_configs[i];
340
341 if (!ldev->config)
342 return -EINVAL;
343
344 if (ldev->config->init) {
345 ret = ldev->config->init(ldev);
346 if (ret)
347 return ret;
348 }
349
350 ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
351 sizeof(struct hidled_rgb), GFP_KERNEL);
352 if (!ldev->rgb)
353 return -ENOMEM;
354
355 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
356 if (ret)
357 return ret;
358
359 minor = ((struct hidraw *) hdev->hidraw)->minor;
360
361 for (i = 0; i < ldev->config->num_leds; i++) {
362 ldev->rgb[i].ldev = ldev;
363 ldev->rgb[i].num = i;
364 ret = hidled_init_rgb(&ldev->rgb[i], minor);
365 if (ret) {
366 hid_hw_stop(hdev);
367 return ret;
368 }
369 }
370
371 hid_info(hdev, "%s initialized\n", ldev->config->name);
372
373 return 0;
374 }
375
376 static const struct hid_device_id hidled_table[] = {
377 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU,
378 USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU },
379 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
380 USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY },
381 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
382 USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY },
383 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM,
384 USB_DEVICE_ID_BLINK1), .driver_data = THINGM },
385 { }
386 };
387 MODULE_DEVICE_TABLE(hid, hidled_table);
388
389 static struct hid_driver hidled_driver = {
390 .name = "hid-led",
391 .probe = hidled_probe,
392 .id_table = hidled_table,
393 };
394
395 module_hid_driver(hidled_driver);
396
397 MODULE_LICENSE("GPL");
398 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
399 MODULE_DESCRIPTION("Simple USB RGB LED driver");
This page took 0.047659 seconds and 4 git commands to generate.