Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * ThingM blink(1) USB RGB LED driver |
| 3 | * |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 4 | * Copyright 2013-2014 Savoir-faire Linux Inc. |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 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> |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 13 | #include <linux/hidraw.h> |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 14 | #include <linux/leds.h> |
| 15 | #include <linux/module.h> |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 17 | |
| 18 | #include "hid-ids.h" |
| 19 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 20 | #define REPORT_ID 1 |
| 21 | #define REPORT_SIZE 9 |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 22 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 23 | /* Firmware major number of supported devices */ |
| 24 | #define THINGM_MAJOR_MK1 '1' |
Vivien Didelot | 3121b1c | 2014-04-14 16:50:20 -0400 | [diff] [blame] | 25 | #define THINGM_MAJOR_MK2 '2' |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 26 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 27 | struct thingm_fwinfo { |
| 28 | char major; |
| 29 | unsigned numrgb; |
| 30 | unsigned first; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 31 | }; |
| 32 | |
Jiri Kosina | e4aecaf | 2014-06-03 13:29:38 +0200 | [diff] [blame] | 33 | static const struct thingm_fwinfo thingm_fwinfo[] = { |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 34 | { |
| 35 | .major = THINGM_MAJOR_MK1, |
| 36 | .numrgb = 1, |
| 37 | .first = 0, |
Vivien Didelot | 3121b1c | 2014-04-14 16:50:20 -0400 | [diff] [blame] | 38 | }, { |
| 39 | .major = THINGM_MAJOR_MK2, |
| 40 | .numrgb = 2, |
| 41 | .first = 1, |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 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; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 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]) |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 73 | { |
| 74 | int ret; |
| 75 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 76 | hid_dbg(tdev->hdev, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n", |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 77 | buf[0], buf[1], buf[2], buf[3], buf[4], |
| 78 | buf[5], buf[6], buf[7], buf[8]); |
| 79 | |
Heiner Kallweit | 43a4a04 | 2016-03-01 20:49:54 +0100 | [diff] [blame] | 80 | mutex_lock(&tdev->lock); |
| 81 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 82 | ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE, |
| 83 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 84 | |
Heiner Kallweit | 43a4a04 | 2016-03-01 20:49:54 +0100 | [diff] [blame] | 85 | mutex_unlock(&tdev->lock); |
| 86 | |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 87 | return ret < 0 ? ret : 0; |
| 88 | } |
| 89 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 90 | static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE]) |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 91 | { |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 92 | int ret; |
| 93 | |
Heiner Kallweit | 43a4a04 | 2016-03-01 20:49:54 +0100 | [diff] [blame] | 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 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 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) |
Heiner Kallweit | 43a4a04 | 2016-03-01 20:49:54 +0100 | [diff] [blame] | 109 | goto err; |
| 110 | |
| 111 | ret = 0; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 112 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 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]); |
Heiner Kallweit | 43a4a04 | 2016-03-01 20:49:54 +0100 | [diff] [blame] | 116 | err: |
| 117 | mutex_unlock(&tdev->lock); |
| 118 | return ret; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 121 | static int thingm_version(struct thingm_device *tdev) |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 122 | { |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 123 | u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 }; |
| 124 | int err; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 125 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 126 | err = thingm_recv(tdev, buf); |
| 127 | if (err) |
| 128 | return err; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 129 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 130 | tdev->version.major = buf[3]; |
| 131 | tdev->version.minor = buf[4]; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 132 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 133 | return 0; |
| 134 | } |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 135 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 136 | static int thingm_write_color(struct thingm_rgb *rgb) |
| 137 | { |
Vivien Didelot | 3121b1c | 2014-04-14 16:50:20 -0400 | [diff] [blame] | 138 | u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 }; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 139 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 140 | 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 Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 147 | static int thingm_led_set(struct led_classdev *ldev, |
| 148 | enum led_brightness brightness) |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 149 | { |
| 150 | struct thingm_led *led = container_of(ldev, struct thingm_led, ldev); |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 151 | int ret; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 152 | |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 153 | ret = thingm_write_color(led->rgb); |
| 154 | if (ret) |
| 155 | hid_err(led->rgb->tdev->hdev, "failed to write color\n"); |
| 156 | |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 157 | return ret; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int thingm_init_rgb(struct thingm_rgb *rgb) |
| 161 | { |
| 162 | const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor; |
| 163 | int err; |
| 164 | |
| 165 | /* Register the red diode */ |
| 166 | snprintf(rgb->red.name, sizeof(rgb->red.name), |
| 167 | "thingm%d:red:led%d", minor, rgb->num); |
| 168 | rgb->red.ldev.name = rgb->red.name; |
| 169 | rgb->red.ldev.max_brightness = 255; |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 170 | rgb->red.ldev.brightness_set_blocking = thingm_led_set; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 171 | rgb->red.rgb = rgb; |
| 172 | |
Heiner Kallweit | c46fab2 | 2016-02-29 21:38:26 +0100 | [diff] [blame] | 173 | err = devm_led_classdev_register(&rgb->tdev->hdev->dev, |
| 174 | &rgb->red.ldev); |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 175 | if (err) |
| 176 | return err; |
| 177 | |
| 178 | /* Register the green diode */ |
| 179 | snprintf(rgb->green.name, sizeof(rgb->green.name), |
| 180 | "thingm%d:green:led%d", minor, rgb->num); |
| 181 | rgb->green.ldev.name = rgb->green.name; |
| 182 | rgb->green.ldev.max_brightness = 255; |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 183 | rgb->green.ldev.brightness_set_blocking = thingm_led_set; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 184 | rgb->green.rgb = rgb; |
| 185 | |
Heiner Kallweit | c46fab2 | 2016-02-29 21:38:26 +0100 | [diff] [blame] | 186 | err = devm_led_classdev_register(&rgb->tdev->hdev->dev, |
| 187 | &rgb->green.ldev); |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 188 | if (err) |
Heiner Kallweit | c46fab2 | 2016-02-29 21:38:26 +0100 | [diff] [blame] | 189 | return err; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 190 | |
| 191 | /* Register the blue diode */ |
| 192 | snprintf(rgb->blue.name, sizeof(rgb->blue.name), |
| 193 | "thingm%d:blue:led%d", minor, rgb->num); |
| 194 | rgb->blue.ldev.name = rgb->blue.name; |
| 195 | rgb->blue.ldev.max_brightness = 255; |
Heiner Kallweit | 3de68ce | 2016-02-29 21:34:50 +0100 | [diff] [blame] | 196 | rgb->blue.ldev.brightness_set_blocking = thingm_led_set; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 197 | rgb->blue.rgb = rgb; |
| 198 | |
Heiner Kallweit | c46fab2 | 2016-02-29 21:38:26 +0100 | [diff] [blame] | 199 | err = devm_led_classdev_register(&rgb->tdev->hdev->dev, |
| 200 | &rgb->blue.ldev); |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 201 | return err; |
| 202 | } |
| 203 | |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 204 | static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 205 | { |
| 206 | struct thingm_device *tdev; |
| 207 | int i, err; |
| 208 | |
| 209 | tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device), |
| 210 | GFP_KERNEL); |
| 211 | if (!tdev) |
| 212 | return -ENOMEM; |
| 213 | |
| 214 | tdev->hdev = hdev; |
| 215 | hid_set_drvdata(hdev, tdev); |
| 216 | |
| 217 | err = hid_parse(hdev); |
| 218 | if (err) |
Heiner Kallweit | 1d1b564 | 2016-03-10 20:52:21 +0100 | [diff] [blame] | 219 | return err; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 220 | |
| 221 | mutex_init(&tdev->lock); |
| 222 | |
| 223 | err = thingm_version(tdev); |
| 224 | if (err) |
Heiner Kallweit | 1d1b564 | 2016-03-10 20:52:21 +0100 | [diff] [blame] | 225 | return err; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 226 | |
| 227 | hid_dbg(hdev, "firmware version: %c.%c\n", |
| 228 | tdev->version.major, tdev->version.minor); |
| 229 | |
| 230 | for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i) |
| 231 | if (thingm_fwinfo[i].major == tdev->version.major) |
| 232 | tdev->fwinfo = &thingm_fwinfo[i]; |
| 233 | |
| 234 | if (!tdev->fwinfo) { |
| 235 | hid_err(hdev, "unsupported firmware %c\n", tdev->version.major); |
Heiner Kallweit | 1d1b564 | 2016-03-10 20:52:21 +0100 | [diff] [blame] | 236 | return -ENODEV; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | tdev->rgb = devm_kzalloc(&hdev->dev, |
| 240 | sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb, |
| 241 | GFP_KERNEL); |
Heiner Kallweit | 1d1b564 | 2016-03-10 20:52:21 +0100 | [diff] [blame] | 242 | if (!tdev->rgb) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | err = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 246 | if (err) |
| 247 | return err; |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 248 | |
| 249 | for (i = 0; i < tdev->fwinfo->numrgb; ++i) { |
| 250 | struct thingm_rgb *rgb = tdev->rgb + i; |
| 251 | |
| 252 | rgb->tdev = tdev; |
| 253 | rgb->num = tdev->fwinfo->first + i; |
| 254 | err = thingm_init_rgb(rgb); |
Heiner Kallweit | 1d1b564 | 2016-03-10 20:52:21 +0100 | [diff] [blame] | 255 | if (err) { |
| 256 | hid_hw_stop(hdev); |
| 257 | return err; |
| 258 | } |
Vivien Didelot | f70ed8a | 2014-04-14 16:50:19 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return 0; |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 264 | static const struct hid_device_id thingm_table[] = { |
| 265 | { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) }, |
| 266 | { } |
| 267 | }; |
| 268 | MODULE_DEVICE_TABLE(hid, thingm_table); |
| 269 | |
| 270 | static struct hid_driver thingm_driver = { |
| 271 | .name = "thingm", |
| 272 | .probe = thingm_probe, |
Vivien Didelot | 30ba2fb | 2013-01-22 12:01:21 -0500 | [diff] [blame] | 273 | .id_table = thingm_table, |
| 274 | }; |
| 275 | |
| 276 | module_hid_driver(thingm_driver); |
| 277 | |
| 278 | MODULE_LICENSE("GPL"); |
| 279 | MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>"); |
| 280 | MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver"); |