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