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 /* Rate-limit the lightbar interface to prevent DoS. */
37 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
40 * Whether or not we have given userspace control of the lightbar.
41 * If this is true, we won't do anything during suspend/resume.
43 static bool userspace_control;
44 static struct cros_ec_dev *ec_with_lightbar;
46 static ssize_t interval_msec_show(struct device *dev,
47 struct device_attribute *attr, char *buf)
49 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
51 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
54 static ssize_t interval_msec_store(struct device *dev,
55 struct device_attribute *attr,
56 const char *buf, size_t count)
60 if (kstrtoul(buf, 0, &msec))
63 lb_interval_jiffies = msec * HZ / 1000;
68 static DEFINE_MUTEX(lb_mutex);
69 /* Return 0 if able to throttle correctly, error otherwise */
70 static int lb_throttle(void)
72 static unsigned long last_access;
73 unsigned long now, next_timeslot;
77 mutex_lock(&lb_mutex);
80 next_timeslot = last_access + lb_interval_jiffies;
82 if (time_before(now, next_timeslot)) {
83 delay = (long)(next_timeslot) - (long)now;
84 set_current_state(TASK_INTERRUPTIBLE);
85 if (schedule_timeout(delay) > 0) {
86 /* interrupted - just abort */
95 mutex_unlock(&lb_mutex);
100 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
102 struct cros_ec_command *msg;
105 len = max(sizeof(struct ec_params_lightbar),
106 sizeof(struct ec_response_lightbar));
108 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
113 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
114 msg->outsize = sizeof(struct ec_params_lightbar);
115 msg->insize = sizeof(struct ec_response_lightbar);
120 static int get_lightbar_version(struct cros_ec_dev *ec,
121 uint32_t *ver_ptr, uint32_t *flg_ptr)
123 struct ec_params_lightbar *param;
124 struct ec_response_lightbar *resp;
125 struct cros_ec_command *msg;
128 msg = alloc_lightbar_cmd_msg(ec);
132 param = (struct ec_params_lightbar *)msg->data;
133 param->cmd = LIGHTBAR_CMD_VERSION;
134 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
140 switch (msg->result) {
141 case EC_RES_INVALID_PARAM:
142 /* Pixel had no version command. */
151 resp = (struct ec_response_lightbar *)msg->data;
153 /* Future devices w/lightbars should implement this command */
155 *ver_ptr = resp->version.num;
157 *flg_ptr = resp->version.flags;
162 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
169 static ssize_t version_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 uint32_t version = 0, flags = 0;
173 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
180 /* This should always succeed, because we check during init. */
181 if (!get_lightbar_version(ec, &version, &flags))
184 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
187 static ssize_t brightness_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct ec_params_lightbar *param;
192 struct cros_ec_command *msg;
195 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
197 if (kstrtouint(buf, 0, &val))
200 msg = alloc_lightbar_cmd_msg(ec);
204 param = (struct ec_params_lightbar *)msg->data;
205 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
206 param->set_brightness.num = val;
211 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
215 if (msg->result != EC_RES_SUCCESS) {
228 * We expect numbers, and we'll keep reading until we find them, skipping over
229 * any whitespace (sysfs guarantees that the input is null-terminated). Every
230 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
231 * parsing error, if we don't parse any numbers, or if we have numbers left
234 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct ec_params_lightbar *param;
238 struct cros_ec_command *msg;
239 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
241 int ret, i = 0, j = 0, ok = 0;
243 msg = alloc_lightbar_cmd_msg(ec);
248 /* Skip any whitespace */
249 while (*buf && isspace(*buf))
255 ret = sscanf(buf, "%i", &val[i++]);
260 param = (struct ec_params_lightbar *)msg->data;
261 param->cmd = LIGHTBAR_CMD_SET_RGB;
262 param->set_rgb.led = val[0];
263 param->set_rgb.red = val[1];
264 param->set_rgb.green = val[2];
265 param->set_rgb.blue = val[3];
267 * Throttle only the first of every four transactions,
268 * so that the user can update all four LEDs at once.
270 if ((j++ % 4) == 0) {
276 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
280 if (msg->result != EC_RES_SUCCESS)
287 /* Skip over the number we just read */
288 while (*buf && !isspace(*buf))
295 return (ok && i == 0) ? count : -EINVAL;
298 static char const *seqname[] = {
299 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
300 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
304 static ssize_t sequence_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct ec_params_lightbar *param;
308 struct ec_response_lightbar *resp;
309 struct cros_ec_command *msg;
311 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
313 msg = alloc_lightbar_cmd_msg(ec);
317 param = (struct ec_params_lightbar *)msg->data;
318 param->cmd = LIGHTBAR_CMD_GET_SEQ;
323 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
327 if (msg->result != EC_RES_SUCCESS) {
328 ret = scnprintf(buf, PAGE_SIZE,
329 "ERROR: EC returned %d\n", msg->result);
333 resp = (struct ec_response_lightbar *)msg->data;
334 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
335 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
337 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
338 seqname[resp->get_seq.num]);
345 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
347 struct ec_params_lightbar *param;
348 struct cros_ec_command *msg;
351 msg = alloc_lightbar_cmd_msg(ec);
355 param = (struct ec_params_lightbar *)msg->data;
362 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
365 if (msg->result != EC_RES_SUCCESS) {
376 int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
378 struct ec_params_lightbar *param;
379 struct cros_ec_command *msg;
382 if (ec != ec_with_lightbar)
385 msg = alloc_lightbar_cmd_msg(ec);
389 param = (struct ec_params_lightbar *)msg->data;
391 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
392 param->manual_suspend_ctrl.enable = enable;
398 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
401 if (msg->result != EC_RES_SUCCESS) {
411 EXPORT_SYMBOL(lb_manual_suspend_ctrl);
413 int lb_suspend(struct cros_ec_dev *ec)
415 if (userspace_control || ec != ec_with_lightbar)
418 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
420 EXPORT_SYMBOL(lb_suspend);
422 int lb_resume(struct cros_ec_dev *ec)
424 if (userspace_control || ec != ec_with_lightbar)
427 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME);
429 EXPORT_SYMBOL(lb_resume);
431 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
434 struct ec_params_lightbar *param;
435 struct cros_ec_command *msg;
438 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
440 for (len = 0; len < count; len++)
441 if (!isalnum(buf[len]))
444 for (num = 0; num < ARRAY_SIZE(seqname); num++)
445 if (!strncasecmp(seqname[num], buf, len))
448 if (num >= ARRAY_SIZE(seqname)) {
449 ret = kstrtouint(buf, 0, &num);
454 msg = alloc_lightbar_cmd_msg(ec);
458 param = (struct ec_params_lightbar *)msg->data;
459 param->cmd = LIGHTBAR_CMD_SEQ;
460 param->seq.num = num;
465 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
469 if (msg->result != EC_RES_SUCCESS) {
480 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
483 int extra_bytes, max_size, ret;
484 struct ec_params_lightbar *param;
485 struct cros_ec_command *msg;
486 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
489 * We might need to reject the program for size reasons. The EC
490 * enforces a maximum program size, but we also don't want to try
491 * and send a program that is too big for the protocol. In order
492 * to ensure the latter, we also need to ensure we have extra bytes
493 * to represent the rest of the packet.
495 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
496 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
497 if (count > max_size) {
498 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
499 (unsigned int)count, max_size);
504 msg = alloc_lightbar_cmd_msg(ec);
512 dev_info(dev, "Copying %zu byte program to EC", count);
514 param = (struct ec_params_lightbar *)msg->data;
515 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
517 param->set_program.size = count;
518 memcpy(param->set_program.data, buf, count);
521 * We need to set the message size manually or else it will use
522 * EC_LB_PROG_LEN. This might be too long, and the program
523 * is unlikely to use all of the space.
525 msg->outsize = count + extra_bytes;
527 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
530 if (msg->result != EC_RES_SUCCESS) {
542 static ssize_t userspace_control_show(struct device *dev,
543 struct device_attribute *attr,
546 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
549 static ssize_t userspace_control_store(struct device *dev,
550 struct device_attribute *attr,
557 ret = strtobool(buf, &enable);
561 userspace_control = enable;
566 /* Module initialization */
568 static DEVICE_ATTR_RW(interval_msec);
569 static DEVICE_ATTR_RO(version);
570 static DEVICE_ATTR_WO(brightness);
571 static DEVICE_ATTR_WO(led_rgb);
572 static DEVICE_ATTR_RW(sequence);
573 static DEVICE_ATTR_WO(program);
574 static DEVICE_ATTR_RW(userspace_control);
576 static struct attribute *__lb_cmds_attrs[] = {
577 &dev_attr_interval_msec.attr,
578 &dev_attr_version.attr,
579 &dev_attr_brightness.attr,
580 &dev_attr_led_rgb.attr,
581 &dev_attr_sequence.attr,
582 &dev_attr_program.attr,
583 &dev_attr_userspace_control.attr,
587 bool ec_has_lightbar(struct cros_ec_dev *ec)
589 return !!get_lightbar_version(ec, NULL, NULL);
592 static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
593 struct attribute *a, int n)
595 struct device *dev = container_of(kobj, struct device, kobj);
596 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
597 struct platform_device *pdev = to_platform_device(ec->dev);
598 struct cros_ec_platform *pdata = pdev->dev.platform_data;
601 is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
606 /* Only instantiate this stuff if the EC has a lightbar */
607 if (ec_has_lightbar(ec)) {
608 ec_with_lightbar = ec;
614 struct attribute_group cros_ec_lightbar_attr_group = {
616 .attrs = __lb_cmds_attrs,
617 .is_visible = cros_ec_lightbar_attrs_are_visible,
619 EXPORT_SYMBOL(cros_ec_lightbar_attr_group);