Linux Audio

Check our new training course

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