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