Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2// ChromeOS EC communication protocol helper functions
   3//
   4// Copyright (C) 2015 Google, Inc
   5
   6#include <linux/delay.h>
   7#include <linux/device.h>
   8#include <linux/module.h>
   9#include <linux/platform_data/cros_ec_commands.h>
  10#include <linux/platform_data/cros_ec_proto.h>
  11#include <linux/slab.h>
  12#include <asm/unaligned.h>
  13
  14#include "cros_ec_trace.h"
  15
  16#define EC_COMMAND_RETRIES	50
  17
  18static const int cros_ec_error_map[] = {
  19	[EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
  20	[EC_RES_ERROR] = -EIO,
  21	[EC_RES_INVALID_PARAM] = -EINVAL,
  22	[EC_RES_ACCESS_DENIED] = -EACCES,
  23	[EC_RES_INVALID_RESPONSE] = -EPROTO,
  24	[EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
  25	[EC_RES_INVALID_CHECKSUM] = -EBADMSG,
  26	[EC_RES_IN_PROGRESS] = -EINPROGRESS,
  27	[EC_RES_UNAVAILABLE] = -ENODATA,
  28	[EC_RES_TIMEOUT] = -ETIMEDOUT,
  29	[EC_RES_OVERFLOW] = -EOVERFLOW,
  30	[EC_RES_INVALID_HEADER] = -EBADR,
  31	[EC_RES_REQUEST_TRUNCATED] = -EBADR,
  32	[EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
  33	[EC_RES_BUS_ERROR] = -EFAULT,
  34	[EC_RES_BUSY] = -EBUSY,
  35	[EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
  36	[EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
  37	[EC_RES_INVALID_DATA_CRC] = -EBADMSG,
  38	[EC_RES_DUP_UNAVAILABLE] = -ENODATA,
  39};
  40
  41static int cros_ec_map_error(uint32_t result)
  42{
  43	int ret = 0;
  44
  45	if (result != EC_RES_SUCCESS) {
  46		if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
  47			ret = cros_ec_error_map[result];
  48		else
  49			ret = -EPROTO;
  50	}
  51
  52	return ret;
  53}
  54
  55static int prepare_tx(struct cros_ec_device *ec_dev,
  56		      struct cros_ec_command *msg)
  57{
  58	struct ec_host_request *request;
  59	u8 *out;
  60	int i;
  61	u8 csum = 0;
  62
  63	if (msg->outsize + sizeof(*request) > ec_dev->dout_size)
  64		return -EINVAL;
  65
  66	out = ec_dev->dout;
  67	request = (struct ec_host_request *)out;
  68	request->struct_version = EC_HOST_REQUEST_VERSION;
  69	request->checksum = 0;
  70	request->command = msg->command;
  71	request->command_version = msg->version;
  72	request->reserved = 0;
  73	request->data_len = msg->outsize;
  74
  75	for (i = 0; i < sizeof(*request); i++)
  76		csum += out[i];
  77
  78	/* Copy data and update checksum */
  79	memcpy(out + sizeof(*request), msg->data, msg->outsize);
  80	for (i = 0; i < msg->outsize; i++)
  81		csum += msg->data[i];
  82
  83	request->checksum = -csum;
  84
  85	return sizeof(*request) + msg->outsize;
  86}
  87
  88static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
  89			     struct cros_ec_command *msg)
  90{
  91	u8 *out;
  92	u8 csum;
  93	int i;
  94
  95	if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE)
  96		return -EINVAL;
  97
  98	out = ec_dev->dout;
  99	out[0] = EC_CMD_VERSION0 + msg->version;
 100	out[1] = msg->command;
 101	out[2] = msg->outsize;
 102	csum = out[0] + out[1] + out[2];
 103	for (i = 0; i < msg->outsize; i++)
 104		csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
 105	out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
 106
 107	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
 108}
 109
 110static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
 111{
 112	int ret;
 113	int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
 114
 
 
 115	if (ec_dev->proto_version > 2)
 116		xfer_fxn = ec_dev->pkt_xfer;
 117	else
 118		xfer_fxn = ec_dev->cmd_xfer;
 119
 120	if (!xfer_fxn) {
 121		/*
 122		 * This error can happen if a communication error happened and
 123		 * the EC is trying to use protocol v2, on an underlying
 124		 * communication mechanism that does not support v2.
 125		 */
 126		dev_err_once(ec_dev->dev, "missing EC transfer API, cannot send command\n");
 
 127		return -EIO;
 128	}
 129
 130	trace_cros_ec_request_start(msg);
 131	ret = (*xfer_fxn)(ec_dev, msg);
 132	trace_cros_ec_request_done(msg, ret);
 133
 134	return ret;
 135}
 
 
 
 
 
 
 
 
 
 
 136
 137static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
 138{
 139	struct {
 140		struct cros_ec_command msg;
 141		struct ec_response_get_comms_status status;
 142	} __packed buf;
 143	struct cros_ec_command *msg = &buf.msg;
 144	struct ec_response_get_comms_status *status = &buf.status;
 145	int ret = 0, i;
 146
 147	msg->version = 0;
 148	msg->command = EC_CMD_GET_COMMS_STATUS;
 149	msg->insize = sizeof(*status);
 150	msg->outsize = 0;
 151
 152	/* Query the EC's status until it's no longer busy or we encounter an error. */
 153	for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
 154		usleep_range(10000, 11000);
 155
 156		ret = cros_ec_xfer_command(ec_dev, msg);
 157		if (ret == -EAGAIN)
 158			continue;
 159		if (ret < 0)
 160			return ret;
 161
 162		*result = msg->result;
 163		if (msg->result != EC_RES_SUCCESS)
 164			return ret;
 165
 166		if (ret == 0) {
 167			ret = -EPROTO;
 168			break;
 
 
 
 
 
 
 
 
 
 
 
 169		}
 170
 171		if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
 172			return ret;
 173	}
 174
 175	if (i >= EC_COMMAND_RETRIES)
 176		ret = -EAGAIN;
 177
 178	return ret;
 179}
 180
 181static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
 182{
 183	int ret = cros_ec_xfer_command(ec_dev, msg);
 184
 185	if (msg->result == EC_RES_IN_PROGRESS)
 186		ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
 187
 188	return ret;
 189}
 190
 191/**
 192 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
 193 * @ec_dev: Device to register.
 194 * @msg: Message to write.
 195 *
 196 * This is used by all ChromeOS EC drivers to prepare the outgoing message
 197 * according to different protocol versions.
 198 *
 199 * Return: number of prepared bytes on success or negative error code.
 200 */
 201int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
 202		       struct cros_ec_command *msg)
 203{
 
 
 
 
 204	if (ec_dev->proto_version > 2)
 205		return prepare_tx(ec_dev, msg);
 
 
 
 
 
 
 
 
 
 
 206
 207	return prepare_tx_legacy(ec_dev, msg);
 208}
 209EXPORT_SYMBOL(cros_ec_prepare_tx);
 210
 211/**
 212 * cros_ec_check_result() - Check ec_msg->result.
 213 * @ec_dev: EC device.
 214 * @msg: Message to check.
 215 *
 216 * This is used by ChromeOS EC drivers to check the ec_msg->result for
 217 * EC_RES_IN_PROGRESS and to warn about them.
 218 *
 219 * The function should not check for furthermore error codes.  Otherwise,
 220 * it would break the ABI.
 221 *
 222 * Return: -EAGAIN if ec_msg->result == EC_RES_IN_PROGRESS.  Otherwise, 0.
 223 */
 224int cros_ec_check_result(struct cros_ec_device *ec_dev,
 225			 struct cros_ec_command *msg)
 226{
 227	switch (msg->result) {
 228	case EC_RES_SUCCESS:
 229		return 0;
 230	case EC_RES_IN_PROGRESS:
 231		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
 232			msg->command);
 233		return -EAGAIN;
 234	default:
 235		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
 236			msg->command, msg->result);
 237		return 0;
 238	}
 239}
 240EXPORT_SYMBOL(cros_ec_check_result);
 241
 242/*
 243 * cros_ec_get_host_event_wake_mask
 244 *
 245 * Get the mask of host events that cause wake from suspend.
 246 *
 247 * @ec_dev: EC device to call
 248 * @msg: message structure to use
 249 * @mask: result when function returns 0.
 250 *
 251 * LOCKING:
 252 * the caller has ec_dev->lock mutex, or the caller knows there is
 253 * no other command in progress.
 254 */
 255static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint32_t *mask)
 
 
 256{
 257	struct cros_ec_command *msg;
 258	struct ec_response_host_event_mask *r;
 259	int ret, mapped;
 260
 261	msg = kzalloc(sizeof(*msg) + sizeof(*r), GFP_KERNEL);
 262	if (!msg)
 263		return -ENOMEM;
 264
 265	msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
 
 
 266	msg->insize = sizeof(*r);
 267
 268	ret = cros_ec_send_command(ec_dev, msg);
 269	if (ret < 0)
 270		goto exit;
 271
 272	mapped = cros_ec_map_error(msg->result);
 273	if (mapped) {
 274		ret = mapped;
 275		goto exit;
 276	}
 277
 278	if (ret == 0) {
 279		ret = -EPROTO;
 280		goto exit;
 281	}
 282
 283	r = (struct ec_response_host_event_mask *)msg->data;
 284	*mask = r->mask;
 285	ret = 0;
 286exit:
 287	kfree(msg);
 288	return ret;
 289}
 290
 291static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
 
 
 292{
 293	struct cros_ec_command *msg;
 294	struct ec_response_get_protocol_info *info;
 295	int ret, mapped;
 296
 297	ec_dev->proto_version = 3;
 298	if (devidx > 0)
 299		ec_dev->max_passthru = 0;
 300
 301	msg = kzalloc(sizeof(*msg) + sizeof(*info), GFP_KERNEL);
 302	if (!msg)
 303		return -ENOMEM;
 304
 
 305	msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
 306	msg->insize = sizeof(*info);
 307
 308	ret = cros_ec_send_command(ec_dev, msg);
 309	/*
 310	 * Send command once again when timeout occurred.
 311	 * Fingerprint MCU (FPMCU) is restarted during system boot which
 312	 * introduces small window in which FPMCU won't respond for any
 313	 * messages sent by kernel. There is no need to wait before next
 314	 * attempt because we waited at least EC_MSG_DEADLINE_MS.
 315	 */
 316	if (ret == -ETIMEDOUT)
 317		ret = cros_ec_send_command(ec_dev, msg);
 318
 319	if (ret < 0) {
 320		dev_dbg(ec_dev->dev,
 321			"failed to check for EC[%d] protocol version: %d\n",
 322			devidx, ret);
 323		goto exit;
 324	}
 325
 326	mapped = cros_ec_map_error(msg->result);
 327	if (mapped) {
 328		ret = mapped;
 329		goto exit;
 330	}
 331
 332	if (ret == 0) {
 333		ret = -EPROTO;
 334		goto exit;
 335	}
 336
 337	info = (struct ec_response_get_protocol_info *)msg->data;
 
 
 
 338
 339	switch (devidx) {
 340	case CROS_EC_DEV_EC_INDEX:
 341		ec_dev->max_request = info->max_request_packet_size -
 342						sizeof(struct ec_host_request);
 343		ec_dev->max_response = info->max_response_packet_size -
 344						sizeof(struct ec_host_response);
 345		ec_dev->proto_version = min(EC_HOST_REQUEST_VERSION,
 346					    fls(info->protocol_versions) - 1);
 347		ec_dev->din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
 348		ec_dev->dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
 349
 350		dev_dbg(ec_dev->dev, "using proto v%u\n", ec_dev->proto_version);
 351		break;
 352	case CROS_EC_DEV_PD_INDEX:
 353		ec_dev->max_passthru = info->max_request_packet_size -
 354						sizeof(struct ec_host_request);
 355
 356		dev_dbg(ec_dev->dev, "found PD chip\n");
 357		break;
 358	default:
 359		dev_dbg(ec_dev->dev, "unknown passthru index: %d\n", devidx);
 360		break;
 361	}
 362
 363	ret = 0;
 364exit:
 365	kfree(msg);
 366	return ret;
 367}
 368
 369static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
 370{
 371	struct cros_ec_command *msg;
 372	struct ec_params_hello *params;
 373	struct ec_response_hello *response;
 374	int ret, mapped;
 375
 376	ec_dev->proto_version = 2;
 377
 378	msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)), GFP_KERNEL);
 379	if (!msg)
 380		return -ENOMEM;
 381
 
 382	msg->command = EC_CMD_HELLO;
 383	msg->insize = sizeof(*response);
 384	msg->outsize = sizeof(*params);
 
 
 385
 386	params = (struct ec_params_hello *)msg->data;
 387	params->in_data = 0xa0b0c0d0;
 388
 389	ret = cros_ec_send_command(ec_dev, msg);
 390	if (ret < 0) {
 391		dev_dbg(ec_dev->dev, "EC failed to respond to v2 hello: %d\n", ret);
 392		goto exit;
 393	}
 394
 395	mapped = cros_ec_map_error(msg->result);
 396	if (mapped) {
 397		ret = mapped;
 398		dev_err(ec_dev->dev, "EC responded to v2 hello with error: %d\n", msg->result);
 399		goto exit;
 400	}
 401
 402	if (ret == 0) {
 403		ret = -EPROTO;
 
 404		goto exit;
 405	}
 406
 407	response = (struct ec_response_hello *)msg->data;
 408	if (response->out_data != 0xa1b2c3d4) {
 409		dev_err(ec_dev->dev,
 410			"EC responded to v2 hello with bad result: %u\n",
 411			response->out_data);
 412		ret = -EBADMSG;
 413		goto exit;
 414	}
 415
 416	ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
 417	ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
 418	ec_dev->max_passthru = 0;
 419	ec_dev->pkt_xfer = NULL;
 420	ec_dev->din_size = EC_PROTO2_MSG_BYTES;
 421	ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
 422
 423	dev_dbg(ec_dev->dev, "falling back to proto v2\n");
 424	ret = 0;
 425exit:
 
 426	kfree(msg);
 427	return ret;
 428}
 429
 430/*
 431 * cros_ec_get_host_command_version_mask
 432 *
 433 * Get the version mask of a given command.
 434 *
 435 * @ec_dev: EC device to call
 436 * @msg: message structure to use
 437 * @cmd: command to get the version of.
 438 * @mask: result when function returns 0.
 439 *
 440 * @return 0 on success, error code otherwise
 441 *
 442 * LOCKING:
 443 * the caller has ec_dev->lock mutex or the caller knows there is
 444 * no other command in progress.
 445 */
 446static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev, u16 cmd, u32 *mask)
 
 447{
 448	struct ec_params_get_cmd_versions *pver;
 449	struct ec_response_get_cmd_versions *rver;
 450	struct cros_ec_command *msg;
 451	int ret, mapped;
 452
 453	msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
 454		      GFP_KERNEL);
 455	if (!msg)
 456		return -ENOMEM;
 457
 458	msg->version = 0;
 459	msg->command = EC_CMD_GET_CMD_VERSIONS;
 460	msg->insize = sizeof(*rver);
 461	msg->outsize = sizeof(*pver);
 462
 463	pver = (struct ec_params_get_cmd_versions *)msg->data;
 464	pver->cmd = cmd;
 465
 466	ret = cros_ec_send_command(ec_dev, msg);
 467	if (ret < 0)
 468		goto exit;
 469
 470	mapped = cros_ec_map_error(msg->result);
 471	if (mapped) {
 472		ret = mapped;
 473		goto exit;
 474	}
 475
 476	if (ret == 0) {
 477		ret = -EPROTO;
 478		goto exit;
 479	}
 480
 481	rver = (struct ec_response_get_cmd_versions *)msg->data;
 482	*mask = rver->version_mask;
 483	ret = 0;
 484exit:
 485	kfree(msg);
 
 486	return ret;
 487}
 488
 489/**
 490 * cros_ec_query_all() -  Query the protocol version supported by the
 491 *         ChromeOS EC.
 492 * @ec_dev: Device to register.
 493 *
 494 * Return: 0 on success or negative error code.
 495 */
 496int cros_ec_query_all(struct cros_ec_device *ec_dev)
 497{
 498	struct device *dev = ec_dev->dev;
 499	u32 ver_mask;
 
 
 500	int ret;
 501
 
 
 
 
 
 502	/* First try sending with proto v3. */
 503	if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
 504		/* Check for PD. */
 505		cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 506	} else {
 507		/* Try querying with a v2 hello message. */
 508		ret = cros_ec_get_proto_info_legacy(ec_dev);
 509		if (ret) {
 
 
 
 
 
 
 
 
 
 
 
 
 510			/*
 511			 * It's possible for a test to occur too early when
 512			 * the EC isn't listening. If this happens, we'll
 513			 * test later when the first command is run.
 514			 */
 515			ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
 516			dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
 517			return ret;
 518		}
 519	}
 520
 521	devm_kfree(dev, ec_dev->din);
 522	devm_kfree(dev, ec_dev->dout);
 523
 524	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
 525	if (!ec_dev->din) {
 526		ret = -ENOMEM;
 527		goto exit;
 528	}
 529
 530	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
 531	if (!ec_dev->dout) {
 532		devm_kfree(dev, ec_dev->din);
 533		ret = -ENOMEM;
 534		goto exit;
 535	}
 536
 537	/* Probe if MKBP event is supported */
 538	ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_GET_NEXT_EVENT, &ver_mask);
 539	if (ret < 0 || ver_mask == 0) {
 
 
 540		ec_dev->mkbp_event_supported = 0;
 541	} else {
 542		ec_dev->mkbp_event_supported = fls(ver_mask);
 543
 544		dev_dbg(ec_dev->dev, "MKBP support version %u\n", ec_dev->mkbp_event_supported - 1);
 545	}
 546
 547	/* Probe if host sleep v1 is supported for S0ix failure detection. */
 548	ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_HOST_SLEEP_EVENT, &ver_mask);
 549	ec_dev->host_sleep_v1 = (ret == 0 && (ver_mask & EC_VER_MASK(1)));
 
 
 550
 551	/* Get host event wake mask. */
 552	ret = cros_ec_get_host_event_wake_mask(ec_dev, &ec_dev->host_event_wake_mask);
 553	if (ret < 0) {
 554		/*
 555		 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
 556		 * use a reasonable default. Note that we ignore various
 557		 * battery, AC status, and power-state events, because (a)
 558		 * those can be quite common (e.g., when sitting at full
 559		 * charge, on AC) and (b) these are not actionable wake events;
 560		 * if anything, we'd like to continue suspending (to save
 561		 * power), not wake up.
 562		 */
 563		ec_dev->host_event_wake_mask = U32_MAX &
 564			~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
 565			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
 566			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
 567			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
 568			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
 569			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
 570			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
 571		/*
 572		 * Old ECs may not support this command. Complain about all
 573		 * other errors.
 574		 */
 575		if (ret != -EOPNOTSUPP)
 576			dev_err(ec_dev->dev,
 577				"failed to retrieve wake mask: %d\n", ret);
 578	}
 579
 580	ret = 0;
 581
 582exit:
 
 583	return ret;
 584}
 585EXPORT_SYMBOL(cros_ec_query_all);
 586
 587/**
 588 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
 589 * @ec_dev: EC device.
 590 * @msg: Message to write.
 591 *
 592 * Call this to send a command to the ChromeOS EC. This should be used instead
 593 * of calling the EC's cmd_xfer() callback directly. This function does not
 594 * convert EC command execution error codes to Linux error codes. Most
 595 * in-kernel users will want to use cros_ec_cmd_xfer_status() instead since
 596 * that function implements the conversion.
 597 *
 598 * Return:
 599 * >0 - EC command was executed successfully. The return value is the number
 600 *      of bytes returned by the EC (excluding the header).
 601 * =0 - EC communication was successful. EC command execution results are
 602 *      reported in msg->result. The result will be EC_RES_SUCCESS if the
 603 *      command was executed successfully or report an EC command execution
 604 *      error.
 605 * <0 - EC communication error. Return value is the Linux error code.
 606 */
 607int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
 608{
 609	int ret;
 610
 611	mutex_lock(&ec_dev->lock);
 612	if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
 613		ret = cros_ec_query_all(ec_dev);
 614		if (ret) {
 615			dev_err(ec_dev->dev,
 616				"EC version unknown and query failed; aborting command\n");
 617			mutex_unlock(&ec_dev->lock);
 618			return ret;
 619		}
 620	}
 621
 622	if (msg->insize > ec_dev->max_response) {
 623		dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
 624		msg->insize = ec_dev->max_response;
 625	}
 626
 627	if (msg->command < EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX)) {
 628		if (msg->outsize > ec_dev->max_request) {
 629			dev_err(ec_dev->dev,
 630				"request of size %u is too big (max: %u)\n",
 631				msg->outsize,
 632				ec_dev->max_request);
 633			mutex_unlock(&ec_dev->lock);
 634			return -EMSGSIZE;
 635		}
 636	} else {
 637		if (msg->outsize > ec_dev->max_passthru) {
 638			dev_err(ec_dev->dev,
 639				"passthru rq of size %u is too big (max: %u)\n",
 640				msg->outsize,
 641				ec_dev->max_passthru);
 642			mutex_unlock(&ec_dev->lock);
 643			return -EMSGSIZE;
 644		}
 645	}
 646
 647	ret = cros_ec_send_command(ec_dev, msg);
 648	mutex_unlock(&ec_dev->lock);
 649
 650	return ret;
 651}
 652EXPORT_SYMBOL(cros_ec_cmd_xfer);
 653
 654/**
 655 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
 656 * @ec_dev: EC device.
 657 * @msg: Message to write.
 658 *
 659 * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
 660 * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
 661 * successfully and the EC replied with success status.
 662 *
 663 * Return:
 664 * >=0 - The number of bytes transferred.
 665 * <0 - Linux error code
 666 */
 667int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
 668			    struct cros_ec_command *msg)
 669{
 670	int ret, mapped;
 671
 672	ret = cros_ec_cmd_xfer(ec_dev, msg);
 673	if (ret < 0)
 674		return ret;
 675
 676	mapped = cros_ec_map_error(msg->result);
 677	if (mapped) {
 678		dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
 679			msg->result, mapped);
 680		ret = mapped;
 681	}
 682
 683	return ret;
 684}
 685EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
 686
 687static int get_next_event_xfer(struct cros_ec_device *ec_dev,
 688			       struct cros_ec_command *msg,
 689			       struct ec_response_get_next_event_v1 *event,
 690			       int version, uint32_t size)
 691{
 692	int ret;
 693
 694	msg->version = version;
 695	msg->command = EC_CMD_GET_NEXT_EVENT;
 696	msg->insize = size;
 697	msg->outsize = 0;
 698
 699	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
 700	if (ret > 0) {
 701		ec_dev->event_size = ret - 1;
 702		ec_dev->event_data = *event;
 703	}
 704
 705	return ret;
 706}
 707
 708static int get_next_event(struct cros_ec_device *ec_dev)
 709{
 710	struct {
 711		struct cros_ec_command msg;
 712		struct ec_response_get_next_event_v1 event;
 713	} __packed buf;
 714	struct cros_ec_command *msg = &buf.msg;
 715	struct ec_response_get_next_event_v1 *event = &buf.event;
 716	const int cmd_version = ec_dev->mkbp_event_supported - 1;
 717
 718	memset(msg, 0, sizeof(*msg));
 719	if (ec_dev->suspended) {
 720		dev_dbg(ec_dev->dev, "Device suspended.\n");
 721		return -EHOSTDOWN;
 722	}
 723
 724	if (cmd_version == 0)
 725		return get_next_event_xfer(ec_dev, msg, event, 0,
 
 
 
 
 
 
 
 
 
 726				  sizeof(struct ec_response_get_next_event));
 727
 728	return get_next_event_xfer(ec_dev, msg, event, cmd_version,
 729				sizeof(struct ec_response_get_next_event_v1));
 730}
 731
 732static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
 733{
 734	u8 buffer[sizeof(struct cros_ec_command) +
 735		  sizeof(ec_dev->event_data.data)];
 736	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
 737
 738	msg->version = 0;
 739	msg->command = EC_CMD_MKBP_STATE;
 740	msg->insize = sizeof(ec_dev->event_data.data);
 741	msg->outsize = 0;
 742
 743	ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
 744	ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
 745	memcpy(&ec_dev->event_data.data, msg->data,
 746	       sizeof(ec_dev->event_data.data));
 747
 748	return ec_dev->event_size;
 749}
 750
 751/**
 752 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
 753 * @ec_dev: Device to fetch event from.
 754 * @wake_event: Pointer to a bool set to true upon return if the event might be
 755 *              treated as a wake event. Ignored if null.
 756 * @has_more_events: Pointer to bool set to true if more than one event is
 757 *              pending.
 758 *              Some EC will set this flag to indicate cros_ec_get_next_event()
 759 *              can be called multiple times in a row.
 760 *              It is an optimization to prevent issuing a EC command for
 761 *              nothing or wait for another interrupt from the EC to process
 762 *              the next message.
 763 *              Ignored if null.
 764 *
 765 * Return: negative error code on errors; 0 for no data; or else number of
 766 * bytes received (i.e., an event was retrieved successfully). Event types are
 767 * written out to @ec_dev->event_data.event_type on success.
 768 */
 769int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
 770			   bool *wake_event,
 771			   bool *has_more_events)
 772{
 773	u8 event_type;
 774	u32 host_event;
 775	int ret;
 776	u32 ver_mask;
 777
 778	/*
 779	 * Default value for wake_event.
 780	 * Wake up on keyboard event, wake up for spurious interrupt or link
 781	 * error to the EC.
 782	 */
 783	if (wake_event)
 784		*wake_event = true;
 785
 786	/*
 787	 * Default value for has_more_events.
 788	 * EC will raise another interrupt if AP does not process all events
 789	 * anyway.
 790	 */
 791	if (has_more_events)
 792		*has_more_events = false;
 793
 794	if (!ec_dev->mkbp_event_supported)
 795		return get_keyboard_state_event(ec_dev);
 796
 797	ret = get_next_event(ec_dev);
 798	/*
 799	 * -ENOPROTOOPT is returned when EC returns EC_RES_INVALID_VERSION.
 800	 * This can occur when EC based device (e.g. Fingerprint MCU) jumps to
 801	 * the RO image which doesn't support newer version of the command. In
 802	 * this case we will attempt to update maximum supported version of the
 803	 * EC_CMD_GET_NEXT_EVENT.
 804	 */
 805	if (ret == -ENOPROTOOPT) {
 806		dev_dbg(ec_dev->dev,
 807			"GET_NEXT_EVENT returned invalid version error.\n");
 808		ret = cros_ec_get_host_command_version_mask(ec_dev,
 809							EC_CMD_GET_NEXT_EVENT,
 810							&ver_mask);
 811		if (ret < 0 || ver_mask == 0)
 812			/*
 813			 * Do not change the MKBP supported version if we can't
 814			 * obtain supported version correctly. Please note that
 815			 * calling EC_CMD_GET_NEXT_EVENT returned
 816			 * EC_RES_INVALID_VERSION which means that the command
 817			 * is present.
 818			 */
 819			return -ENOPROTOOPT;
 820
 821		ec_dev->mkbp_event_supported = fls(ver_mask);
 822		dev_dbg(ec_dev->dev, "MKBP support version changed to %u\n",
 823			ec_dev->mkbp_event_supported - 1);
 824
 825		/* Try to get next event with new MKBP support version set. */
 826		ret = get_next_event(ec_dev);
 827	}
 828
 
 829	if (ret <= 0)
 830		return ret;
 831
 832	if (has_more_events)
 833		*has_more_events = ec_dev->event_data.event_type &
 834			EC_MKBP_HAS_MORE_EVENTS;
 835	ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
 836
 837	if (wake_event) {
 838		event_type = ec_dev->event_data.event_type;
 839		host_event = cros_ec_get_host_event(ec_dev);
 840
 841		/*
 842		 * Sensor events need to be parsed by the sensor sub-device.
 843		 * Defer them, and don't report the wakeup here.
 844		 */
 845		if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
 846			*wake_event = false;
 847		} else if (host_event) {
 848			/* rtc_update_irq() already handles wakeup events. */
 849			if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
 850				*wake_event = false;
 851			/* Masked host-events should not count as wake events. */
 852			if (!(host_event & ec_dev->host_event_wake_mask))
 853				*wake_event = false;
 854		}
 855	}
 856
 857	return ret;
 858}
 859EXPORT_SYMBOL(cros_ec_get_next_event);
 860
 861/**
 862 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
 863 * @ec_dev: Device to fetch event from.
 864 *
 865 * When MKBP is supported, when the EC raises an interrupt, we collect the
 866 * events raised and call the functions in the ec notifier. This function
 867 * is a helper to know which events are raised.
 868 *
 869 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
 870 */
 871u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
 872{
 873	u32 host_event;
 874
 875	if (!ec_dev->mkbp_event_supported)
 876		return 0;
 877
 878	if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
 879		return 0;
 880
 881	if (ec_dev->event_size != sizeof(host_event)) {
 882		dev_warn(ec_dev->dev, "Invalid host event size\n");
 883		return 0;
 884	}
 885
 886	host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
 887
 888	return host_event;
 889}
 890EXPORT_SYMBOL(cros_ec_get_host_event);
 891
 892/**
 893 * cros_ec_check_features() - Test for the presence of EC features
 894 *
 895 * @ec: EC device, does not have to be connected directly to the AP,
 896 *      can be daisy chained through another device.
 897 * @feature: One of ec_feature_code bit.
 898 *
 899 * Call this function to test whether the ChromeOS EC supports a feature.
 900 *
 901 * Return: true if supported, false if not (or if an error was encountered).
 902 */
 903bool cros_ec_check_features(struct cros_ec_dev *ec, int feature)
 904{
 905	struct ec_response_get_features *features = &ec->features;
 906	int ret;
 907
 908	if (features->flags[0] == -1U && features->flags[1] == -1U) {
 909		/* features bitmap not read yet */
 910		ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_GET_FEATURES + ec->cmd_offset,
 911				  NULL, 0, features, sizeof(*features));
 912		if (ret < 0) {
 913			dev_warn(ec->dev, "cannot get EC features: %d\n", ret);
 914			memset(features, 0, sizeof(*features));
 915		}
 916
 917		dev_dbg(ec->dev, "EC features %08x %08x\n",
 918			features->flags[0], features->flags[1]);
 919	}
 920
 921	return !!(features->flags[feature / 32] & EC_FEATURE_MASK_0(feature));
 922}
 923EXPORT_SYMBOL_GPL(cros_ec_check_features);
 924
 925/**
 926 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
 927 *
 928 * @ec: EC device, does not have to be connected directly to the AP,
 929 *      can be daisy chained through another device.
 930 * Return: < 0 in case of error.
 931 */
 932int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
 933{
 934	/*
 935	 * Issue a command to get the number of sensor reported.
 936	 * If not supported, check for legacy mode.
 937	 */
 938	int ret, sensor_count;
 939	struct ec_params_motion_sense *params;
 940	struct ec_response_motion_sense *resp;
 941	struct cros_ec_command *msg;
 942	struct cros_ec_device *ec_dev = ec->ec_dev;
 943	u8 status;
 944
 945	msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
 946		      GFP_KERNEL);
 947	if (!msg)
 948		return -ENOMEM;
 949
 950	msg->version = 1;
 951	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 952	msg->outsize = sizeof(*params);
 953	msg->insize = sizeof(*resp);
 954
 955	params = (struct ec_params_motion_sense *)msg->data;
 956	params->cmd = MOTIONSENSE_CMD_DUMP;
 957
 958	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
 959	if (ret < 0) {
 960		sensor_count = ret;
 961	} else {
 962		resp = (struct ec_response_motion_sense *)msg->data;
 963		sensor_count = resp->dump.sensor_count;
 964	}
 965	kfree(msg);
 966
 967	/*
 968	 * Check legacy mode: Let's find out if sensors are accessible
 969	 * via LPC interface.
 970	 */
 971	if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
 972		ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
 973				1, &status);
 974		if (ret >= 0 &&
 975		    (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
 976			/*
 977			 * We have 2 sensors, one in the lid, one in the base.
 978			 */
 979			sensor_count = 2;
 980		} else {
 981			/*
 982			 * EC uses LPC interface and no sensors are presented.
 983			 */
 984			sensor_count = 0;
 985		}
 986	}
 987	return sensor_count;
 988}
 989EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
 990
 991/**
 992 * cros_ec_cmd - Send a command to the EC.
 993 *
 994 * @ec_dev: EC device
 995 * @version: EC command version
 996 * @command: EC command
 997 * @outdata: EC command output data
 998 * @outsize: Size of outdata
 999 * @indata: EC command input data
1000 * @insize: Size of indata
1001 *
1002 * Return: >= 0 on success, negative error number on failure.
1003 */
1004int cros_ec_cmd(struct cros_ec_device *ec_dev,
1005		unsigned int version,
1006		int command,
1007		const void *outdata,
1008		size_t outsize,
1009		void *indata,
1010		size_t insize)
1011{
1012	struct cros_ec_command *msg;
1013	int ret;
1014
1015	msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
1016	if (!msg)
1017		return -ENOMEM;
1018
1019	msg->version = version;
1020	msg->command = command;
1021	msg->outsize = outsize;
1022	msg->insize = insize;
1023
1024	if (outsize)
1025		memcpy(msg->data, outdata, outsize);
1026
1027	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
1028	if (ret < 0)
1029		goto error;
1030
1031	if (insize)
1032		memcpy(indata, msg->data, insize);
1033error:
1034	kfree(msg);
1035	return ret;
1036}
1037EXPORT_SYMBOL_GPL(cros_ec_cmd);
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2// ChromeOS EC communication protocol helper functions
  3//
  4// Copyright (C) 2015 Google, Inc
  5
  6#include <linux/delay.h>
  7#include <linux/device.h>
  8#include <linux/module.h>
  9#include <linux/platform_data/cros_ec_commands.h>
 10#include <linux/platform_data/cros_ec_proto.h>
 11#include <linux/slab.h>
 12#include <asm/unaligned.h>
 13
 14#include "cros_ec_trace.h"
 15
 16#define EC_COMMAND_RETRIES	50
 17
 18static int prepare_packet(struct cros_ec_device *ec_dev,
 19			  struct cros_ec_command *msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 20{
 21	struct ec_host_request *request;
 22	u8 *out;
 23	int i;
 24	u8 csum = 0;
 25
 26	BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
 27	BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
 28
 29	out = ec_dev->dout;
 30	request = (struct ec_host_request *)out;
 31	request->struct_version = EC_HOST_REQUEST_VERSION;
 32	request->checksum = 0;
 33	request->command = msg->command;
 34	request->command_version = msg->version;
 35	request->reserved = 0;
 36	request->data_len = msg->outsize;
 37
 38	for (i = 0; i < sizeof(*request); i++)
 39		csum += out[i];
 40
 41	/* Copy data and update checksum */
 42	memcpy(out + sizeof(*request), msg->data, msg->outsize);
 43	for (i = 0; i < msg->outsize; i++)
 44		csum += msg->data[i];
 45
 46	request->checksum = -csum;
 47
 48	return sizeof(*request) + msg->outsize;
 49}
 50
 51static int send_command(struct cros_ec_device *ec_dev,
 52			struct cros_ec_command *msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 53{
 54	int ret;
 55	int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
 56
 57	trace_cros_ec_cmd(msg);
 58
 59	if (ec_dev->proto_version > 2)
 60		xfer_fxn = ec_dev->pkt_xfer;
 61	else
 62		xfer_fxn = ec_dev->cmd_xfer;
 63
 64	if (!xfer_fxn) {
 65		/*
 66		 * This error can happen if a communication error happened and
 67		 * the EC is trying to use protocol v2, on an underlying
 68		 * communication mechanism that does not support v2.
 69		 */
 70		dev_err_once(ec_dev->dev,
 71			     "missing EC transfer API, cannot send command\n");
 72		return -EIO;
 73	}
 74
 
 75	ret = (*xfer_fxn)(ec_dev, msg);
 76	if (msg->result == EC_RES_IN_PROGRESS) {
 77		int i;
 78		struct cros_ec_command *status_msg;
 79		struct ec_response_get_comms_status *status;
 80
 81		status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
 82				     GFP_KERNEL);
 83		if (!status_msg)
 84			return -ENOMEM;
 85
 86		status_msg->version = 0;
 87		status_msg->command = EC_CMD_GET_COMMS_STATUS;
 88		status_msg->insize = sizeof(*status);
 89		status_msg->outsize = 0;
 90
 91		/*
 92		 * Query the EC's status until it's no longer busy or
 93		 * we encounter an error.
 94		 */
 95		for (i = 0; i < EC_COMMAND_RETRIES; i++) {
 96			usleep_range(10000, 11000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97
 98			ret = (*xfer_fxn)(ec_dev, status_msg);
 99			if (ret == -EAGAIN)
100				continue;
101			if (ret < 0)
102				break;
103
104			msg->result = status_msg->result;
105			if (status_msg->result != EC_RES_SUCCESS)
106				break;
107
108			status = (struct ec_response_get_comms_status *)
109				 status_msg->data;
110			if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
111				break;
112		}
113
114		kfree(status_msg);
 
115	}
116
 
 
 
 
 
 
 
 
 
 
 
 
 
117	return ret;
118}
119
 
 
 
 
 
 
 
 
 
 
120int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
121		       struct cros_ec_command *msg)
122{
123	u8 *out;
124	u8 csum;
125	int i;
126
127	if (ec_dev->proto_version > 2)
128		return prepare_packet(ec_dev, msg);
129
130	BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
131	out = ec_dev->dout;
132	out[0] = EC_CMD_VERSION0 + msg->version;
133	out[1] = msg->command;
134	out[2] = msg->outsize;
135	csum = out[0] + out[1] + out[2];
136	for (i = 0; i < msg->outsize; i++)
137		csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
138	out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
139
140	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
141}
142EXPORT_SYMBOL(cros_ec_prepare_tx);
143
 
 
 
 
 
 
 
 
 
 
 
 
 
144int cros_ec_check_result(struct cros_ec_device *ec_dev,
145			 struct cros_ec_command *msg)
146{
147	switch (msg->result) {
148	case EC_RES_SUCCESS:
149		return 0;
150	case EC_RES_IN_PROGRESS:
151		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
152			msg->command);
153		return -EAGAIN;
154	default:
155		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
156			msg->command, msg->result);
157		return 0;
158	}
159}
160EXPORT_SYMBOL(cros_ec_check_result);
161
162/*
163 * cros_ec_get_host_event_wake_mask
164 *
165 * Get the mask of host events that cause wake from suspend.
166 *
167 * @ec_dev: EC device to call
168 * @msg: message structure to use
169 * @mask: result when function returns >=0.
170 *
171 * LOCKING:
172 * the caller has ec_dev->lock mutex, or the caller knows there is
173 * no other command in progress.
174 */
175static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
176					    struct cros_ec_command *msg,
177					    uint32_t *mask)
178{
 
179	struct ec_response_host_event_mask *r;
180	int ret;
 
 
 
 
181
182	msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
183	msg->version = 0;
184	msg->outsize = 0;
185	msg->insize = sizeof(*r);
186
187	ret = send_command(ec_dev, msg);
188	if (ret > 0) {
189		r = (struct ec_response_host_event_mask *)msg->data;
190		*mask = r->mask;
 
 
 
 
 
 
 
 
 
191	}
192
 
 
 
 
 
193	return ret;
194}
195
196static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
197					    int devidx,
198					    struct cros_ec_command *msg)
199{
200	/*
201	 * Try using v3+ to query for supported protocols. If this
202	 * command fails, fall back to v2. Returns the highest protocol
203	 * supported by the EC.
204	 * Also sets the max request/response/passthru size.
205	 */
206	int ret;
207
208	if (!ec_dev->pkt_xfer)
209		return -EPROTONOSUPPORT;
 
210
211	memset(msg, 0, sizeof(*msg));
212	msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
213	msg->insize = sizeof(struct ec_response_get_protocol_info);
214
215	ret = send_command(ec_dev, msg);
 
 
 
 
 
 
 
 
 
216
217	if (ret < 0) {
218		dev_dbg(ec_dev->dev,
219			"failed to check for EC[%d] protocol version: %d\n",
220			devidx, ret);
221		return ret;
 
 
 
 
 
 
 
 
 
 
 
222	}
223
224	if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
225		return -ENODEV;
226	else if (msg->result != EC_RES_SUCCESS)
227		return msg->result;
228
229	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230}
231
232static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
233{
234	struct cros_ec_command *msg;
235	struct ec_params_hello *hello_params;
236	struct ec_response_hello *hello_response;
237	int ret;
238	int len = max(sizeof(*hello_params), sizeof(*hello_response));
 
239
240	msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
241	if (!msg)
242		return -ENOMEM;
243
244	msg->version = 0;
245	msg->command = EC_CMD_HELLO;
246	hello_params = (struct ec_params_hello *)msg->data;
247	msg->outsize = sizeof(*hello_params);
248	hello_response = (struct ec_response_hello *)msg->data;
249	msg->insize = sizeof(*hello_response);
250
251	hello_params->in_data = 0xa0b0c0d0;
 
252
253	ret = send_command(ec_dev, msg);
 
 
 
 
254
255	if (ret < 0) {
256		dev_dbg(ec_dev->dev,
257			"EC failed to respond to v2 hello: %d\n",
258			ret);
259		goto exit;
260	} else if (msg->result != EC_RES_SUCCESS) {
261		dev_err(ec_dev->dev,
262			"EC responded to v2 hello with error: %d\n",
263			msg->result);
264		ret = msg->result;
265		goto exit;
266	} else if (hello_response->out_data != 0xa1b2c3d4) {
 
 
 
267		dev_err(ec_dev->dev,
268			"EC responded to v2 hello with bad result: %u\n",
269			hello_response->out_data);
270		ret = -EBADMSG;
271		goto exit;
272	}
273
 
 
 
 
 
 
 
 
274	ret = 0;
275
276 exit:
277	kfree(msg);
278	return ret;
279}
280
281/*
282 * cros_ec_get_host_command_version_mask
283 *
284 * Get the version mask of a given command.
285 *
286 * @ec_dev: EC device to call
287 * @msg: message structure to use
288 * @cmd: command to get the version of.
289 * @mask: result when function returns 0.
290 *
291 * @return 0 on success, error code otherwise
292 *
293 * LOCKING:
294 * the caller has ec_dev->lock mutex or the caller knows there is
295 * no other command in progress.
296 */
297static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
298	u16 cmd, u32 *mask)
299{
300	struct ec_params_get_cmd_versions *pver;
301	struct ec_response_get_cmd_versions *rver;
302	struct cros_ec_command *msg;
303	int ret;
304
305	msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
306		      GFP_KERNEL);
307	if (!msg)
308		return -ENOMEM;
309
310	msg->version = 0;
311	msg->command = EC_CMD_GET_CMD_VERSIONS;
312	msg->insize = sizeof(*rver);
313	msg->outsize = sizeof(*pver);
314
315	pver = (struct ec_params_get_cmd_versions *)msg->data;
316	pver->cmd = cmd;
317
318	ret = send_command(ec_dev, msg);
319	if (ret > 0) {
320		rver = (struct ec_response_get_cmd_versions *)msg->data;
321		*mask = rver->version_mask;
 
 
 
 
 
 
 
 
 
322	}
323
 
 
 
 
324	kfree(msg);
325
326	return ret;
327}
328
 
 
 
 
 
 
 
