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
{
31 static unsigned const char riso_kagaku_tbl
[] = {
32 /* R+2G+4B -> riso kagaku color index */
38 [5] = 6, /* magenta */
43 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
48 struct hidled_config
{
49 enum hidled_type type
;
51 const char *short_name
;
52 enum led_brightness max_brightness
;
55 enum hidled_report_type report_type
;
57 int (*init
)(struct hidled_device
*ldev
);
58 int (*write
)(struct led_classdev
*cdev
, enum led_brightness br
);
62 struct led_classdev cdev
;
63 struct hidled_rgb
*rgb
;
68 struct hidled_device
*ldev
;
69 struct hidled_led red
;
70 struct hidled_led green
;
71 struct hidled_led blue
;
75 struct hidled_device
{
76 const struct hidled_config
*config
;
77 struct hid_device
*hdev
;
78 struct hidled_rgb
*rgb
;
82 #define MAX_REPORT_SIZE 16
84 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
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");
91 static int hidled_send(struct hidled_device
*ldev
, __u8
*buf
)
95 buf
[0] = ldev
->config
->report_id
;
97 mutex_lock(&ldev
->lock
);
99 if (ldev
->config
->report_type
== RAW_REQUEST
)
100 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
101 ldev
->config
->report_size
,
104 else if (ldev
->config
->report_type
== OUTPUT_REPORT
)
105 ret
= hid_hw_output_report(ldev
->hdev
, buf
,
106 ldev
->config
->report_size
);
110 mutex_unlock(&ldev
->lock
);
115 return ret
== ldev
->config
->report_size
? 0 : -EMSGSIZE
;
118 static u8
riso_kagaku_index(struct hidled_rgb
*rgb
)
120 enum led_brightness r
, g
, b
;
122 r
= rgb
->red
.cdev
.brightness
;
123 g
= rgb
->green
.cdev
.brightness
;
124 b
= rgb
->blue
.cdev
.brightness
;
126 if (riso_kagaku_switch_green_blue
)
127 return RISO_KAGAKU_IX(r
, b
, g
);
129 return RISO_KAGAKU_IX(r
, g
, b
);
132 static int riso_kagaku_write(struct led_classdev
*cdev
, enum led_brightness br
)
134 struct hidled_led
*led
= to_hidled_led(cdev
);
135 struct hidled_rgb
*rgb
= led
->rgb
;
136 __u8 buf
[MAX_REPORT_SIZE
] = {};
138 buf
[1] = riso_kagaku_index(rgb
);
140 return hidled_send(rgb
->ldev
, buf
);
143 static int dream_cheeky_write(struct led_classdev
*cdev
, enum led_brightness br
)
145 struct hidled_led
*led
= to_hidled_led(cdev
);
146 struct hidled_rgb
*rgb
= led
->rgb
;
147 __u8 buf
[MAX_REPORT_SIZE
] = {};
149 buf
[1] = rgb
->red
.cdev
.brightness
;
150 buf
[2] = rgb
->green
.cdev
.brightness
;
151 buf
[3] = rgb
->blue
.cdev
.brightness
;
155 return hidled_send(rgb
->ldev
, buf
);
158 static int dream_cheeky_init(struct hidled_device
*ldev
)
160 __u8 buf
[MAX_REPORT_SIZE
] = {};
162 /* Dream Cheeky magic */
169 return hidled_send(ldev
, buf
);
172 static const struct hidled_config hidled_configs
[] = {
175 .name
= "Riso Kagaku Webmail Notifier",
176 .short_name
= "riso_kagaku",
180 .report_type
= OUTPUT_REPORT
,
182 .write
= riso_kagaku_write
,
185 .type
= DREAM_CHEEKY
,
186 .name
= "Dream Cheeky Webmail Notifier",
187 .short_name
= "dream_cheeky",
188 .max_brightness
= 31,
191 .report_type
= RAW_REQUEST
,
193 .init
= dream_cheeky_init
,
194 .write
= dream_cheeky_write
,
198 static int hidled_init_led(struct hidled_led
*led
, const char *color_name
,
199 struct hidled_rgb
*rgb
, unsigned int minor
)
201 const struct hidled_config
*config
= rgb
->ldev
->config
;
203 if (config
->num_leds
> 1)
204 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s:led%u",
205 config
->short_name
, minor
, color_name
, rgb
->num
);
207 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s",
208 config
->short_name
, minor
, color_name
);
209 led
->cdev
.name
= led
->name
;
210 led
->cdev
.max_brightness
= config
->max_brightness
;
211 led
->cdev
.brightness_set_blocking
= config
->write
;
212 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
215 return devm_led_classdev_register(&rgb
->ldev
->hdev
->dev
, &led
->cdev
);
218 static int hidled_init_rgb(struct hidled_rgb
*rgb
, unsigned int minor
)
222 /* Register the red diode */
223 ret
= hidled_init_led(&rgb
->red
, "red", rgb
, minor
);
227 /* Register the green diode */
228 ret
= hidled_init_led(&rgb
->green
, "green", rgb
, minor
);
232 /* Register the blue diode */
233 return hidled_init_led(&rgb
->blue
, "blue", rgb
, minor
);
236 static int hidled_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
238 struct hidled_device
*ldev
;
242 ldev
= devm_kzalloc(&hdev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
246 ret
= hid_parse(hdev
);
251 mutex_init(&ldev
->lock
);
253 for (i
= 0; !ldev
->config
&& i
< ARRAY_SIZE(hidled_configs
); i
++)
254 if (hidled_configs
[i
].type
== id
->driver_data
)
255 ldev
->config
= &hidled_configs
[i
];
260 if (ldev
->config
->init
) {
261 ret
= ldev
->config
->init(ldev
);
266 ldev
->rgb
= devm_kcalloc(&hdev
->dev
, ldev
->config
->num_leds
,
267 sizeof(struct hidled_rgb
), GFP_KERNEL
);
271 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
275 minor
= ((struct hidraw
*) hdev
->hidraw
)->minor
;
277 for (i
= 0; i
< ldev
->config
->num_leds
; i
++) {
278 ldev
->rgb
[i
].ldev
= ldev
;
279 ldev
->rgb
[i
].num
= i
;
280 ret
= hidled_init_rgb(&ldev
->rgb
[i
], minor
);
287 hid_info(hdev
, "%s initialized\n", ldev
->config
->name
);
292 static const struct hid_device_id hidled_table
[] = {
293 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU
,
294 USB_DEVICE_ID_RI_KA_WEBMAIL
), .driver_data
= RISO_KAGAKU
},
295 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
296 USB_DEVICE_ID_DREAM_CHEEKY_WN
), .driver_data
= DREAM_CHEEKY
},
297 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
298 USB_DEVICE_ID_DREAM_CHEEKY_FA
), .driver_data
= DREAM_CHEEKY
},
301 MODULE_DEVICE_TABLE(hid
, hidled_table
);
303 static struct hid_driver hidled_driver
= {
305 .probe
= hidled_probe
,
306 .id_table
= hidled_table
,
309 module_hid_driver(hidled_driver
);
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
313 MODULE_DESCRIPTION("Simple USB RGB LED driver");