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 = container_of(dev,
174 struct cros_ec_dev, class_dev);
181 /* This should always succeed, because we check during init. */
182 if (!get_lightbar_version(ec, &version, &flags))
185 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
188 static ssize_t brightness_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct ec_params_lightbar *param;
193 struct cros_ec_command *msg;
196 struct cros_ec_dev *ec = container_of(dev,
197 struct cros_ec_dev, class_dev);
199 if (kstrtouint(buf, 0, &val))
202 msg = alloc_lightbar_cmd_msg(ec);
206 param = (struct ec_params_lightbar *)msg->data;
207 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
208 param->set_brightness.num = val;
213 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
217 if (msg->result != EC_RES_SUCCESS) {
230 * We expect numbers, and we'll keep reading until we find them, skipping over
231 * any whitespace (sysfs guarantees that the input is null-terminated). Every
232 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
233 * parsing error, if we don't parse any numbers, or if we have numbers left
236 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
237 const char *buf, size_t count)
239 struct ec_params_lightbar *param;
240 struct cros_ec_command *msg;
241 struct cros_ec_dev *ec = container_of(dev,
242 struct cros_ec_dev, class_dev);
244 int ret, i = 0, j = 0, ok = 0;
246 msg = alloc_lightbar_cmd_msg(ec);
251 /* Skip any whitespace */
252 while (*buf && isspace(*buf))
258 ret = sscanf(buf, "%i", &val[i++]);
263 param = (struct ec_params_lightbar *)msg->data;
264 param->cmd = LIGHTBAR_CMD_SET_RGB;
265 param->set_rgb.led = val[0];
266 param->set_rgb.red = val[1];
267 param->set_rgb.green = val[2];
268 param->set_rgb.blue = val[3];
270 * Throttle only the first of every four transactions,
271 * so that the user can update all four LEDs at once.
273 if ((j++ % 4) == 0) {
279 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
283 if (msg->result != EC_RES_SUCCESS)
290 /* Skip over the number we just read */
291 while (*buf && !isspace(*buf))
298 return (ok && i == 0) ? count : -EINVAL;
301 static char const *seqname[] = {
302 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
303 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
307 static ssize_t sequence_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct ec_params_lightbar *param;
311 struct ec_response_lightbar *resp;
312 struct cros_ec_command *msg;
314 struct cros_ec_dev *ec = container_of(dev,
315 struct cros_ec_dev, class_dev);
317 msg = alloc_lightbar_cmd_msg(ec);
321 param = (struct ec_params_lightbar *)msg->data;
322 param->cmd = LIGHTBAR_CMD_GET_SEQ;
327 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
331 if (msg->result != EC_RES_SUCCESS) {
332 ret = scnprintf(buf, PAGE_SIZE,
333 "ERROR: EC returned %d\n", msg->result);
337 resp = (struct ec_response_lightbar *)msg->data;
338 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
339 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
341 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
342 seqname[resp->get_seq.num]);
349 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
351 struct ec_params_lightbar *param;
352 struct cros_ec_command *msg;
355 msg = alloc_lightbar_cmd_msg(ec);
359 param = (struct ec_params_lightbar *)msg->data;
366 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
369 if (msg->result != EC_RES_SUCCESS) {
380 int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
382 struct ec_params_lightbar *param;
383 struct cros_ec_command *msg;
386 if (ec != ec_with_lightbar)
389 msg = alloc_lightbar_cmd_msg(ec);
393 param = (struct ec_params_lightbar *)msg->data;
395 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
396 param->manual_suspend_ctrl.enable = enable;
402 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
405 if (msg->result != EC_RES_SUCCESS) {
415 EXPORT_SYMBOL(lb_manual_suspend_ctrl);
417 int lb_suspend(struct cros_ec_dev *ec)
419 if (userspace_control || ec != ec_with_lightbar)
422 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
424 EXPORT_SYMBOL(lb_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);
433 EXPORT_SYMBOL(lb_resume);
435 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
438 struct ec_params_lightbar *param;
439 struct cros_ec_command *msg;
442 struct cros_ec_dev *ec = container_of(dev,
443 struct cros_ec_dev, class_dev);
445 for (len = 0; len < count; len++)
446 if (!isalnum(buf[len]))
449 for (num = 0; num < ARRAY_SIZE(seqname); num++)
450 if (!strncasecmp(seqname[num], buf, len))
453 if (num >= ARRAY_SIZE(seqname)) {
454 ret = kstrtouint(buf, 0, &num);
459 msg = alloc_lightbar_cmd_msg(ec);
463 param = (struct ec_params_lightbar *)msg->data;
464 param->cmd = LIGHTBAR_CMD_SEQ;
465 param->seq.num = num;
470 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
474 if (msg->result != EC_RES_SUCCESS) {
485 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
486 const char *buf, size_t count)
488 int extra_bytes, max_size, ret;
489 struct ec_params_lightbar *param;
490 struct cros_ec_command *msg;
491 struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
495 * We might need to reject the program for size reasons. The EC
496 * enforces a maximum program size, but we also don't want to try
497 * and send a program that is too big for the protocol. In order
498 * to ensure the latter, we also need to ensure we have extra bytes
499 * to represent the rest of the packet.
501 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
502 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
503 if (count > max_size) {
504 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
505 (unsigned int)count, max_size);
510 msg = alloc_lightbar_cmd_msg(ec);
518 dev_info(dev, "Copying %zu byte program to EC", count);
520 param = (struct ec_params_lightbar *)msg->data;
521 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
523 param->set_program.size = count;
524 memcpy(param->set_program.data, buf, count);
527 * We need to set the message size manually or else it will use
528 * EC_LB_PROG_LEN. This might be too long, and the program
529 * is unlikely to use all of the space.
531 msg->outsize = count + extra_bytes;
533 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
536 if (msg->result != EC_RES_SUCCESS) {
548 static ssize_t userspace_control_show(struct device *dev,
549 struct device_attribute *attr,
552 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
555 static ssize_t userspace_control_store(struct device *dev,
556 struct device_attribute *attr,
563 ret = strtobool(buf, &enable);
567 userspace_control = enable;
572 /* Module initialization */
574 static DEVICE_ATTR_RW(interval_msec);
575 static DEVICE_ATTR_RO(version);
576 static DEVICE_ATTR_WO(brightness);
577 static DEVICE_ATTR_WO(led_rgb);
578 static DEVICE_ATTR_RW(sequence);
579 static DEVICE_ATTR_WO(program);
580 static DEVICE_ATTR_RW(userspace_control);
582 static struct attribute *__lb_cmds_attrs[] = {
583 &dev_attr_interval_msec.attr,
584 &dev_attr_version.attr,
585 &dev_attr_brightness.attr,
586 &dev_attr_led_rgb.attr,
587 &dev_attr_sequence.attr,
588 &dev_attr_program.attr,
589 &dev_attr_userspace_control.attr,
593 bool ec_has_lightbar(struct cros_ec_dev *ec)
595 return !!get_lightbar_version(ec, NULL, NULL);
598 static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
599 struct attribute *a, int n)
601 struct device *dev = container_of(kobj, struct device, kobj);
602 struct cros_ec_dev *ec = container_of(dev,
603 struct cros_ec_dev, class_dev);
604 struct platform_device *pdev = to_platform_device(ec->dev);
605 struct cros_ec_platform *pdata = pdev->dev.platform_data;
608 is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
613 /* Only instantiate this stuff if the EC has a lightbar */
614 if (ec_has_lightbar(ec)) {
615 ec_with_lightbar = ec;
621 struct attribute_group cros_ec_lightbar_attr_group = {
623 .attrs = __lb_cmds_attrs,
624 .is_visible = cros_ec_lightbar_attrs_are_visible,
626 EXPORT_SYMBOL(cros_ec_lightbar_attr_group);