2 * ThingM blink(1) USB RGB LED driver
4 * Copyright 2013 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
12 #include <linux/hid.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
18 #define BLINK1_CMD_SIZE 9
20 #define blink1_rgb_to_r(rgb) ((rgb & 0xFF0000) >> 16)
21 #define blink1_rgb_to_g(rgb) ((rgb & 0x00FF00) >> 8)
22 #define blink1_rgb_to_b(rgb) ((rgb & 0x0000FF) >> 0)
25 * struct blink1_data - blink(1) device specific data
27 * @led_cdev: LED class instance.
28 * @rgb: 8-bit per channel RGB notation.
29 * @fade: fade time in hundredths of a second.
30 * @brightness: brightness coefficient.
31 * @play: play/pause in-memory patterns.
34 struct hid_device
*hdev
;
35 struct led_classdev led_cdev
;
42 static int blink1_send_command(struct blink1_data
*data
,
43 u8 buf
[BLINK1_CMD_SIZE
])
47 hid_dbg(data
->hdev
, "command: %d%c%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
48 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4],
49 buf
[5], buf
[6], buf
[7], buf
[8]);
51 ret
= data
->hdev
->hid_output_raw_report(data
->hdev
, buf
,
52 BLINK1_CMD_SIZE
, HID_FEATURE_REPORT
);
54 return ret
< 0 ? ret
: 0;
57 static int blink1_update_color(struct blink1_data
*data
)
59 u8 buf
[BLINK1_CMD_SIZE
] = { 1, 'n', 0, 0, 0, 0, 0, 0, 0 };
61 if (data
->brightness
) {
62 unsigned int coef
= DIV_ROUND_CLOSEST(255, data
->brightness
);
64 buf
[2] = DIV_ROUND_CLOSEST(blink1_rgb_to_r(data
->rgb
), coef
);
65 buf
[3] = DIV_ROUND_CLOSEST(blink1_rgb_to_g(data
->rgb
), coef
);
66 buf
[4] = DIV_ROUND_CLOSEST(blink1_rgb_to_b(data
->rgb
), coef
);
71 buf
[5] = (data
->fade
& 0xFF00) >> 8;
72 buf
[6] = (data
->fade
& 0x00FF);
75 return blink1_send_command(data
, buf
);
78 static void blink1_led_set(struct led_classdev
*led_cdev
,
79 enum led_brightness brightness
)
81 struct blink1_data
*data
= dev_get_drvdata(led_cdev
->dev
->parent
);
83 data
->brightness
= brightness
;
84 if (blink1_update_color(data
))
85 hid_err(data
->hdev
, "failed to update color\n");
88 static enum led_brightness
blink1_led_get(struct led_classdev
*led_cdev
)
90 struct blink1_data
*data
= dev_get_drvdata(led_cdev
->dev
->parent
);
92 return data
->brightness
;
95 static ssize_t
blink1_show_rgb(struct device
*dev
,
96 struct device_attribute
*attr
, char *buf
)
98 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
100 return sprintf(buf
, "%.6X\n", data
->rgb
);
103 static ssize_t
blink1_store_rgb(struct device
*dev
,
104 struct device_attribute
*attr
, const char *buf
, size_t count
)
106 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
107 long unsigned int rgb
;
110 ret
= kstrtoul(buf
, 16, &rgb
);
114 /* RGB triplet notation is 24-bit hexadecimal */
119 ret
= blink1_update_color(data
);
121 return ret
? ret
: count
;
124 static DEVICE_ATTR(rgb
, S_IRUGO
| S_IWUSR
, blink1_show_rgb
, blink1_store_rgb
);
126 static ssize_t
blink1_show_fade(struct device
*dev
,
127 struct device_attribute
*attr
, char *buf
)
129 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
131 return sprintf(buf
, "%d\n", data
->fade
* 10);
134 static ssize_t
blink1_store_fade(struct device
*dev
,
135 struct device_attribute
*attr
, const char *buf
, size_t count
)
137 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
138 long unsigned int fade
;
141 ret
= kstrtoul(buf
, 10, &fade
);
145 /* blink(1) accepts 16-bit fade time, number of 10ms ticks */
146 fade
= DIV_ROUND_CLOSEST(fade
, 10);
155 static DEVICE_ATTR(fade
, S_IRUGO
| S_IWUSR
,
156 blink1_show_fade
, blink1_store_fade
);
158 static ssize_t
blink1_show_play(struct device
*dev
,
159 struct device_attribute
*attr
, char *buf
)
161 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
163 return sprintf(buf
, "%d\n", data
->play
);
166 static ssize_t
blink1_store_play(struct device
*dev
,
167 struct device_attribute
*attr
, const char *buf
, size_t count
)
169 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
170 u8 cmd
[BLINK1_CMD_SIZE
] = { 1, 'p', 0, 0, 0, 0, 0, 0, 0 };
171 long unsigned int play
;
174 ret
= kstrtoul(buf
, 10, &play
);
180 ret
= blink1_send_command(data
, cmd
);
182 return ret
? ret
: count
;
185 static DEVICE_ATTR(play
, S_IRUGO
| S_IWUSR
,
186 blink1_show_play
, blink1_store_play
);
188 static const struct attribute_group blink1_sysfs_group
= {
189 .attrs
= (struct attribute
*[]) {
197 static int thingm_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
199 struct blink1_data
*data
;
200 struct led_classdev
*led
;
204 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct blink1_data
), GFP_KERNEL
);
208 hid_set_drvdata(hdev
, data
);
210 data
->rgb
= 0xFFFFFF; /* set a default white color */
212 ret
= hid_parse(hdev
);
216 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
220 /* blink(1) serial numbers range is 0x1A001000 to 0x1A002FFF */
221 led
= &data
->led_cdev
;
222 snprintf(led_name
, sizeof(led_name
), "blink1::%s", hdev
->uniq
+ 4);
223 led
->name
= led_name
;
224 led
->brightness_set
= blink1_led_set
;
225 led
->brightness_get
= blink1_led_get
;
226 ret
= led_classdev_register(&hdev
->dev
, led
);
230 ret
= sysfs_create_group(&led
->dev
->kobj
, &blink1_sysfs_group
);
237 led_classdev_unregister(led
);
244 static void thingm_remove(struct hid_device
*hdev
)
246 struct blink1_data
*data
= hid_get_drvdata(hdev
);
247 struct led_classdev
*led
= &data
->led_cdev
;
249 sysfs_remove_group(&led
->dev
->kobj
, &blink1_sysfs_group
);
250 led_classdev_unregister(led
);
254 static const struct hid_device_id thingm_table
[] = {
255 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM
, USB_DEVICE_ID_BLINK1
) },
258 MODULE_DEVICE_TABLE(hid
, thingm_table
);
260 static struct hid_driver thingm_driver
= {
262 .probe
= thingm_probe
,
263 .remove
= thingm_remove
,
264 .id_table
= thingm_table
,
267 module_hid_driver(thingm_driver
);
269 MODULE_LICENSE("GPL");
270 MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
271 MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");