1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose the Chromebook Pixel lightbar to userspace
4 // Copyright (C) 2014 Google, Inc.
6 #include <linux/ctype.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
10 #include <linux/kobject.h>
11 #include <linux/mfd/cros_ec.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_ec_proto.h>
15 #include <linux/platform_device.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/slab.h>
21 #define DRV_NAME "cros-ec-lightbar"
23 /* Rate-limit the lightbar interface to prevent DoS. */
24 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
27 * Whether or not we have given userspace control of the lightbar.
28 * If this is true, we won't do anything during suspend/resume.
30 static bool userspace_control;
32 static ssize_t interval_msec_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
35 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
37 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
40 static ssize_t interval_msec_store(struct device *dev,
41 struct device_attribute *attr,
42 const char *buf, size_t count)
46 if (kstrtoul(buf, 0, &msec))
49 lb_interval_jiffies = msec * HZ / 1000;
54 static DEFINE_MUTEX(lb_mutex);
55 /* Return 0 if able to throttle correctly, error otherwise */
56 static int lb_throttle(void)
58 static unsigned long last_access;
59 unsigned long now, next_timeslot;
63 mutex_lock(&lb_mutex);
66 next_timeslot = last_access + lb_interval_jiffies;
68 if (time_before(now, next_timeslot)) {
69 delay = (long)(next_timeslot) - (long)now;
70 set_current_state(TASK_INTERRUPTIBLE);
71 if (schedule_timeout(delay) > 0) {
72 /* interrupted - just abort */
81 mutex_unlock(&lb_mutex);
86 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
88 struct cros_ec_command *msg;
91 len = max(sizeof(struct ec_params_lightbar),
92 sizeof(struct ec_response_lightbar));
94 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
99 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
100 msg->outsize = sizeof(struct ec_params_lightbar);
101 msg->insize = sizeof(struct ec_response_lightbar);
106 static int get_lightbar_version(struct cros_ec_dev *ec,
107 uint32_t *ver_ptr, uint32_t *flg_ptr)
109 struct ec_params_lightbar *param;
110 struct ec_response_lightbar *resp;
111 struct cros_ec_command *msg;
114 msg = alloc_lightbar_cmd_msg(ec);
118 param = (struct ec_params_lightbar *)msg->data;
119 param->cmd = LIGHTBAR_CMD_VERSION;
120 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
126 switch (msg->result) {
127 case EC_RES_INVALID_PARAM:
128 /* Pixel had no version command. */
137 resp = (struct ec_response_lightbar *)msg->data;
139 /* Future devices w/lightbars should implement this command */
141 *ver_ptr = resp->version.num;
143 *flg_ptr = resp->version.flags;
148 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
155 static ssize_t version_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
158 uint32_t version = 0, flags = 0;
159 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
166 /* This should always succeed, because we check during init. */
167 if (!get_lightbar_version(ec, &version, &flags))
170 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
173 static ssize_t brightness_store(struct device *dev,
174 struct device_attribute *attr,
175 const char *buf, size_t count)
177 struct ec_params_lightbar *param;
178 struct cros_ec_command *msg;
181 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
183 if (kstrtouint(buf, 0, &val))
186 msg = alloc_lightbar_cmd_msg(ec);
190 param = (struct ec_params_lightbar *)msg->data;
191 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
192 param->set_brightness.num = val;
197 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
201 if (msg->result != EC_RES_SUCCESS) {
214 * We expect numbers, and we'll keep reading until we find them, skipping over
215 * any whitespace (sysfs guarantees that the input is null-terminated). Every
216 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
217 * parsing error, if we don't parse any numbers, or if we have numbers left
220 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
221 const char *buf, size_t count)
223 struct ec_params_lightbar *param;
224 struct cros_ec_command *msg;
225 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
227 int ret, i = 0, j = 0, ok = 0;
229 msg = alloc_lightbar_cmd_msg(ec);
234 /* Skip any whitespace */
235 while (*buf && isspace(*buf))
241 ret = sscanf(buf, "%i", &val[i++]);
246 param = (struct ec_params_lightbar *)msg->data;
247 param->cmd = LIGHTBAR_CMD_SET_RGB;
248 param->set_rgb.led = val[0];
249 param->set_rgb.red = val[1];
250 param->set_rgb.green = val[2];
251 param->set_rgb.blue = val[3];
253 * Throttle only the first of every four transactions,
254 * so that the user can update all four LEDs at once.
256 if ((j++ % 4) == 0) {
262 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
266 if (msg->result != EC_RES_SUCCESS)
273 /* Skip over the number we just read */
274 while (*buf && !isspace(*buf))
281 return (ok && i == 0) ? count : -EINVAL;
284 static char const *seqname[] = {
285 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
286 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
290 static ssize_t sequence_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
293 struct ec_params_lightbar *param;
294 struct ec_response_lightbar *resp;
295 struct cros_ec_command *msg;
297 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
299 msg = alloc_lightbar_cmd_msg(ec);
303 param = (struct ec_params_lightbar *)msg->data;
304 param->cmd = LIGHTBAR_CMD_GET_SEQ;
309 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
313 if (msg->result != EC_RES_SUCCESS) {
314 ret = scnprintf(buf, PAGE_SIZE,
315 "ERROR: EC returned %d\n", msg->result);
319 resp = (struct ec_response_lightbar *)msg->data;
320 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
321 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
323 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
324 seqname[resp->get_seq.num]);
331 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
333 struct ec_params_lightbar *param;
334 struct cros_ec_command *msg;
337 msg = alloc_lightbar_cmd_msg(ec);
341 param = (struct ec_params_lightbar *)msg->data;
348 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
351 if (msg->result != EC_RES_SUCCESS) {
362 static int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
364 struct ec_params_lightbar *param;
365 struct cros_ec_command *msg;
368 msg = alloc_lightbar_cmd_msg(ec);
372 param = (struct ec_params_lightbar *)msg->data;
374 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
375 param->manual_suspend_ctrl.enable = enable;
381 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
384 if (msg->result != EC_RES_SUCCESS) {
395 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
396 const char *buf, size_t count)
398 struct ec_params_lightbar *param;
399 struct cros_ec_command *msg;
402 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
404 for (len = 0; len < count; len++)
405 if (!isalnum(buf[len]))
408 for (num = 0; num < ARRAY_SIZE(seqname); num++)
409 if (!strncasecmp(seqname[num], buf, len))
412 if (num >= ARRAY_SIZE(seqname)) {
413 ret = kstrtouint(buf, 0, &num);
418 msg = alloc_lightbar_cmd_msg(ec);
422 param = (struct ec_params_lightbar *)msg->data;
423 param->cmd = LIGHTBAR_CMD_SEQ;
424 param->seq.num = num;
429 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
433 if (msg->result != EC_RES_SUCCESS) {
444 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t count)
447 int extra_bytes, max_size, ret;
448 struct ec_params_lightbar *param;
449 struct cros_ec_command *msg;
450 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
453 * We might need to reject the program for size reasons. The EC
454 * enforces a maximum program size, but we also don't want to try
455 * and send a program that is too big for the protocol. In order
456 * to ensure the latter, we also need to ensure we have extra bytes
457 * to represent the rest of the packet.
459 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
460 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
461 if (count > max_size) {
462 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
463 (unsigned int)count, max_size);
468 msg = alloc_lightbar_cmd_msg(ec);
476 dev_info(dev, "Copying %zu byte program to EC", count);
478 param = (struct ec_params_lightbar *)msg->data;
479 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
481 param->set_program.size = count;
482 memcpy(param->set_program.data, buf, count);
485 * We need to set the message size manually or else it will use
486 * EC_LB_PROG_LEN. This might be too long, and the program
487 * is unlikely to use all of the space.
489 msg->outsize = count + extra_bytes;
491 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
494 if (msg->result != EC_RES_SUCCESS) {
506 static ssize_t userspace_control_show(struct device *dev,
507 struct device_attribute *attr,
510 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
513 static ssize_t userspace_control_store(struct device *dev,
514 struct device_attribute *attr,
521 ret = strtobool(buf, &enable);
525 userspace_control = enable;
530 /* Module initialization */
532 static DEVICE_ATTR_RW(interval_msec);
533 static DEVICE_ATTR_RO(version);
534 static DEVICE_ATTR_WO(brightness);
535 static DEVICE_ATTR_WO(led_rgb);
536 static DEVICE_ATTR_RW(sequence);
537 static DEVICE_ATTR_WO(program);
538 static DEVICE_ATTR_RW(userspace_control);
540 static struct attribute *__lb_cmds_attrs[] = {
541 &dev_attr_interval_msec.attr,
542 &dev_attr_version.attr,
543 &dev_attr_brightness.attr,
544 &dev_attr_led_rgb.attr,
545 &dev_attr_sequence.attr,
546 &dev_attr_program.attr,
547 &dev_attr_userspace_control.attr,
551 static struct attribute_group cros_ec_lightbar_attr_group = {
553 .attrs = __lb_cmds_attrs,
556 static int cros_ec_lightbar_probe(struct platform_device *pd)
558 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
559 struct cros_ec_platform *pdata = dev_get_platdata(ec_dev->dev);
560 struct device *dev = &pd->dev;
564 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
565 * devices like 'cros_pd' doesn't have a lightbar.
567 if (strcmp(pdata->ec_name, CROS_EC_DEV_NAME) != 0)
571 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
572 * doesn't have a lightbar.
574 if (!get_lightbar_version(ec_dev, NULL, NULL))
577 /* Take control of the lightbar from the EC. */
578 lb_manual_suspend_ctrl(ec_dev, 1);
580 ret = sysfs_create_group(&ec_dev->class_dev.kobj,
581 &cros_ec_lightbar_attr_group);
583 dev_err(dev, "failed to create %s attributes. err=%d\n",
584 cros_ec_lightbar_attr_group.name, ret);
589 static int cros_ec_lightbar_remove(struct platform_device *pd)
591 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
593 sysfs_remove_group(&ec_dev->class_dev.kobj,
594 &cros_ec_lightbar_attr_group);
596 /* Let the EC take over the lightbar again. */
597 lb_manual_suspend_ctrl(ec_dev, 0);
602 static int __maybe_unused cros_ec_lightbar_resume(struct device *dev)
604 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
606 if (userspace_control)
609 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_RESUME);
612 static int __maybe_unused cros_ec_lightbar_suspend(struct device *dev)
614 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
616 if (userspace_control)
619 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_SUSPEND);
622 static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops,
623 cros_ec_lightbar_suspend, cros_ec_lightbar_resume);
625 static struct platform_driver cros_ec_lightbar_driver = {
628 .pm = &cros_ec_lightbar_pm_ops,
630 .probe = cros_ec_lightbar_probe,
631 .remove = cros_ec_lightbar_remove,
634 module_platform_driver(cros_ec_lightbar_driver);
636 MODULE_LICENSE("GPL");
637 MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");
638 MODULE_ALIAS("platform:" DRV_NAME);