329int cros_ec_query_all(struct cros_ec_device *ec_dev)
330{
331	struct device *dev = ec_dev->dev;
332	struct cros_ec_command *proto_msg;
333	struct ec_response_get_protocol_info *proto_info;
334	u32 ver_mask = 0;
335	int ret;
336
337	proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
338			    GFP_KERNEL);
339	if (!proto_msg)
340		return -ENOMEM;
341
342	/* First try sending with proto v3. */
343	ec_dev->proto_version = 3;
344	ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
345
346	if (ret == 0) {
347		proto_info = (struct ec_response_get_protocol_info *)
348			proto_msg->data;
349		ec_dev->max_request = proto_info->max_request_packet_size -
350			sizeof(struct ec_host_request);
351		ec_dev->max_response = proto_info->max_response_packet_size -
352			sizeof(struct ec_host_response);
353		ec_dev->proto_version =
354			min(EC_HOST_REQUEST_VERSION,
355					fls(proto_info->protocol_versions) - 1);
356		dev_dbg(ec_dev->dev,
357			"using proto v%u\n",
358			ec_dev->proto_version);
359
360		ec_dev->din_size = ec_dev->max_response +
361			sizeof(struct ec_host_response) +
362			EC_MAX_RESPONSE_OVERHEAD;
363		ec_dev->dout_size = ec_dev->max_request +
364			sizeof(struct ec_host_request) +
365			EC_MAX_REQUEST_OVERHEAD;
366
367		/*
368		 * Check for PD
369		 */
370		ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
371
372		if (ret) {
373			dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
374			ec_dev->max_passthru = 0;
375		} else {
376			dev_dbg(ec_dev->dev, "found PD chip\n");
377			ec_dev->max_passthru =
378				proto_info->max_request_packet_size -
379				sizeof(struct ec_host_request);
380		}
381	} else {
382		/* Try querying with a v2 hello message. */
383		ec_dev->proto_version = 2;
384		ret = cros_ec_host_command_proto_query_v2(ec_dev);
385
386		if (ret == 0) {
387			/* V2 hello succeeded. */
388			dev_dbg(ec_dev->dev, "falling back to proto v2\n");
389
390			ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
391			ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
392			ec_dev->max_passthru = 0;
393			ec_dev->pkt_xfer = NULL;
394			ec_dev->din_size = EC_PROTO2_MSG_BYTES;
395			ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
396		} else {
397			/*
398			 * It's possible for a test to occur too early when
399			 * the EC isn't listening. If this happens, we'll
400			 * test later when the first command is run.
401			 */
402			ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
403			dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
404			goto exit;
405		}
406	}
407
408	devm_kfree(dev, ec_dev->din);
409	devm_kfree(dev, ec_dev->dout);
410
411	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
412	if (!ec_dev->din) {
413		ret = -ENOMEM;
414		goto exit;
415	}
416
417	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
418	if (!ec_dev->dout) {
419		devm_kfree(dev, ec_dev->din);
420		ret = -ENOMEM;
421		goto exit;
422	}
423
424	/* Probe if MKBP event is supported */
425	ret = cros_ec_get_host_command_version_mask(ec_dev,
426						    EC_CMD_GET_NEXT_EVENT,
427						    &ver_mask);
428	if (ret < 0 || ver_mask == 0)
429		ec_dev->mkbp_event_supported = 0;
430	else
431		ec_dev->mkbp_event_supported = 1;
 
 
 
