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)]
47 struct hidled_config
{
48 enum hidled_type type
;
50 const char *short_name
;
51 enum led_brightness max_brightness
;
53 enum hidled_report_type report_type
;
55 int (*init
)(struct hidled_device
*ldev
);
56 int (*write
)(struct led_classdev
*cdev
, enum led_brightness br
);
60 struct led_classdev cdev
;
61 struct hidled_device
*ldev
;
65 struct hidled_device
{
66 const struct hidled_config
*config
;
67 struct hidled_led red
;
68 struct hidled_led green
;
69 struct hidled_led blue
;
70 struct hid_device
*hdev
;
74 #define MAX_REPORT_SIZE 16
76 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
78 static bool riso_kagaku_switch_green_blue
;
79 module_param(riso_kagaku_switch_green_blue
, bool, S_IRUGO
| S_IWUSR
);
80 MODULE_PARM_DESC(riso_kagaku_switch_green_blue
,
81 "switch green and blue RGB component for Riso Kagaku devices");
83 static int hidled_send(struct hidled_device
*ldev
, __u8
*buf
)
87 buf
[0] = ldev
->config
->report_id
;
89 mutex_lock(&ldev
->lock
);
91 if (ldev
->config
->report_type
== RAW_REQUEST
)
92 ret
= hid_hw_raw_request(ldev
->hdev
, buf
[0], buf
,
93 ldev
->config
->report_size
,
96 else if (ldev
->config
->report_type
== OUTPUT_REPORT
)
97 ret
= hid_hw_output_report(ldev
->hdev
, buf
,
98 ldev
->config
->report_size
);
102 mutex_unlock(&ldev
->lock
);
107 return ret
== ldev
->config
->report_size
? 0 : -EMSGSIZE
;
110 static u8
riso_kagaku_index(struct hidled_device
*ldev
)
112 enum led_brightness r
, g
, b
;
114 r
= ldev
->red
.cdev
.brightness
;
115 g
= ldev
->green
.cdev
.brightness
;
116 b
= ldev
->blue
.cdev
.brightness
;
118 if (riso_kagaku_switch_green_blue
)
119 return RISO_KAGAKU_IX(r
, b
, g
);
121 return RISO_KAGAKU_IX(r
, g
, b
);
124 static int riso_kagaku_write(struct led_classdev
*cdev
, enum led_brightness br
)
126 struct hidled_led
*led
= to_hidled_led(cdev
);
127 struct hidled_device
*ldev
= led
->ldev
;
128 __u8 buf
[MAX_REPORT_SIZE
] = {};
130 buf
[1] = riso_kagaku_index(ldev
);
132 return hidled_send(ldev
, buf
);
135 static int dream_cheeky_write(struct led_classdev
*cdev
, enum led_brightness br
)
137 struct hidled_led
*led
= to_hidled_led(cdev
);
138 struct hidled_device
*ldev
= led
->ldev
;
139 __u8 buf
[MAX_REPORT_SIZE
] = {};
141 buf
[1] = ldev
->red
.cdev
.brightness
;
142 buf
[2] = ldev
->green
.cdev
.brightness
;
143 buf
[3] = ldev
->blue
.cdev
.brightness
;
147 return hidled_send(ldev
, buf
);
150 static int dream_cheeky_init(struct hidled_device
*ldev
)
152 __u8 buf
[MAX_REPORT_SIZE
] = {};
154 /* Dream Cheeky magic */
161 return hidled_send(ldev
, buf
);
164 static const struct hidled_config hidled_configs
[] = {
167 .name
= "Riso Kagaku Webmail Notifier",
168 .short_name
= "riso_kagaku",
171 .report_type
= OUTPUT_REPORT
,
173 .write
= riso_kagaku_write
,
176 .type
= DREAM_CHEEKY
,
177 .name
= "Dream Cheeky Webmail Notifier",
178 .short_name
= "dream_cheeky",
179 .max_brightness
= 31,
181 .report_type
= RAW_REQUEST
,
183 .init
= dream_cheeky_init
,
184 .write
= dream_cheeky_write
,
188 static int hidled_init_led(struct hidled_led
*led
, const char *color_name
,
189 struct hidled_device
*ldev
, unsigned int minor
)
191 snprintf(led
->name
, sizeof(led
->name
), "%s%u:%s",
192 ldev
->config
->short_name
, minor
, color_name
);
193 led
->cdev
.name
= led
->name
;
194 led
->cdev
.max_brightness
= ldev
->config
->max_brightness
;
195 led
->cdev
.brightness_set_blocking
= ldev
->config
->write
;
196 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
199 return devm_led_classdev_register(&ldev
->hdev
->dev
, &led
->cdev
);
202 static int hidled_init_rgb(struct hidled_device
*ldev
, unsigned int minor
)
206 /* Register the red diode */
207 ret
= hidled_init_led(&ldev
->red
, "red", ldev
, minor
);
211 /* Register the green diode */
212 ret
= hidled_init_led(&ldev
->green
, "green", ldev
, minor
);
216 /* Register the blue diode */
217 return hidled_init_led(&ldev
->blue
, "blue", ldev
, minor
);
220 static int hidled_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
222 struct hidled_device
*ldev
;
226 ldev
= devm_kzalloc(&hdev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
230 ret
= hid_parse(hdev
);
235 mutex_init(&ldev
->lock
);
237 for (i
= 0; !ldev
->config
&& i
< ARRAY_SIZE(hidled_configs
); i
++)
238 if (hidled_configs
[i
].type
== id
->driver_data
)
239 ldev
->config
= &hidled_configs
[i
];
244 if (ldev
->config
->init
) {
245 ret
= ldev
->config
->init(ldev
);
250 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
254 minor
= ((struct hidraw
*) hdev
->hidraw
)->minor
;
256 ret
= hidled_init_rgb(ldev
, minor
);
262 hid_info(hdev
, "%s initialized\n", ldev
->config
->name
);
267 static const struct hid_device_id hidled_table
[] = {
268 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU
,
269 USB_DEVICE_ID_RI_KA_WEBMAIL
), .driver_data
= RISO_KAGAKU
},
270 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
271 USB_DEVICE_ID_DREAM_CHEEKY_WN
), .driver_data
= DREAM_CHEEKY
},
272 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY
,
273 USB_DEVICE_ID_DREAM_CHEEKY_FA
), .driver_data
= DREAM_CHEEKY
},
276 MODULE_DEVICE_TABLE(hid
, hidled_table
);
278 static struct hid_driver hidled_driver
= {
280 .probe
= hidled_probe
,
281 .id_table
= hidled_table
,
284 module_hid_driver(hidled_driver
);
286 MODULE_LICENSE("GPL");
287 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
288 MODULE_DESCRIPTION("Simple USB RGB LED driver");