procfs: Mark create_proc_read_entry deprecated
[linux-2.6.git] / drivers / hid / hid-thingm.c
blob2055a52e9a20f4682866c65b972a6ad96afce8e8
1 /*
2 * ThingM blink(1) USB RGB LED driver
4 * Copyright 2013 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
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.
12 #include <linux/hid.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
17 #include "hid-ids.h"
19 #define BLINK1_CMD_SIZE 9
21 #define blink1_rgb_to_r(rgb) ((rgb & 0xFF0000) >> 16)
22 #define blink1_rgb_to_g(rgb) ((rgb & 0x00FF00) >> 8)
23 #define blink1_rgb_to_b(rgb) ((rgb & 0x0000FF) >> 0)
25 /**
26 * struct blink1_data - blink(1) device specific data
27 * @hdev: HID device.
28 * @led_cdev: LED class instance.
29 * @rgb: 8-bit per channel RGB notation.
30 * @fade: fade time in hundredths of a second.
31 * @brightness: brightness coefficient.
32 * @play: play/pause in-memory patterns.
34 struct blink1_data {
35 struct hid_device *hdev;
36 struct led_classdev led_cdev;
37 u32 rgb;
38 u16 fade;
39 u8 brightness;
40 bool play;
43 static int blink1_send_command(struct blink1_data *data,
44 u8 buf[BLINK1_CMD_SIZE])
46 int ret;
48 hid_dbg(data->hdev, "command: %d%c%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
49 buf[0], buf[1], buf[2], buf[3], buf[4],
50 buf[5], buf[6], buf[7], buf[8]);
52 ret = data->hdev->hid_output_raw_report(data->hdev, buf,
53 BLINK1_CMD_SIZE, HID_FEATURE_REPORT);
55 return ret < 0 ? ret : 0;
58 static int blink1_update_color(struct blink1_data *data)
60 u8 buf[BLINK1_CMD_SIZE] = { 1, 'n', 0, 0, 0, 0, 0, 0, 0 };
62 if (data->brightness) {
63 unsigned int coef = DIV_ROUND_CLOSEST(255, data->brightness);
65 buf[2] = DIV_ROUND_CLOSEST(blink1_rgb_to_r(data->rgb), coef);
66 buf[3] = DIV_ROUND_CLOSEST(blink1_rgb_to_g(data->rgb), coef);
67 buf[4] = DIV_ROUND_CLOSEST(blink1_rgb_to_b(data->rgb), coef);
70 if (data->fade) {
71 buf[1] = 'c';
72 buf[5] = (data->fade & 0xFF00) >> 8;
73 buf[6] = (data->fade & 0x00FF);
76 return blink1_send_command(data, buf);
79 static void blink1_led_set(struct led_classdev *led_cdev,
80 enum led_brightness brightness)
82 struct blink1_data *data = dev_get_drvdata(led_cdev->dev->parent);
84 data->brightness = brightness;
85 if (blink1_update_color(data))
86 hid_err(data->hdev, "failed to update color\n");
89 static enum led_brightness blink1_led_get(struct led_classdev *led_cdev)
91 struct blink1_data *data = dev_get_drvdata(led_cdev->dev->parent);
93 return data->brightness;
96 static ssize_t blink1_show_rgb(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 struct blink1_data *data = dev_get_drvdata(dev->parent);
101 return sprintf(buf, "%.6X\n", data->rgb);
104 static ssize_t blink1_store_rgb(struct device *dev,
105 struct device_attribute *attr, const char *buf, size_t count)
107 struct blink1_data *data = dev_get_drvdata(dev->parent);
108 long unsigned int rgb;
109 int ret;
111 ret = kstrtoul(buf, 16, &rgb);
112 if (ret)
113 return ret;
115 /* RGB triplet notation is 24-bit hexadecimal */
116 if (rgb > 0xFFFFFF)
117 return -EINVAL;
119 data->rgb = rgb;
120 ret = blink1_update_color(data);
122 return ret ? ret : count;
125 static DEVICE_ATTR(rgb, S_IRUGO | S_IWUSR, blink1_show_rgb, blink1_store_rgb);
127 static ssize_t blink1_show_fade(struct device *dev,
128 struct device_attribute *attr, char *buf)
130 struct blink1_data *data = dev_get_drvdata(dev->parent);
132 return sprintf(buf, "%d\n", data->fade * 10);
135 static ssize_t blink1_store_fade(struct device *dev,
136 struct device_attribute *attr, const char *buf, size_t count)
138 struct blink1_data *data = dev_get_drvdata(dev->parent);
139 long unsigned int fade;
140 int ret;
142 ret = kstrtoul(buf, 10, &fade);
143 if (ret)
144 return ret;
146 /* blink(1) accepts 16-bit fade time, number of 10ms ticks */
147 fade = DIV_ROUND_CLOSEST(fade, 10);
148 if (fade > 65535)
149 return -EINVAL;
151 data->fade = fade;
153 return count;
156 static DEVICE_ATTR(fade, S_IRUGO | S_IWUSR,
157 blink1_show_fade, blink1_store_fade);
159 static ssize_t blink1_show_play(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct blink1_data *data = dev_get_drvdata(dev->parent);
164 return sprintf(buf, "%d\n", data->play);
167 static ssize_t blink1_store_play(struct device *dev,
168 struct device_attribute *attr, const char *buf, size_t count)
170 struct blink1_data *data = dev_get_drvdata(dev->parent);
171 u8 cmd[BLINK1_CMD_SIZE] = { 1, 'p', 0, 0, 0, 0, 0, 0, 0 };
172 long unsigned int play;
173 int ret;
175 ret = kstrtoul(buf, 10, &play);
176 if (ret)
177 return ret;
179 data->play = !!play;
180 cmd[2] = data->play;
181 ret = blink1_send_command(data, cmd);
183 return ret ? ret : count;
186 static DEVICE_ATTR(play, S_IRUGO | S_IWUSR,
187 blink1_show_play, blink1_store_play);
189 static const struct attribute_group blink1_sysfs_group = {
190 .attrs = (struct attribute *[]) {
191 &dev_attr_rgb.attr,
192 &dev_attr_fade.attr,
193 &dev_attr_play.attr,
194 NULL
198 static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
200 struct blink1_data *data;
201 struct led_classdev *led;
202 char led_name[13];
203 int ret;
205 data = devm_kzalloc(&hdev->dev, sizeof(struct blink1_data), GFP_KERNEL);
206 if (!data)
207 return -ENOMEM;
209 hid_set_drvdata(hdev, data);
210 data->hdev = hdev;
211 data->rgb = 0xFFFFFF; /* set a default white color */
213 ret = hid_parse(hdev);
214 if (ret)
215 goto error;
217 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
218 if (ret)
219 goto error;
221 /* blink(1) serial numbers range is 0x1A001000 to 0x1A002FFF */
222 led = &data->led_cdev;
223 snprintf(led_name, sizeof(led_name), "blink1::%s", hdev->uniq + 4);
224 led->name = led_name;
225 led->brightness_set = blink1_led_set;
226 led->brightness_get = blink1_led_get;
227 ret = led_classdev_register(&hdev->dev, led);
228 if (ret)
229 goto stop;
231 ret = sysfs_create_group(&led->dev->kobj, &blink1_sysfs_group);
232 if (ret)
233 goto remove_led;
235 return 0;
237 remove_led:
238 led_classdev_unregister(led);
239 stop:
240 hid_hw_stop(hdev);
241 error:
242 return ret;
245 static void thingm_remove(struct hid_device *hdev)
247 struct blink1_data *data = hid_get_drvdata(hdev);
248 struct led_classdev *led = &data->led_cdev;
250 sysfs_remove_group(&led->dev->kobj, &blink1_sysfs_group);
251 led_classdev_unregister(led);
252 hid_hw_stop(hdev);
255 static const struct hid_device_id thingm_table[] = {
256 { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
259 MODULE_DEVICE_TABLE(hid, thingm_table);
261 static struct hid_driver thingm_driver = {
262 .name = "thingm",
263 .probe = thingm_probe,
264 .remove = thingm_remove,
265 .id_table = thingm_table,
268 module_hid_driver(thingm_driver);
270 MODULE_LICENSE("GPL");
271 MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
272 MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");