Loading...
1// SPDX-License-Identifier: GPL-2.0+
2// Expose the Chromebook Pixel lightbar to userspace
3//
4// Copyright (C) 2014 Google, Inc.
5
6#include <linux/ctype.h>
7#include <linux/delay.h>
8#include <linux/device.h>
9#include <linux/fs.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>
20
21#define DRV_NAME "cros-ec-lightbar"
22
23/* Rate-limit the lightbar interface to prevent DoS. */
24static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
25
26/*
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.
29 */
30static bool userspace_control;
31
32static ssize_t interval_msec_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
34{
35 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
36
37 return sysfs_emit(buf, "%lu\n", msec);
38}
39
40static ssize_t interval_msec_store(struct device *dev,
41 struct device_attribute *attr,
42 const char *buf, size_t count)
43{
44 unsigned long msec;
45
46 if (kstrtoul(buf, 0, &msec))
47 return -EINVAL;
48
49 lb_interval_jiffies = msec * HZ / 1000;
50
51 return count;
52}
53
54static DEFINE_MUTEX(lb_mutex);
55/* Return 0 if able to throttle correctly, error otherwise */
56static int lb_throttle(void)
57{
58 static unsigned long last_access;
59 unsigned long now, next_timeslot;
60 long delay;
61 int ret = 0;
62
63 mutex_lock(&lb_mutex);
64
65 now = jiffies;
66 next_timeslot = last_access + lb_interval_jiffies;
67
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 */
73 ret = -EINTR;
74 goto out;
75 }
76 now = jiffies;
77 }
78
79 last_access = now;
80out:
81 mutex_unlock(&lb_mutex);
82
83 return ret;
84}
85
86static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
87{
88 struct cros_ec_command *msg;
89 int len;
90
91 len = max(sizeof(struct ec_params_lightbar),
92 sizeof(struct ec_response_lightbar));
93
94 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
95 if (!msg)
96 return NULL;
97
98 msg->version = 0;
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);
102
103 return msg;
104}
105
106static int get_lightbar_version(struct cros_ec_dev *ec,
107 uint32_t *ver_ptr, uint32_t *flg_ptr)
108{
109 struct ec_params_lightbar *param;
110 struct ec_response_lightbar *resp;
111 struct cros_ec_command *msg;
112 int ret;
113
114 msg = alloc_lightbar_cmd_msg(ec);
115 if (!msg)
116 return 0;
117
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) {
124 ret = 0;
125 goto exit;
126 }
127
128 switch (msg->result) {
129 case EC_RES_INVALID_PARAM:
130 /* Pixel had no version command. */
131 if (ver_ptr)
132 *ver_ptr = 0;
133 if (flg_ptr)
134 *flg_ptr = 0;
135 ret = 1;
136 goto exit;
137
138 case EC_RES_SUCCESS:
139 resp = (struct ec_response_lightbar *)msg->data;
140
141 /* Future devices w/lightbars should implement this command */
142 if (ver_ptr)
143 *ver_ptr = resp->version.num;
144 if (flg_ptr)
145 *flg_ptr = resp->version.flags;
146 ret = 1;
147 goto exit;
148 }
149
150 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
151 ret = 0;
152exit:
153 kfree(msg);
154 return ret;
155}
156
157static ssize_t version_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 uint32_t version = 0, flags = 0;
161 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
162 int ret;
163
164 ret = lb_throttle();
165 if (ret)
166 return ret;
167
168 /* This should always succeed, because we check during init. */
169 if (!get_lightbar_version(ec, &version, &flags))
170 return -EIO;
171
172 return sysfs_emit(buf, "%d %d\n", version, flags);
173}
174
175static ssize_t brightness_store(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf, size_t count)
178{
179 struct ec_params_lightbar *param;
180 struct cros_ec_command *msg;
181 int ret;
182 unsigned int val;
183 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
184
185 if (kstrtouint(buf, 0, &val))
186 return -EINVAL;
187
188 msg = alloc_lightbar_cmd_msg(ec);
189 if (!msg)
190 return -ENOMEM;
191
192 param = (struct ec_params_lightbar *)msg->data;
193 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
194 param->set_brightness.num = val;
195 ret = lb_throttle();
196 if (ret)
197 goto exit;
198
199 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
200 if (ret < 0)
201 goto exit;
202
203 ret = count;
204exit:
205 kfree(msg);
206 return ret;
207}
208
209
210/*
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
215 * over.
216 */
217static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
219{
220 struct ec_params_lightbar *param;
221 struct cros_ec_command *msg;
222 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
223 unsigned int val[4];
224 int ret, i = 0, j = 0, ok = 0;
225
226 msg = alloc_lightbar_cmd_msg(ec);
227 if (!msg)
228 return -ENOMEM;
229
230 do {
231 /* Skip any whitespace */
232 while (*buf && isspace(*buf))
233 buf++;
234
235 if (!*buf)
236 break;
237
238 ret = sscanf(buf, "%i", &val[i++]);
239 if (ret == 0)
240 goto exit;
241
242 if (i == 4) {
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];
249 /*
250 * Throttle only the first of every four transactions,
251 * so that the user can update all four LEDs at once.
252 */
253 if ((j++ % 4) == 0) {
254 ret = lb_throttle();
255 if (ret)
256 goto exit;
257 }
258
259 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
260 if (ret < 0)
261 goto exit;
262
263 i = 0;
264 ok = 1;
265 }
266
267 /* Skip over the number we just read */
268 while (*buf && !isspace(*buf))
269 buf++;
270
271 } while (*buf);
272
273exit:
274 kfree(msg);
275 return (ok && i == 0) ? count : -EINVAL;
276}
277
278static char const *seqname[] = {
279 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
280 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
281 "TAP", "PROGRAM",
282};
283
284static ssize_t sequence_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct ec_params_lightbar *param;
288 struct ec_response_lightbar *resp;
289 struct cros_ec_command *msg;
290 int ret;
291 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
292
293 msg = alloc_lightbar_cmd_msg(ec);
294 if (!msg)
295 return -ENOMEM;
296
297 param = (struct ec_params_lightbar *)msg->data;
298 param->cmd = LIGHTBAR_CMD_GET_SEQ;
299 ret = lb_throttle();
300 if (ret)
301 goto exit;
302
303 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
304 if (ret < 0) {
305 ret = sysfs_emit(buf, "XFER / EC ERROR %d / %d\n", ret, msg->result);
306 goto exit;
307 }
308
309 resp = (struct ec_response_lightbar *)msg->data;
310 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
311 ret = sysfs_emit(buf, "%d\n", resp->get_seq.num);
312 else
313 ret = sysfs_emit(buf, "%s\n", seqname[resp->get_seq.num]);
314
315exit:
316 kfree(msg);
317 return ret;
318}
319
320static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
321{
322 struct ec_params_lightbar *param;
323 struct cros_ec_command *msg;
324 int ret;
325
326 msg = alloc_lightbar_cmd_msg(ec);
327 if (!msg)
328 return -ENOMEM;
329
330 param = (struct ec_params_lightbar *)msg->data;
331 param->cmd = cmd;
332
333 ret = lb_throttle();
334 if (ret)
335 goto error;
336
337 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
338 if (ret < 0)
339 goto error;
340
341 ret = 0;
342error:
343 kfree(msg);
344
345 return ret;
346}
347
348static int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
349{
350 struct ec_params_lightbar *param;
351 struct cros_ec_command *msg;
352 int ret;
353
354 msg = alloc_lightbar_cmd_msg(ec);
355 if (!msg)
356 return -ENOMEM;
357
358 param = (struct ec_params_lightbar *)msg->data;
359
360 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
361 param->manual_suspend_ctrl.enable = enable;
362
363 ret = lb_throttle();
364 if (ret)
365 goto error;
366
367 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
368 if (ret < 0)
369 goto error;
370
371 ret = 0;
372error:
373 kfree(msg);
374
375 return ret;
376}
377
378static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
379 const char *buf, size_t count)
380{
381 struct ec_params_lightbar *param;
382 struct cros_ec_command *msg;
383 unsigned int num;
384 int ret, len;
385 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
386
387 for (len = 0; len < count; len++)
388 if (!isalnum(buf[len]))
389 break;
390
391 for (num = 0; num < ARRAY_SIZE(seqname); num++)
392 if (!strncasecmp(seqname[num], buf, len))
393 break;
394
395 if (num >= ARRAY_SIZE(seqname)) {
396 ret = kstrtouint(buf, 0, &num);
397 if (ret)
398 return ret;
399 }
400
401 msg = alloc_lightbar_cmd_msg(ec);
402 if (!msg)
403 return -ENOMEM;
404
405 param = (struct ec_params_lightbar *)msg->data;
406 param->cmd = LIGHTBAR_CMD_SEQ;
407 param->seq.num = num;
408 ret = lb_throttle();
409 if (ret)
410 goto exit;
411
412 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
413 if (ret < 0)
414 goto exit;
415
416 ret = count;
417exit:
418 kfree(msg);
419 return ret;
420}
421
422static ssize_t program_store(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
424{
425 int extra_bytes, max_size, ret;
426 struct ec_params_lightbar *param;
427 struct cros_ec_command *msg;
428 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
429
430 /*
431 * We might need to reject the program for size reasons. The EC
432 * enforces a maximum program size, but we also don't want to try
433 * and send a program that is too big for the protocol. In order
434 * to ensure the latter, we also need to ensure we have extra bytes
435 * to represent the rest of the packet.
436 */
437 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
438 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
439 if (count > max_size) {
440 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
441 (unsigned int)count, max_size);
442
443 return -EINVAL;
444 }
445
446 msg = alloc_lightbar_cmd_msg(ec);
447 if (!msg)
448 return -ENOMEM;
449
450 ret = lb_throttle();
451 if (ret)
452 goto exit;
453
454 dev_info(dev, "Copying %zu byte program to EC", count);
455
456 param = (struct ec_params_lightbar *)msg->data;
457 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
458
459 param->set_program.size = count;
460 memcpy(param->set_program.data, buf, count);
461
462 /*
463 * We need to set the message size manually or else it will use
464 * EC_LB_PROG_LEN. This might be too long, and the program
465 * is unlikely to use all of the space.
466 */
467 msg->outsize = count + extra_bytes;
468
469 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
470 if (ret < 0)
471 goto exit;
472
473 ret = count;
474exit:
475 kfree(msg);
476
477 return ret;
478}
479
480static ssize_t userspace_control_show(struct device *dev,
481 struct device_attribute *attr,
482 char *buf)
483{
484 return sysfs_emit(buf, "%d\n", userspace_control);
485}
486
487static ssize_t userspace_control_store(struct device *dev,
488 struct device_attribute *attr,
489 const char *buf,
490 size_t count)
491{
492 bool enable;
493 int ret;
494
495 ret = kstrtobool(buf, &enable);
496 if (ret < 0)
497 return ret;
498
499 userspace_control = enable;
500
501 return count;
502}
503
504/* Module initialization */
505
506static DEVICE_ATTR_RW(interval_msec);
507static DEVICE_ATTR_RO(version);
508static DEVICE_ATTR_WO(brightness);
509static DEVICE_ATTR_WO(led_rgb);
510static DEVICE_ATTR_RW(sequence);
511static DEVICE_ATTR_WO(program);
512static DEVICE_ATTR_RW(userspace_control);
513
514static struct attribute *__lb_cmds_attrs[] = {
515 &dev_attr_interval_msec.attr,
516 &dev_attr_version.attr,
517 &dev_attr_brightness.attr,
518 &dev_attr_led_rgb.attr,
519 &dev_attr_sequence.attr,
520 &dev_attr_program.attr,
521 &dev_attr_userspace_control.attr,
522 NULL,
523};
524
525static const struct attribute_group cros_ec_lightbar_attr_group = {
526 .name = "lightbar",
527 .attrs = __lb_cmds_attrs,
528};
529
530static int cros_ec_lightbar_probe(struct platform_device *pd)
531{
532 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
533 struct cros_ec_platform *pdata = dev_get_platdata(ec_dev->dev);
534 struct device *dev = &pd->dev;
535 int ret;
536
537 /*
538 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
539 * devices like 'cros_pd' doesn't have a lightbar.
540 */
541 if (strcmp(pdata->ec_name, CROS_EC_DEV_NAME) != 0)
542 return -ENODEV;
543
544 /*
545 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
546 * doesn't have a lightbar.
547 */
548 if (!get_lightbar_version(ec_dev, NULL, NULL))
549 return -ENODEV;
550
551 /* Take control of the lightbar from the EC. */
552 lb_manual_suspend_ctrl(ec_dev, 1);
553
554 ret = sysfs_create_group(&ec_dev->class_dev.kobj,
555 &cros_ec_lightbar_attr_group);
556 if (ret < 0)
557 dev_err(dev, "failed to create %s attributes. err=%d\n",
558 cros_ec_lightbar_attr_group.name, ret);
559
560 return ret;
561}
562
563static void cros_ec_lightbar_remove(struct platform_device *pd)
564{
565 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
566
567 sysfs_remove_group(&ec_dev->class_dev.kobj,
568 &cros_ec_lightbar_attr_group);
569
570 /* Let the EC take over the lightbar again. */
571 lb_manual_suspend_ctrl(ec_dev, 0);
572}
573
574static int __maybe_unused cros_ec_lightbar_resume(struct device *dev)
575{
576 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
577
578 if (userspace_control)
579 return 0;
580
581 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_RESUME);
582}
583
584static int __maybe_unused cros_ec_lightbar_suspend(struct device *dev)
585{
586 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
587
588 if (userspace_control)
589 return 0;
590
591 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_SUSPEND);
592}
593
594static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops,
595 cros_ec_lightbar_suspend, cros_ec_lightbar_resume);
596
597static struct platform_driver cros_ec_lightbar_driver = {
598 .driver = {
599 .name = DRV_NAME,
600 .pm = &cros_ec_lightbar_pm_ops,
601 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
602 },
603 .probe = cros_ec_lightbar_probe,
604 .remove_new = cros_ec_lightbar_remove,
605};
606
607module_platform_driver(cros_ec_lightbar_driver);
608
609MODULE_LICENSE("GPL");
610MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");
611MODULE_ALIAS("platform:" DRV_NAME);
1// SPDX-License-Identifier: GPL-2.0+
2// Expose the Chromebook Pixel lightbar to userspace
3//
4// Copyright (C) 2014 Google, Inc.
5
6#include <linux/ctype.h>
7#include <linux/delay.h>
8#include <linux/device.h>
9#include <linux/fs.h>
10#include <linux/kobject.h>
11#include <linux/module.h>
12#include <linux/platform_data/cros_ec_commands.h>
13#include <linux/platform_data/cros_ec_proto.h>
14#include <linux/platform_device.h>
15#include <linux/sched.h>
16#include <linux/types.h>
17#include <linux/uaccess.h>
18#include <linux/slab.h>
19
20#define DRV_NAME "cros-ec-lightbar"
21
22/* Rate-limit the lightbar interface to prevent DoS. */
23static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
24
25/*
26 * Whether or not we have given userspace control of the lightbar.
27 * If this is true, we won't do anything during suspend/resume.
28 */
29static bool userspace_control;
30
31static ssize_t interval_msec_show(struct device *dev,
32 struct device_attribute *attr, char *buf)
33{
34 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
35
36 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
37}
38
39static ssize_t interval_msec_store(struct device *dev,
40 struct device_attribute *attr,
41 const char *buf, size_t count)
42{
43 unsigned long msec;
44
45 if (kstrtoul(buf, 0, &msec))
46 return -EINVAL;
47
48 lb_interval_jiffies = msec * HZ / 1000;
49
50 return count;
51}
52
53static DEFINE_MUTEX(lb_mutex);
54/* Return 0 if able to throttle correctly, error otherwise */
55static int lb_throttle(void)
56{
57 static unsigned long last_access;
58 unsigned long now, next_timeslot;
59 long delay;
60 int ret = 0;
61
62 mutex_lock(&lb_mutex);
63
64 now = jiffies;
65 next_timeslot = last_access + lb_interval_jiffies;
66
67 if (time_before(now, next_timeslot)) {
68 delay = (long)(next_timeslot) - (long)now;
69 set_current_state(TASK_INTERRUPTIBLE);
70 if (schedule_timeout(delay) > 0) {
71 /* interrupted - just abort */
72 ret = -EINTR;
73 goto out;
74 }
75 now = jiffies;
76 }
77
78 last_access = now;
79out:
80 mutex_unlock(&lb_mutex);
81
82 return ret;
83}
84
85static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
86{
87 struct cros_ec_command *msg;
88 int len;
89
90 len = max(sizeof(struct ec_params_lightbar),
91 sizeof(struct ec_response_lightbar));
92
93 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
94 if (!msg)
95 return NULL;
96
97 msg->version = 0;
98 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
99 msg->outsize = sizeof(struct ec_params_lightbar);
100 msg->insize = sizeof(struct ec_response_lightbar);
101
102 return msg;
103}
104
105static int get_lightbar_version(struct cros_ec_dev *ec,
106 uint32_t *ver_ptr, uint32_t *flg_ptr)
107{
108 struct ec_params_lightbar *param;
109 struct ec_response_lightbar *resp;
110 struct cros_ec_command *msg;
111 int ret;
112
113 msg = alloc_lightbar_cmd_msg(ec);
114 if (!msg)
115 return 0;
116
117 param = (struct ec_params_lightbar *)msg->data;
118 param->cmd = LIGHTBAR_CMD_VERSION;
119 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
120 if (ret < 0) {
121 ret = 0;
122 goto exit;
123 }
124
125 switch (msg->result) {
126 case EC_RES_INVALID_PARAM:
127 /* Pixel had no version command. */
128 if (ver_ptr)
129 *ver_ptr = 0;
130 if (flg_ptr)
131 *flg_ptr = 0;
132 ret = 1;
133 goto exit;
134
135 case EC_RES_SUCCESS:
136 resp = (struct ec_response_lightbar *)msg->data;
137
138 /* Future devices w/lightbars should implement this command */
139 if (ver_ptr)
140 *ver_ptr = resp->version.num;
141 if (flg_ptr)
142 *flg_ptr = resp->version.flags;
143 ret = 1;
144 goto exit;
145 }
146
147 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
148 ret = 0;
149exit:
150 kfree(msg);
151 return ret;
152}
153
154static ssize_t version_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
157 uint32_t version = 0, flags = 0;
158 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
159 int ret;
160
161 ret = lb_throttle();
162 if (ret)
163 return ret;
164
165 /* This should always succeed, because we check during init. */
166 if (!get_lightbar_version(ec, &version, &flags))
167 return -EIO;
168
169 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
170}
171
172static ssize_t brightness_store(struct device *dev,
173 struct device_attribute *attr,
174 const char *buf, size_t count)
175{
176 struct ec_params_lightbar *param;
177 struct cros_ec_command *msg;
178 int ret;
179 unsigned int val;
180 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
181
182 if (kstrtouint(buf, 0, &val))
183 return -EINVAL;
184
185 msg = alloc_lightbar_cmd_msg(ec);
186 if (!msg)
187 return -ENOMEM;
188
189 param = (struct ec_params_lightbar *)msg->data;
190 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
191 param->set_brightness.num = val;
192 ret = lb_throttle();
193 if (ret)
194 goto exit;
195
196 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
197 if (ret < 0)
198 goto exit;
199
200 ret = count;
201exit:
202 kfree(msg);
203 return ret;
204}
205
206
207/*
208 * We expect numbers, and we'll keep reading until we find them, skipping over
209 * any whitespace (sysfs guarantees that the input is null-terminated). Every
210 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
211 * parsing error, if we don't parse any numbers, or if we have numbers left
212 * over.
213 */
214static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
215 const char *buf, size_t count)
216{
217 struct ec_params_lightbar *param;
218 struct cros_ec_command *msg;
219 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
220 unsigned int val[4];
221 int ret, i = 0, j = 0, ok = 0;
222
223 msg = alloc_lightbar_cmd_msg(ec);
224 if (!msg)
225 return -ENOMEM;
226
227 do {
228 /* Skip any whitespace */
229 while (*buf && isspace(*buf))
230 buf++;
231
232 if (!*buf)
233 break;
234
235 ret = sscanf(buf, "%i", &val[i++]);
236 if (ret == 0)
237 goto exit;
238
239 if (i == 4) {
240 param = (struct ec_params_lightbar *)msg->data;
241 param->cmd = LIGHTBAR_CMD_SET_RGB;
242 param->set_rgb.led = val[0];
243 param->set_rgb.red = val[1];
244 param->set_rgb.green = val[2];
245 param->set_rgb.blue = val[3];
246 /*
247 * Throttle only the first of every four transactions,
248 * so that the user can update all four LEDs at once.
249 */
250 if ((j++ % 4) == 0) {
251 ret = lb_throttle();
252 if (ret)
253 goto exit;
254 }
255
256 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
257 if (ret < 0)
258 goto exit;
259
260 i = 0;
261 ok = 1;
262 }
263
264 /* Skip over the number we just read */
265 while (*buf && !isspace(*buf))
266 buf++;
267
268 } while (*buf);
269
270exit:
271 kfree(msg);
272 return (ok && i == 0) ? count : -EINVAL;
273}
274
275static char const *seqname[] = {
276 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
277 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
278 "TAP", "PROGRAM",
279};
280
281static ssize_t sequence_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 struct ec_params_lightbar *param;
285 struct ec_response_lightbar *resp;
286 struct cros_ec_command *msg;
287 int ret;
288 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
289
290 msg = alloc_lightbar_cmd_msg(ec);
291 if (!msg)
292 return -ENOMEM;
293
294 param = (struct ec_params_lightbar *)msg->data;
295 param->cmd = LIGHTBAR_CMD_GET_SEQ;
296 ret = lb_throttle();
297 if (ret)
298 goto exit;
299
300 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
301 if (ret == -EPROTO) {
302 ret = scnprintf(buf, PAGE_SIZE,
303 "ERROR: EC returned %d\n", msg->result);
304 goto exit;
305 } else if (ret < 0) {
306 goto exit;
307 }
308
309 resp = (struct ec_response_lightbar *)msg->data;
310 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
311 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
312 else
313 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
314 seqname[resp->get_seq.num]);
315
316exit:
317 kfree(msg);
318 return ret;
319}
320
321static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
322{
323 struct ec_params_lightbar *param;
324 struct cros_ec_command *msg;
325 int ret;
326
327 msg = alloc_lightbar_cmd_msg(ec);
328 if (!msg)
329 return -ENOMEM;
330
331 param = (struct ec_params_lightbar *)msg->data;
332 param->cmd = cmd;
333
334 ret = lb_throttle();
335 if (ret)
336 goto error;
337
338 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
339 if (ret < 0)
340 goto error;
341
342 ret = 0;
343error:
344 kfree(msg);
345
346 return ret;
347}
348
349static int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
350{
351 struct ec_params_lightbar *param;
352 struct cros_ec_command *msg;
353 int ret;
354
355 msg = alloc_lightbar_cmd_msg(ec);
356 if (!msg)
357 return -ENOMEM;
358
359 param = (struct ec_params_lightbar *)msg->data;
360
361 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
362 param->manual_suspend_ctrl.enable = enable;
363
364 ret = lb_throttle();
365 if (ret)
366 goto error;
367
368 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
369 if (ret < 0)
370 goto error;
371
372 ret = 0;
373error:
374 kfree(msg);
375
376 return ret;
377}
378
379static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
381{
382 struct ec_params_lightbar *param;
383 struct cros_ec_command *msg;
384 unsigned int num;
385 int ret, len;
386 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
387
388 for (len = 0; len < count; len++)
389 if (!isalnum(buf[len]))
390 break;
391
392 for (num = 0; num < ARRAY_SIZE(seqname); num++)
393 if (!strncasecmp(seqname[num], buf, len))
394 break;
395
396 if (num >= ARRAY_SIZE(seqname)) {
397 ret = kstrtouint(buf, 0, &num);
398 if (ret)
399 return ret;
400 }
401
402 msg = alloc_lightbar_cmd_msg(ec);
403 if (!msg)
404 return -ENOMEM;
405
406 param = (struct ec_params_lightbar *)msg->data;
407 param->cmd = LIGHTBAR_CMD_SEQ;
408 param->seq.num = num;
409 ret = lb_throttle();
410 if (ret)
411 goto exit;
412
413 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
414 if (ret < 0)
415 goto exit;
416
417 ret = count;
418exit:
419 kfree(msg);
420 return ret;
421}
422
423static ssize_t program_store(struct device *dev, struct device_attribute *attr,
424 const char *buf, size_t count)
425{
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);
430
431 /*
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.
437 */
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);
443
444 return -EINVAL;
445 }
446
447 msg = alloc_lightbar_cmd_msg(ec);
448 if (!msg)
449 return -ENOMEM;
450
451 ret = lb_throttle();
452 if (ret)
453 goto exit;
454
455 dev_info(dev, "Copying %zu byte program to EC", count);
456
457 param = (struct ec_params_lightbar *)msg->data;
458 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
459
460 param->set_program.size = count;
461 memcpy(param->set_program.data, buf, count);
462
463 /*
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.
467 */
468 msg->outsize = count + extra_bytes;
469
470 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
471 if (ret < 0)
472 goto exit;
473
474 ret = count;
475exit:
476 kfree(msg);
477
478 return ret;
479}
480
481static ssize_t userspace_control_show(struct device *dev,
482 struct device_attribute *attr,
483 char *buf)
484{
485 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
486}
487
488static ssize_t userspace_control_store(struct device *dev,
489 struct device_attribute *attr,
490 const char *buf,
491 size_t count)
492{
493 bool enable;
494 int ret;
495
496 ret = strtobool(buf, &enable);
497 if (ret < 0)
498 return ret;
499
500 userspace_control = enable;
501
502 return count;
503}
504
505/* Module initialization */
506
507static DEVICE_ATTR_RW(interval_msec);
508static DEVICE_ATTR_RO(version);
509static DEVICE_ATTR_WO(brightness);
510static DEVICE_ATTR_WO(led_rgb);
511static DEVICE_ATTR_RW(sequence);
512static DEVICE_ATTR_WO(program);
513static DEVICE_ATTR_RW(userspace_control);
514
515static 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,
523 NULL,
524};
525
526static struct attribute_group cros_ec_lightbar_attr_group = {
527 .name = "lightbar",
528 .attrs = __lb_cmds_attrs,
529};
530
531static int cros_ec_lightbar_probe(struct platform_device *pd)
532{
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;
536 int ret;
537
538 /*
539 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
540 * devices like 'cros_pd' doesn't have a lightbar.
541 */
542 if (strcmp(pdata->ec_name, CROS_EC_DEV_NAME) != 0)
543 return -ENODEV;
544
545 /*
546 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
547 * doesn't have a lightbar.
548 */
549 if (!get_lightbar_version(ec_dev, NULL, NULL))
550 return -ENODEV;
551
552 /* Take control of the lightbar from the EC. */
553 lb_manual_suspend_ctrl(ec_dev, 1);
554
555 ret = sysfs_create_group(&ec_dev->class_dev.kobj,
556 &cros_ec_lightbar_attr_group);
557 if (ret < 0)
558 dev_err(dev, "failed to create %s attributes. err=%d\n",
559 cros_ec_lightbar_attr_group.name, ret);
560
561 return ret;
562}
563
564static int cros_ec_lightbar_remove(struct platform_device *pd)
565{
566 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
567
568 sysfs_remove_group(&ec_dev->class_dev.kobj,
569 &cros_ec_lightbar_attr_group);
570
571 /* Let the EC take over the lightbar again. */
572 lb_manual_suspend_ctrl(ec_dev, 0);
573
574 return 0;
575}
576
577static int __maybe_unused cros_ec_lightbar_resume(struct device *dev)
578{
579 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
580
581 if (userspace_control)
582 return 0;
583
584 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_RESUME);
585}
586
587static int __maybe_unused cros_ec_lightbar_suspend(struct device *dev)
588{
589 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
590
591 if (userspace_control)
592 return 0;
593
594 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_SUSPEND);
595}
596
597static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops,
598 cros_ec_lightbar_suspend, cros_ec_lightbar_resume);
599
600static struct platform_driver cros_ec_lightbar_driver = {
601 .driver = {
602 .name = DRV_NAME,
603 .pm = &cros_ec_lightbar_pm_ops,
604 },
605 .probe = cros_ec_lightbar_probe,
606 .remove = cros_ec_lightbar_remove,
607};
608
609module_platform_driver(cros_ec_lightbar_driver);
610
611MODULE_LICENSE("GPL");
612MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");
613MODULE_ALIAS("platform:" DRV_NAME);