blob: 2507bbe0eea7fac5cc27f66ac49d778d85b01169 [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
Heiner Kallweit43a4a042016-03-01 20:49:54 +010080 mutex_lock(&tdev->lock);
81
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040082 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
83 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050084
Heiner Kallweit43a4a042016-03-01 20:49:54 +010085 mutex_unlock(&tdev->lock);
86
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050087 return ret < 0 ? ret : 0;
88}
89
Vivien Didelotf70ed8a2014-04-14 16:50:19 -040090static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050091{
Vivien Didelot30ba2fb2013-01-22 12:01:21 -050092 int ret;
93
Heiner Kallweit43a4a042016-03-01 20:49:54 +010094 /*
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
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400106 ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
107 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
108 if (ret < 0)
Heiner Kallweit43a4a042016-03-01 20:49:54 +0100109 goto err;
110
111 ret = 0;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500112
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400113 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]);
Heiner Kallweit43a4a042016-03-01 20:49:54 +0100116err:
117 mutex_unlock(&tdev->lock);
118 return ret;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500119}
120
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400121static int thingm_version(struct thingm_device *tdev)
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500122{
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400123 u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 };
124 int err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500125
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400126 err = thingm_recv(tdev, buf);
127 if (err)
128 return err;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500129
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400130 tdev->version.major = buf[3];
131 tdev->version.minor = buf[4];
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500132
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400133 return 0;
134}
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500135
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400136static int thingm_write_color(struct thingm_rgb *rgb)
137{
Vivien Didelot3121b1c2014-04-14 16:50:20 -0400138 u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 };
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500139
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400140 buf[2] = rgb->red.ldev.brightness;
141 buf[3] = rgb->green.ldev.brightness;
142 buf[4] = rgb->blue.ldev.brightness;
143
144 return thingm_send(rgb->tdev, buf);
145}
146
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100147static int thingm_led_set(struct led_classdev *ldev,
148 enum led_brightness brightness)
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400149{
150 struct thingm_led *led = container_of(ldev, struct thingm_led, ldev);
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100151 int ret;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400152
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100153 ret = thingm_write_color(led->rgb);
154 if (ret)
155 hid_err(led->rgb->tdev->hdev, "failed to write color\n");
156
Heiner Kallweit3de68ce2016-02-29 21:34:50 +0100157 return ret;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400158}
159
Heiner Kallweit91540cc2016-04-17 22:46:59 +0200160static 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;
Heiner Kallweita4362fd2016-04-17 22:51:46 +0200168 led->ldev.flags = LED_HW_PLUGGABLE;
Heiner Kallweit91540cc2016-04-17 22:46:59 +0200169 led->rgb = rgb;
170 return devm_led_classdev_register(&rgb->tdev->hdev->dev, &led->ldev);
171}
172
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400173static int thingm_init_rgb(struct thingm_rgb *rgb)
174{
175 const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor;
176 int err;
177
178 /* Register the red diode */
Heiner Kallweit91540cc2016-04-17 22:46:59 +0200179 err = thingm_init_led(&rgb->red, "red", rgb, minor);
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400180 if (err)
181 return err;
182
183 /* Register the green diode */
Heiner Kallweit91540cc2016-04-17 22:46:59 +0200184 err = thingm_init_led(&rgb->green, "green", rgb, minor);
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400185 if (err)
Heiner Kallweitc46fab22016-02-29 21:38:26 +0100186 return err;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400187
188 /* Register the blue diode */
Heiner Kallweit91540cc2016-04-17 22:46:59 +0200189 return thingm_init_led(&rgb->blue, "blue", rgb, minor);
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400190}
191
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400192static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
193{
194 struct thingm_device *tdev;
195 int i, err;
196
197 tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device),
198 GFP_KERNEL);
199 if (!tdev)
200 return -ENOMEM;
201
202 tdev->hdev = hdev;
203 hid_set_drvdata(hdev, tdev);
204
205 err = hid_parse(hdev);
206 if (err)
Heiner Kallweit1d1b5642016-03-10 20:52:21 +0100207 return err;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400208
209 mutex_init(&tdev->lock);
210
211 err = thingm_version(tdev);
212 if (err)
Heiner Kallweit1d1b5642016-03-10 20:52:21 +0100213 return err;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400214
215 hid_dbg(hdev, "firmware version: %c.%c\n",
216 tdev->version.major, tdev->version.minor);
217
218 for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i)
219 if (thingm_fwinfo[i].major == tdev->version.major)
220 tdev->fwinfo = &thingm_fwinfo[i];
221
222 if (!tdev->fwinfo) {
223 hid_err(hdev, "unsupported firmware %c\n", tdev->version.major);
Heiner Kallweit1d1b5642016-03-10 20:52:21 +0100224 return -ENODEV;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400225 }
226
227 tdev->rgb = devm_kzalloc(&hdev->dev,
228 sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb,
229 GFP_KERNEL);
Heiner Kallweit1d1b5642016-03-10 20:52:21 +0100230 if (!tdev->rgb)
231 return -ENOMEM;
232
233 err = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
234 if (err)
235 return err;
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400236
237 for (i = 0; i < tdev->fwinfo->numrgb; ++i) {
238 struct thingm_rgb *rgb = tdev->rgb + i;
239
240 rgb->tdev = tdev;
241 rgb->num = tdev->fwinfo->first + i;
242 err = thingm_init_rgb(rgb);
Heiner Kallweit1d1b5642016-03-10 20:52:21 +0100243 if (err) {
244 hid_hw_stop(hdev);
245 return err;
246 }
Vivien Didelotf70ed8a2014-04-14 16:50:19 -0400247 }
248
249 return 0;
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500250}
251
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500252static const struct hid_device_id thingm_table[] = {
253 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
254 { }
255};
256MODULE_DEVICE_TABLE(hid, thingm_table);
257
258static struct hid_driver thingm_driver = {
259 .name = "thingm",
260 .probe = thingm_probe,
Vivien Didelot30ba2fb2013-01-22 12:01:21 -0500261 .id_table = thingm_table,
262};
263
264module_hid_driver(thingm_driver);
265
266MODULE_LICENSE("GPL");
267MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
268MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");