HID: hid-led: add support for ThingM blink(1)
[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 THINGM,
30 };
31
32 static unsigned const char riso_kagaku_tbl[] = {
33 /* R+2G+4B -> riso kagaku color index */
34 [0] = 0, /* black */
35 [1] = 2, /* red */
36 [2] = 1, /* green */
37 [3] = 5, /* yellow */
38 [4] = 3, /* blue */
39 [5] = 6, /* magenta */
40 [6] = 4, /* cyan */
41 [7] = 7 /* white */
42 };
43
44 #define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
45
46 struct hidled_device;
47 struct hidled_rgb;
48
49 struct hidled_config {
50 enum hidled_type type;
51 const char *name;
52 const char *short_name;
53 enum led_brightness max_brightness;
54 int num_leds;
55 size_t report_size;
56 enum hidled_report_type report_type;
57 u8 report_id;
58 int (*init)(struct hidled_device *ldev);
59 int (*write)(struct led_classdev *cdev, enum led_brightness br);
60 };
61
62 struct hidled_led {
63 struct led_classdev cdev;
64 struct hidled_rgb *rgb;
65 char name[32];
66 };
67
68 struct hidled_rgb {
69 struct hidled_device *ldev;
70 struct hidled_led red;
71 struct hidled_led green;
72 struct hidled_led blue;
73 u8 num;
74 };
75
76 struct hidled_device {
77 const struct hidled_config *config;
78 struct hid_device *hdev;
79 struct hidled_rgb *rgb;
80 struct mutex lock;
81 };
82
83 #define MAX_REPORT_SIZE 16
84
85 #define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
86
87 static bool riso_kagaku_switch_green_blue;
88 module_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR);
89 MODULE_PARM_DESC(riso_kagaku_switch_green_blue,
90 "switch green and blue RGB component for Riso Kagaku devices");
91
92 static int hidled_send(struct hidled_device *ldev, __u8 *buf)
93 {
94 int ret;
95
96 buf[0] = ldev->config->report_id;
97
98 mutex_lock(&ldev->lock);
99
100 if (ldev->config->report_type == RAW_REQUEST)
101 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
102 ldev->config->report_size,
103 HID_FEATURE_REPORT,
104 HID_REQ_SET_REPORT);
105 else if (ldev->config->report_type == OUTPUT_REPORT)
106 ret = hid_hw_output_report(ldev->hdev, buf,
107 ldev->config->report_size);
108 else
109 ret = -EINVAL;
110
111 mutex_unlock(&ldev->lock);
112
113 if (ret < 0)
114 return ret;
115
116 return ret == ldev->config->report_size ? 0 : -EMSGSIZE;
117 }
118
119 /* reading data is supported for report type RAW_REQUEST only */
120 static int hidled_recv(struct hidled_device *ldev, __u8 *buf)
121 {
122 int ret;
123
124 if (ldev->config->report_type != RAW_REQUEST)
125 return -EINVAL;
126
127 buf[0] = ldev->config->report_id;
128
129 mutex_lock(&ldev->lock);
130
131 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
132 ldev->config->report_size,
133 HID_FEATURE_REPORT,
134 HID_REQ_SET_REPORT);
135 if (ret < 0)
136 goto err;
137
138 ret = hid_hw_raw_request(ldev->hdev, buf[0], buf,
139 ldev->config->report_size,
140 HID_FEATURE_REPORT,
141 HID_REQ_GET_REPORT);
142 err:
143 mutex_unlock(&ldev->lock);
144
145 return ret < 0 ? ret : 0;
146 }
147
148 static u8 riso_kagaku_index(struct hidled_rgb *rgb)
149 {
150 enum led_brightness r, g, b;
151
152 r = rgb->red.cdev.brightness;
153 g = rgb->green.cdev.brightness;
154 b = rgb->blue.cdev.brightness;
155
156 if (riso_kagaku_switch_green_blue)
157 return RISO_KAGAKU_IX(r, b, g);
158 else
159 return RISO_KAGAKU_IX(r, g, b);
160 }
161
162 static int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br)
163 {
164 struct hidled_led *led = to_hidled_led(cdev);
165 struct hidled_rgb *rgb = led->rgb;
166 __u8 buf[MAX_REPORT_SIZE] = {};
167
168 buf[1] = riso_kagaku_index(rgb);
169
170 return hidled_send(rgb->ldev, buf);
171 }
172
173 static int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br)
174 {
175 struct hidled_led *led = to_hidled_led(cdev);
176 struct hidled_rgb *rgb = led->rgb;
177 __u8 buf[MAX_REPORT_SIZE] = {};
178
179 buf[1] = rgb->red.cdev.brightness;
180 buf[2] = rgb->green.cdev.brightness;
181 buf[3] = rgb->blue.cdev.brightness;
182 buf[7] = 0x1a;
183 buf[8] = 0x05;
184
185 return hidled_send(rgb->ldev, buf);
186 }
187
188 static int dream_cheeky_init(struct hidled_device *ldev)
189 {
190 __u8 buf[MAX_REPORT_SIZE] = {};
191
192 /* Dream Cheeky magic */
193 buf[1] = 0x1f;
194 buf[2] = 0x02;
195 buf[4] = 0x5f;
196 buf[7] = 0x1a;
197 buf[8] = 0x03;
198
199 return hidled_send(ldev, buf);
200 }
201
202 static int _thingm_write(struct led_classdev *cdev, enum led_brightness br,
203 u8 offset)
204 {
205 struct hidled_led *led = to_hidled_led(cdev);
206 __u8 buf[MAX_REPORT_SIZE] = { [1] = 'c' };
207
208 buf[2] = led->rgb->red.cdev.brightness;
209 buf[3] = led->rgb->green.cdev.brightness;
210 buf[4] = led->rgb->blue.cdev.brightness;
211 buf[7] = led->rgb->num + offset;
212
213 return hidled_send(led->rgb->ldev, buf);
214 }
215
216 static int thingm_write_v1(struct led_classdev *cdev, enum led_brightness br)
217 {
218 return _thingm_write(cdev, br, 0);
219 }
220
221 static int thingm_write(struct led_classdev *cdev, enum led_brightness br)
222 {
223 return _thingm_write(cdev, br, 1);
224 }
225
226 static const struct hidled_config hidled_config_thingm_v1 = {
227 .name = "ThingM blink(1) v1",
228 .short_name = "thingm",
229 .max_brightness = 255,
230 .num_leds = 1,
231 .report_size = 9,
232 .report_type = RAW_REQUEST,
233 .report_id = 1,
234 .write = thingm_write_v1,
235 };
236
237 static int thingm_init(struct hidled_device *ldev)
238 {
239 __u8 buf[MAX_REPORT_SIZE] = { [1] = 'v' };
240 int ret;
241
242 ret = hidled_recv(ldev, buf);
243 if (ret)
244 return ret;
245
246 /* Check for firmware major version 1 */
247 if (buf[3] == '1')
248 ldev->config = &hidled_config_thingm_v1;
249
250 return 0;
251 }
252
253 static const struct hidled_config hidled_configs[] = {
254 {
255 .type = RISO_KAGAKU,
256 .name = "Riso Kagaku Webmail Notifier",
257 .short_name = "riso_kagaku",
258 .max_brightness = 1,
259 .num_leds = 1,
260 .report_size = 6,
261 .report_type = OUTPUT_REPORT,
262 .report_id = 0,
263 .write = riso_kagaku_write,
264 },
265 {
266 .type = DREAM_CHEEKY,
267 .name = "Dream Cheeky Webmail Notifier",
268 .short_name = "dream_cheeky",
269 .max_brightness = 31,
270 .num_leds = 1,
271 .report_size = 9,
272 .report_type = RAW_REQUEST,
273 .report_id = 0,
274 .init = dream_cheeky_init,
275 .write = dream_cheeky_write,
276 },
277 {
278 .type = THINGM,
279 .name = "ThingM blink(1)",
280 .short_name = "thingm",
281 .max_brightness = 255,
282 .num_leds = 2,
283 .report_size = 9,
284 .report_type = RAW_REQUEST,
285 .report_id = 1,
286 .init = thingm_init,
287 .write = thingm_write,
288 },
289 };
290
291 static int hidled_init_led(struct hidled_led *led, const char *color_name,
292 struct hidled_rgb *rgb, unsigned int minor)
293 {
294 const struct hidled_config *config = rgb->ldev->config;
295
296 if (config->num_leds > 1)
297 snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u",
298 config->short_name, minor, color_name, rgb->num);
299 else
300 snprintf(led->name, sizeof(led->name), "%s%u:%s",
301 config->short_name, minor, color_name);
302 led->cdev.name = led->name;
303 led->cdev.max_brightness = config->max_brightness;
304 led->cdev.brightness_set_blocking = config->write;
305 led->cdev.flags = LED_HW_PLUGGABLE;
306 led->rgb = rgb;
307
308 return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev);
309 }
310
311 static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
312 {
313 int ret;
314
315 /* Register the red diode */
316 ret = hidled_init_led(&rgb->red, "red", rgb, minor);
317 if (ret)
318 return ret;
319
320 /* Register the green diode */
321 ret = hidled_init_led(&rgb->green, "green", rgb, minor);
322 if (ret)
323 return ret;
324
325 /* Register the blue diode */
326 return hidled_init_led(&rgb->blue, "blue", rgb, minor);
327 }
328
329 static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
330 {
331 struct hidled_device *ldev;
332 unsigned int minor;
333 int ret, i;
334
335 ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
336 if (!ldev)
337 return -ENOMEM;
338
339 ret = hid_parse(hdev);
340 if (ret)
341 return ret;
342
343 ldev->hdev = hdev;
344 mutex_init(&ldev->lock);
345
346 for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++)
347 if (hidled_configs[i].type == id->driver_data)
348 ldev->config = &hidled_configs[i];
349
350 if (!ldev->config)
351 return -EINVAL;
352
353 if (ldev->config->init) {
354 ret = ldev->config->init(ldev);
355 if (ret)
356 return ret;
357 }
358
359 ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
360 sizeof(struct hidled_rgb), GFP_KERNEL);
361 if (!ldev->rgb)
362 return -ENOMEM;
363
364 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
365 if (ret)
366 return ret;
367
368 minor = ((struct hidraw *) hdev->hidraw)->minor;
369
370 for (i = 0; i < ldev->config->num_leds; i++) {
371 ldev->rgb[i].ldev = ldev;
372 ldev->rgb[i].num = i;
373 ret = hidled_init_rgb(&ldev->rgb[i], minor);
374 if (ret) {
375 hid_hw_stop(hdev);
376 return ret;
377 }
378 }
379
380 hid_info(hdev, "%s initialized\n", ldev->config->name);
381
382 return 0;
383 }
384
385 static const struct hid_device_id hidled_table[] = {
386 { HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU,
387 USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU },
388 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
389 USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY },
390 { HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
391 USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY },
392 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM,
393 USB_DEVICE_ID_BLINK1), .driver_data = THINGM },
394 { }
395 };
396 MODULE_DEVICE_TABLE(hid, hidled_table);
397
398 static struct hid_driver hidled_driver = {
399 .name = "hid-led",
400 .probe = hidled_probe,
401 .id_table = hidled_table,
402 };
403
404 module_hid_driver(hidled_driver);
405
406 MODULE_LICENSE("GPL");
407 MODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
408 MODULE_DESCRIPTION("Simple USB RGB LED driver");
This page took 0.046027 seconds and 6 git commands to generate.