cros_ec_lightbar.c 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * cros_ec_lightbar - expose the Chromebook Pixel lightbar to userspace
 *
 * Copyright (C) 2014 Google, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define pr_fmt(fmt) "cros_ec_lightbar: " fmt

#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kobject.h>
#include <linux/mfd/cros_ec.h>
#include <linux/mfd/cros_ec_commands.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/uaccess.h>
34
#include <linux/slab.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

#include "cros_ec_dev.h"

/* Rate-limit the lightbar interface to prevent DoS. */
static unsigned long lb_interval_jiffies = 50 * HZ / 1000;

static ssize_t interval_msec_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	unsigned long msec = lb_interval_jiffies * 1000 / HZ;

	return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
}

static ssize_t interval_msec_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	unsigned long msec;

	if (kstrtoul(buf, 0, &msec))
		return -EINVAL;

	lb_interval_jiffies = msec * HZ / 1000;

	return count;
}

static DEFINE_MUTEX(lb_mutex);
/* Return 0 if able to throttle correctly, error otherwise */
static int lb_throttle(void)
{
	static unsigned long last_access;
	unsigned long now, next_timeslot;
	long delay;
	int ret = 0;

	mutex_lock(&lb_mutex);

	now = jiffies;
	next_timeslot = last_access + lb_interval_jiffies;

	if (time_before(now, next_timeslot)) {
		delay = (long)(next_timeslot) - (long)now;
		set_current_state(TASK_INTERRUPTIBLE);
		if (schedule_timeout(delay) > 0) {
			/* interrupted - just abort */
			ret = -EINTR;
			goto out;
		}
		now = jiffies;
	}

	last_access = now;
out:
	mutex_unlock(&lb_mutex);

	return ret;
}

95
static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
96 97 98 99 100 101 102 103 104 105 106 107
{
	struct cros_ec_command *msg;
	int len;

	len = max(sizeof(struct ec_params_lightbar),
		  sizeof(struct ec_response_lightbar));

	msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
	if (!msg)
		return NULL;

	msg->version = 0;
108
	msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
109 110 111 112 113
	msg->outsize = sizeof(struct ec_params_lightbar);
	msg->insize = sizeof(struct ec_response_lightbar);

	return msg;
}
114

115
static int get_lightbar_version(struct cros_ec_dev *ec,
116 117 118 119
				uint32_t *ver_ptr, uint32_t *flg_ptr)
{
	struct ec_params_lightbar *param;
	struct ec_response_lightbar *resp;
120
	struct cros_ec_command *msg;
121 122
	int ret;

123
	msg = alloc_lightbar_cmd_msg(ec);
124
	if (!msg)
125 126
		return 0;

127 128
	param = (struct ec_params_lightbar *)msg->data;
	param->cmd = LIGHTBAR_CMD_VERSION;
129
	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
130 131 132 133 134 135
	if (ret < 0) {
		ret = 0;
		goto exit;
	}

	switch (msg->result) {
136 137 138 139 140 141
	case EC_RES_INVALID_PARAM:
		/* Pixel had no version command. */
		if (ver_ptr)
			*ver_ptr = 0;
		if (flg_ptr)
			*flg_ptr = 0;
142 143
		ret = 1;
		goto exit;
144 145

	case EC_RES_SUCCESS:
146
		resp = (struct ec_response_lightbar *)msg->data;
147 148 149 150 151 152

		/* Future devices w/lightbars should implement this command */
		if (ver_ptr)
			*ver_ptr = resp->version.num;
		if (flg_ptr)
			*flg_ptr = resp->version.flags;
153 154
		ret = 1;
		goto exit;
155 156 157
	}

	/* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
158 159 160 161
	ret = 0;
exit:
	kfree(msg);
	return ret;
162 163 164 165 166
}

static ssize_t version_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
167
	uint32_t version = 0, flags = 0;
168 169
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	int ret;

	ret = lb_throttle();
	if (ret)
		return ret;

	/* This should always succeed, because we check during init. */
	if (!get_lightbar_version(ec, &version, &flags))
		return -EIO;

	return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
}

static ssize_t brightness_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct ec_params_lightbar *param;
188
	struct cros_ec_command *msg;
189 190
	int ret;
	unsigned int val;
191 192
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
193 194 195 196

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

197
	msg = alloc_lightbar_cmd_msg(ec);
198 199 200 201
	if (!msg)
		return -ENOMEM;

	param = (struct ec_params_lightbar *)msg->data;
202 203
	param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
	param->set_brightness.num = val;
204 205
	ret = lb_throttle();
	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 213 214 215
	if (msg->result != EC_RES_SUCCESS) {
		ret = -EINVAL;
		goto exit;
	}
216

217 218 219 220
	ret = count;
exit:
	kfree(msg);
	return ret;
221 222 223 224 225 226 227 228 229 230 231 232 233 234
}


/*
 * We expect numbers, and we'll keep reading until we find them, skipping over
 * any whitespace (sysfs guarantees that the input is null-terminated). Every
 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
 * parsing error, if we don't parse any numbers, or if we have numbers left
 * over.
 */
