]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-thingm.c
HID: thingm: switch to managed version of led_classdev_register
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-thingm.c
CommitLineData
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
27struct thingm_fwinfo {
28 char major;
29 unsigned numrgb;
30 unsigned first;
31};
32
e4aecaf2 33static 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 */
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;
f70ed8a6
VD
58 u8 num;
59};
60
61struct 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 72static 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
f70ed8a6
VD
80 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
81 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
30ba2fbd
VD
82
83 return ret < 0 ? ret : 0;
84}
85
f70ed8a6 86static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
30ba2fbd 87{
f70ed8a6 88 int ret;
30ba2fbd 89
f70ed8a6
VD
90 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
91 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
92 if (ret < 0)
93 return ret;
30ba2fbd 94
f70ed8a6
VD
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]);
30ba2fbd 98
f70ed8a6 99 return 0;
30ba2fbd
VD
100}
101
f70ed8a6 102static int thingm_version(struct thingm_device *tdev)
30ba2fbd 103{
f70ed8a6
VD
104 u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 };
105 int err;
106
107 err = thingm_send(tdev, buf);
108 if (err)
109 return err;
110
111 err = thingm_recv(tdev, buf);
112 if (err)
113 return err;
30ba2fbd 114
f70ed8a6
VD
115 tdev->version.major = buf[3];
116 tdev->version.minor = buf[4];
117
118 return 0;
30ba2fbd
VD
119}
120
f70ed8a6 121static int thingm_write_color(struct thingm_rgb *rgb)
30ba2fbd 122{
3121b1c4 123 u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 };
f70ed8a6
VD
124
125 buf[2] = rgb->red.ldev.brightness;
126 buf[3] = rgb->green.ldev.brightness;
127 buf[4] = rgb->blue.ldev.brightness;
30ba2fbd 128
f70ed8a6 129 return thingm_send(rgb->tdev, buf);
30ba2fbd
VD
130}
131
3de68ce9
HK
132static int thingm_led_set(struct led_classdev *ldev,
133 enum led_brightness brightness)
30ba2fbd 134{
3de68ce9
HK
135 struct thingm_led *led = container_of(ldev, struct thingm_led, ldev);
136 int ret;
30ba2fbd 137
3de68ce9 138 mutex_lock(&led->rgb->tdev->lock);
f70ed8a6 139
3de68ce9
HK
140 ret = thingm_write_color(led->rgb);
141 if (ret)
142 hid_err(led->rgb->tdev->hdev, "failed to write color\n");
f70ed8a6 143
3de68ce9 144 mutex_unlock(&led->rgb->tdev->lock);
30ba2fbd 145
3de68ce9 146 return ret;
f70ed8a6 147}
30ba2fbd 148
f70ed8a6
VD
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;
3de68ce9 159 rgb->red.ldev.brightness_set_blocking = thingm_led_set;
f70ed8a6
VD
160 rgb->red.rgb = rgb;
161
c46fab28
HK
162 err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
163 &rgb->red.ldev);
f70ed8a6
VD
164 if (err)
165 return err;
166
167 /* Register the green diode */
168 snprintf(rgb->green.name, sizeof(rgb->green.name),
169 "thingm%d:green:led%d", minor, rgb->num);
170 rgb->green.ldev.name = rgb->green.name;
171 rgb->green.ldev.max_brightness = 255;
3de68ce9 172 rgb->green.ldev.brightness_set_blocking = thingm_led_set;
f70ed8a6
VD
173 rgb->green.rgb = rgb;
174
c46fab28
HK
175 err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
176 &rgb->green.ldev);
f70ed8a6 177 if (err)
c46fab28 178 return err;
f70ed8a6
VD
179
180 /* Register the blue diode */
181 snprintf(rgb->blue.name, sizeof(rgb->blue.name),
182 "thingm%d:blue:led%d", minor, rgb->num);
183 rgb->blue.ldev.name = rgb->blue.name;
184 rgb->blue.ldev.max_brightness = 255;
3de68ce9 185 rgb->blue.ldev.brightness_set_blocking = thingm_led_set;
f70ed8a6
VD
186 rgb->blue.rgb = rgb;
187
c46fab28
HK
188 err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
189 &rgb->blue.ldev);
f70ed8a6
VD
190 return err;
191}
192
30ba2fbd
VD
193static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
194{
f70ed8a6
VD
195 struct thingm_device *tdev;
196 int i, err;
30ba2fbd 197
f70ed8a6
VD
198 tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device),
199 GFP_KERNEL);
200 if (!tdev)
30ba2fbd
VD
201 return -ENOMEM;
202
f70ed8a6
VD
203 tdev->hdev = hdev;
204 hid_set_drvdata(hdev, tdev);
30ba2fbd 205
f70ed8a6
VD
206 err = hid_parse(hdev);
207 if (err)
30ba2fbd
VD
208 goto error;
209
f70ed8a6
VD
210 err = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
211 if (err)
30ba2fbd
VD
212 goto error;
213
f70ed8a6
VD
214 mutex_init(&tdev->lock);
215
216 err = thingm_version(tdev);
217 if (err)
30ba2fbd
VD
218 goto stop;
219
f70ed8a6
VD
220 hid_dbg(hdev, "firmware version: %c.%c\n",
221 tdev->version.major, tdev->version.minor);
30ba2fbd 222
f70ed8a6
VD
223 for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i)
224 if (thingm_fwinfo[i].major == tdev->version.major)
225 tdev->fwinfo = &thingm_fwinfo[i];
226
227 if (!tdev->fwinfo) {
228 hid_err(hdev, "unsupported firmware %c\n", tdev->version.major);
e4cf19ff 229 err = -ENODEV;
f70ed8a6
VD
230 goto stop;
231 }
232
233 tdev->rgb = devm_kzalloc(&hdev->dev,
234 sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb,
235 GFP_KERNEL);
236 if (!tdev->rgb) {
237 err = -ENOMEM;
238 goto stop;
239 }
240
241 for (i = 0; i < tdev->fwinfo->numrgb; ++i) {
242 struct thingm_rgb *rgb = tdev->rgb + i;
243
244 rgb->tdev = tdev;
245 rgb->num = tdev->fwinfo->first + i;
246 err = thingm_init_rgb(rgb);
c46fab28 247 if (err)
f70ed8a6 248 goto stop;
f70ed8a6 249 }
30ba2fbd 250
f70ed8a6 251 return 0;
30ba2fbd
VD
252stop:
253 hid_hw_stop(hdev);
254error:
f70ed8a6 255 return err;
30ba2fbd
VD
256}
257
30ba2fbd
VD
258static const struct hid_device_id thingm_table[] = {
259 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
260 { }
261};
262MODULE_DEVICE_TABLE(hid, thingm_table);
263
264static struct hid_driver thingm_driver = {
265 .name = "thingm",
266 .probe = thingm_probe,
30ba2fbd
VD
267 .id_table = thingm_table,
268};
269
270module_hid_driver(thingm_driver);
271
272MODULE_LICENSE("GPL");
273MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
274MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");