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
{
32 static unsigned const char riso_kagaku_tbl
[] = {
33 /* R+2G+4B -> riso kagaku color index */
39 [5] = 6, /* magenta */
44 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
49 struct hidled_config
{
50 enum hidled_type type
;
52 const char *short_name
;
53 enum led_brightness max_brightness
;
56 enum hidled_report_type report_type
;
58 int (*init
)(struct hidled_device
*ldev
);
59 int (*write
)(struct led_classdev
*cdev
, enum led_brightness br
);
63 struct led_classdev cdev
;
64 struct hidled_rgb
*rgb
;
69 struct hidled_device
*ldev
;
70 struct hidled_led red
;
71 struct hidled_led green
;
72 struct hidled_led blue
;
76 struct hidled_device
{
77 const struct hidled_config
*config
;
78 struct hid_device
*hdev
;
79 struct hidled_rgb
*rgb
;
83 #define MAX_REPORT_SIZE 16
85 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
87 static bool riso_kagaku_switch_green_blue
;
88 module_param(riso_kagaku_switch_green_blue
, bool, S_IRUGO
| S_IWUSR
);
89 MODULE_PARM_DESC(riso_kagaku_switch_green_blue
,
90 "switch green and blue RGB component for Riso Kagaku devices");
92 static int hidled_send(struct hidled_device
*ldev
, __u8
*buf
)
96 buf
[0] = ldev
->config
->report_id
;
98 mutex_lock(&ldev
->lock
);
100 if (ldev
->config
->report_type
== RAW_REQUEST
)
101 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
102 ldev
->config
->report_size
,
105 else if (ldev
->config
->report_type
== OUTPUT_REPORT
)
106 ret
= hid_hw_output_report(ldev
->hdev
, buf
,
107 ldev
->config
->report_size
);
111 mutex_unlock(&ldev
->lock
);
116 return ret
== ldev
->config
->report_size
? 0 : -EMSGSIZE
;
119 /* reading data is supported for report type RAW_REQUEST only */
120 static int hidled_recv(struct hidled_device
*ldev
, __u8
*buf
)
124 if (ldev
->config
->report_type
!= RAW_REQUEST
)
127 buf
[0] = ldev
->config
->report_id
;
129 mutex_lock(&ldev
->lock
);
131 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
132 ldev
->config
->report_size
,
138 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
139 ldev
->config
->report_size
,
143 mutex_unlock(&ldev
->lock
);
145 return ret
< 0 ? ret
: 0;
148 static u8
riso_kagaku_index(struct hidled_rgb
*rgb
)
150 enum led_brightness r
, g
, b
;
152 r
= rgb
->red
.cdev
.brightness
;
153 g
= rgb
->green
.cdev
.brightness
;
154 b
= rgb
->blue
.cdev
.brightness
;
156 if (riso_kagaku_switch_green_blue
)
157 return RISO_KAGAKU_IX(r
, b
, g
);
159 return RISO_KAGAKU_IX(r
, g
, b
);
162 static int riso_kagaku_write(struct led_classdev
*cdev
, enum led_brightness br
)
164 struct hidled_led
*led
= to_hidled_led(cdev
);
165 struct hidled_rgb
*rgb
= led
->rgb
;
166 __u8 buf
[MAX_REPORT_SIZE
] = {};
168 buf
[1] = riso_kagaku_index(rgb
);
170 return hidled_send(rgb
->ldev
, buf
);
173 static int dream_cheeky_write(struct led_classdev
*cdev
, enum led_brightness br
)
175 struct hidled_led
*led
= to_hidled_led(cdev
);
176 struct hidled_rgb
*rgb
= led
->rgb
;
177 __u8 buf
[MAX_REPORT_SIZE
] = {};
179 buf
[1] = rgb
->red
.cdev
.brightness
;
180 buf
[2] = rgb
->green
.cdev
.brightness
;
181 buf
[3] = rgb
->blue
.cdev
.brightness
;
185 return hidled_send(rgb
->ldev
, buf
);
188 static int dream_cheeky_init(struct hidled_device
*ldev
)
190 __u8 buf
[MAX_REPORT_SIZE
] = {};
192 /* Dream Cheeky magic */
199 return hidled_send(ldev
, buf
);
202 static int _thingm_write(struct led_classdev
*cdev
, enum led_brightness br
,
205 struct hidled_led
*led
= to_hidled_led(cdev
);
206 __u8 buf
[MAX_REPORT_SIZE
] = { [1] = 'c' };
208 buf
[2] = led
->rgb
->red
.cdev
.brightness
;
209 buf
[3] = led
->rgb
->green
.cdev
.brightness
;
210 buf
[4] = led
->rgb
->blue
.cdev
.brightness
;
211 buf
[7] = led
->rgb
->num
+ offset
;
213 return hidled_send(led
->rgb
->ldev
, buf
);
216 static int thingm_write_v1(struct led_classdev
*cdev
, enum led_brightness br
)
218 return _thingm_write(cdev
, br
, 0);
221 static int thingm_write(struct led_classdev
*cdev
, enum led_brightness br
)
223 return _thingm_write(cdev
, br
, 1);
226 static const struct hidled_config hidled_config_thingm_v1
= {
227 .name
= "ThingM blink(1) v1",
228 .short_name
= "thingm",
229 .max_brightness
= 255,
232 .report_type
= RAW_REQUEST
,
234 .write
= thingm_write_v1
,
237 static int thingm_init(struct hidled_device
*ldev
)
239 __u8 buf
[MAX_REPORT_SIZE
] = { [1] = 'v' };
242 ret
= hidled_recv(ldev
, buf
);
246 /* Check for firmware major version 1 */
248 ldev
->config
= &hidled_config_thingm_v1
;
253 static const struct hidled_config hidled_configs
[] = {
256 .name
= "Riso Kagaku Webmail Notifier",
257 .short_name
= "riso_kagaku",
261 .report_type
= OUTPUT_REPORT
,
263 .write
= riso_kagaku_write
,
266 .type
= DREAM_CHEEKY
,
267 .name
= "Dream Cheeky Webmail Notifier",
268 .short_name
= "dream_cheeky",
269 .max_brightness
= 31,
272 .report_type
= RAW_REQUEST
,
274 .init
= dream_cheeky_init
,
275 .write
= dream_cheeky_write
,
279 .name
= "ThingM blink(1)",
280 .short_name
= "thingm",
281 .max_brightness
= 255,
284 .report_type
= RAW_REQUEST
,
287 .write
= thingm_write
,
291 static int hidled_init_led(struct hidled_led
*led
, const char *color_name
,
292 struct hidled_rgb
*rgb
, unsigned int minor
)
294 const struct hidled_config
*config
= rgb
->ldev
->config
;
296 if (config
->num_leds
> 1)
297 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s:led%u",
298 config
->short_name
, minor
, color_name
, rgb
->num
);
300 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s",
301 config
->short_name
, minor
, color_name
);
302 led
->cdev
.name
= led
->name
;
303 led
->cdev
.max_brightness
= config
->max_brightness
;
304 led
->cdev
.brightness_set_blocking
= config
->write
;
305 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
308 return devm_led_classdev_register(&rgb
->ldev
->hdev
->dev
, &led
->cdev
);
311 static int hidled_init_rgb(struct hidled_rgb
*rgb
, unsigned int minor
)
315 /* Register the red diode */
316 ret
= hidled_init_led(&rgb
->red
, "red", rgb
, minor
);
320 /* Register the green diode */
321 ret
= hidled_init_led(&rgb
->green
, "green", rgb
, minor
);
325 /* Register the blue diode */
326 return hidled_init_led(&rgb
->blue
, "blue", rgb
, minor
);
329 static int hidled_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
331 struct hidled_device
*ldev
;
335 ldev
= devm_kzalloc(&hdev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
339 ret
= hid_parse(hdev
);
344 mutex_init(&ldev
->lock
);
346 for (i
= 0; !ldev
->config
&& i
< ARRAY_SIZE(hidled_configs
); i
++)
347 if (hidled_configs
[i
].type
== id
->driver_data
)
348 ldev
->config
= &hidled_configs
[i
];
353 if (ldev
->config
->init
) {
354 ret
= ldev
->config
->init(ldev
);
359 ldev
->rgb
= devm_kcalloc(&hdev
->dev
, ldev
->config
->num_leds
,
360 sizeof(struct hidled_rgb
), GFP_KERNEL
);
364 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
368 minor
= ((struct hidraw
*) hdev
->hidraw
)->minor
;
370 for (i
= 0; i
< ldev
->config
->num_leds
; i
++) {
371 ldev
->rgb
[i
].ldev
= ldev
;
372 ldev
->rgb
[i
].num
= i
;
373 ret
= hidled_init_rgb(&ldev
->rgb
[i
], minor
);
380 hid_info(hdev
, "%s initialized\n", ldev
->config
->name
);
385 static const struct hid_device_id hidled_table
[] = {
386 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU
,
387 USB_DEVICE_ID_RI_KA_WEBMAIL
), .driver_data
= RISO_KAGAKU
},
388 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
389 USB_DEVICE_ID_DREAM_CHEEKY_WN
), .driver_data
= DREAM_CHEEKY
},
390 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
391 USB_DEVICE_ID_DREAM_CHEEKY_FA
), .driver_data
= DREAM_CHEEKY
},
392 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM
,
393 USB_DEVICE_ID_BLINK1
), .driver_data
= THINGM
},
396 MODULE_DEVICE_TABLE(hid
, hidled_table
);
398 static struct hid_driver hidled_driver
= {
400 .probe
= hidled_probe
,
401 .id_table
= hidled_table
,
404 module_hid_driver(hidled_driver
);
406 MODULE_LICENSE("GPL");
407 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
408 MODULE_DESCRIPTION("Simple USB RGB LED driver");