432
433	/* Probe if host sleep v1 is supported for S0ix failure detection. */
434	ret = cros_ec_get_host_command_version_mask(ec_dev,
435						    EC_CMD_HOST_SLEEP_EVENT,
436						    &ver_mask);
437	ec_dev->host_sleep_v1 = (ret >= 0 && (ver_mask & EC_VER_MASK(1)));
438
439	/*
440	 * Get host event wake mask, assume all events are wake events
441	 * if unavailable.
442	 */
443	ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
444					       &ec_dev->host_event_wake_mask);
445	if (ret < 0)
446		ec_dev->host_event_wake_mask = U32_MAX;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447
448	ret = 0;
449
450exit:
451	kfree(proto_msg);
452	return ret;
453}
454EXPORT_SYMBOL(cros_ec_query_all);
455
456int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
457		     struct cros_ec_command *msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458{
459	int ret;
460
461	mutex_lock(&ec_dev->lock);
462	if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
463		ret = cros_ec_query_all(ec_dev);
464		if (ret) {
465			dev_err(ec_dev->dev,
466				"EC version unknown and query failed; aborting command\n");
467			mutex_unlock(&ec_dev->lock);
468			return ret;
469		}
470	}
471
472	if (msg->insize > ec_dev->max_response) {
473		dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
474		msg->insize = ec_dev->max_response;
475	}
476
477	if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
478		if (msg->outsize > ec_dev->max_request) {
479			dev_err(ec_dev->dev,
480				"request of size %u is too big (max: %u)\n",
481				msg->outsize,
482				ec_dev->max_request);
483			mutex_unlock(&ec_dev->lock);
484			return -EMSGSIZE;
485		}
486	} else {
487		if (msg->outsize > ec_dev->max_passthru) {
488			dev_err(ec_dev->dev,
489				"passthru rq of size %u is too big (max: %u)\n",
490				msg->outsize,
491				ec_dev->max_passthru);
492			mutex_unlock(&ec_dev->lock);
493			return -EMSGSIZE;
494		}
495	}
496	ret = send_command(ec_dev, msg);
 
