2 * cros_ec_lightbar - expose the Chromebook Pixel lightbar to userspace
4 * Copyright (C) 2014 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #define pr_fmt(fmt) "cros_ec_lightbar: " fmt
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
26 #include <linux/kobject.h>
27 #include <linux/mfd/cros_ec.h>
28 #include <linux/mfd/cros_ec_commands.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <linux/uaccess.h>
35 #include "cros_ec_dev.h"
37 /* Rate-limit the lightbar interface to prevent DoS. */
38 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
40 static ssize_t interval_msec_show(struct device *dev,
41 struct device_attribute *attr, char *buf)
43 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
45 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
48 static ssize_t interval_msec_store(struct device *dev,
49 struct device_attribute *attr,
50 const char *buf, size_t count)
54 if (kstrtoul(buf, 0, &msec))
57 lb_interval_jiffies = msec * HZ / 1000;
62 static DEFINE_MUTEX(lb_mutex);
63 /* Return 0 if able to throttle correctly, error otherwise */
64 static int lb_throttle(void)
66 static unsigned long last_access;
67 unsigned long now, next_timeslot;
71 mutex_lock(&lb_mutex);
74 next_timeslot = last_access + lb_interval_jiffies;
76 if (time_before(now, next_timeslot)) {
77 delay = (long)(next_timeslot) - (long)now;
78 set_current_state(TASK_INTERRUPTIBLE);
79 if (schedule_timeout(delay) > 0) {
80 /* interrupted - just abort */
89 mutex_unlock(&lb_mutex);
94 #define INIT_MSG(P, R) { \
95 .command = EC_CMD_LIGHTBAR_CMD, \
96 .outsize = sizeof(*P), \
97 .insize = sizeof(*R), \
100 static int get_lightbar_version(struct cros_ec_device *ec,
101 uint32_t *ver_ptr, uint32_t *flg_ptr)
103 struct ec_params_lightbar *param;
104 struct ec_response_lightbar *resp;
105 struct cros_ec_command msg = INIT_MSG(param, resp);
108 param = (struct ec_params_lightbar *)msg.outdata;
109 param->cmd = LIGHTBAR_CMD_VERSION;
110 ret = cros_ec_cmd_xfer(ec, &msg);
114 switch (msg.result) {
115 case EC_RES_INVALID_PARAM:
116 /* Pixel had no version command. */
124 resp = (struct ec_response_lightbar *)msg.indata;
126 /* Future devices w/lightbars should implement this command */
128 *ver_ptr = resp->version.num;
130 *flg_ptr = resp->version.flags;
134 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
138 static ssize_t version_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
141 uint32_t version, flags;
142 struct cros_ec_device *ec = dev_get_drvdata(dev);
149 /* This should always succeed, because we check during init. */
150 if (!get_lightbar_version(ec, &version, &flags))
153 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
156 static ssize_t brightness_store(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
160 struct ec_params_lightbar *param;
161 struct ec_response_lightbar *resp;
162 struct cros_ec_command msg = INIT_MSG(param, resp);
165 struct cros_ec_device *ec = dev_get_drvdata(dev);
167 if (kstrtouint(buf, 0, &val))
170 param = (struct ec_params_lightbar *)msg.outdata;
171 param->cmd = LIGHTBAR_CMD_BRIGHTNESS;
172 param->brightness.num = val;
177 ret = cros_ec_cmd_xfer(ec, &msg);
181 if (msg.result != EC_RES_SUCCESS)
189 * We expect numbers, and we'll keep reading until we find them, skipping over
190 * any whitespace (sysfs guarantees that the input is null-terminated). Every
191 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
192 * parsing error, if we don't parse any numbers, or if we have numbers left
195 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
198 struct ec_params_lightbar *param;
199 struct ec_response_lightbar *resp;
200 struct cros_ec_command msg = INIT_MSG(param, resp);
201 struct cros_ec_device *ec = dev_get_drvdata(dev);
203 int ret, i = 0, j = 0, ok = 0;
206 /* Skip any whitespace */
207 while (*buf && isspace(*buf))
213 ret = sscanf(buf, "%i", &val[i++]);
218 param = (struct ec_params_lightbar *)msg.outdata;
219 param->cmd = LIGHTBAR_CMD_RGB;
220 param->rgb.led = val[0];
221 param->rgb.red = val[1];
222 param->rgb.green = val[2];
223 param->rgb.blue = val[3];
225 * Throttle only the first of every four transactions,
226 * so that the user can update all four LEDs at once.
228 if ((j++ % 4) == 0) {
234 ret = cros_ec_cmd_xfer(ec, &msg);
238 if (msg.result != EC_RES_SUCCESS)
245 /* Skip over the number we just read */
246 while (*buf && !isspace(*buf))
251 return (ok && i == 0) ? count : -EINVAL;
254 static char const *seqname[] = {
255 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
256 "S0S3", "S3S5", "STOP", "RUN", "PULSE", "TEST", "KONAMI",
259 static ssize_t sequence_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
262 struct ec_params_lightbar *param;
263 struct ec_response_lightbar *resp;
264 struct cros_ec_command msg = INIT_MSG(param, resp);
266 struct cros_ec_device *ec = dev_get_drvdata(dev);
268 param = (struct ec_params_lightbar *)msg.outdata;
269 param->cmd = LIGHTBAR_CMD_GET_SEQ;
274 ret = cros_ec_cmd_xfer(ec, &msg);
278 if (msg.result != EC_RES_SUCCESS)
279 return scnprintf(buf, PAGE_SIZE,
280 "ERROR: EC returned %d\n", msg.result);
282 resp = (struct ec_response_lightbar *)msg.indata;
283 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
284 return scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
286 return scnprintf(buf, PAGE_SIZE, "%s\n",
287 seqname[resp->get_seq.num]);
290 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
291 const char *buf, size_t count)
293 struct ec_params_lightbar *param;
294 struct ec_response_lightbar *resp;
295 struct cros_ec_command msg = INIT_MSG(param, resp);
298 struct cros_ec_device *ec = dev_get_drvdata(dev);
300 for (len = 0; len < count; len++)
301 if (!isalnum(buf[len]))
304 for (num = 0; num < ARRAY_SIZE(seqname); num++)
305 if (!strncasecmp(seqname[num], buf, len))
308 if (num >= ARRAY_SIZE(seqname)) {
309 ret = kstrtouint(buf, 0, &num);
314 param = (struct ec_params_lightbar *)msg.outdata;
315 param->cmd = LIGHTBAR_CMD_SEQ;
316 param->seq.num = num;
321 ret = cros_ec_cmd_xfer(ec, &msg);
325 if (msg.result != EC_RES_SUCCESS)
331 /* Module initialization */
333 static DEVICE_ATTR_RW(interval_msec);
334 static DEVICE_ATTR_RO(version);
335 static DEVICE_ATTR_WO(brightness);
336 static DEVICE_ATTR_WO(led_rgb);
337 static DEVICE_ATTR_RW(sequence);
338 static struct attribute *__lb_cmds_attrs[] = {
339 &dev_attr_interval_msec.attr,
340 &dev_attr_version.attr,
341 &dev_attr_brightness.attr,
342 &dev_attr_led_rgb.attr,
343 &dev_attr_sequence.attr,
346 static struct attribute_group lb_cmds_attr_group = {
348 .attrs = __lb_cmds_attrs,
351 void ec_dev_lightbar_init(struct cros_ec_device *ec)
355 /* Only instantiate this stuff if the EC has a lightbar */
356 if (!get_lightbar_version(ec, NULL, NULL))
359 ret = sysfs_create_group(&ec->vdev->kobj, &lb_cmds_attr_group);
361 pr_warn("sysfs_create_group() failed: %d\n", ret);
364 void ec_dev_lightbar_remove(struct cros_ec_device *ec)
366 sysfs_remove_group(&ec->vdev->kobj, &lb_cmds_attr_group);