]>
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, | |
007414e8 | 29 | THINGM, |
de908650 | 30 | DELCOM, |
9d1e048c | 31 | LUXAFOR, |
6c7ad07e HK |
32 | }; |
33 | ||
34 | static unsigned const char riso_kagaku_tbl[] = { | |
35 | /* R+2G+4B -> riso kagaku color index */ | |
36 | [0] = 0, /* black */ | |
37 | [1] = 2, /* red */ | |
38 | [2] = 1, /* green */ | |
39 | [3] = 5, /* yellow */ | |
40 | [4] = 3, /* blue */ | |
41 | [5] = 6, /* magenta */ | |
42 | [6] = 4, /* cyan */ | |
43 | [7] = 7 /* white */ | |
44 | }; | |
45 | ||
46 | #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)] | |
47 | ||
de908650 HK |
48 | union delcom_packet { |
49 | __u8 data[8]; | |
50 | struct { | |
51 | __u8 major_cmd; | |
52 | __u8 minor_cmd; | |
53 | __u8 data_lsb; | |
54 | __u8 data_msb; | |
55 | } tx; | |
56 | struct { | |
57 | __u8 cmd; | |
58 | } rx; | |
59 | struct { | |
60 | __le16 family_code; | |
61 | __le16 security_code; | |
62 | __u8 fw_version; | |
63 | } fw; | |
64 | }; | |
65 | ||
66 | #define DELCOM_GREEN_LED 0 | |
67 | #define DELCOM_RED_LED 1 | |
68 | #define DELCOM_BLUE_LED 2 | |
69 | ||
6c7ad07e | 70 | struct hidled_device; |
5bc83936 | 71 | struct hidled_rgb; |
6c7ad07e HK |
72 | |
73 | struct hidled_config { | |
74 | enum hidled_type type; | |
75 | const char *name; | |
76 | const char *short_name; | |
77 | enum led_brightness max_brightness; | |
5bc83936 | 78 | int num_leds; |
6c7ad07e HK |
79 | size_t report_size; |
80 | enum hidled_report_type report_type; | |
6c7ad07e HK |
81 | int (*init)(struct hidled_device *ldev); |
82 | int (*write)(struct led_classdev *cdev, enum led_brightness br); | |
83 | }; | |
84 | ||
85 | struct hidled_led { | |
86 | struct led_classdev cdev; | |
5bc83936 | 87 | struct hidled_rgb *rgb; |
6c7ad07e HK |
88 | char name[32]; |
89 | }; | |
90 | ||
5bc83936 HK |
91 | struct hidled_rgb { |
92 | struct hidled_device *ldev; | |
6c7ad07e HK |
93 | struct hidled_led red; |
94 | struct hidled_led green; | |
95 | struct hidled_led blue; | |
5bc83936 HK |
96 | u8 num; |
97 | }; | |
98 | ||
99 | struct hidled_device { | |
100 | const struct hidled_config *config; | |
6c7ad07e | 101 | struct hid_device *hdev; |
5bc83936 | 102 | struct hidled_rgb *rgb; |
3d1355b3 | 103 | u8 *buf; |
6c7ad07e HK |
104 | struct mutex lock; |
105 | }; | |
106 | ||
107 | #define MAX_REPORT_SIZE 16 | |
108 | ||
109 | #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev) | |
110 | ||
111 | static bool riso_kagaku_switch_green_blue; | |
112 | module_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR); | |
113 | MODULE_PARM_DESC(riso_kagaku_switch_green_blue, | |
114 | "switch green and blue RGB component for Riso Kagaku devices"); | |
115 | ||
116 | static int hidled_send(struct hidled_device *ldev, __u8 *buf) | |
117 | { | |
118 | int ret; | |
119 | ||
6c7ad07e HK |
120 | mutex_lock(&ldev->lock); |
121 | ||
3d1355b3 HK |
122 | /* |
123 | * buffer provided to hid_hw_raw_request must not be on the stack | |
124 | * and must not be part of a data structure | |
125 | */ | |
126 | memcpy(ldev->buf, buf, ldev->config->report_size); | |
127 | ||
6c7ad07e | 128 | if (ldev->config->report_type == RAW_REQUEST) |
3d1355b3 | 129 | ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf, |
6c7ad07e HK |
130 | ldev->config->report_size, |
131 | HID_FEATURE_REPORT, | |
132 | HID_REQ_SET_REPORT); | |
133 | else if (ldev->config->report_type == OUTPUT_REPORT) | |
3d1355b3 | 134 | ret = hid_hw_output_report(ldev->hdev, ldev->buf, |
6c7ad07e HK |
135 | ldev->config->report_size); |
136 | else | |
137 | ret = -EINVAL; | |
138 | ||
139 | mutex_unlock(&ldev->lock); | |
140 | ||
141 | if (ret < 0) | |
142 | return ret; | |
143 | ||
144 | return ret == ldev->config->report_size ? 0 : -EMSGSIZE; | |
145 | } | |
146 | ||
43745730 HK |
147 | /* reading data is supported for report type RAW_REQUEST only */ |
148 | static int hidled_recv(struct hidled_device *ldev, __u8 *buf) | |
149 | { | |
150 | int ret; | |
151 | ||
152 | if (ldev->config->report_type != RAW_REQUEST) | |
153 | return -EINVAL; | |
154 | ||
43745730 HK |
155 | mutex_lock(&ldev->lock); |
156 | ||
3d1355b3 HK |
157 | memcpy(ldev->buf, buf, ldev->config->report_size); |
158 | ||
159 | ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf, | |
43745730 HK |
160 | ldev->config->report_size, |
161 | HID_FEATURE_REPORT, | |
162 | HID_REQ_SET_REPORT); | |
163 | if (ret < 0) | |
164 | goto err; | |
165 | ||
3d1355b3 | 166 | ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf, |
43745730 HK |
167 | ldev->config->report_size, |
168 | HID_FEATURE_REPORT, | |
169 | HID_REQ_GET_REPORT); | |
3d1355b3 HK |
170 | |
171 | memcpy(buf, ldev->buf, ldev->config->report_size); | |
43745730 HK |
172 | err: |
173 | mutex_unlock(&ldev->lock); | |
174 | ||
175 | return ret < 0 ? ret : 0; | |
176 | } | |
177 | ||
5bc83936 | 178 | static u8 riso_kagaku_index(struct hidled_rgb *rgb) |
6c7ad07e HK |
179 | { |
180 | enum led_brightness r, g, b; | |
181 | ||
5bc83936 HK |
182 | r = rgb->red.cdev.brightness; |
183 | g = rgb->green.cdev.brightness; | |
184 | b = rgb->blue.cdev.brightness; | |
6c7ad07e HK |
185 | |
186 | if (riso_kagaku_switch_green_blue) | |
187 | return RISO_KAGAKU_IX(r, b, g); | |
188 | else | |
189 | return RISO_KAGAKU_IX(r, g, b); | |
190 | } | |
191 | ||
192 | static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br) | |
193 | { | |
194 | struct hidled_led *led = to_hidled_led(cdev); | |
5bc83936 | 195 | struct hidled_rgb *rgb = led->rgb; |
6c7ad07e HK |
196 | __u8 buf[MAX_REPORT_SIZE] = {}; |
197 | ||
5bc83936 | 198 | buf[1] = riso_kagaku_index(rgb); |
6c7ad07e | 199 | |
5bc83936 | 200 | return hidled_send(rgb->ldev, buf); |
6c7ad07e HK |
201 | } |
202 | ||
203 | static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br) | |
204 | { | |
205 | struct hidled_led *led = to_hidled_led(cdev); | |
5bc83936 | 206 | struct hidled_rgb *rgb = led->rgb; |
6c7ad07e HK |
207 | __u8 buf[MAX_REPORT_SIZE] = {}; |
208 | ||
5bc83936 HK |
209 | buf[1] = rgb->red.cdev.brightness; |
210 | buf[2] = rgb->green.cdev.brightness; | |
211 | buf[3] = rgb->blue.cdev.brightness; | |
6c7ad07e HK |
212 | buf[7] = 0x1a; |
213 | buf[8] = 0x05; | |
214 | ||
5bc83936 | 215 | return hidled_send(rgb->ldev, buf); |
6c7ad07e HK |
216 | } |
217 | ||
218 | static int dream_cheeky_init(struct hidled_device *ldev) | |
219 | { | |
220 | __u8 buf[MAX_REPORT_SIZE] = {}; | |
221 | ||
222 | /* Dream Cheeky magic */ | |
223 | buf[1] = 0x1f; | |
224 | buf[2] = 0x02; | |
225 | buf[4] = 0x5f; | |
226 | buf[7] = 0x1a; | |
227 | buf[8] = 0x03; | |
228 | ||
229 | return hidled_send(ldev, buf); | |
230 | } | |
231 | ||
007414e8 HK |
232 | static int _thingm_write(struct led_classdev *cdev, enum led_brightness br, |
233 | u8 offset) | |
234 | { | |
235 | struct hidled_led *led = to_hidled_led(cdev); | |
34d9810b | 236 | __u8 buf[MAX_REPORT_SIZE] = { 1, 'c' }; |
007414e8 HK |
237 | |
238 | buf[2] = led->rgb->red.cdev.brightness; | |
239 | buf[3] = led->rgb->green.cdev.brightness; | |
240 | buf[4] = led->rgb->blue.cdev.brightness; | |
241 | buf[7] = led->rgb->num + offset; | |
242 | ||
243 | return hidled_send(led->rgb->ldev, buf); | |
244 | } | |
245 | ||
246 | static int thingm_write_v1(struct led_classdev *cdev, enum led_brightness br) | |
247 | { | |
248 | return _thingm_write(cdev, br, 0); | |
249 | } | |
250 | ||
251 | static int thingm_write(struct led_classdev *cdev, enum led_brightness br) | |
252 | { | |
253 | return _thingm_write(cdev, br, 1); | |
254 | } | |
255 | ||
256 | static const struct hidled_config hidled_config_thingm_v1 = { | |
257 | .name = "ThingM blink(1) v1", | |
258 | .short_name = "thingm", | |
259 | .max_brightness = 255, | |
260 | .num_leds = 1, | |
261 | .report_size = 9, | |
262 | .report_type = RAW_REQUEST, | |
007414e8 HK |
263 | .write = thingm_write_v1, |
264 | }; | |
265 | ||
266 | static int thingm_init(struct hidled_device *ldev) | |
267 | { | |
34d9810b | 268 | __u8 buf[MAX_REPORT_SIZE] = { 1, 'v' }; |
007414e8 HK |
269 | int ret; |
270 | ||
271 | ret = hidled_recv(ldev, buf); | |
272 | if (ret) | |
273 | return ret; | |
274 | ||
275 | /* Check for firmware major version 1 */ | |
276 | if (buf[3] == '1') | |
277 | ldev->config = &hidled_config_thingm_v1; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
de908650 HK |
282 | static inline int delcom_get_lednum(const struct hidled_led *led) |
283 | { | |
284 | if (led == &led->rgb->red) | |
285 | return DELCOM_RED_LED; | |
286 | else if (led == &led->rgb->green) | |
287 | return DELCOM_GREEN_LED; | |
288 | else | |
289 | return DELCOM_BLUE_LED; | |
290 | } | |
291 | ||
292 | static int delcom_enable_led(struct hidled_led *led) | |
293 | { | |
294 | union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 12 }; | |
295 | ||
296 | dp.tx.data_lsb = 1 << delcom_get_lednum(led); | |
297 | dp.tx.data_msb = 0; | |
298 | ||
299 | return hidled_send(led->rgb->ldev, dp.data); | |
300 | } | |
301 | ||
302 | static int delcom_set_pwm(struct hidled_led *led) | |
303 | { | |
304 | union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 34 }; | |
305 | ||
306 | dp.tx.data_lsb = delcom_get_lednum(led); | |
307 | dp.tx.data_msb = led->cdev.brightness; | |
308 | ||
309 | return hidled_send(led->rgb->ldev, dp.data); | |
310 | } | |
311 | ||
312 | static int delcom_write(struct led_classdev *cdev, enum led_brightness br) | |
313 | { | |
314 | struct hidled_led *led = to_hidled_led(cdev); | |
315 | int ret; | |
316 | ||
317 | /* | |
318 | * enable LED | |
319 | * We can't do this in the init function already because the device | |
320 | * is internally reset later. | |
321 | */ | |
322 | ret = delcom_enable_led(led); | |
323 | if (ret) | |
324 | return ret; | |
325 | ||
326 | return delcom_set_pwm(led); | |
327 | } | |
328 | ||
329 | static int delcom_init(struct hidled_device *ldev) | |
330 | { | |
331 | union delcom_packet dp = { .rx.cmd = 104 }; | |
332 | int ret; | |
333 | ||
334 | ret = hidled_recv(ldev, dp.data); | |
335 | if (ret) | |
336 | return ret; | |
337 | /* | |
338 | * Several Delcom devices share the same USB VID/PID | |
339 | * Check for family id 2 for Visual Signal Indicator | |
340 | */ | |
f4c109b6 | 341 | return le16_to_cpu(dp.fw.family_code) == 2 ? 0 : -ENODEV; |
de908650 HK |
342 | } |
343 | ||
9d1e048c HK |
344 | static int luxafor_write(struct led_classdev *cdev, enum led_brightness br) |
345 | { | |
346 | struct hidled_led *led = to_hidled_led(cdev); | |
347 | __u8 buf[MAX_REPORT_SIZE] = { [1] = 1 }; | |
348 | ||
349 | buf[2] = led->rgb->num + 1; | |
350 | buf[3] = led->rgb->red.cdev.brightness; | |
351 | buf[4] = led->rgb->green.cdev.brightness; | |
352 | buf[5] = led->rgb->blue.cdev.brightness; | |
353 | ||
354 | return hidled_send(led->rgb->ldev, buf); | |
355 | } | |
356 | ||
6c7ad07e HK |
357 | static const struct hidled_config hidled_configs[] = { |
358 | { | |
359 | .type = RISO_KAGAKU, | |
360 | .name = "Riso Kagaku Webmail Notifier", | |
361 | .short_name = "riso_kagaku", | |
362 | .max_brightness = 1, | |
5bc83936 | 363 | .num_leds = 1, |
6c7ad07e HK |
364 | .report_size = 6, |
365 | .report_type = OUTPUT_REPORT, | |
6c7ad07e HK |
366 | .write = riso_kagaku_write, |
367 | }, | |
368 | { | |
369 | .type = DREAM_CHEEKY, | |
370 | .name = "Dream Cheeky Webmail Notifier", | |
371 | .short_name = "dream_cheeky", | |
372 | .max_brightness = 31, | |
5bc83936 | 373 | .num_leds = 1, |
6c7ad07e HK |
374 | .report_size = 9, |
375 | .report_type = RAW_REQUEST, | |
6c7ad07e HK |
376 | .init = dream_cheeky_init, |
377 | .write = dream_cheeky_write, | |
378 | }, | |
007414e8 HK |
379 | { |
380 | .type = THINGM, | |
381 | .name = "ThingM blink(1)", | |
382 | .short_name = "thingm", | |
383 | .max_brightness = 255, | |
384 | .num_leds = 2, | |
385 | .report_size = 9, | |
386 | .report_type = RAW_REQUEST, | |
007414e8 HK |
387 | .init = thingm_init, |
388 | .write = thingm_write, | |
389 | }, | |
de908650 HK |
390 | { |
391 | .type = DELCOM, | |
392 | .name = "Delcom Visual Signal Indicator G2", | |
393 | .short_name = "delcom", | |
394 | .max_brightness = 100, | |
395 | .num_leds = 1, | |
396 | .report_size = 8, | |
397 | .report_type = RAW_REQUEST, | |
398 | .init = delcom_init, | |
399 | .write = delcom_write, | |
400 | }, | |
9d1e048c HK |
401 | { |
402 | .type = LUXAFOR, | |
403 | .name = "Greynut Luxafor", | |
404 | .short_name = "luxafor", | |
405 | .max_brightness = 255, | |
406 | .num_leds = 6, | |
407 | .report_size = 9, | |
408 | .report_type = OUTPUT_REPORT, | |
409 | .write = luxafor_write, | |
410 | }, | |
6c7ad07e HK |
411 | }; |
412 | ||
413 | static int hidled_init_led(struct hidled_led *led, const char *color_name, | |
5bc83936 | 414 | struct hidled_rgb *rgb, unsigned int minor) |
6c7ad07e | 415 | { |
5bc83936 HK |
416 | const struct hidled_config *config = rgb->ldev->config; |
417 | ||
418 | if (config->num_leds > 1) | |
419 | snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u", | |
420 | config->short_name, minor, color_name, rgb->num); | |
421 | else | |
422 | snprintf(led->name, sizeof(led->name), "%s%u:%s", | |
423 | config->short_name, minor, color_name); | |
6c7ad07e | 424 | led->cdev.name = led->name; |
5bc83936 HK |
425 | led->cdev.max_brightness = config->max_brightness; |
426 | led->cdev.brightness_set_blocking = config->write; | |
6c7ad07e | 427 | led->cdev.flags = LED_HW_PLUGGABLE; |
5bc83936 | 428 | led->rgb = rgb; |
6c7ad07e | 429 | |
5bc83936 | 430 | return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev); |
6c7ad07e HK |
431 | } |
432 | ||
5bc83936 | 433 | static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor) |
6c7ad07e HK |
434 | { |
435 | int ret; | |
436 | ||
437 | /* Register the red diode */ | |
5bc83936 | 438 | ret = hidled_init_led(&rgb->red, "red", rgb, minor); |
6c7ad07e HK |
439 | if (ret) |
440 | return ret; | |
441 | ||
442 | /* Register the green diode */ | |
5bc83936 | 443 | ret = hidled_init_led(&rgb->green, "green", rgb, minor); |
6c7ad07e HK |
444 | if (ret) |
445 | return ret; | |
446 | ||
447 | /* Register the blue diode */ | |
5bc83936 | 448 | return hidled_init_led(&rgb->blue, "blue", rgb, minor); |
6c7ad07e HK |
449 | } |
450 | ||
451 | static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
452 | { | |
453 | struct hidled_device *ldev; | |
454 | unsigned int minor; | |
455 | int ret, i; | |
456 | ||
457 | ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL); | |
458 | if (!ldev) | |
459 | return -ENOMEM; | |
460 | ||
3d1355b3 HK |
461 | ldev->buf = devm_kmalloc(&hdev->dev, MAX_REPORT_SIZE, GFP_KERNEL); |
462 | if (!ldev->buf) | |
463 | return -ENOMEM; | |
464 | ||
6c7ad07e HK |
465 | ret = hid_parse(hdev); |
466 | if (ret) | |
467 | return ret; | |
468 | ||
469 | ldev->hdev = hdev; | |
470 | mutex_init(&ldev->lock); | |
471 | ||
472 | for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++) | |
473 | if (hidled_configs[i].type == id->driver_data) | |
474 | ldev->config = &hidled_configs[i]; | |
475 | ||
476 | if (!ldev->config) | |
477 | return -EINVAL; | |
478 | ||
479 | if (ldev->config->init) { | |
480 | ret = ldev->config->init(ldev); | |
481 | if (ret) | |
482 | return ret; | |
483 | } | |
484 | ||
5bc83936 HK |
485 | ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds, |
486 | sizeof(struct hidled_rgb), GFP_KERNEL); | |
487 | if (!ldev->rgb) | |
488 | return -ENOMEM; | |
489 | ||
6c7ad07e HK |
490 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
491 | if (ret) | |
492 | return ret; | |
493 | ||
494 | minor = ((struct hidraw *) hdev->hidraw)->minor; | |
495 | ||
5bc83936 HK |
496 | for (i = 0; i < ldev->config->num_leds; i++) { |
497 | ldev->rgb[i].ldev = ldev; | |
498 | ldev->rgb[i].num = i; | |
499 | ret = hidled_init_rgb(&ldev->rgb[i], minor); | |
500 | if (ret) { | |
501 | hid_hw_stop(hdev); | |
502 | return ret; | |
503 | } | |
6c7ad07e HK |
504 | } |
505 | ||
506 | hid_info(hdev, "%s initialized\n", ldev->config->name); | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | static const struct hid_device_id hidled_table[] = { | |
512 | { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU, | |
513 | USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU }, | |
514 | { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, | |
515 | USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY }, | |
516 | { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY, | |
517 | USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY }, | |
007414e8 HK |
518 | { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, |
519 | USB_DEVICE_ID_BLINK1), .driver_data = THINGM }, | |
de908650 HK |
520 | { HID_USB_DEVICE(USB_VENDOR_ID_DELCOM, |
521 | USB_DEVICE_ID_DELCOM_VISUAL_IND), .driver_data = DELCOM }, | |
9d1e048c HK |
522 | { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, |
523 | USB_DEVICE_ID_LUXAFOR), .driver_data = LUXAFOR }, | |
6c7ad07e HK |
524 | { } |
525 | }; | |
526 | MODULE_DEVICE_TABLE(hid, hidled_table); | |
527 | ||
528 | static struct hid_driver hidled_driver = { | |
529 | .name = "hid-led", | |
530 | .probe = hidled_probe, | |
531 | .id_table = hidled_table, | |
532 | }; | |
533 | ||
534 | module_hid_driver(hidled_driver); | |
535 | ||
536 | MODULE_LICENSE("GPL"); | |
537 | MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>"); | |
538 | MODULE_DESCRIPTION("Simple USB RGB LED driver"); |