497	mutex_unlock(&ec_dev->lock);
498
499	return ret;
500}
501EXPORT_SYMBOL(cros_ec_cmd_xfer);
502
 
 
 
 
 
 
 
 
 
 
 
 
 
503int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
504			    struct cros_ec_command *msg)
505{
506	int ret;
507
508	ret = cros_ec_cmd_xfer(ec_dev, msg);
509	if (ret < 0) {
510		dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
511	} else if (msg->result != EC_RES_SUCCESS) {
512		dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
513		return -EPROTO;
 
 
 
514	}
515
516	return ret;
517}
518EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
519
520static int get_next_event_xfer(struct cros_ec_device *ec_dev,
521			       struct cros_ec_command *msg,
 
522			       int version, uint32_t size)
523{
524	int ret;
525
526	msg->version = version;
527	msg->command = EC_CMD_GET_NEXT_EVENT;
528	msg->insize = size;
529	msg->outsize = 0;
530
531	ret = cros_ec_cmd_xfer(ec_dev, msg);
532	if (ret > 0) {
533		ec_dev->event_size = ret - 1;
534		memcpy(&ec_dev->event_data, msg->data, ret);
535	}
536
537	return ret;
538}
539
540static int get_next_event(struct cros_ec_device *ec_dev)
541{
542	u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
543	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
544	static int cmd_version = 1;
545	int ret;
 
 
 
546
 
547	if (ec_dev->suspended) {
548		dev_dbg(ec_dev->dev, "Device suspended.\n");
549		return -EHOSTDOWN;
550	}
551
552	if (cmd_version == 1) {
553		ret = get_next_event_xfer(ec_dev, msg, cmd_version,
554				sizeof(struct ec_response_get_next_event_v1));
555		if (ret < 0 || msg->result != EC_RES_INVALID_VERSION)
556			return ret;
557
558		/* Fallback to version 0 for future send attempts */
559		cmd_version = 0;
560	}
561
562	ret = get_next_event_xfer(ec_dev, msg, cmd_version,
563				  sizeof(struct ec_response_get_next_event));
564
565	return ret;
 
566}
567
568static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
569{
570	u8 buffer[sizeof(struct cros_ec_command) +
571		  sizeof(ec_dev->event_data.data)];
572	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
573
574	msg->version = 0;
575	msg->command = EC_CMD_MKBP_STATE;
576	msg->insize = sizeof(ec_dev->event_data.data);
577	msg->outsize = 0;
578
579	ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
580	ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
581	memcpy(&ec_dev->event_data.data, msg->data,
582	       sizeof(ec_dev->event_data.data));
583
584	return ec_dev->event_size;
585}
586
587int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
588{
589	u8 event_type;
590	u32 host_event;
591	int ret;
 
 
 
 
 
 
 
 
 
592
593	if (!ec_dev->mkbp_event_supported) {
594		ret = get_keyboard_state_event(ec_dev);
595		if (ret <= 0)
596			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
598		if (wake_event)
599			*wake_event = true;
 
600
601		return ret;
 
602	}
603
604	ret = get_next_event(ec_dev);
605	if (ret <= 0)
606		return ret;
607
 
 
 
 
 
608	if (wake_event) {
609		event_type = ec_dev->event_data.event_type;
610		host_event = cros_ec_get_host_event(ec_dev);
611
612		/*
613		 * Sensor events need to be parsed by the sensor sub-device.
614		 * Defer them, and don't report the wakeup here.
615		 */
616		if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
617			*wake_event = false;
618		/* Masked host-events should not count as wake events. */
619		else if (host_event &&
620			 !(host_event & ec_dev->host_event_wake_mask))
621			*wake_event = false;
622		/* Consider all other events as wake events. */
623		else
624			*wake_event = true;
 
625	}
626
627	return ret;
628}
629EXPORT_SYMBOL(cros_ec_get_next_event);
630
 
 
 
 
 
 
 
 
 
 
631u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
632{
633	u32 host_event;
634
635	BUG_ON(!ec_dev->mkbp_event_supported);
 
636
637	if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
638		return 0;
639
640	if (ec_dev->event_size != sizeof(host_event)) {
641		dev_warn(ec_dev->dev, "Invalid host event size\n");
642		return 0;
643	}
644
645	host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
646
647	return host_event;
648}
649EXPORT_SYMBOL(cros_ec_get_host_event);