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>
21 enum hidled_report_type {
34 static unsigned const char riso_kagaku_tbl[] = {
35 /* R+2G+4B -> riso kagaku color index */
41 [5] = 6, /* magenta */
46 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
66 #define DELCOM_GREEN_LED 0
67 #define DELCOM_RED_LED 1
68 #define DELCOM_BLUE_LED 2
73 struct hidled_config {
74 enum hidled_type type;
76 const char *short_name;
77 enum led_brightness max_brightness;
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);
86 struct led_classdev cdev;
87 struct hidled_rgb *rgb;
92 struct hidled_device *ldev;
93 struct hidled_led red;
94 struct hidled_led green;
95 struct hidled_led blue;
99 struct hidled_device {
100 const struct hidled_config *config;
101 struct hid_device *hdev;
102 struct hidled_rgb *rgb;
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)
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,
133 else if (ldev->config->report_type == OUTPUT_REPORT)
134 ret = hid_hw_output_report(ldev->hdev, ldev->buf,
135 ldev->config->report_size);
139 mutex_unlock(&ldev->lock);
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)
152 if (ldev->config->report_type != RAW_REQUEST)
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,
166 ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
167 ldev->config->report_size,
171 memcpy(buf, ldev->buf, ldev->config->report_size);
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);
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;
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 */
229 return hidled_send(ldev, buf);
232 static int _thingm_write(struct led_classdev *cdev, enum led_brightness br,
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,
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' };
271 ret = hidled_recv(ldev, buf);
275 /* Check for firmware major version 1 */
277 ldev->config = &hidled_config_thingm_v1;
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;
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);
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);
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);
326 return delcom_set_pwm(led);
329 static int delcom_init(struct hidled_device *ldev)
331 union delcom_packet dp = { .rx.cmd = 104 };
334 ret = hidled_recv(ldev, dp.data);
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[] = {
360 .name = "Riso Kagaku Webmail Notifier",
361 .short_name = "riso_kagaku",
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,
375 .report_type = RAW_REQUEST,
376 .init = dream_cheeky_init,
377 .write = dream_cheeky_write,
381 .name = "ThingM blink(1)",
382 .short_name = "thingm",
383 .max_brightness = 255,
386 .report_type = RAW_REQUEST,
388 .write = thingm_write,
392 .name = "Delcom Visual Signal Indicator G2",
393 .short_name = "delcom",
394 .max_brightness = 100,
397 .report_type = RAW_REQUEST,
399 .write = delcom_write,
403 .name = "Greynut Luxafor",
404 .short_name = "luxafor",
405 .max_brightness = 255,
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);
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;
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)
437 /* Register the red diode */
438 ret = hidled_init_led(&rgb->red, "red", rgb, minor);
442 /* Register the green diode */
443 ret = hidled_init_led(&rgb->green, "green", rgb, minor);
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;
457 ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
461 ldev->buf = devm_kmalloc(&hdev->dev, MAX_REPORT_SIZE, GFP_KERNEL);
465 ret = hid_parse(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];
479 if (ldev->config->init) {
480 ret = ldev->config->init(ldev);
485 ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
486 sizeof(struct hidled_rgb), GFP_KERNEL);
490 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
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);
506 hid_info(hdev, "%s initialized\n", ldev->config->name);
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 = {
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");