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>
34 #include <linux/slab.h>
36 #include "cros_ec_dev.h"
38 /* Rate-limit the lightbar interface to prevent DoS. */
39 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
42 * Whether or not we have given userspace control of the lightbar.
43 * If this is true, we won't do anything during suspend/resume.
45 static bool userspace_control;
46 static struct cros_ec_dev *ec_with_lightbar;
48 static ssize_t interval_msec_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
51 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
53 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
56 static ssize_t interval_msec_store(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
62 if (kstrtoul(buf, 0, &msec))
65 lb_interval_jiffies = msec * HZ / 1000;
70 static DEFINE_MUTEX(lb_mutex);
71 /* Return 0 if able to throttle correctly, error otherwise */
72 static int lb_throttle(void)
74 static unsigned long last_access;
75 unsigned long now, next_timeslot;
79 mutex_lock(&lb_mutex);
82 next_timeslot = last_access + lb_interval_jiffies;
84 if (time_before(now, next_timeslot)) {
85 delay = (long)(next_timeslot) - (long)now;
86 set_current_state(TASK_INTERRUPTIBLE);
87 if (schedule_timeout(delay) > 0) {
88 /* interrupted - just abort */
97 mutex_unlock(&lb_mutex);
102 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
104 struct cros_ec_command *msg;
107 len = max(sizeof(struct ec_params_lightbar),
108 sizeof(struct ec_response_lightbar));
110 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
115 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
116 msg->outsize = sizeof(struct ec_params_lightbar);
117 msg->insize = sizeof(struct ec_response_lightbar);
122 static int get_lightbar_version(struct cros_ec_dev *ec,
123 uint32_t *ver_ptr, uint32_t *flg_ptr)
125 struct ec_params_lightbar *param;
126 struct ec_response_lightbar *resp;
127 struct cros_ec_command *msg;
130 msg = alloc_lightbar_cmd_msg(ec);
134 param = (struct ec_params_lightbar *)msg->data;
135 param->cmd = LIGHTBAR_CMD_VERSION;
136 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
142 switch (msg->result) {
143 case EC_RES_INVALID_PARAM:
144 /* Pixel had no version command. */
153 resp = (struct ec_response_lightbar *)msg->data;
155 /* Future devices w/lightbars should implement this command */
157 *ver_ptr = resp->version.num;
159 *flg_ptr = resp->version.flags;
164 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
171 static ssize_t version_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
174 uint32_t version = 0, flags = 0;
175 struct cros_ec_dev *ec = container_of(dev,
176 struct cros_ec_dev, class_dev);
183 /* This should always succeed, because we check during init. */
184 if (!get_lightbar_version(ec, &version, &flags))
187 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
190 static ssize_t brightness_store(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
194 struct ec_params_lightbar *param;
195 struct cros_ec_command *msg;
198 struct cros_ec_dev *ec = container_of(dev,
199 struct cros_ec_dev, class_dev);
201 if (kstrtouint(buf, 0, &val))
204 msg = alloc_lightbar_cmd_msg(ec);
208 param = (struct ec_params_lightbar *)msg->data;
209 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
210 param->set_brightness.num = val;
215 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
219 if (msg->result != EC_RES_SUCCESS) {
232 * We expect numbers, and we'll keep reading until we find them, skipping over
233 * any whitespace (sysfs guarantees that the input is null-terminated). Every
234 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
235 * parsing error, if we don't parse any numbers, or if we have numbers left
238 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
239 const char *buf, size_t count)
241 struct ec_params_lightbar *param;
242 struct cros_ec_command *msg;
243 struct cros_ec_dev *ec = container_of(dev,
244 struct cros_ec_dev, class_dev);
246 int ret, i = 0, j = 0, ok = 0;
248 msg = alloc_lightbar_cmd_msg(ec);
253 /* Skip any whitespace */
254 while (*buf && isspace(*buf))
260 ret = sscanf(buf, "%i", &val[i++]);
265 param = (struct ec_params_lightbar *)msg->data;
266 param->cmd = LIGHTBAR_CMD_SET_RGB;
267 param->set_rgb.led = val[0];
268 param->set_rgb.red = val[1];
269 param->set_rgb.green = val[2];
270 param->set_rgb.blue = val[3];
272 * Throttle only the first of every four transactions,
273 * so that the user can update all four LEDs at once.
275 if ((j++ % 4) == 0) {
281 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
285 if (msg->result != EC_RES_SUCCESS)
292 /* Skip over the number we just read */
293 while (*buf && !isspace(*buf))
300 return (ok && i == 0) ? count : -EINVAL;
303 static char const *seqname[] = {
304 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
305 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
309 static ssize_t sequence_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct ec_params_lightbar *param;
313 struct ec_response_lightbar *resp;
314 struct cros_ec_command *msg;
316 struct cros_ec_dev *ec = container_of(dev,
317 struct cros_ec_dev, class_dev);
319 msg = alloc_lightbar_cmd_msg(ec);
323 param = (struct ec_params_lightbar *)msg->data;
324 param->cmd = LIGHTBAR_CMD_GET_SEQ;
329 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
333 if (msg->result != EC_RES_SUCCESS) {
334 ret = scnprintf(buf, PAGE_SIZE,
335 "ERROR: EC returned %d\n", msg->result);
339 resp = (struct ec_response_lightbar *)msg->data;
340 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
341 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
343 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
344 seqname[resp->get_seq.num]);
351 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
353 struct ec_params_lightbar *param;
354 struct cros_ec_command *msg;
357 msg = alloc_lightbar_cmd_msg(ec);
361 param = (struct ec_params_lightbar *)msg->data;
368 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
371 if (msg->result != EC_RES_SUCCESS) {
382 int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
384 struct ec_params_lightbar *param;
385 struct cros_ec_command *msg;
388 if (ec != ec_with_lightbar)
391 msg = alloc_lightbar_cmd_msg(ec);
395 param = (struct ec_params_lightbar *)msg->data;
397 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
398 param->manual_suspend_ctrl.enable = enable;
404 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
407 if (msg->result != EC_RES_SUCCESS) {
418 int lb_suspend(struct cros_ec_dev *ec)
420 if (userspace_control || ec != ec_with_lightbar)
423 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
426 int lb_resume(struct cros_ec_dev *ec)
428 if (userspace_control || ec != ec_with_lightbar)
431 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME);
434 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct ec_params_lightbar *param;
438 struct cros_ec_command *msg;
441 struct cros_ec_dev *ec = container_of(dev,
442 struct cros_ec_dev, class_dev);
444 for (len = 0; len < count; len++)
445 if (!isalnum(buf[len]))
448 for (num = 0; num < ARRAY_SIZE(seqname); num++)
449 if (!strncasecmp(seqname[num], buf, len))
452 if (num >= ARRAY_SIZE(seqname)) {
453 ret = kstrtouint(buf, 0, &num);
458 msg = alloc_lightbar_cmd_msg(ec);
462 param = (struct ec_params_lightbar *)msg->data;
463 param->cmd = LIGHTBAR_CMD_SEQ;
464 param->seq.num = num;
469 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
473 if (msg->result != EC_RES_SUCCESS) {
484 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
485 const char *buf, size_t count)
487 int extra_bytes, max_size, ret;
488 struct ec_params_lightbar *param;
489 struct cros_ec_command *msg;
490 struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
494 * We might need to reject the program for size reasons. The EC
495 * enforces a maximum program size, but we also don't want to try
496 * and send a program that is too big for the protocol. In order
497 * to ensure the latter, we also need to ensure we have extra bytes
498 * to represent the rest of the packet.
500 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
501 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
502 if (count > max_size) {
503 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
504 (unsigned int)count, max_size);
509 msg = alloc_lightbar_cmd_msg(ec);
517 dev_info(dev, "Copying %zu byte program to EC", count);
519 param = (struct ec_params_lightbar *)msg->data;
520 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
522 param->set_program.size = count;
523 memcpy(param->set_program.data, buf, count);
526 * We need to set the message size manually or else it will use
527 * EC_LB_PROG_LEN. This might be too long, and the program
528 * is unlikely to use all of the space.
530 msg->outsize = count + extra_bytes;
532 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
535 if (msg->result != EC_RES_SUCCESS) {
547 static ssize_t userspace_control_show(struct device *dev,
548 struct device_attribute *attr,
551 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
554 static ssize_t userspace_control_store(struct device *dev,
555 struct device_attribute *attr,
562 ret = strtobool(buf, &enable);
566 userspace_control = enable;
571 /* Module initialization */
573 static DEVICE_ATTR_RW(interval_msec);
574 static DEVICE_ATTR_RO(version);
575 static DEVICE_ATTR_WO(brightness);
576 static DEVICE_ATTR_WO(led_rgb);
577 static DEVICE_ATTR_RW(sequence);
578 static DEVICE_ATTR_WO(program);
579 static DEVICE_ATTR_RW(userspace_control);
581 static struct attribute *__lb_cmds_attrs[] = {
582 &dev_attr_interval_msec.attr,
583 &dev_attr_version.attr,
584 &dev_attr_brightness.attr,
585 &dev_attr_led_rgb.attr,
586 &dev_attr_sequence.attr,
587 &dev_attr_program.attr,
588 &dev_attr_userspace_control.attr,
592 bool ec_has_lightbar(struct cros_ec_dev *ec)
594 return !!get_lightbar_version(ec, NULL, NULL);
597 static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
598 struct attribute *a, int n)
600 struct device *dev = container_of(kobj, struct device, kobj);
601 struct cros_ec_dev *ec = container_of(dev,
602 struct cros_ec_dev, class_dev);
603 struct platform_device *pdev = to_platform_device(ec->dev);
604 struct cros_ec_platform *pdata = pdev->dev.platform_data;
607 is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
612 /* Only instantiate this stuff if the EC has a lightbar */
613 if (ec_has_lightbar(ec)) {
614 ec_with_lightbar = ec;
620 struct attribute_group cros_ec_lightbar_attr_group = {
622 .attrs = __lb_cmds_attrs,
623 .is_visible = cros_ec_lightbar_attrs_are_visible,