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
;
106 #define MAX_REPORT_SIZE 16
108 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
110 static bool riso_kagaku_switch_green_blue
;
111 module_param(riso_kagaku_switch_green_blue
, bool, S_IRUGO
| S_IWUSR
);
112 MODULE_PARM_DESC(riso_kagaku_switch_green_blue
,
113 "switch green and blue RGB component for Riso Kagaku devices");
115 static int hidled_send(struct hidled_device
*ldev
, __u8
*buf
)
119 mutex_lock(&ldev
->lock
);
121 if (ldev
->config
->report_type
== RAW_REQUEST
)
122 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
123 ldev
->config
->report_size
,
126 else if (ldev
->config
->report_type
== OUTPUT_REPORT
)
127 ret
= hid_hw_output_report(ldev
->hdev
, buf
,
128 ldev
->config
->report_size
);
132 mutex_unlock(&ldev
->lock
);
137 return ret
== ldev
->config
->report_size
? 0 : -EMSGSIZE
;
140 /* reading data is supported for report type RAW_REQUEST only */
141 static int hidled_recv(struct hidled_device
*ldev
, __u8
*buf
)
145 if (ldev
->config
->report_type
!= RAW_REQUEST
)
148 mutex_lock(&ldev
->lock
);
150 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
151 ldev
->config
->report_size
,
157 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
158 ldev
->config
->report_size
,
162 mutex_unlock(&ldev
->lock
);
164 return ret
< 0 ? ret
: 0;
167 static u8
riso_kagaku_index(struct hidled_rgb
*rgb
)
169 enum led_brightness r
, g
, b
;
171 r
= rgb
->red
.cdev
.brightness
;
172 g
= rgb
->green
.cdev
.brightness
;
173 b
= rgb
->blue
.cdev
.brightness
;
175 if (riso_kagaku_switch_green_blue
)
176 return RISO_KAGAKU_IX(r
, b
, g
);
178 return RISO_KAGAKU_IX(r
, g
, b
);
181 static int riso_kagaku_write(struct led_classdev
*cdev
, enum led_brightness br
)
183 struct hidled_led
*led
= to_hidled_led(cdev
);
184 struct hidled_rgb
*rgb
= led
->rgb
;
185 __u8 buf
[MAX_REPORT_SIZE
] = {};
187 buf
[1] = riso_kagaku_index(rgb
);
189 return hidled_send(rgb
->ldev
, buf
);
192 static int dream_cheeky_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] = rgb
->red
.cdev
.brightness
;
199 buf
[2] = rgb
->green
.cdev
.brightness
;
200 buf
[3] = rgb
->blue
.cdev
.brightness
;
204 return hidled_send(rgb
->ldev
, buf
);
207 static int dream_cheeky_init(struct hidled_device
*ldev
)
209 __u8 buf
[MAX_REPORT_SIZE
] = {};
211 /* Dream Cheeky magic */
218 return hidled_send(ldev
, buf
);
221 static int _thingm_write(struct led_classdev
*cdev
, enum led_brightness br
,
224 struct hidled_led
*led
= to_hidled_led(cdev
);
225 __u8 buf
[MAX_REPORT_SIZE
] = { 1, 'c' };
227 buf
[2] = led
->rgb
->red
.cdev
.brightness
;
228 buf
[3] = led
->rgb
->green
.cdev
.brightness
;
229 buf
[4] = led
->rgb
->blue
.cdev
.brightness
;
230 buf
[7] = led
->rgb
->num
+ offset
;
232 return hidled_send(led
->rgb
->ldev
, buf
);
235 static int thingm_write_v1(struct led_classdev
*cdev
, enum led_brightness br
)
237 return _thingm_write(cdev
, br
, 0);
240 static int thingm_write(struct led_classdev
*cdev
, enum led_brightness br
)
242 return _thingm_write(cdev
, br
, 1);
245 static const struct hidled_config hidled_config_thingm_v1
= {
246 .name
= "ThingM blink(1) v1",
247 .short_name
= "thingm",
248 .max_brightness
= 255,
251 .report_type
= RAW_REQUEST
,
252 .write
= thingm_write_v1
,
255 static int thingm_init(struct hidled_device
*ldev
)
257 __u8 buf
[MAX_REPORT_SIZE
] = { 1, 'v' };
260 ret
= hidled_recv(ldev
, buf
);
264 /* Check for firmware major version 1 */
266 ldev
->config
= &hidled_config_thingm_v1
;
271 static inline int delcom_get_lednum(const struct hidled_led
*led
)
273 if (led
== &led
->rgb
->red
)
274 return DELCOM_RED_LED
;
275 else if (led
== &led
->rgb
->green
)
276 return DELCOM_GREEN_LED
;
278 return DELCOM_BLUE_LED
;
281 static int delcom_enable_led(struct hidled_led
*led
)
283 union delcom_packet dp
= { .tx
.major_cmd
= 101, .tx
.minor_cmd
= 12 };
285 dp
.tx
.data_lsb
= 1 << delcom_get_lednum(led
);
288 return hidled_send(led
->rgb
->ldev
, dp
.data
);
291 static int delcom_set_pwm(struct hidled_led
*led
)
293 union delcom_packet dp
= { .tx
.major_cmd
= 101, .tx
.minor_cmd
= 34 };
295 dp
.tx
.data_lsb
= delcom_get_lednum(led
);
296 dp
.tx
.data_msb
= led
->cdev
.brightness
;
298 return hidled_send(led
->rgb
->ldev
, dp
.data
);
301 static int delcom_write(struct led_classdev
*cdev
, enum led_brightness br
)
303 struct hidled_led
*led
= to_hidled_led(cdev
);
308 * We can't do this in the init function already because the device
309 * is internally reset later.
311 ret
= delcom_enable_led(led
);
315 return delcom_set_pwm(led
);
318 static int delcom_init(struct hidled_device
*ldev
)
320 union delcom_packet dp
= { .rx
.cmd
= 104 };
323 ret
= hidled_recv(ldev
, dp
.data
);
327 * Several Delcom devices share the same USB VID/PID
328 * Check for family id 2 for Visual Signal Indicator
330 return le16_to_cpu(dp
.fw
.family_code
) == 2 ? 0 : -ENODEV
;
333 static int luxafor_write(struct led_classdev
*cdev
, enum led_brightness br
)
335 struct hidled_led
*led
= to_hidled_led(cdev
);
336 __u8 buf
[MAX_REPORT_SIZE
] = { [1] = 1 };
338 buf
[2] = led
->rgb
->num
+ 1;
339 buf
[3] = led
->rgb
->red
.cdev
.brightness
;
340 buf
[4] = led
->rgb
->green
.cdev
.brightness
;
341 buf
[5] = led
->rgb
->blue
.cdev
.brightness
;
343 return hidled_send(led
->rgb
->ldev
, buf
);
346 static const struct hidled_config hidled_configs
[] = {
349 .name
= "Riso Kagaku Webmail Notifier",
350 .short_name
= "riso_kagaku",
354 .report_type
= OUTPUT_REPORT
,
355 .write
= riso_kagaku_write
,
358 .type
= DREAM_CHEEKY
,
359 .name
= "Dream Cheeky Webmail Notifier",
360 .short_name
= "dream_cheeky",
361 .max_brightness
= 31,
364 .report_type
= RAW_REQUEST
,
365 .init
= dream_cheeky_init
,
366 .write
= dream_cheeky_write
,
370 .name
= "ThingM blink(1)",
371 .short_name
= "thingm",
372 .max_brightness
= 255,
375 .report_type
= RAW_REQUEST
,
377 .write
= thingm_write
,
381 .name
= "Delcom Visual Signal Indicator G2",
382 .short_name
= "delcom",
383 .max_brightness
= 100,
386 .report_type
= RAW_REQUEST
,
388 .write
= delcom_write
,
392 .name
= "Greynut Luxafor",
393 .short_name
= "luxafor",
394 .max_brightness
= 255,
397 .report_type
= OUTPUT_REPORT
,
398 .write
= luxafor_write
,
402 static int hidled_init_led(struct hidled_led
*led
, const char *color_name
,
403 struct hidled_rgb
*rgb
, unsigned int minor
)
405 const struct hidled_config
*config
= rgb
->ldev
->config
;
407 if (config
->num_leds
> 1)
408 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s:led%u",
409 config
->short_name
, minor
, color_name
, rgb
->num
);
411 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s",
412 config
->short_name
, minor
, color_name
);
413 led
->cdev
.name
= led
->name
;
414 led
->cdev
.max_brightness
= config
->max_brightness
;
415 led
->cdev
.brightness_set_blocking
= config
->write
;
416 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
419 return devm_led_classdev_register(&rgb
->ldev
->hdev
->dev
, &led
->cdev
);
422 static int hidled_init_rgb(struct hidled_rgb
*rgb
, unsigned int minor
)
426 /* Register the red diode */
427 ret
= hidled_init_led(&rgb
->red
, "red", rgb
, minor
);
431 /* Register the green diode */
432 ret
= hidled_init_led(&rgb
->green
, "green", rgb
, minor
);
436 /* Register the blue diode */
437 return hidled_init_led(&rgb
->blue
, "blue", rgb
, minor
);
440 static int hidled_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
442 struct hidled_device
*ldev
;
446 ldev
= devm_kzalloc(&hdev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
450 ret
= hid_parse(hdev
);
455 mutex_init(&ldev
->lock
);
457 for (i
= 0; !ldev
->config
&& i
< ARRAY_SIZE(hidled_configs
); i
++)
458 if (hidled_configs
[i
].type
== id
->driver_data
)
459 ldev
->config
= &hidled_configs
[i
];
464 if (ldev
->config
->init
) {
465 ret
= ldev
->config
->init(ldev
);
470 ldev
->rgb
= devm_kcalloc(&hdev
->dev
, ldev
->config
->num_leds
,
471 sizeof(struct hidled_rgb
), GFP_KERNEL
);
475 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
479 minor
= ((struct hidraw
*) hdev
->hidraw
)->minor
;
481 for (i
= 0; i
< ldev
->config
->num_leds
; i
++) {
482 ldev
->rgb
[i
].ldev
= ldev
;
483 ldev
->rgb
[i
].num
= i
;
484 ret
= hidled_init_rgb(&ldev
->rgb
[i
], minor
);
491 hid_info(hdev
, "%s initialized\n", ldev
->config
->name
);
496 static const struct hid_device_id hidled_table
[] = {
497 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU
,
498 USB_DEVICE_ID_RI_KA_WEBMAIL
), .driver_data
= RISO_KAGAKU
},
499 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
500 USB_DEVICE_ID_DREAM_CHEEKY_WN
), .driver_data
= DREAM_CHEEKY
},
501 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
502 USB_DEVICE_ID_DREAM_CHEEKY_FA
), .driver_data
= DREAM_CHEEKY
},
503 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM
,
504 USB_DEVICE_ID_BLINK1
), .driver_data
= THINGM
},
505 { HID_USB_DEVICE(USB_VENDOR_ID_DELCOM
,
506 USB_DEVICE_ID_DELCOM_VISUAL_IND
), .driver_data
= DELCOM
},
507 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
,
508 USB_DEVICE_ID_LUXAFOR
), .driver_data
= LUXAFOR
},
511 MODULE_DEVICE_TABLE(hid
, hidled_table
);
513 static struct hid_driver hidled_driver
= {
515 .probe
= hidled_probe
,
516 .id_table
= hidled_table
,
519 module_hid_driver(hidled_driver
);
521 MODULE_LICENSE("GPL");
522 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
523 MODULE_DESCRIPTION("Simple USB RGB LED driver");