blob: 69a6ada09b399776ab3c5658e6cf545d18e24b7d [file] [log] [blame]
Vivien Didelot30ba2fb2013-01-22 12:01:21 -05001/*
2 * ThingM blink(1) USB RGB LED driver
3 *
Vivien Didelotf70ed8a2014-04-14 16:50:19 -04004 * Copyright 2013-2014 Savoir-faire Linux Inc.
Vivien Didelot30ba2fb2013-01-22 12:01:21 -05005 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6 *
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.
10 */
11
12#include <linux/hid.h>
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040013#include <linux/hidraw.h>
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050014#include <linux/leds.h>
15#include <linux/module.h>
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040016#include <linux/mutex.h>
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050017
18#include "hid-ids.h"
19
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040020#define REPORT_ID 1
21#define REPORT_SIZE 9
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050022
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040023/* Firmware major number of supported devices */
24#define THINGM_MAJOR_MK1 '1'
Vivien Didelot3121b1c2014-04-14 16:50:20 -040025#define THINGM_MAJOR_MK2 '2'
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050026
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040027struct thingm_fwinfo {
28 char major;
29 unsigned numrgb;
30 unsigned first;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050031};
32
Jiri Kosinae4aecaf2014-06-03 13:29:38 +020033static const struct thingm_fwinfo thingm_fwinfo[] = {
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040034 {
35 .major = THINGM_MAJOR_MK1,
36 .numrgb = 1,
37 .first = 0,
Vivien Didelot3121b1c2014-04-14 16:50:20 -040038 }, {
39 .major = THINGM_MAJOR_MK2,
40 .numrgb = 2,
41 .first = 1,
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040042 }
43};
44
45/* A red, green or blue channel, part of an RGB chip */
46struct thingm_led {
47 struct thingm_rgb *rgb;
48 struct led_classdev ldev;
49 char name[32];
50};
51
52/* Basically a WS2812 5050 RGB LED chip */
53struct thingm_rgb {
54 struct thingm_device *tdev;
55 struct thingm_led red;
56 struct thingm_led green;
57 struct thingm_led blue;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040058 u8 num;
59};
60
61struct thingm_device {
62 struct hid_device *hdev;
63 struct {
64 char major;
65 char minor;
66 } version;
67 const struct thingm_fwinfo *fwinfo;
68 struct mutex lock;
69 struct thingm_rgb *rgb;
70};
71
72static int thingm_send(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050073{
74 int ret;
75
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040076 hid_dbg(tdev->hdev, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050077 buf[0], buf[1], buf[2], buf[3], buf[4],
78 buf[5], buf[6], buf[7], buf[8]);
79
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040080 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
81 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050082
83 return ret < 0 ? ret : 0;
84}
85
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040086static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050087{
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050088 int ret;
89
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040090 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
91 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
92 if (ret < 0)
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050093 return ret;
94
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040095 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]);
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050098
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040099 return 0;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500100}
101
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400102static int thingm_version(struct thingm_device *tdev)
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500103{
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400104 u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 };
105 int err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500106
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400107 err = thingm_send(tdev, buf);
108 if (err)
109 return err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500110
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400111 err = thingm_recv(tdev, buf);
112 if (err)
113 return err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500114
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400115 tdev->version.major = buf[3];
116 tdev->version.minor = buf[4];
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500117
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400118 return 0;
119}
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500120
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400121static int thingm_write_color(struct thingm_rgb *rgb)
122{
Vivien Didelot3121b1c2014-04-14 16:50:20 -0400123 u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 };
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500124
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400125 buf[2] = rgb->red.ldev.brightness;
126 buf[3] = rgb->green.ldev.brightness;
127 buf[4] = rgb->blue.ldev.brightness;
128
129 return thingm_send(rgb->tdev, buf);
130}
131
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100132static int thingm_led_set(struct led_classdev *ldev,
133 enum led_brightness brightness)
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400134{
135 struct thingm_led *led = container_of(ldev, struct thingm_led, ldev);
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100136 int ret;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400137
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100138 mutex_lock(&led->rgb->tdev->lock);
139
140 ret = thingm_write_color(led->rgb);
141 if (ret)
142 hid_err(led->rgb->tdev->hdev, "failed to write color\n");
143
144 mutex_unlock(&led->rgb->tdev->lock);
145
146 return ret;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400147}
148
149static int thingm_init_rgb(struct thingm_rgb *rgb)
150{
151 const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor;
152 int err;
153
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;
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100159 rgb->red.ldev.brightness_set_blocking = thingm_led_set;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400160 rgb->red.rgb = rgb;
161
162 err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->red.ldev);
163 if (err)
164 return err;
165
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;
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100171 rgb->green.ldev.brightness_set_blocking = thingm_led_set;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400172 rgb->green.rgb = rgb;
173
174 err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->green.ldev);
175 if (err)
176 goto unregister_red;
177
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;
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100183 rgb->blue.ldev.brightness_set_blocking = thingm_led_set;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400184 rgb->blue.rgb = rgb;
185
186 err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->blue.ldev);
187 if (err)
188 goto unregister_green;
189
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500190 return 0;
191
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400192unregister_green:
193 led_classdev_unregister(&rgb->green.ldev);
194
195unregister_red:
196 led_classdev_unregister(&rgb->red.ldev);
197
198 return err;
199}
200
201static void thingm_remove_rgb(struct thingm_rgb *rgb)
202{
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400203 led_classdev_unregister(&rgb->red.ldev);
204 led_classdev_unregister(&rgb->green.ldev);
205 led_classdev_unregister(&rgb->blue.ldev);
206}
207
208static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
209{
210 struct thingm_device *tdev;
211 int i, err;
212
213 tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device),
214 GFP_KERNEL);
215 if (!tdev)
216 return -ENOMEM;
217
218 tdev->hdev = hdev;
219 hid_set_drvdata(hdev, tdev);
220
221 err = hid_parse(hdev);
222 if (err)
223 goto error;
224
225 err = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
226 if (err)
227 goto error;
228
229 mutex_init(&tdev->lock);
230
231 err = thingm_version(tdev);
232 if (err)
233 goto stop;
234
235 hid_dbg(hdev, "firmware version: %c.%c\n",
236 tdev->version.major, tdev->version.minor);
237
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];
241
242 if (!tdev->fwinfo) {
243 hid_err(hdev, "unsupported firmware %c\n", tdev->version.major);
Benjamin Tissoirese4cf19f2014-09-02 15:50:43 -0400244 err = -ENODEV;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400245 goto stop;
246 }
247
248 tdev->rgb = devm_kzalloc(&hdev->dev,
249 sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb,
250 GFP_KERNEL);
251 if (!tdev->rgb) {
252 err = -ENOMEM;
253 goto stop;
254 }
255
256 for (i = 0; i < tdev->fwinfo->numrgb; ++i) {
257 struct thingm_rgb *rgb = tdev->rgb + i;
258
259 rgb->tdev = tdev;
260 rgb->num = tdev->fwinfo->first + i;
261 err = thingm_init_rgb(rgb);
262 if (err) {
263 while (--i >= 0)
264 thingm_remove_rgb(tdev->rgb + i);
265 goto stop;
266 }
267 }
268
269 return 0;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500270stop:
271 hid_hw_stop(hdev);
272error:
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400273 return err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500274}
275
276static void thingm_remove(struct hid_device *hdev)
277{
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400278 struct thingm_device *tdev = hid_get_drvdata(hdev);
279 int i;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500280
Jiri Kosina67a97842014-09-04 08:56:06 +0200281 hid_hw_stop(hdev);
282
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400283 for (i = 0; i < tdev->fwinfo->numrgb; ++i)
284 thingm_remove_rgb(tdev->rgb + i);
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500285}
286
287static const struct hid_device_id thingm_table[] = {
288 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
289 { }
290};
291MODULE_DEVICE_TABLE(hid, thingm_table);
292
293static struct hid_driver thingm_driver = {
294 .name = "thingm",
295 .probe = thingm_probe,
296 .remove = thingm_remove,
297 .id_table = thingm_table,
298};
299
300module_hid_driver(thingm_driver);
301
302MODULE_LICENSE("GPL");
303MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
304MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");