2 * ThingM blink(1) USB RGB LED driver
4 * Copyright 2013-2014 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/hidraw.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
23 /* Firmware major number of supported devices */
24 #define THINGM_MAJOR_MK1 '1'
25 #define THINGM_MAJOR_MK2 '2'
27 struct thingm_fwinfo
{
33 static const struct thingm_fwinfo thingm_fwinfo
[] = {
35 .major
= THINGM_MAJOR_MK1
,
39 .major
= THINGM_MAJOR_MK2
,
45 /* A red, green or blue channel, part of an RGB chip */
47 struct thingm_rgb
*rgb
;
48 struct led_classdev ldev
;
52 /* Basically a WS2812 5050 RGB LED chip */
54 struct thingm_device
*tdev
;
55 struct thingm_led red
;
56 struct thingm_led green
;
57 struct thingm_led blue
;
61 struct thingm_device
{
62 struct hid_device
*hdev
;
67 const struct thingm_fwinfo
*fwinfo
;
69 struct thingm_rgb
*rgb
;
72 static int thingm_send(struct thingm_device
*tdev
, u8 buf
[REPORT_SIZE
])
76 hid_dbg(tdev
->hdev
, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
77 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4],
78 buf
[5], buf
[6], buf
[7], buf
[8]);
80 ret
= hid_hw_raw_request(tdev
->hdev
, buf
[0], buf
, REPORT_SIZE
,
81 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
83 return ret
< 0 ? ret
: 0;
86 static int thingm_recv(struct thingm_device
*tdev
, u8 buf
[REPORT_SIZE
])
90 ret
= hid_hw_raw_request(tdev
->hdev
, buf
[0], buf
, REPORT_SIZE
,
91 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
95 hid_dbg(tdev
->hdev
, "<- %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
96 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4],
97 buf
[5], buf
[6], buf
[7], buf
[8]);
102 static int thingm_version(struct thingm_device
*tdev
)
104 u8 buf
[REPORT_SIZE
] = { REPORT_ID
, 'v', 0, 0, 0, 0, 0, 0, 0 };
107 err
= thingm_send(tdev
, buf
);
111 err
= thingm_recv(tdev
, buf
);
115 tdev
->version
.major
= buf
[3];
116 tdev
->version
.minor
= buf
[4];
121 static int thingm_write_color(struct thingm_rgb
*rgb
)
123 u8 buf
[REPORT_SIZE
] = { REPORT_ID
, 'c', 0, 0, 0, 0, 0, rgb
->num
, 0 };
125 buf
[2] = rgb
->red
.ldev
.brightness
;
126 buf
[3] = rgb
->green
.ldev
.brightness
;
127 buf
[4] = rgb
->blue
.ldev
.brightness
;
129 return thingm_send(rgb
->tdev
, buf
);
132 static int thingm_led_set(struct led_classdev
*ldev
,
133 enum led_brightness brightness
)
135 struct thingm_led
*led
= container_of(ldev
, struct thingm_led
, ldev
);
138 mutex_lock(&led
->rgb
->tdev
->lock
);
140 ret
= thingm_write_color(led
->rgb
);
142 hid_err(led
->rgb
->tdev
->hdev
, "failed to write color\n");
144 mutex_unlock(&led
->rgb
->tdev
->lock
);
149 static int thingm_init_rgb(struct thingm_rgb
*rgb
)
151 const int minor
= ((struct hidraw
*) rgb
->tdev
->hdev
->hidraw
)->minor
;
154 /* Register the red diode */
155 snprintf(rgb
->red
.name
, sizeof(rgb
->red
.name
),
156 "thingm%d:red:led%d", minor
, rgb
->num
);
157 rgb
->red
.ldev
.name
= rgb
->red
.name
;
158 rgb
->red
.ldev
.max_brightness
= 255;
159 rgb
->red
.ldev
.brightness_set_blocking
= thingm_led_set
;
162 err
= led_classdev_register(&rgb
->tdev
->hdev
->dev
, &rgb
->red
.ldev
);
166 /* Register the green diode */
167 snprintf(rgb
->green
.name
, sizeof(rgb
->green
.name
),
168 "thingm%d:green:led%d", minor
, rgb
->num
);
169 rgb
->green
.ldev
.name
= rgb
->green
.name
;
170 rgb
->green
.ldev
.max_brightness
= 255;
171 rgb
->green
.ldev
.brightness_set_blocking
= thingm_led_set
;
172 rgb
->green
.rgb
= rgb
;
174 err
= led_classdev_register(&rgb
->tdev
->hdev
->dev
, &rgb
->green
.ldev
);
178 /* Register the blue diode */
179 snprintf(rgb
->blue
.name
, sizeof(rgb
->blue
.name
),
180 "thingm%d:blue:led%d", minor
, rgb
->num
);
181 rgb
->blue
.ldev
.name
= rgb
->blue
.name
;
182 rgb
->blue
.ldev
.max_brightness
= 255;
183 rgb
->blue
.ldev
.brightness_set_blocking
= thingm_led_set
;
186 err
= led_classdev_register(&rgb
->tdev
->hdev
->dev
, &rgb
->blue
.ldev
);
188 goto unregister_green
;
193 led_classdev_unregister(&rgb
->green
.ldev
);
196 led_classdev_unregister(&rgb
->red
.ldev
);
201 static void thingm_remove_rgb(struct thingm_rgb
*rgb
)
203 led_classdev_unregister(&rgb
->red
.ldev
);
204 led_classdev_unregister(&rgb
->green
.ldev
);
205 led_classdev_unregister(&rgb
->blue
.ldev
);
208 static int thingm_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
210 struct thingm_device
*tdev
;
213 tdev
= devm_kzalloc(&hdev
->dev
, sizeof(struct thingm_device
),
219 hid_set_drvdata(hdev
, tdev
);
221 err
= hid_parse(hdev
);
225 err
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
229 mutex_init(&tdev
->lock
);
231 err
= thingm_version(tdev
);
235 hid_dbg(hdev
, "firmware version: %c.%c\n",
236 tdev
->version
.major
, tdev
->version
.minor
);
238 for (i
= 0; i
< ARRAY_SIZE(thingm_fwinfo
) && !tdev
->fwinfo
; ++i
)
239 if (thingm_fwinfo
[i
].major
== tdev
->version
.major
)
240 tdev
->fwinfo
= &thingm_fwinfo
[i
];
243 hid_err(hdev
, "unsupported firmware %c\n", tdev
->version
.major
);
248 tdev
->rgb
= devm_kzalloc(&hdev
->dev
,
249 sizeof(struct thingm_rgb
) * tdev
->fwinfo
->numrgb
,
256 for (i
= 0; i
< tdev
->fwinfo
->numrgb
; ++i
) {
257 struct thingm_rgb
*rgb
= tdev
->rgb
+ i
;
260 rgb
->num
= tdev
->fwinfo
->first
+ i
;
261 err
= thingm_init_rgb(rgb
);
264 thingm_remove_rgb(tdev
->rgb
+ i
);
276 static void thingm_remove(struct hid_device
*hdev
)
278 struct thingm_device
*tdev
= hid_get_drvdata(hdev
);
283 for (i
= 0; i
< tdev
->fwinfo
->numrgb
; ++i
)
284 thingm_remove_rgb(tdev
->rgb
+ i
);
287 static const struct hid_device_id thingm_table
[] = {
288 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM
, USB_DEVICE_ID_BLINK1
) },
291 MODULE_DEVICE_TABLE(hid
, thingm_table
);
293 static struct hid_driver thingm_driver
= {
295 .probe
= thingm_probe
,
296 .remove
= thingm_remove
,
297 .id_table
= thingm_table
,
300 module_hid_driver(thingm_driver
);
302 MODULE_LICENSE("GPL");
303 MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
304 MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");