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/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_data/cros_ec_commands.h>
15 #include <linux/platform_data/cros_ec_proto.h>
16 #include <linux/platform_device.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
20 #include <linux/slab.h>
22 #define DRV_NAME "cros-ec-lightbar"
24 /* Rate-limit the lightbar interface to prevent DoS. */
25 static unsigned long lb_interval_jiffies
= 50 * HZ
/ 1000;
28 * Whether or not we have given userspace control of the lightbar.
29 * If this is true, we won't do anything during suspend/resume.
31 static bool userspace_control
;
33 static ssize_t
interval_msec_show(struct device
*dev
,
34 struct device_attribute
*attr
, char *buf
)
36 unsigned long msec
= lb_interval_jiffies
* 1000 / HZ
;
38 return sysfs_emit(buf
, "%lu\n", msec
);
41 static ssize_t
interval_msec_store(struct device
*dev
,
42 struct device_attribute
*attr
,
43 const char *buf
, size_t count
)
47 if (kstrtoul(buf
, 0, &msec
))
50 lb_interval_jiffies
= msec
* HZ
/ 1000;
55 static DEFINE_MUTEX(lb_mutex
);
56 /* Return 0 if able to throttle correctly, error otherwise */
57 static int lb_throttle(void)
59 static unsigned long last_access
;
60 unsigned long now
, next_timeslot
;
64 mutex_lock(&lb_mutex
);
67 next_timeslot
= last_access
+ lb_interval_jiffies
;
69 if (time_before(now
, next_timeslot
)) {
70 delay
= (long)(next_timeslot
) - (long)now
;
71 set_current_state(TASK_INTERRUPTIBLE
);
72 if (schedule_timeout(delay
) > 0) {
73 /* interrupted - just abort */
82 mutex_unlock(&lb_mutex
);
87 static struct cros_ec_command
*alloc_lightbar_cmd_msg(struct cros_ec_dev
*ec
)
89 struct cros_ec_command
*msg
;
92 len
= max(sizeof(struct ec_params_lightbar
),
93 sizeof(struct ec_response_lightbar
));
95 msg
= kmalloc(sizeof(*msg
) + len
, GFP_KERNEL
);
100 msg
->command
= EC_CMD_LIGHTBAR_CMD
+ ec
->cmd_offset
;
101 msg
->outsize
= sizeof(struct ec_params_lightbar
);
102 msg
->insize
= sizeof(struct ec_response_lightbar
);
107 static int get_lightbar_version(struct cros_ec_dev
*ec
,
108 uint32_t *ver_ptr
, uint32_t *flg_ptr
)
110 struct ec_params_lightbar
*param
;
111 struct ec_response_lightbar
*resp
;
112 struct cros_ec_command
*msg
;
115 msg
= alloc_lightbar_cmd_msg(ec
);
119 param
= (struct ec_params_lightbar
*)msg
->data
;
120 param
->cmd
= LIGHTBAR_CMD_VERSION
;
121 msg
->outsize
= sizeof(param
->cmd
);
122 msg
->result
= sizeof(resp
->version
);
123 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
124 if (ret
< 0 && ret
!= -EINVAL
) {
129 switch (msg
->result
) {
130 case EC_RES_INVALID_PARAM
:
131 /* Pixel had no version command. */
140 resp
= (struct ec_response_lightbar
*)msg
->data
;
142 /* Future devices w/lightbars should implement this command */
144 *ver_ptr
= resp
->version
.num
;
146 *flg_ptr
= resp
->version
.flags
;
151 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
158 static ssize_t
version_show(struct device
*dev
,
159 struct device_attribute
*attr
, char *buf
)
161 uint32_t version
= 0, flags
= 0;
162 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
169 /* This should always succeed, because we check during init. */
170 if (!get_lightbar_version(ec
, &version
, &flags
))
173 return sysfs_emit(buf
, "%d %d\n", version
, flags
);
176 static ssize_t
brightness_store(struct device
*dev
,
177 struct device_attribute
*attr
,
178 const char *buf
, size_t count
)
180 struct ec_params_lightbar
*param
;
181 struct cros_ec_command
*msg
;
184 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
186 if (kstrtouint(buf
, 0, &val
))
189 msg
= alloc_lightbar_cmd_msg(ec
);
193 param
= (struct ec_params_lightbar
*)msg
->data
;
194 param
->cmd
= LIGHTBAR_CMD_SET_BRIGHTNESS
;
195 param
->set_brightness
.num
= val
;
200 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
212 * We expect numbers, and we'll keep reading until we find them, skipping over
213 * any whitespace (sysfs guarantees that the input is null-terminated). Every
214 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
215 * parsing error, if we don't parse any numbers, or if we have numbers left
218 static ssize_t
led_rgb_store(struct device
*dev
, struct device_attribute
*attr
,
219 const char *buf
, size_t count
)
221 struct ec_params_lightbar
*param
;
222 struct cros_ec_command
*msg
;
223 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
225 int ret
, i
= 0, j
= 0, ok
= 0;
227 msg
= alloc_lightbar_cmd_msg(ec
);
232 /* Skip any whitespace */
233 while (*buf
&& isspace(*buf
))
239 ret
= sscanf(buf
, "%i", &val
[i
++]);
244 param
= (struct ec_params_lightbar
*)msg
->data
;
245 param
->cmd
= LIGHTBAR_CMD_SET_RGB
;
246 param
->set_rgb
.led
= val
[0];
247 param
->set_rgb
.red
= val
[1];
248 param
->set_rgb
.green
= val
[2];
249 param
->set_rgb
.blue
= val
[3];
251 * Throttle only the first of every four transactions,
252 * so that the user can update all four LEDs at once.
254 if ((j
++ % 4) == 0) {
260 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
268 /* Skip over the number we just read */
269 while (*buf
&& !isspace(*buf
))
276 return (ok
&& i
== 0) ? count
: -EINVAL
;
279 static char const *seqname
[] = {
280 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
281 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
285 static ssize_t
sequence_show(struct device
*dev
,
286 struct device_attribute
*attr
, char *buf
)
288 struct ec_params_lightbar
*param
;
289 struct ec_response_lightbar
*resp
;
290 struct cros_ec_command
*msg
;
292 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
294 msg
= alloc_lightbar_cmd_msg(ec
);
298 param
= (struct ec_params_lightbar
*)msg
->data
;
299 param
->cmd
= LIGHTBAR_CMD_GET_SEQ
;
304 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
306 ret
= sysfs_emit(buf
, "XFER / EC ERROR %d / %d\n", ret
, msg
->result
);
310 resp
= (struct ec_response_lightbar
*)msg
->data
;
311 if (resp
->get_seq
.num
>= ARRAY_SIZE(seqname
))
312 ret
= sysfs_emit(buf
, "%d\n", resp
->get_seq
.num
);
314 ret
= sysfs_emit(buf
, "%s\n", seqname
[resp
->get_seq
.num
]);
321 static int lb_send_empty_cmd(struct cros_ec_dev
*ec
, uint8_t cmd
)
323 struct ec_params_lightbar
*param
;
324 struct cros_ec_command
*msg
;
327 msg
= alloc_lightbar_cmd_msg(ec
);
331 param
= (struct ec_params_lightbar
*)msg
->data
;
338 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
349 static int lb_manual_suspend_ctrl(struct cros_ec_dev
*ec
, uint8_t enable
)
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
;
361 param
->cmd
= LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL
;
362 param
->manual_suspend_ctrl
.enable
= enable
;
368 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
379 static ssize_t
sequence_store(struct device
*dev
, struct device_attribute
*attr
,
380 const char *buf
, size_t count
)
382 struct ec_params_lightbar
*param
;
383 struct cros_ec_command
*msg
;
386 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
388 for (len
= 0; len
< count
; len
++)
389 if (!isalnum(buf
[len
]))
392 for (num
= 0; num
< ARRAY_SIZE(seqname
); num
++)
393 if (!strncasecmp(seqname
[num
], buf
, len
))
396 if (num
>= ARRAY_SIZE(seqname
)) {
397 ret
= kstrtouint(buf
, 0, &num
);
402 msg
= alloc_lightbar_cmd_msg(ec
);
406 param
= (struct ec_params_lightbar
*)msg
->data
;
407 param
->cmd
= LIGHTBAR_CMD_SEQ
;
408 param
->seq
.num
= num
;
413 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
423 static ssize_t
program_store(struct device
*dev
, struct device_attribute
*attr
,
424 const char *buf
, size_t count
)
426 int extra_bytes
, max_size
, ret
;
427 struct ec_params_lightbar
*param
;
428 struct cros_ec_command
*msg
;
429 struct cros_ec_dev
*ec
= to_cros_ec_dev(dev
);
432 * We might need to reject the program for size reasons. The EC
433 * enforces a maximum program size, but we also don't want to try
434 * and send a program that is too big for the protocol. In order
435 * to ensure the latter, we also need to ensure we have extra bytes
436 * to represent the rest of the packet.
438 extra_bytes
= sizeof(*param
) - sizeof(param
->set_program
.data
);
439 max_size
= min(EC_LB_PROG_LEN
, ec
->ec_dev
->max_request
- extra_bytes
);
440 if (count
> max_size
) {
441 dev_err(dev
, "Program is %u bytes, too long to send (max: %u)",
442 (unsigned int)count
, max_size
);
447 msg
= alloc_lightbar_cmd_msg(ec
);
455 dev_info(dev
, "Copying %zu byte program to EC", count
);
457 param
= (struct ec_params_lightbar
*)msg
->data
;
458 param
->cmd
= LIGHTBAR_CMD_SET_PROGRAM
;
460 param
->set_program
.size
= count
;
461 memcpy(param
->set_program
.data
, buf
, count
);
464 * We need to set the message size manually or else it will use
465 * EC_LB_PROG_LEN. This might be too long, and the program
466 * is unlikely to use all of the space.
468 msg
->outsize
= count
+ extra_bytes
;
470 ret
= cros_ec_cmd_xfer_status(ec
->ec_dev
, msg
);
481 static ssize_t
userspace_control_show(struct device
*dev
,
482 struct device_attribute
*attr
,
485 return sysfs_emit(buf
, "%d\n", userspace_control
);
488 static ssize_t
userspace_control_store(struct device
*dev
,
489 struct device_attribute
*attr
,
496 ret
= kstrtobool(buf
, &enable
);
500 userspace_control
= enable
;
505 /* Module initialization */
507 static DEVICE_ATTR_RW(interval_msec
);
508 static DEVICE_ATTR_RO(version
);
509 static DEVICE_ATTR_WO(brightness
);
510 static DEVICE_ATTR_WO(led_rgb
);
511 static DEVICE_ATTR_RW(sequence
);
512 static DEVICE_ATTR_WO(program
);
513 static DEVICE_ATTR_RW(userspace_control
);
515 static struct attribute
*__lb_cmds_attrs
[] = {
516 &dev_attr_interval_msec
.attr
,
517 &dev_attr_version
.attr
,
518 &dev_attr_brightness
.attr
,
519 &dev_attr_led_rgb
.attr
,
520 &dev_attr_sequence
.attr
,
521 &dev_attr_program
.attr
,
522 &dev_attr_userspace_control
.attr
,
526 static const struct attribute_group cros_ec_lightbar_attr_group
= {
528 .attrs
= __lb_cmds_attrs
,
531 static int cros_ec_lightbar_probe(struct platform_device
*pd
)
533 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pd
->dev
.parent
);
534 struct cros_ec_platform
*pdata
= dev_get_platdata(ec_dev
->dev
);
535 struct device
*dev
= &pd
->dev
;
539 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
540 * devices like 'cros_pd' doesn't have a lightbar.
542 if (strcmp(pdata
->ec_name
, CROS_EC_DEV_NAME
) != 0)
546 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
547 * doesn't have a lightbar.
549 if (!get_lightbar_version(ec_dev
, NULL
, NULL
))
552 /* Take control of the lightbar from the EC. */
553 lb_manual_suspend_ctrl(ec_dev
, 1);
555 ret
= sysfs_create_group(&ec_dev
->class_dev
.kobj
,
556 &cros_ec_lightbar_attr_group
);
558 dev_err(dev
, "failed to create %s attributes. err=%d\n",
559 cros_ec_lightbar_attr_group
.name
, ret
);
564 static void cros_ec_lightbar_remove(struct platform_device
*pd
)
566 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pd
->dev
.parent
);
568 sysfs_remove_group(&ec_dev
->class_dev
.kobj
,
569 &cros_ec_lightbar_attr_group
);
571 /* Let the EC take over the lightbar again. */
572 lb_manual_suspend_ctrl(ec_dev
, 0);
575 static int __maybe_unused
cros_ec_lightbar_resume(struct device
*dev
)
577 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(dev
->parent
);
579 if (userspace_control
)
582 return lb_send_empty_cmd(ec_dev
, LIGHTBAR_CMD_RESUME
);
585 static int __maybe_unused
cros_ec_lightbar_suspend(struct device
*dev
)
587 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(dev
->parent
);
589 if (userspace_control
)
592 return lb_send_empty_cmd(ec_dev
, LIGHTBAR_CMD_SUSPEND
);
595 static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops
,
596 cros_ec_lightbar_suspend
, cros_ec_lightbar_resume
);
598 static const struct platform_device_id cros_ec_lightbar_id
[] = {
602 MODULE_DEVICE_TABLE(platform
, cros_ec_lightbar_id
);
604 static struct platform_driver cros_ec_lightbar_driver
= {
607 .pm
= &cros_ec_lightbar_pm_ops
,
608 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
610 .probe
= cros_ec_lightbar_probe
,
611 .remove
= cros_ec_lightbar_remove
,
612 .id_table
= cros_ec_lightbar_id
,
615 module_platform_driver(cros_ec_lightbar_driver
);
617 MODULE_LICENSE("GPL");
618 MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");