signal: Ensure generic siginfos the kernel sends have all bits initialized
[cris-mirror.git] / drivers / platform / chrome / cros_ec_lightbar.c
blobfd2b047a2748204a734814255cb5c44128d5149e
1 /*
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>
25 #include <linux/fs.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)
60 unsigned long msec;
62 if (kstrtoul(buf, 0, &msec))
63 return -EINVAL;
65 lb_interval_jiffies = msec * HZ / 1000;
67 return count;
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;
76 long delay;
77 int ret = 0;
79 mutex_lock(&lb_mutex);
81 now = jiffies;
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 */
89 ret = -EINTR;
90 goto out;
92 now = jiffies;
95 last_access = now;
96 out:
97 mutex_unlock(&lb_mutex);
99 return ret;
102 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
104 struct cros_ec_command *msg;
105 int len;
107 len = max(sizeof(struct ec_params_lightbar),
108 sizeof(struct ec_response_lightbar));
110 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
111 if (!msg)
112 return NULL;
114 msg->version = 0;
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);
119 return msg;
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;
128 int ret;
130 msg = alloc_lightbar_cmd_msg(ec);
131 if (!msg)
132 return 0;
134 param = (struct ec_params_lightbar *)msg->data;
135 param->cmd = LIGHTBAR_CMD_VERSION;
136 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
137 if (ret < 0) {
138 ret = 0;
139 goto exit;
142 switch (msg->result) {
143 case EC_RES_INVALID_PARAM:
144 /* Pixel had no version command. */
145 if (ver_ptr)
146 *ver_ptr = 0;
147 if (flg_ptr)
148 *flg_ptr = 0;
149 ret = 1;
150 goto exit;
152 case EC_RES_SUCCESS:
153 resp = (struct ec_response_lightbar *)msg->data;
155 /* Future devices w/lightbars should implement this command */
156 if (ver_ptr)
157 *ver_ptr = resp->version.num;
158 if (flg_ptr)
159 *flg_ptr = resp->version.flags;
160 ret = 1;
161 goto exit;
164 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
165 ret = 0;
166 exit:
167 kfree(msg);
168 return ret;
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);
177 int ret;
179 ret = lb_throttle();
180 if (ret)
181 return ret;
183 /* This should always succeed, because we check during init. */
184 if (!get_lightbar_version(ec, &version, &flags))
185 return -EIO;
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;
196 int ret;
197 unsigned int val;
198 struct cros_ec_dev *ec = container_of(dev,
199 struct cros_ec_dev, class_dev);
201 if (kstrtouint(buf, 0, &val))
202 return -EINVAL;
204 msg = alloc_lightbar_cmd_msg(ec);
205 if (!msg)
206 return -ENOMEM;
208 param = (struct ec_params_lightbar *)msg->data;
209 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
210 param->set_brightness.num = val;
211 ret = lb_throttle();
212 if (ret)
213 goto exit;
215 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
216 if (ret < 0)
217 goto exit;
219 if (msg->result != EC_RES_SUCCESS) {
220 ret = -EINVAL;
221 goto exit;
224 ret = count;
225 exit:
226 kfree(msg);
227 return ret;
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
236 * over.
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);
245 unsigned int val[4];
246 int ret, i = 0, j = 0, ok = 0;
248 msg = alloc_lightbar_cmd_msg(ec);
249 if (!msg)
250 return -ENOMEM;
252 do {
253 /* Skip any whitespace */
254 while (*buf && isspace(*buf))
255 buf++;
257 if (!*buf)
258 break;
260 ret = sscanf(buf, "%i", &val[i++]);
261 if (ret == 0)
262 goto exit;
264 if (i == 4) {
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) {
276 ret = lb_throttle();
277 if (ret)
278 goto exit;
281 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
282 if (ret < 0)
283 goto exit;
285 if (msg->result != EC_RES_SUCCESS)
286 goto exit;
288 i = 0;
289 ok = 1;
292 /* Skip over the number we just read */
293 while (*buf && !isspace(*buf))
294 buf++;
296 } while (*buf);
298 exit:
299 kfree(msg);
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",
306 "TAP", "PROGRAM",
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;
315 int ret;
316 struct cros_ec_dev *ec = container_of(dev,
317 struct cros_ec_dev, class_dev);
319 msg = alloc_lightbar_cmd_msg(ec);
320 if (!msg)
321 return -ENOMEM;
323 param = (struct ec_params_lightbar *)msg->data;
324 param->cmd = LIGHTBAR_CMD_GET_SEQ;
325 ret = lb_throttle();
326 if (ret)
327 goto exit;
329 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
330 if (ret < 0)
331 goto exit;
333 if (msg->result != EC_RES_SUCCESS) {
334 ret = scnprintf(buf, PAGE_SIZE,
335 "ERROR: EC returned %d\n", msg->result);
336 goto exit;
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);
342 else
343 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
344 seqname[resp->get_seq.num]);
346 exit:
347 kfree(msg);
348 return ret;
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;
355 int ret;
357 msg = alloc_lightbar_cmd_msg(ec);
358 if (!msg)
359 return -ENOMEM;
361 param = (struct ec_params_lightbar *)msg->data;
362 param->cmd = cmd;
364 ret = lb_throttle();
365 if (ret)
366 goto error;
368 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
369 if (ret < 0)
370 goto error;
371 if (msg->result != EC_RES_SUCCESS) {
372 ret = -EINVAL;
373 goto error;
375 ret = 0;
376 error:
377 kfree(msg);
379 return ret;
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;
386 int ret;
388 if (ec != ec_with_lightbar)
389 return 0;
391 msg = alloc_lightbar_cmd_msg(ec);
392 if (!msg)
393 return -ENOMEM;
395 param = (struct ec_params_lightbar *)msg->data;
397 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
398 param->manual_suspend_ctrl.enable = enable;
400 ret = lb_throttle();
401 if (ret)
402 goto error;
404 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
405 if (ret < 0)
406 goto error;
407 if (msg->result != EC_RES_SUCCESS) {
408 ret = -EINVAL;
409 goto error;
411 ret = 0;
412 error:
413 kfree(msg);
415 return ret;
418 int lb_suspend(struct cros_ec_dev *ec)
420 if (userspace_control || ec != ec_with_lightbar)
421 return 0;
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)
429 return 0;
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;
439 unsigned int num;
440 int ret, len;
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]))
446 break;
448 for (num = 0; num < ARRAY_SIZE(seqname); num++)
449 if (!strncasecmp(seqname[num], buf, len))
450 break;
452 if (num >= ARRAY_SIZE(seqname)) {
453 ret = kstrtouint(buf, 0, &num);
454 if (ret)
455 return ret;
458 msg = alloc_lightbar_cmd_msg(ec);
459 if (!msg)
460 return -ENOMEM;
462 param = (struct ec_params_lightbar *)msg->data;
463 param->cmd = LIGHTBAR_CMD_SEQ;
464 param->seq.num = num;
465 ret = lb_throttle();
466 if (ret)
467 goto exit;
469 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
470 if (ret < 0)
471 goto exit;
473 if (msg->result != EC_RES_SUCCESS) {
474 ret = -EINVAL;
475 goto exit;
478 ret = count;
479 exit:
480 kfree(msg);
481 return ret;
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,
491 class_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);
506 return -EINVAL;
509 msg = alloc_lightbar_cmd_msg(ec);
510 if (!msg)
511 return -ENOMEM;
513 ret = lb_throttle();
514 if (ret)
515 goto exit;
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);
533 if (ret < 0)
534 goto exit;
535 if (msg->result != EC_RES_SUCCESS) {
536 ret = -EINVAL;
537 goto exit;
540 ret = count;
541 exit:
542 kfree(msg);
544 return ret;
547 static ssize_t userspace_control_show(struct device *dev,
548 struct device_attribute *attr,
549 char *buf)
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,
556 const char *buf,
557 size_t count)
559 bool enable;
560 int ret;
562 ret = strtobool(buf, &enable);
563 if (ret < 0)
564 return ret;
566 userspace_control = enable;
568 return count;
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,
589 NULL,
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;
605 int is_cros_ec;
607 is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
609 if (is_cros_ec != 0)
610 return 0;
612 /* Only instantiate this stuff if the EC has a lightbar */
613 if (ec_has_lightbar(ec)) {
614 ec_with_lightbar = ec;
615 return a->mode;
617 return 0;
620 struct attribute_group cros_ec_lightbar_attr_group = {
621 .name = "lightbar",
622 .attrs = __lb_cmds_attrs,
623 .is_visible = cros_ec_lightbar_attrs_are_visible,