Linux 4.9.146
[linux/fpc-iii.git] / drivers / hid / hid-led.c
blobd3e1ab162f7c6ade6e2a83053ec1d719f1a08daa
1 /*
2 * Simple USB RGB LED driver
4 * Copyright 2016 Heiner Kallweit <hkallweit1@gmail.com>
5 * Based on drivers/hid/hid-thingm.c and
6 * drivers/usb/misc/usbled.c
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.
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>
19 #include "hid-ids.h"
21 enum hidled_report_type {
22 RAW_REQUEST,
23 OUTPUT_REPORT
26 enum hidled_type {
27 RISO_KAGAKU,
28 DREAM_CHEEKY,
29 THINGM,
30 DELCOM,
31 LUXAFOR,
34 static unsigned const char riso_kagaku_tbl[] = {
35 /* R+2G+4B -> riso kagaku color index */
36 [0] = 0, /* black */
37 [1] = 2, /* red */
38 [2] = 1, /* green */
39 [3] = 5, /* yellow */
40 [4] = 3, /* blue */
41 [5] = 6, /* magenta */
42 [6] = 4, /* cyan */
43 [7] = 7 /* white */
46 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
48 union delcom_packet {
49 __u8 data[8];
50 struct {
51 __u8 major_cmd;
52 __u8 minor_cmd;
53 __u8 data_lsb;
54 __u8 data_msb;
55 } tx;
56 struct {
57 __u8 cmd;
58 } rx;
59 struct {
60 __le16 family_code;
61 __le16 security_code;
62 __u8 fw_version;
63 } fw;
66 #define DELCOM_GREEN_LED 0
67 #define DELCOM_RED_LED 1
68 #define DELCOM_BLUE_LED 2
70 struct hidled_device;
71 struct hidled_rgb;
73 struct hidled_config {
74 enum hidled_type type;
75 const char *name;
76 const char *short_name;
77 enum led_brightness max_brightness;
78 int num_leds;
79 size_t report_size;
80 enum hidled_report_type report_type;
81 int (*init)(struct hidled_device *ldev);
82 int (*write)(struct led_classdev *cdev, enum led_brightness br);
85 struct hidled_led {
86 struct led_classdev cdev;
87 struct hidled_rgb *rgb;
88 char name[32];
91 struct hidled_rgb {
92 struct hidled_device *ldev;
93 struct hidled_led red;
94 struct hidled_led green;
95 struct hidled_led blue;
96 u8 num;
99 struct hidled_device {
100 const struct hidled_config *config;
101 struct hid_device *hdev;
102 struct hidled_rgb *rgb;
103 u8 *buf;
104 struct mutex lock;
107 #define MAX_REPORT_SIZE 16
109 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
111 static bool riso_kagaku_switch_green_blue;
112 module_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR);
113 MODULE_PARM_DESC(riso_kagaku_switch_green_blue,
114 "switch green and blue RGB component for Riso Kagaku devices");
116 static int hidled_send(struct hidled_device *ldev, __u8 *buf)
118 int ret;
120 mutex_lock(&ldev->lock);
123 * buffer provided to hid_hw_raw_request must not be on the stack
124 * and must not be part of a data structure
126 memcpy(ldev->buf, buf, ldev->config->report_size);
128 if (ldev->config->report_type == RAW_REQUEST)
129 ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
130 ldev->config->report_size,
131 HID_FEATURE_REPORT,
132 HID_REQ_SET_REPORT);
133 else if (ldev->config->report_type == OUTPUT_REPORT)
134 ret = hid_hw_output_report(ldev->hdev, ldev->buf,
135 ldev->config->report_size);
136 else
137 ret = -EINVAL;
139 mutex_unlock(&ldev->lock);
141 if (ret < 0)
142 return ret;
144 return ret == ldev->config->report_size ? 0 : -EMSGSIZE;
147 /* reading data is supported for report type RAW_REQUEST only */
148 static int hidled_recv(struct hidled_device *ldev, __u8 *buf)
150 int ret;
152 if (ldev->config->report_type != RAW_REQUEST)
153 return -EINVAL;
155 mutex_lock(&ldev->lock);
157 memcpy(ldev->buf, buf, ldev->config->report_size);
159 ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
160 ldev->config->report_size,
161 HID_FEATURE_REPORT,
162 HID_REQ_SET_REPORT);
163 if (ret < 0)
164 goto err;
166 ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
167 ldev->config->report_size,
168 HID_FEATURE_REPORT,
169 HID_REQ_GET_REPORT);
171 memcpy(buf, ldev->buf, ldev->config->report_size);
172 err:
173 mutex_unlock(&ldev->lock);
175 return ret < 0 ? ret : 0;
178 static u8 riso_kagaku_index(struct hidled_rgb *rgb)
180 enum led_brightness r, g, b;
182 r = rgb->red.cdev.brightness;
183 g = rgb->green.cdev.brightness;
184 b = rgb->blue.cdev.brightness;
186 if (riso_kagaku_switch_green_blue)
187 return RISO_KAGAKU_IX(r, b, g);
188 else
189 return RISO_KAGAKU_IX(r, g, b);
192 static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br)
194 struct hidled_led *led = to_hidled_led(cdev);
195 struct hidled_rgb *rgb = led->rgb;
196 __u8 buf[MAX_REPORT_SIZE] = {};
198 buf[1] = riso_kagaku_index(rgb);
200 return hidled_send(rgb->ldev, buf);
203 static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br)
205 struct hidled_led *led = to_hidled_led(cdev);
206 struct hidled_rgb *rgb = led->rgb;
207 __u8 buf[MAX_REPORT_SIZE] = {};
209 buf[1] = rgb->red.cdev.brightness;
210 buf[2] = rgb->green.cdev.brightness;
211 buf[3] = rgb->blue.cdev.brightness;
212 buf[7] = 0x1a;
213 buf[8] = 0x05;
215 return hidled_send(rgb->ldev, buf);
218 static int dream_cheeky_init(struct hidled_device *ldev)
220 __u8 buf[MAX_REPORT_SIZE] = {};
222 /* Dream Cheeky magic */
223 buf[1] = 0x1f;
224 buf[2] = 0x02;
225 buf[4] = 0x5f;
226 buf[7] = 0x1a;
227 buf[8] = 0x03;
229 return hidled_send(ldev, buf);
232 static int _thingm_write(struct led_classdev *cdev, enum led_brightness br,
233 u8 offset)
235 struct hidled_led *led = to_hidled_led(cdev);
236 __u8 buf[MAX_REPORT_SIZE] = { 1, 'c' };
238 buf[2] = led->rgb->red.cdev.brightness;
239 buf[3] = led->rgb->green.cdev.brightness;
240 buf[4] = led->rgb->blue.cdev.brightness;
241 buf[7] = led->rgb->num + offset;
243 return hidled_send(led->rgb->ldev, buf);
246 static int thingm_write_v1(struct led_classdev *cdev, enum led_brightness br)
248 return _thingm_write(cdev, br, 0);
251 static int thingm_write(struct led_classdev *cdev, enum led_brightness br)
253 return _thingm_write(cdev, br, 1);
256 static const struct hidled_config hidled_config_thingm_v1 = {
257 .name = "ThingM blink(1) v1",
258 .short_name = "thingm",
259 .max_brightness = 255,
260 .num_leds = 1,
261 .report_size = 9,
262 .report_type = RAW_REQUEST,
263 .write = thingm_write_v1,
266 static int thingm_init(struct hidled_device *ldev)
268 __u8 buf[MAX_REPORT_SIZE] = { 1, 'v' };
269 int ret;
271 ret = hidled_recv(ldev, buf);
272 if (ret)
273 return ret;
275 /* Check for firmware major version 1 */
276 if (buf[3] == '1')
277 ldev->config = &hidled_config_thingm_v1;
279 return 0;
282 static inline int delcom_get_lednum(const struct hidled_led *led)
284 if (led == &led->rgb->red)
285 return DELCOM_RED_LED;
286 else if (led == &led->rgb->green)
287 return DELCOM_GREEN_LED;
288 else
289 return DELCOM_BLUE_LED;
292 static int delcom_enable_led(struct hidled_led *led)
294 union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 12 };
296 dp.tx.data_lsb = 1 << delcom_get_lednum(led);
297 dp.tx.data_msb = 0;
299 return hidled_send(led->rgb->ldev, dp.data);
302 static int delcom_set_pwm(struct hidled_led *led)
304 union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 34 };
306 dp.tx.data_lsb = delcom_get_lednum(led);
307 dp.tx.data_msb = led->cdev.brightness;
309 return hidled_send(led->rgb->ldev, dp.data);
312 static int delcom_write(struct led_classdev *cdev, enum led_brightness br)
314 struct hidled_led *led = to_hidled_led(cdev);
315 int ret;
318 * enable LED
319 * We can't do this in the init function already because the device
320 * is internally reset later.
322 ret = delcom_enable_led(led);
323 if (ret)
324 return ret;
326 return delcom_set_pwm(led);
329 static int delcom_init(struct hidled_device *ldev)
331 union delcom_packet dp = { .rx.cmd = 104 };
332 int ret;
334 ret = hidled_recv(ldev, dp.data);
335 if (ret)
336 return ret;
338 * Several Delcom devices share the same USB VID/PID
339 * Check for family id 2 for Visual Signal Indicator
341 return le16_to_cpu(dp.fw.family_code) == 2 ? 0 : -ENODEV;
344 static int luxafor_write(struct led_classdev *cdev, enum led_brightness br)
346 struct hidled_led *led = to_hidled_led(cdev);
347 __u8 buf[MAX_REPORT_SIZE] = { [1] = 1 };
349 buf[2] = led->rgb->num + 1;
350 buf[3] = led->rgb->red.cdev.brightness;
351 buf[4] = led->rgb->green.cdev.brightness;
352 buf[5] = led->rgb->blue.cdev.brightness;
354 return hidled_send(led->rgb->ldev, buf);
357 static const struct hidled_config hidled_configs[] = {
359 .type = RISO_KAGAKU,
360 .name = "Riso Kagaku Webmail Notifier",
361 .short_name = "riso_kagaku",
362 .max_brightness = 1,
363 .num_leds = 1,
364 .report_size = 6,
365 .report_type = OUTPUT_REPORT,
366 .write = riso_kagaku_write,
369 .type = DREAM_CHEEKY,
370 .name = "Dream Cheeky Webmail Notifier",
371 .short_name = "dream_cheeky",
372 .max_brightness = 31,
373 .num_leds = 1,
374 .report_size = 9,
375 .report_type = RAW_REQUEST,
376 .init = dream_cheeky_init,
377 .write = dream_cheeky_write,
380 .type = THINGM,
381 .name = "ThingM blink(1)",
382 .short_name = "thingm",
383 .max_brightness = 255,
384 .num_leds = 2,
385 .report_size = 9,
386 .report_type = RAW_REQUEST,
387 .init = thingm_init,
388 .write = thingm_write,
391 .type = DELCOM,
392 .name = "Delcom Visual Signal Indicator G2",
393 .short_name = "delcom",
394 .max_brightness = 100,
395 .num_leds = 1,
396 .report_size = 8,
397 .report_type = RAW_REQUEST,
398 .init = delcom_init,
399 .write = delcom_write,
402 .type = LUXAFOR,
403 .name = "Greynut Luxafor",
404 .short_name = "luxafor",
405 .max_brightness = 255,
406 .num_leds = 6,
407 .report_size = 9,
408 .report_type = OUTPUT_REPORT,
409 .write = luxafor_write,
413 static int hidled_init_led(struct hidled_led *led, const char *color_name,
414 struct hidled_rgb *rgb, unsigned int minor)
416 const struct hidled_config *config = rgb->ldev->config;
418 if (config->num_leds > 1)
419 snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u",
420 config->short_name, minor, color_name, rgb->num);
421 else
422 snprintf(led->name, sizeof(led->name), "%s%u:%s",
423 config->short_name, minor, color_name);
424 led->cdev.name = led->name;
425 led->cdev.max_brightness = config->max_brightness;
426 led->cdev.brightness_set_blocking = config->write;
427 led->cdev.flags = LED_HW_PLUGGABLE;
428 led->rgb = rgb;
430 return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev);
433 static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
435 int ret;
437 /* Register the red diode */
438 ret = hidled_init_led(&rgb->red, "red", rgb, minor);
439 if (ret)
440 return ret;
442 /* Register the green diode */
443 ret = hidled_init_led(&rgb->green, "green", rgb, minor);
444 if (ret)
445 return ret;
447 /* Register the blue diode */
448 return hidled_init_led(&rgb->blue, "blue", rgb, minor);
451 static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
453 struct hidled_device *ldev;
454 unsigned int minor;
455 int ret, i;
457 ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
458 if (!ldev)
459 return -ENOMEM;
461 ldev->buf = devm_kmalloc(&hdev->dev, MAX_REPORT_SIZE, GFP_KERNEL);
462 if (!ldev->buf)
463 return -ENOMEM;
465 ret = hid_parse(hdev);
466 if (ret)
467 return ret;
469 ldev->hdev = hdev;
470 mutex_init(&ldev->lock);
472 for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++)
473 if (hidled_configs[i].type == id->driver_data)
474 ldev->config = &hidled_configs[i];
476 if (!ldev->config)
477 return -EINVAL;
479 if (ldev->config->init) {
480 ret = ldev->config->init(ldev);
481 if (ret)
482 return ret;
485 ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
486 sizeof(struct hidled_rgb), GFP_KERNEL);
487 if (!ldev->rgb)
488 return -ENOMEM;
490 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
491 if (ret)
492 return ret;
494 minor = ((struct hidraw *) hdev->hidraw)->minor;
496 for (i = 0; i < ldev->config->num_leds; i++) {
497 ldev->rgb[i].ldev = ldev;
498 ldev->rgb[i].num = i;
499 ret = hidled_init_rgb(&ldev->rgb[i], minor);
500 if (ret) {
501 hid_hw_stop(hdev);
502 return ret;
506 hid_info(hdev, "%s initialized\n", ldev->config->name);
508 return 0;
511 static const struct hid_device_id hidled_table[] = {
512 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU,
513 USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU },
514 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
515 USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY },
516 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
517 USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY },
518 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM,
519 USB_DEVICE_ID_BLINK1), .driver_data = THINGM },
520 { HID_USB_DEVICE(USB_VENDOR_ID_DELCOM,
521 USB_DEVICE_ID_DELCOM_VISUAL_IND), .driver_data = DELCOM },
522 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP,
523 USB_DEVICE_ID_LUXAFOR), .driver_data = LUXAFOR },
526 MODULE_DEVICE_TABLE(hid, hidled_table);
528 static struct hid_driver hidled_driver = {
529 .name = "hid-led",
530 .probe = hidled_probe,
531 .id_table = hidled_table,
534 module_hid_driver(hidled_driver);
536 MODULE_LICENSE("GPL");
537 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
538 MODULE_DESCRIPTION("Simple USB RGB LED driver");