]>
Commit | Line | Data |
---|---|---|
6c7ad07e HK |
1 | /* |
2 | * Simple USB RGB LED driver | |
3 | * | |
4 | * Copyright 2016 Heiner Kallweit <hkallweit1@gmail.com> | |
5 | * Based on drivers/hid/hid-thingm.c and | |
6 | * drivers/usb/misc/usbled.c | |
7 | * | |
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. | |
11 | */ | |
12 | ||
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> | |
18 | ||
19 | #include "hid-ids.h" | |
20 | ||
21 | enum hidled_report_type { | |
22 | RAW_REQUEST, | |
23 | OUTPUT_REPORT | |
24 | }; | |
25 | ||
26 | enum hidled_type { | |
27 | RISO_KAGAKU, | |
28 | DREAM_CHEEKY, | |
29 | }; | |
30 | ||
31 | static unsigned const char riso_kagaku_tbl[] = { | |
32 | /* R+2G+4B -> riso kagaku color index */ | |
33 | [0] = 0, /* black */ | |
34 | [1] = 2, /* red */ | |
35 | [2] = 1, /* green */ | |
36 | [3] = 5, /* yellow */ | |
37 | [4] = 3, /* blue */ | |
38 | [5] = 6, /* magenta */ | |
39 | [6] = 4, /* cyan */ | |
40 | [7] = 7 /* white */ | |
41 | }; | |
42 | ||
43 | #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)] | |
44 | ||
45 | struct hidled_device; | |
46 | ||
47 | struct hidled_config { | |
48 | enum hidled_type type; | |
49 | const char *name; | |
50 | const char *short_name; | |
51 | enum led_brightness max_brightness; | |
52 | size_t report_size; | |
53 | enum hidled_report_type report_type; | |
54 | u8 report_id; | |
55 | int (*init)(struct hidled_device *ldev); | |
56 | int (*write)(struct led_classdev *cdev, enum led_brightness br); | |
57 | }; | |
58 | ||
59 | struct hidled_led { | |
60 | struct led_classdev cdev; | |
61 | struct hidled_device *ldev; | |
62 | char name[32]; | |
63 | }; | |
64 | ||
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; | |
71 | struct mutex lock; | |
72 | }; | |
73 | ||
74 | #define MAX_REPORT_SIZE 16 | |
75 | ||
76 | #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev) | |
77 | ||
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"); | |
82 | ||
83 | static int hidled_send(struct hidled_device *ldev, __u8 *buf) | |
84 | { | |
85 | int ret; | |
86 | ||
87 | buf[0] = ldev->config->report_id; | |
88 | ||
89 | mutex_lock(&ldev->lock); | |
90 | ||
91 | if (ldev->config->report_type == RAW_REQUEST) | |
92 | ret = hid_hw_raw_request(ldev->hdev, buf[0], buf, | |
93 | ldev->config->report_size, | |
94 | HID_FEATURE_REPORT, | |
95 | HID_REQ_SET_REPORT); | |
96 | else if (ldev->config->report_type == OUTPUT_REPORT) | |
97 | ret = hid_hw_output_report(ldev->hdev, buf, | |
98 | ldev->config->report_size); | |
99 | else | |
100 | ret = -EINVAL; | |
101 | ||
102 | mutex_unlock(&ldev->lock); | |
103 | ||
104 | if (ret < 0) | |
105 | return ret; | |
106 | ||
107 | return ret == ldev->config->report_size ? 0 : -EMSGSIZE; | |
108 | } | |
109 | ||
110 | static u8 riso_kagaku_index(struct hidled_device *ldev) | |
111 | { | |
112 | enum led_brightness r, g, b; | |
113 | ||
114 | r = ldev->red.cdev.brightness; | |
115 | g = ldev->green.cdev.brightness; | |
116 | b = ldev->blue.cdev.brightness; | |
117 | ||
118 | if (riso_kagaku_switch_green_blue) | |
119 | return RISO_KAGAKU_IX(r, b, g); | |
120 | else | |
121 | return RISO_KAGAKU_IX(r, g, b); | |
122 | } | |
123 | ||
124 | static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br) | |
125 | { | |
126 | struct hidled_led *led = to_hidled_led(cdev); | |
127 | struct hidled_device *ldev = led->ldev; | |
128 | __u8 buf[MAX_REPORT_SIZE] = {}; | |
129 | ||
130 | buf[1] = riso_kagaku_index(ldev); | |
131 | ||
132 | return hidled_send(ldev, buf); | |
133 | } | |
134 | ||
135 | static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br) | |
136 | { | |
137 | struct hidled_led *led = to_hidled_led(cdev); | |
138 | struct hidled_device *ldev = led->ldev; | |
139 | __u8 buf[MAX_REPORT_SIZE] = {}; | |
140 | ||
141 | buf[1] = ldev->red.cdev.brightness; | |
142 | buf[2] = ldev->green.cdev.brightness; | |
143 | buf[3] = ldev->blue.cdev.brightness; | |
144 | buf[7] = 0x1a; | |
145 | buf[8] = 0x05; | |
146 | ||
147 | return hidled_send(ldev, buf); | |
148 | } | |
149 | ||
150 | static int dream_cheeky_init(struct hidled_device *ldev) | |
151 | { | |
152 | __u8 buf[MAX_REPORT_SIZE] = {}; | |
153 | ||
154 | /* Dream Cheeky magic */ | |
155 | buf[1] = 0x1f; | |
156 | buf[2] = 0x02; | |
157 | buf[4] = 0x5f; | |
158 | buf[7] = 0x1a; | |
159 | buf[8] = 0x03; | |
160 | ||
161 | return hidled_send(ldev, buf); | |
162 | } | |
163 | ||
164 | static const struct hidled_config hidled_configs[] = { | |
165 | { | |
166 | .type = RISO_KAGAKU, | |
167 | .name = "Riso Kagaku Webmail Notifier", | |
168 | .short_name = "riso_kagaku", | |
169 | .max_brightness = 1, | |
170 | .report_size = 6, | |
171 | .report_type = OUTPUT_REPORT, | |
172 | .report_id = 0, | |
173 | .write = riso_kagaku_write, | |
174 | }, | |
175 | { | |
176 | .type = DREAM_CHEEKY, | |
177 | .name = "Dream Cheeky Webmail Notifier", | |
178 | .short_name = "dream_cheeky", | |
179 | .max_brightness = 31, | |
180 | .report_size = 9, | |
181 | .report_type = RAW_REQUEST, | |
182 | .report_id = 0, | |
183 | .init = dream_cheeky_init, | |
184 | .write = dream_cheeky_write, | |
185 | }, | |
186 | }; | |
187 | ||
188 | static int hidled_init_led(struct hidled_led *led, const char *color_name, | |
189 | struct hidled_device *ldev, unsigned int minor) | |
190 | { | |
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; | |
197 | led->ldev = ldev; | |
198 | ||
199 | return devm_led_classdev_register(&ldev->hdev->dev, &led->cdev); | |
200 | } | |
201 | ||
202 | static int hidled_init_rgb(struct hidled_device *ldev, unsigned int minor) | |
203 | { | |
204 | int ret; | |
205 | ||
206 | /* Register the red diode */ | |
207 | ret = hidled_init_led(&ldev->red, "red", ldev, minor); | |
208 | if (ret) | |
209 | return ret; | |
210 | ||
211 | /* Register the green diode */ | |
212 | ret = hidled_init_led(&ldev->green, "green", ldev, minor); | |
213 | if (ret) | |
214 | return ret; | |
215 | ||
216 | /* Register the blue diode */ | |
217 | return hidled_init_led(&ldev->blue, "blue", ldev, minor); | |
218 | } | |
219 | ||
220 | static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
221 | { | |
222 | struct hidled_device *ldev; | |
223 | unsigned int minor; | |
224 | int ret, i; | |
225 | ||
226 | ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL); | |
227 | if (!ldev) | |
228 | return -ENOMEM; | |
229 | ||
230 | ret = hid_parse(hdev); | |
231 | if (ret) | |
232 | return ret; | |
233 | ||
234 | ldev->hdev = hdev; | |
235 | mutex_init(&ldev->lock); | |
236 | ||
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]; | |
240 | ||
241 | if (!ldev->config) | |
242 | return -EINVAL; | |
243 | ||
244 | if (ldev->config->init) { | |
245 | ret = ldev->config->init(ldev); | |
246 | if (ret) | |
247 | return ret; | |
248 | } | |
249 | ||
250 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
251 | if (ret) | |
252 | return ret; | |
253 | ||
254 | minor = ((struct hidraw *) hdev->hidraw)->minor; | |
255 | ||
256 | ret = hidled_init_rgb(ldev, minor); | |
257 | if (ret) { | |
258 | hid_hw_stop(hdev); | |
259 | return ret; | |
260 | } | |
261 | ||
262 | hid_info(hdev, "%s initialized\n", ldev->config->name); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
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 }, | |
274 | { } | |
275 | }; | |
276 | MODULE_DEVICE_TABLE(hid, hidled_table); | |
277 | ||
278 | static struct hid_driver hidled_driver = { | |
279 | .name = "hid-led", | |
280 | .probe = hidled_probe, | |
281 | .id_table = hidled_table, | |
282 | }; | |
283 | ||
284 | module_hid_driver(hidled_driver); | |
285 | ||
286 | MODULE_LICENSE("GPL"); | |
287 | MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>"); | |
288 | MODULE_DESCRIPTION("Simple USB RGB LED driver"); |