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>
15 #include <linux/usb.h>
19 #define BLINK1_CMD_SIZE 9
21 #define blink1_rgb_to_r(rgb) ((rgb & 0xFF0000) >> 16)
22 #define blink1_rgb_to_g(rgb) ((rgb & 0x00FF00) >> 8)
23 #define blink1_rgb_to_b(rgb) ((rgb & 0x0000FF) >> 0)
26 * struct blink1_data - blink(1) device specific data
28 * @led_cdev: LED class instance.
29 * @rgb: 8-bit per channel RGB notation.
30 * @fade: fade time in hundredths of a second.
31 * @brightness: brightness coefficient.
32 * @play: play/pause in-memory patterns.
35 struct hid_device
*hdev
;
36 struct led_classdev led_cdev
;
43 static int blink1_send_command(struct blink1_data
*data
,
44 u8 buf
[BLINK1_CMD_SIZE
])
48 hid_dbg(data
->hdev
, "command: %d%c%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
49 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4],
50 buf
[5], buf
[6], buf
[7], buf
[8]);
52 ret
= data
->hdev
->hid_output_raw_report(data
->hdev
, buf
,
53 BLINK1_CMD_SIZE
, HID_FEATURE_REPORT
);
55 return ret
< 0 ? ret
: 0;
58 static int blink1_update_color(struct blink1_data
*data
)
60 u8 buf
[BLINK1_CMD_SIZE
] = { 1, 'n', 0, 0, 0, 0, 0, 0, 0 };
62 if (data
->brightness
) {
63 unsigned int coef
= DIV_ROUND_CLOSEST(255, data
->brightness
);
65 buf
[2] = DIV_ROUND_CLOSEST(blink1_rgb_to_r(data
->rgb
), coef
);
66 buf
[3] = DIV_ROUND_CLOSEST(blink1_rgb_to_g(data
->rgb
), coef
);
67 buf
[4] = DIV_ROUND_CLOSEST(blink1_rgb_to_b(data
->rgb
), coef
);
72 buf
[5] = (data
->fade
& 0xFF00) >> 8;
73 buf
[6] = (data
->fade
& 0x00FF);
76 return blink1_send_command(data
, buf
);
79 static void blink1_led_set(struct led_classdev
*led_cdev
,
80 enum led_brightness brightness
)
82 struct blink1_data
*data
= dev_get_drvdata(led_cdev
->dev
->parent
);
84 data
->brightness
= brightness
;
85 if (blink1_update_color(data
))
86 hid_err(data
->hdev
, "failed to update color\n");
89 static enum led_brightness
blink1_led_get(struct led_classdev
*led_cdev
)
91 struct blink1_data
*data
= dev_get_drvdata(led_cdev
->dev
->parent
);
93 return data
->brightness
;
96 static ssize_t
blink1_show_rgb(struct device
*dev
,
97 struct device_attribute
*attr
, char *buf
)
99 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
101 return sprintf(buf
, "%.6X\n", data
->rgb
);
104 static ssize_t
blink1_store_rgb(struct device
*dev
,
105 struct device_attribute
*attr
, const char *buf
, size_t count
)
107 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
108 long unsigned int rgb
;
111 ret
= kstrtoul(buf
, 16, &rgb
);
115 /* RGB triplet notation is 24-bit hexadecimal */
120 ret
= blink1_update_color(data
);
122 return ret
? ret
: count
;
125 static DEVICE_ATTR(rgb
, S_IRUGO
| S_IWUSR
, blink1_show_rgb
, blink1_store_rgb
);
127 static ssize_t
blink1_show_fade(struct device
*dev
,
128 struct device_attribute
*attr
, char *buf
)
130 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
132 return sprintf(buf
, "%d\n", data
->fade
* 10);
135 static ssize_t
blink1_store_fade(struct device
*dev
,
136 struct device_attribute
*attr
, const char *buf
, size_t count
)
138 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
139 long unsigned int fade
;
142 ret
= kstrtoul(buf
, 10, &fade
);
146 /* blink(1) accepts 16-bit fade time, number of 10ms ticks */
147 fade
= DIV_ROUND_CLOSEST(fade
, 10);
156 static DEVICE_ATTR(fade
, S_IRUGO
| S_IWUSR
,
157 blink1_show_fade
, blink1_store_fade
);
159 static ssize_t
blink1_show_play(struct device
*dev
,
160 struct device_attribute
*attr
, char *buf
)
162 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
164 return sprintf(buf
, "%d\n", data
->play
);
167 static ssize_t
blink1_store_play(struct device
*dev
,
168 struct device_attribute
*attr
, const char *buf
, size_t count
)
170 struct blink1_data
*data
= dev_get_drvdata(dev
->parent
);
171 u8 cmd
[BLINK1_CMD_SIZE
] = { 1, 'p', 0, 0, 0, 0, 0, 0, 0 };
172 long unsigned int play
;
175 ret
= kstrtoul(buf
, 10, &play
);
181 ret
= blink1_send_command(data
, cmd
);
183 return ret
? ret
: count
;
186 static DEVICE_ATTR(play
, S_IRUGO
| S_IWUSR
,
187 blink1_show_play
, blink1_store_play
);
189 static const struct attribute_group blink1_sysfs_group
= {
190 .attrs
= (struct attribute
*[]) {
198 static int thingm_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
200 struct blink1_data
*data
;
201 struct led_classdev
*led
;
205 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct blink1_data
), GFP_KERNEL
);
209 hid_set_drvdata(hdev
, data
);
211 data
->rgb
= 0xFFFFFF; /* set a default white color */
213 ret
= hid_parse(hdev
);
217 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
221 /* blink(1) serial numbers range is 0x1A001000 to 0x1A002FFF */
222 led
= &data
->led_cdev
;
223 snprintf(led_name
, sizeof(led_name
), "blink1::%s", hdev
->uniq
+ 4);
224 led
->name
= led_name
;
225 led
->brightness_set
= blink1_led_set
;
226 led
->brightness_get
= blink1_led_get
;
227 ret
= led_classdev_register(&hdev
->dev
, led
);
231 ret
= sysfs_create_group(&led
->dev
->kobj
, &blink1_sysfs_group
);
238 led_classdev_unregister(led
);
245 static void thingm_remove(struct hid_device
*hdev
)
247 struct blink1_data
*data
= hid_get_drvdata(hdev
);
248 struct led_classdev
*led
= &data
->led_cdev
;
250 sysfs_remove_group(&led
->dev
->kobj
, &blink1_sysfs_group
);
251 led_classdev_unregister(led
);
255 static const struct hid_device_id thingm_table
[] = {
256 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM
, USB_DEVICE_ID_BLINK1
) },
259 MODULE_DEVICE_TABLE(hid
, thingm_table
);
261 static struct hid_driver thingm_driver
= {
263 .probe
= thingm_probe
,
264 .remove
= thingm_remove
,
265 .id_table
= thingm_table
,
268 module_hid_driver(thingm_driver
);
270 MODULE_LICENSE("GPL");
271 MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
272 MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");