d5d8d941c4b0a79e614ed6fd61253318e0f3765d
[deliverable/linux.git] / drivers / hid / hid-led.c
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 struct hidled_rgb;
47
48 struct hidled_config {
49 enum hidled_type type;
50 const char *name;
51 const char *short_name;
52 enum led_brightness max_brightness;
53 int num_leds;
54 size_t report_size;
55 enum hidled_report_type report_type;
56 u8 report_id;
57 int (*init)(struct hidled_device *ldev);
58 int (*write)(struct led_classdev *cdev, enum led_brightness br);
59 };
60
61 struct hidled_led {
62 struct led_classdev cdev;
63 struct hidled_rgb *rgb;
64 char name[32];
65 };
66
67 struct hidled_rgb {
68 struct hidled_device *ldev;
69 struct hidled_led red;
70 struct hidled_led green;
71 struct hidled_led blue;
72 u8 num;
73 };
74
75 struct hidled_device {
76 const struct hidled_config *config;
77 struct hid_device *hdev;
78 struct hidled_rgb *rgb;
79 struct mutex lock;
80 };
81
82 #define MAX_REPORT_SIZE 16
83
84 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
85
86 static bool riso_kagaku_switch_green_blue;
87 module_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(riso_kagaku_switch_green_blue,
89 "switch green and blue RGB component for Riso Kagaku devices");
90
91 static int hidled_send(struct hidled_device *ldev, __u8 *buf)
92 {
93 int ret;
94
95 buf[0] = ldev->config->report_id;
96
97 mutex_lock(&ldev->lock);
98
99 if (ldev->config->report_type == RAW_REQUEST)
100 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
101 ldev->config->report_size,
102 HID_FEATURE_REPORT,
103 HID_REQ_SET_REPORT);
104 else if (ldev->config->report_type == OUTPUT_REPORT)
105 ret = hid_hw_output_report(ldev->hdev, buf,
106 ldev->config->report_size);
107 else
108 ret = -EINVAL;
109
110 mutex_unlock(&ldev->lock);
111
112 if (ret < 0)
113 return ret;
114
115 return ret == ldev->config->report_size ? 0 : -EMSGSIZE;
116 }
117
118 static u8 riso_kagaku_index(struct hidled_rgb *rgb)
119 {
120 enum led_brightness r, g, b;
121
122 r = rgb->red.cdev.brightness;
123 g = rgb->green.cdev.brightness;
124 b = rgb->blue.cdev.brightness;
125
126 if (riso_kagaku_switch_green_blue)
127 return RISO_KAGAKU_IX(r, b, g);
128 else
129 return RISO_KAGAKU_IX(r, g, b);
130 }
131
132 static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br)
133 {
134 struct hidled_led *led = to_hidled_led(cdev);
135 struct hidled_rgb *rgb = led->rgb;
136 __u8 buf[MAX_REPORT_SIZE] = {};
137
138 buf[1] = riso_kagaku_index(rgb);
139
140 return hidled_send(rgb->ldev, buf);
141 }
142
143 static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br)
144 {
145 struct hidled_led *led = to_hidled_led(cdev);
146 struct hidled_rgb *rgb = led->rgb;
147 __u8 buf[MAX_REPORT_SIZE] = {};
148
149 buf[1] = rgb->red.cdev.brightness;
150 buf[2] = rgb->green.cdev.brightness;
151 buf[3] = rgb->blue.cdev.brightness;
152 buf[7] = 0x1a;
153 buf[8] = 0x05;
154
155 return hidled_send(rgb->ldev, buf);
156 }
157
158 static int dream_cheeky_init(struct hidled_device *ldev)
159 {
160 __u8 buf[MAX_REPORT_SIZE] = {};
161
162 /* Dream Cheeky magic */
163 buf[1] = 0x1f;
164 buf[2] = 0x02;
165 buf[4] = 0x5f;
166 buf[7] = 0x1a;
167 buf[8] = 0x03;
168
169 return hidled_send(ldev, buf);
170 }
171
172 static const struct hidled_config hidled_configs[] = {
173 {
174 .type = RISO_KAGAKU,
175 .name = "Riso Kagaku Webmail Notifier",
176 .short_name = "riso_kagaku",
177 .max_brightness = 1,
178 .num_leds = 1,
179 .report_size = 6,
180 .report_type = OUTPUT_REPORT,
181 .report_id = 0,
182 .write = riso_kagaku_write,
183 },
184 {
185 .type = DREAM_CHEEKY,
186 .name = "Dream Cheeky Webmail Notifier",
187 .short_name = "dream_cheeky",
188 .max_brightness = 31,
189 .num_leds = 1,
190 .report_size = 9,
191 .report_type = RAW_REQUEST,
192 .report_id = 0,
193 .init = dream_cheeky_init,
194 .write = dream_cheeky_write,
195 },
196 };
197
198 static int hidled_init_led(struct hidled_led *led, const char *color_name,
199 struct hidled_rgb *rgb, unsigned int minor)
200 {
201 const struct hidled_config *config = rgb->ldev->config;
202
203 if (config->num_leds > 1)
204 snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u",
205 config->short_name, minor, color_name, rgb->num);
206 else
207 snprintf(led->name, sizeof(led->name), "%s%u:%s",
208 config->short_name, minor, color_name);
209 led->cdev.name = led->name;
210 led->cdev.max_brightness = config->max_brightness;
211 led->cdev.brightness_set_blocking = config->write;
212 led->cdev.flags = LED_HW_PLUGGABLE;
213 led->rgb = rgb;
214
215 return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev);
216 }
217
218 static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
219 {
220 int ret;
221
222 /* Register the red diode */
223 ret = hidled_init_led(&rgb->red, "red", rgb, minor);
224 if (ret)
225 return ret;
226
227 /* Register the green diode */
228 ret = hidled_init_led(&rgb->green, "green", rgb, minor);
229 if (ret)
230 return ret;
231
232 /* Register the blue diode */
233 return hidled_init_led(&rgb->blue, "blue", rgb, minor);
234 }
235
236 static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
237 {
238 struct hidled_device *ldev;
239 unsigned int minor;
240 int ret, i;
241
242 ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
243 if (!ldev)
244 return -ENOMEM;
245
246 ret = hid_parse(hdev);
247 if (ret)
248 return ret;
249
250 ldev->hdev = hdev;
251 mutex_init(&ldev->lock);
252
253 for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++)
254 if (hidled_configs[i].type == id->driver_data)
255 ldev->config = &hidled_configs[i];
256
257 if (!ldev->config)
258 return -EINVAL;
259
260 if (ldev->config->init) {
261 ret = ldev->config->init(ldev);
262 if (ret)
263 return ret;
264 }
265
266 ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
267 sizeof(struct hidled_rgb), GFP_KERNEL);
268 if (!ldev->rgb)
269 return -ENOMEM;
270
271 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
272 if (ret)
273 return ret;
274
275 minor = ((struct hidraw *) hdev->hidraw)->minor;
276
277 for (i = 0; i < ldev->config->num_leds; i++) {
278 ldev->rgb[i].ldev = ldev;
279 ldev->rgb[i].num = i;
280 ret = hidled_init_rgb(&ldev->rgb[i], minor);
281 if (ret) {
282 hid_hw_stop(hdev);
283 return ret;
284 }
285 }
286
287 hid_info(hdev, "%s initialized\n", ldev->config->name);
288
289 return 0;
290 }
291
292 static const struct hid_device_id hidled_table[] = {
293 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU,
294 USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU },
295 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
296 USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY },
297 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
298 USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY },
299 { }
300 };
301 MODULE_DEVICE_TABLE(hid, hidled_table);
302
303 static struct hid_driver hidled_driver = {
304 .name = "hid-led",
305 .probe = hidled_probe,
306 .id_table = hidled_table,
307 };
308
309 module_hid_driver(hidled_driver);
310
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
313 MODULE_DESCRIPTION("Simple USB RGB LED driver");
This page took 0.0372 seconds and 4 git commands to generate.