Commit | Line | Data |
---|---|---|
30ba2fbd VD |
1 | /* |
2 | * ThingM blink(1) USB RGB LED driver | |
3 | * | |
f70ed8a6 | 4 | * Copyright 2013-2014 Savoir-faire Linux Inc. |
30ba2fbd VD |
5 | * 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> | |
f70ed8a6 | 13 | #include <linux/hidraw.h> |
30ba2fbd VD |
14 | #include <linux/leds.h> |
15 | #include <linux/module.h> | |
f70ed8a6 | 16 | #include <linux/mutex.h> |
30ba2fbd VD |
17 | |
18 | #include "hid-ids.h" | |
19 | ||
f70ed8a6 VD |
20 | #define REPORT_ID 1 |
21 | #define REPORT_SIZE 9 | |
30ba2fbd | 22 | |
f70ed8a6 VD |
23 | /* Firmware major number of supported devices */ |
24 | #define THINGM_MAJOR_MK1 '1' | |
3121b1c4 | 25 | #define THINGM_MAJOR_MK2 '2' |
30ba2fbd | 26 | |
f70ed8a6 VD |
27 | struct thingm_fwinfo { |
28 | char major; | |
29 | unsigned numrgb; | |
30 | unsigned first; | |
31 | }; | |
32 | ||
e4aecaf2 | 33 | static const struct thingm_fwinfo thingm_fwinfo[] = { |
f70ed8a6 VD |
34 | { |
35 | .major = THINGM_MAJOR_MK1, | |
36 | .numrgb = 1, | |
37 | .first = 0, | |
3121b1c4 VD |
38 | }, { |
39 | .major = THINGM_MAJOR_MK2, | |
40 | .numrgb = 2, | |
41 | .first = 1, | |
f70ed8a6 VD |
42 | } |
43 | }; | |
44 | ||
45 | /* A red, green or blue channel, part of an RGB chip */ | |
46 | struct 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 */ | |
53 | struct thingm_rgb { | |
54 | struct thingm_device *tdev; | |
55 | struct thingm_led red; | |
56 | struct thingm_led green; | |
57 | struct thingm_led blue; | |
f70ed8a6 VD |
58 | u8 num; |
59 | }; | |
60 | ||
61 | struct thingm_device { | |
30ba2fbd | 62 | struct hid_device *hdev; |
f70ed8a6 VD |
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; | |
30ba2fbd VD |
70 | }; |
71 | ||
f70ed8a6 | 72 | static int thingm_send(struct thingm_device *tdev, u8 buf[REPORT_SIZE]) |
30ba2fbd VD |
73 | { |
74 | int ret; | |
75 | ||
f70ed8a6 | 76 | hid_dbg(tdev->hdev, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n", |
30ba2fbd VD |
77 | buf[0], buf[1], buf[2], buf[3], buf[4], |
78 | buf[5], buf[6], buf[7], buf[8]); | |
79 | ||
43a4a04d HK |
80 | mutex_lock(&tdev->lock); |
81 | ||
f70ed8a6 VD |
82 | ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE, |
83 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); | |
30ba2fbd | 84 | |
43a4a04d HK |
85 | mutex_unlock(&tdev->lock); |
86 | ||
30ba2fbd VD |
87 | return ret < 0 ? ret : 0; |
88 | } | |
89 | ||
f70ed8a6 | 90 | static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE]) |
30ba2fbd | 91 | { |
f70ed8a6 | 92 | int ret; |
30ba2fbd | 93 | |
43a4a04d HK |
94 | /* |
95 | * A read consists of two operations: sending the read command | |
96 | * and the actual read from the device. Use the mutex to protect | |
97 | * the full sequence of both operations. | |
98 | */ | |
99 | mutex_lock(&tdev->lock); | |
100 | ||
101 | ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE, | |
102 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); | |
103 | if (ret < 0) | |
104 | goto err; | |
105 | ||
f70ed8a6 VD |
106 | ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE, |
107 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | |
108 | if (ret < 0) | |
43a4a04d HK |
109 | goto err; |
110 | ||
111 | ret = 0; | |
30ba2fbd | 112 | |
f70ed8a6 VD |
113 | hid_dbg(tdev->hdev, "<- %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n", |
114 | buf[0], buf[1], buf[2], buf[3], buf[4], | |
115 | buf[5], buf[6], buf[7], buf[8]); | |
43a4a04d HK |
116 | err: |
117 | mutex_unlock(&tdev->lock); | |
118 | return ret; | |
30ba2fbd VD |
119 | } |
120 | ||
f70ed8a6 | 121 | static int thingm_version(struct thingm_device *tdev) |
30ba2fbd | 122 | { |
f70ed8a6 VD |
123 | u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 }; |
124 | int err; | |
125 | ||
f70ed8a6 VD |
126 | err = thingm_recv(tdev, buf); |
127 | if (err) | |
128 | return err; | |
30ba2fbd | 129 | |
f70ed8a6 VD |
130 | tdev->version.major = buf[3]; |
131 | tdev->version.minor = buf[4]; | |
132 | ||
133 | return 0; | |
30ba2fbd VD |
134 | } |
135 | ||
f70ed8a6 | 136 | static int thingm_write_color(struct thingm_rgb *rgb) |
30ba2fbd | 137 | { |
3121b1c4 | 138 | u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 }; |
f70ed8a6 VD |
139 | |
140 | buf[2] = rgb->red.ldev.brightness; | |
141 | buf[3] = rgb->green.ldev.brightness; | |
142 | buf[4] = rgb->blue.ldev.brightness; | |
30ba2fbd | 143 | |
f70ed8a6 | 144 | return thingm_send(rgb->tdev, buf); |
30ba2fbd VD |
145 | } |
146 | ||
3de68ce9 HK |
147 | static int thingm_led_set(struct led_classdev *ldev, |
148 | enum led_brightness brightness) | |
30ba2fbd | 149 | { |
3de68ce9 HK |
150 | struct thingm_led *led = container_of(ldev, struct thingm_led, ldev); |
151 | int ret; | |
30ba2fbd | 152 | |
3de68ce9 HK |
153 | ret = thingm_write_color(led->rgb); |
154 | if (ret) | |
155 | hid_err(led->rgb->tdev->hdev, "failed to write color\n"); | |
f70ed8a6 | 156 | |
3de68ce9 | 157 | return ret; |
f70ed8a6 | 158 | } |
30ba2fbd | 159 | |
91540ccc HK |
160 | static int thingm_init_led(struct thingm_led *led, const char *color_name, |
161 | struct thingm_rgb *rgb, int minor) | |
162 | { | |
163 | snprintf(led->name, sizeof(led->name), "thingm%d:%s:led%d", | |
164 | minor, color_name, rgb->num); | |
165 | led->ldev.name = led->name; | |
166 | led->ldev.max_brightness = 255; | |
167 | led->ldev.brightness_set_blocking = thingm_led_set; | |
168 | led->rgb = rgb; | |
169 | return devm_led_classdev_register(&rgb->tdev->hdev->dev, &led->ldev); | |
170 | } | |
171 | ||
f70ed8a6 VD |
172 | static int thingm_init_rgb(struct thingm_rgb *rgb) |
173 | { | |
174 | const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor; | |
175 | int err; | |
176 | ||
177 | /* Register the red diode */ | |
91540ccc | 178 | err = thingm_init_led(&rgb->red, "red", rgb, minor); |
f70ed8a6 VD |
179 | if (err) |
180 | return err; | |
181 | ||
182 | /* Register the green diode */ | |
91540ccc | 183 | err = thingm_init_led(&rgb->green, "green", rgb, minor); |
f70ed8a6 | 184 | if (err) |
c46fab28 | 185 | return err; |
f70ed8a6 VD |
186 | |
187 | /* Register the blue diode */ | |
91540ccc | 188 | return thingm_init_led(&rgb->blue, "blue", rgb, minor); |
f70ed8a6 VD |
189 | } |
190 | ||
30ba2fbd VD |
191 | static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id) |
192 | { | |
f70ed8a6 VD |
193 | struct thingm_device *tdev; |
194 | int i, err; | |
30ba2fbd | 195 | |
f70ed8a6 VD |
196 | tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device), |
197 | GFP_KERNEL); | |
198 | if (!tdev) | |
30ba2fbd VD |
199 | return -ENOMEM; |
200 | ||
f70ed8a6 VD |
201 | tdev->hdev = hdev; |
202 | hid_set_drvdata(hdev, tdev); | |
30ba2fbd | 203 | |
f70ed8a6 VD |
204 | err = hid_parse(hdev); |
205 | if (err) | |
1d1b564f | 206 | return err; |
30ba2fbd | 207 | |
f70ed8a6 VD |
208 | mutex_init(&tdev->lock); |
209 | ||
210 | err = thingm_version(tdev); | |
211 | if (err) | |
1d1b564f | 212 | return err; |
30ba2fbd | 213 | |
f70ed8a6 VD |
214 | hid_dbg(hdev, "firmware version: %c.%c\n", |
215 | tdev->version.major, tdev->version.minor); | |
30ba2fbd | 216 | |
f70ed8a6 VD |
217 | for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i) |
218 | if (thingm_fwinfo[i].major == tdev->version.major) | |
219 | tdev->fwinfo = &thingm_fwinfo[i]; | |
220 | ||
221 | if (!tdev->fwinfo) { | |
222 | hid_err(hdev, "unsupported firmware %c\n", tdev->version.major); | |
1d1b564f | 223 | return -ENODEV; |
f70ed8a6 VD |
224 | } |
225 | ||
226 | tdev->rgb = devm_kzalloc(&hdev->dev, | |
227 | sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb, | |
228 | GFP_KERNEL); | |
1d1b564f HK |
229 | if (!tdev->rgb) |
230 | return -ENOMEM; | |
231 | ||
232 | err = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
233 | if (err) | |
234 | return err; | |
f70ed8a6 VD |
235 | |
236 | for (i = 0; i < tdev->fwinfo->numrgb; ++i) { | |
237 | struct thingm_rgb *rgb = tdev->rgb + i; | |
238 | ||
239 | rgb->tdev = tdev; | |
240 | rgb->num = tdev->fwinfo->first + i; | |
241 | err = thingm_init_rgb(rgb); | |
1d1b564f HK |
242 | if (err) { |
243 | hid_hw_stop(hdev); | |
244 | return err; | |
245 | } | |
f70ed8a6 | 246 | } |
30ba2fbd | 247 | |
f70ed8a6 | 248 | return 0; |
30ba2fbd VD |
249 | } |
250 | ||
30ba2fbd VD |
251 | static const struct hid_device_id thingm_table[] = { |
252 | { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) }, | |
253 | { } | |
254 | }; | |
255 | MODULE_DEVICE_TABLE(hid, thingm_table); | |
256 | ||
257 | static struct hid_driver thingm_driver = { | |
258 | .name = "thingm", | |
259 | .probe = thingm_probe, | |
30ba2fbd VD |
260 | .id_table = thingm_table, |
261 | }; | |
262 | ||
263 | module_hid_driver(thingm_driver); | |
264 | ||
265 | MODULE_LICENSE("GPL"); | |
266 | MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>"); | |
267 | MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver"); |