Linux Audio

Check our new training course

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