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/kstrtox.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 msg->outsize = sizeof(param->cmd);
121 msg->result = sizeof(resp->version);
122 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
123 if (ret < 0 && ret != -EINVAL) {
128 switch (msg->result) {
129 case EC_RES_INVALID_PARAM:
130 /* Pixel had no version command. */
139 resp = (struct ec_response_lightbar *)msg->data;
141 /* Future devices w/lightbars should implement this command */
143 *ver_ptr = resp->version.num;
145 *flg_ptr = resp->version.flags;
150 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
157 static ssize_t version_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 uint32_t version = 0, flags = 0;
161 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
168 /* This should always succeed, because we check during init. */
169 if (!get_lightbar_version(ec, &version, &flags))
172 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
175 static ssize_t brightness_store(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct ec_params_lightbar *param;
180 struct cros_ec_command *msg;
183 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
185 if (kstrtouint(buf, 0, &val))
188 msg = alloc_lightbar_cmd_msg(ec);
192 param = (struct ec_params_lightbar *)msg->data;
193 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
194 param->set_brightness.num = val;
199 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
211 * We expect numbers, and we'll keep reading until we find them, skipping over
212 * any whitespace (sysfs guarantees that the input is null-terminated). Every
213 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
214 * parsing error, if we don't parse any numbers, or if we have numbers left
217 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
220 struct ec_params_lightbar *param;
221 struct cros_ec_command *msg;
222 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
224 int ret, i = 0, j = 0, ok = 0;
226 msg = alloc_lightbar_cmd_msg(ec);
231 /* Skip any whitespace */
232 while (*buf && isspace(*buf))
238 ret = sscanf(buf, "%i", &val[i++]);
243 param = (struct ec_params_lightbar *)msg->data;
244 param->cmd = LIGHTBAR_CMD_SET_RGB;
245 param->set_rgb.led = val[0];
246 param->set_rgb.red = val[1];
247 param->set_rgb.green = val[2];
248 param->set_rgb.blue = val[3];
250 * Throttle only the first of every four transactions,
251 * so that the user can update all four LEDs at once.
253 if ((j++ % 4) == 0) {
259 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
267 /* Skip over the number we just read */
268 while (*buf && !isspace(*buf))
275 return (ok && i == 0) ? count : -EINVAL;
278 static char const *seqname[] = {
279 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
280 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
284 static ssize_t sequence_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct ec_params_lightbar *param;
288 struct ec_response_lightbar *resp;
289 struct cros_ec_command *msg;
291 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
293 msg = alloc_lightbar_cmd_msg(ec);
297 param = (struct ec_params_lightbar *)msg->data;
298 param->cmd = LIGHTBAR_CMD_GET_SEQ;
303 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
305 ret = scnprintf(buf, PAGE_SIZE, "XFER / EC ERROR %d / %d\n",
310 resp = (struct ec_response_lightbar *)msg->data;
311 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
312 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
314 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
315 seqname[resp->get_seq.num]);
322 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
324 struct ec_params_lightbar *param;
325 struct cros_ec_command *msg;
328 msg = alloc_lightbar_cmd_msg(ec);
332 param = (struct ec_params_lightbar *)msg->data;
339 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
350 static int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
352 struct ec_params_lightbar *param;
353 struct cros_ec_command *msg;
356 msg = alloc_lightbar_cmd_msg(ec);
360 param = (struct ec_params_lightbar *)msg->data;
362 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
363 param->manual_suspend_ctrl.enable = enable;
369 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
380 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
383 struct ec_params_lightbar *param;
384 struct cros_ec_command *msg;
387 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
389 for (len = 0; len < count; len++)
390 if (!isalnum(buf[len]))
393 for (num = 0; num < ARRAY_SIZE(seqname); num++)
394 if (!strncasecmp(seqname[num], buf, len))
397 if (num >= ARRAY_SIZE(seqname)) {
398 ret = kstrtouint(buf, 0, &num);
403 msg = alloc_lightbar_cmd_msg(ec);
407 param = (struct ec_params_lightbar *)msg->data;
408 param->cmd = LIGHTBAR_CMD_SEQ;
409 param->seq.num = num;
414 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
424 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t count)
427 int extra_bytes, max_size, ret;
428 struct ec_params_lightbar *param;
429 struct cros_ec_command *msg;
430 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
433 * We might need to reject the program for size reasons. The EC
434 * enforces a maximum program size, but we also don't want to try
435 * and send a program that is too big for the protocol. In order
436 * to ensure the latter, we also need to ensure we have extra bytes
437 * to represent the rest of the packet.
439 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
440 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
441 if (count > max_size) {
442 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
443 (unsigned int)count, max_size);
448 msg = alloc_lightbar_cmd_msg(ec);
456 dev_info(dev, "Copying %zu byte program to EC", count);
458 param = (struct ec_params_lightbar *)msg->data;
459 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
461 param->set_program.size = count;
462 memcpy(param->set_program.data, buf, count);
465 * We need to set the message size manually or else it will use
466 * EC_LB_PROG_LEN. This might be too long, and the program
467 * is unlikely to use all of the space.
469 msg->outsize = count + extra_bytes;
471 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
482 static ssize_t userspace_control_show(struct device *dev,
483 struct device_attribute *attr,
486 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
489 static ssize_t userspace_control_store(struct device *dev,
490 struct device_attribute *attr,
497 ret = kstrtobool(buf, &enable);
501 userspace_control = enable;
506 /* Module initialization */
508 static DEVICE_ATTR_RW(interval_msec);
509 static DEVICE_ATTR_RO(version);
510 static DEVICE_ATTR_WO(brightness);
511 static DEVICE_ATTR_WO(led_rgb);
512 static DEVICE_ATTR_RW(sequence);
513 static DEVICE_ATTR_WO(program);
514 static DEVICE_ATTR_RW(userspace_control);
516 static struct attribute *__lb_cmds_attrs[] = {
517 &dev_attr_interval_msec.attr,
518 &dev_attr_version.attr,
519 &dev_attr_brightness.attr,
520 &dev_attr_led_rgb.attr,
521 &dev_attr_sequence.attr,
522 &dev_attr_program.attr,
523 &dev_attr_userspace_control.attr,
527 static const struct attribute_group cros_ec_lightbar_attr_group = {
529 .attrs = __lb_cmds_attrs,
532 static int cros_ec_lightbar_probe(struct platform_device *pd)
534 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
535 struct cros_ec_platform *pdata = dev_get_platdata(ec_dev->dev);
536 struct device *dev = &pd->dev;
540 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
541 * devices like 'cros_pd' doesn't have a lightbar.
543 if (strcmp(pdata->ec_name, CROS_EC_DEV_NAME) != 0)
547 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
548 * doesn't have a lightbar.
550 if (!get_lightbar_version(ec_dev, NULL, NULL))
553 /* Take control of the lightbar from the EC. */
554 lb_manual_suspend_ctrl(ec_dev, 1);
556 ret = sysfs_create_group(&ec_dev->class_dev.kobj,
557 &cros_ec_lightbar_attr_group);
559 dev_err(dev, "failed to create %s attributes. err=%d\n",
560 cros_ec_lightbar_attr_group.name, ret);
565 static int cros_ec_lightbar_remove(struct platform_device *pd)
567 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
569 sysfs_remove_group(&ec_dev->class_dev.kobj,
570 &cros_ec_lightbar_attr_group);
572 /* Let the EC take over the lightbar again. */
573 lb_manual_suspend_ctrl(ec_dev, 0);
578 static int __maybe_unused cros_ec_lightbar_resume(struct device *dev)
580 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
582 if (userspace_control)
585 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_RESUME);
588 static int __maybe_unused cros_ec_lightbar_suspend(struct device *dev)
590 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
592 if (userspace_control)
595 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_SUSPEND);
598 static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops,
599 cros_ec_lightbar_suspend, cros_ec_lightbar_resume);
601 static struct platform_driver cros_ec_lightbar_driver = {
604 .pm = &cros_ec_lightbar_pm_ops,
605 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
607 .probe = cros_ec_lightbar_probe,
608 .remove = cros_ec_lightbar_remove,
611 module_platform_driver(cros_ec_lightbar_driver);
613 MODULE_LICENSE("GPL");
614 MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");
615 MODULE_ALIAS("platform:" DRV_NAME);