static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
{
	struct ec_params_lightbar *param;
235
	struct cros_ec_command *msg;
236 237
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
238 239 240
	unsigned int val[4];
	int ret, i = 0, j = 0, ok = 0;

241
	msg = alloc_lightbar_cmd_msg(ec);
242 243 244
	if (!msg)
		return -ENOMEM;

245 246 247 248 249 250 251 252 253 254
	do {
		/* Skip any whitespace */
		while (*buf && isspace(*buf))
			buf++;

		if (!*buf)
			break;

		ret = sscanf(buf, "%i", &val[i++]);
		if (ret == 0)
255
			goto exit;
256 257

		if (i == 4) {
258
			param = (struct ec_params_lightbar *)msg->data;
259 260 261 262 263
			param->cmd = LIGHTBAR_CMD_SET_RGB;
			param->set_rgb.led = val[0];
			param->set_rgb.red = val[1];
			param->set_rgb.green = val[2];
			param->set_rgb.blue = val[3];
264 265 266 267 268 269 270
			/*
			 * Throttle only the first of every four transactions,
			 * so that the user can update all four LEDs at once.
			 */
			if ((j++ % 4) == 0) {
				ret = lb_throttle();
				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 282 283 284 285 286 287 288 289 290

			i = 0;
			ok = 1;
		}

		/* Skip over the number we just read */
		while (*buf && !isspace(*buf))
			buf++;

	} while (*buf);

291 292
exit:
	kfree(msg);
293 294 295
	return (ok && i == 0) ? count : -EINVAL;
}

296
static char const *seqname[] = {
297 298 299 300 301 302 303 304 305
	"ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
	"S0S3", "S3S5", "STOP", "RUN", "PULSE", "TEST", "KONAMI",
};

static ssize_t sequence_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	struct ec_params_lightbar *param;
	struct ec_response_lightbar *resp;
306
	struct cros_ec_command *msg;
307
	int ret;
308 309
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
310

311
	msg = alloc_lightbar_cmd_msg(ec);
312 313 314 315
	if (!msg)
		return -ENOMEM;

	param = (struct ec_params_lightbar *)msg->data;
316 317 318
	param->cmd = LIGHTBAR_CMD_GET_SEQ;
	ret = lb_throttle();
	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 326 327 328 329
	if (msg->result != EC_RES_SUCCESS) {
		ret = scnprintf(buf, PAGE_SIZE,
				"ERROR: EC returned %d\n", msg->result);
		goto exit;
	}
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 336 337 338 339 340
		ret = scnprintf(buf, PAGE_SIZE, "%s\n",
				seqname[resp->get_seq.num]);

exit:
	kfree(msg);
	return ret;
341 342 343 344 345 346
}

static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct ec_params_lightbar *param;
347
	struct cros_ec_command *msg;
348 349
	unsigned int num;
	int ret, len;
350 351
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

	for (len = 0; len < count; len++)
		if (!isalnum(buf[len]))
			break;

	for (num = 0; num < ARRAY_SIZE(seqname); num++)
		if (!strncasecmp(seqname[num], buf, len))
			break;

	if (num >= ARRAY_SIZE(seqname)) {
		ret = kstrtouint(buf, 0, &num);
		if (ret)
			return ret;
	}

367 368 369 370
	msg = alloc_lightbar_cmd_msg(ec);
	if (!msg)
		return -ENOMEM;

371
	param = (struct ec_params_lightbar *)msg->data;
372 373 374 375
	param->cmd = LIGHTBAR_CMD_SEQ;
	param->seq.num = num;
	ret = lb_throttle();
	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 383 384 385
	if (msg->result != EC_RES_SUCCESS) {
		ret = -EINVAL;
		goto exit;
	}
386

387 388 389 390
	ret = count;
exit:
	kfree(msg);
	return ret;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
}

/* Module initialization */

static DEVICE_ATTR_RW(interval_msec);
static DEVICE_ATTR_RO(version);
static DEVICE_ATTR_WO(brightness);
static DEVICE_ATTR_WO(led_rgb);
static DEVICE_ATTR_RW(sequence);
static struct attribute *__lb_cmds_attrs[] = {
	&dev_attr_interval_msec.attr,
	&dev_attr_version.attr,
	&dev_attr_brightness.attr,
	&dev_attr_led_rgb.attr,
	&dev_attr_sequence.attr,
	NULL,
};

409 410
static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
						  struct attribute *a, int n)
411
{
412 413 414 415 416 417 418
	struct device *dev = container_of(kobj, struct device, kobj);
	struct cros_ec_dev *ec = container_of(dev,
					      struct cros_ec_dev, class_dev);
	struct platform_device *pdev = container_of(ec->dev,
						   struct platform_device, dev);
	if (pdev->id != 0)
		return 0;
419 420

	/* Only instantiate this stuff if the EC has a lightbar */
421 422 423 424
	if (get_lightbar_version(ec, NULL, NULL))
		return a->mode;
	else
		return 0;
425 426
}

427 428 429 430 431
struct attribute_group cros_ec_lightbar_attr_group = {
	.name = "lightbar",
	.attrs = __lb_cmds_attrs,
	.is_visible = cros_ec_lightbar_attrs_are_visible,
};