1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC communication protocol helper functions
4 // Copyright (C) 2015 Google, Inc
6 #include <linux/mfd/cros_ec.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <asm/unaligned.h>
13 #define EC_COMMAND_RETRIES 50
15 static int prepare_packet(struct cros_ec_device *ec_dev,
16 struct cros_ec_command *msg)
18 struct ec_host_request *request;
23 BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
24 BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
27 request = (struct ec_host_request *)out;
28 request->struct_version = EC_HOST_REQUEST_VERSION;
29 request->checksum = 0;
30 request->command = msg->command;
31 request->command_version = msg->version;
32 request->reserved = 0;
33 request->data_len = msg->outsize;
35 for (i = 0; i < sizeof(*request); i++)
38 /* Copy data and update checksum */
39 memcpy(out + sizeof(*request), msg->data, msg->outsize);
40 for (i = 0; i < msg->outsize; i++)
43 request->checksum = -csum;
45 return sizeof(*request) + msg->outsize;
48 static int send_command(struct cros_ec_device *ec_dev,
49 struct cros_ec_command *msg)
52 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
54 if (ec_dev->proto_version > 2)
55 xfer_fxn = ec_dev->pkt_xfer;
57 xfer_fxn = ec_dev->cmd_xfer;
59 ret = (*xfer_fxn)(ec_dev, msg);
60 if (msg->result == EC_RES_IN_PROGRESS) {
62 struct cros_ec_command *status_msg;
63 struct ec_response_get_comms_status *status;
65 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
70 status_msg->version = 0;
71 status_msg->command = EC_CMD_GET_COMMS_STATUS;
72 status_msg->insize = sizeof(*status);
73 status_msg->outsize = 0;
76 * Query the EC's status until it's no longer busy or
77 * we encounter an error.
79 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
80 usleep_range(10000, 11000);
82 ret = (*xfer_fxn)(ec_dev, status_msg);
88 msg->result = status_msg->result;
89 if (status_msg->result != EC_RES_SUCCESS)
92 status = (struct ec_response_get_comms_status *)
94 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
104 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
105 struct cros_ec_command *msg)
111 if (ec_dev->proto_version > 2)
112 return prepare_packet(ec_dev, msg);
114 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
116 out[0] = EC_CMD_VERSION0 + msg->version;
117 out[1] = msg->command;
118 out[2] = msg->outsize;
119 csum = out[0] + out[1] + out[2];
120 for (i = 0; i < msg->outsize; i++)
121 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
122 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
124 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
126 EXPORT_SYMBOL(cros_ec_prepare_tx);
128 int cros_ec_check_result(struct cros_ec_device *ec_dev,
129 struct cros_ec_command *msg)
131 switch (msg->result) {
134 case EC_RES_IN_PROGRESS:
135 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
139 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
140 msg->command, msg->result);
144 EXPORT_SYMBOL(cros_ec_check_result);
147 * cros_ec_get_host_event_wake_mask
149 * Get the mask of host events that cause wake from suspend.
151 * @ec_dev: EC device to call
152 * @msg: message structure to use
153 * @mask: result when function returns >=0.
156 * the caller has ec_dev->lock mutex, or the caller knows there is
157 * no other command in progress.
159 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
160 struct cros_ec_command *msg,
163 struct ec_response_host_event_mask *r;
166 msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
169 msg->insize = sizeof(*r);
171 ret = send_command(ec_dev, msg);
173 r = (struct ec_response_host_event_mask *)msg->data;
180 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
182 struct cros_ec_command *msg)
185 * Try using v3+ to query for supported protocols. If this
186 * command fails, fall back to v2. Returns the highest protocol
187 * supported by the EC.
188 * Also sets the max request/response/passthru size.
192 if (!ec_dev->pkt_xfer)
193 return -EPROTONOSUPPORT;
195 memset(msg, 0, sizeof(*msg));
196 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
197 msg->insize = sizeof(struct ec_response_get_protocol_info);
199 ret = send_command(ec_dev, msg);
203 "failed to check for EC[%d] protocol version: %d\n",
208 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
210 else if (msg->result != EC_RES_SUCCESS)
216 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
218 struct cros_ec_command *msg;
219 struct ec_params_hello *hello_params;
220 struct ec_response_hello *hello_response;
222 int len = max(sizeof(*hello_params), sizeof(*hello_response));
224 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
229 msg->command = EC_CMD_HELLO;
230 hello_params = (struct ec_params_hello *)msg->data;
231 msg->outsize = sizeof(*hello_params);
232 hello_response = (struct ec_response_hello *)msg->data;
233 msg->insize = sizeof(*hello_response);
235 hello_params->in_data = 0xa0b0c0d0;
237 ret = send_command(ec_dev, msg);
241 "EC failed to respond to v2 hello: %d\n",
244 } else if (msg->result != EC_RES_SUCCESS) {
246 "EC responded to v2 hello with error: %d\n",
250 } else if (hello_response->out_data != 0xa1b2c3d4) {
252 "EC responded to v2 hello with bad result: %u\n",
253 hello_response->out_data);
266 * cros_ec_get_host_command_version_mask
268 * Get the version mask of a given command.
270 * @ec_dev: EC device to call
271 * @msg: message structure to use
272 * @cmd: command to get the version of.
273 * @mask: result when function returns 0.
275 * @return 0 on success, error code otherwise
278 * the caller has ec_dev->lock mutex or the caller knows there is
279 * no other command in progress.
281 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
284 struct ec_params_get_cmd_versions *pver;
285 struct ec_response_get_cmd_versions *rver;
286 struct cros_ec_command *msg;
289 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
295 msg->command = EC_CMD_GET_CMD_VERSIONS;
296 msg->insize = sizeof(*rver);
297 msg->outsize = sizeof(*pver);
299 pver = (struct ec_params_get_cmd_versions *)msg->data;
302 ret = send_command(ec_dev, msg);
304 rver = (struct ec_response_get_cmd_versions *)msg->data;
305 *mask = rver->version_mask;
313 int cros_ec_query_all(struct cros_ec_device *ec_dev)
315 struct device *dev = ec_dev->dev;
316 struct cros_ec_command *proto_msg;
317 struct ec_response_get_protocol_info *proto_info;
321 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
326 /* First try sending with proto v3. */
327 ec_dev->proto_version = 3;
328 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
331 proto_info = (struct ec_response_get_protocol_info *)
333 ec_dev->max_request = proto_info->max_request_packet_size -
334 sizeof(struct ec_host_request);
335 ec_dev->max_response = proto_info->max_response_packet_size -
336 sizeof(struct ec_host_response);
337 ec_dev->proto_version =
338 min(EC_HOST_REQUEST_VERSION,
339 fls(proto_info->protocol_versions) - 1);
342 ec_dev->proto_version);
344 ec_dev->din_size = ec_dev->max_response +
345 sizeof(struct ec_host_response) +
346 EC_MAX_RESPONSE_OVERHEAD;
347 ec_dev->dout_size = ec_dev->max_request +
348 sizeof(struct ec_host_request) +
349 EC_MAX_REQUEST_OVERHEAD;
354 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
357 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
358 ec_dev->max_passthru = 0;
360 dev_dbg(ec_dev->dev, "found PD chip\n");
361 ec_dev->max_passthru =
362 proto_info->max_request_packet_size -
363 sizeof(struct ec_host_request);
366 /* Try querying with a v2 hello message. */
367 ec_dev->proto_version = 2;
368 ret = cros_ec_host_command_proto_query_v2(ec_dev);
371 /* V2 hello succeeded. */
372 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
374 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
375 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
376 ec_dev->max_passthru = 0;
377 ec_dev->pkt_xfer = NULL;
378 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
379 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
382 * It's possible for a test to occur too early when
383 * the EC isn't listening. If this happens, we'll
384 * test later when the first command is run.
386 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
387 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
392 devm_kfree(dev, ec_dev->din);
393 devm_kfree(dev, ec_dev->dout);
395 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
401 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
403 devm_kfree(dev, ec_dev->din);
408 /* Probe if MKBP event is supported */
409 ret = cros_ec_get_host_command_version_mask(ec_dev,
410 EC_CMD_GET_NEXT_EVENT,
412 if (ret < 0 || ver_mask == 0)
413 ec_dev->mkbp_event_supported = 0;
415 ec_dev->mkbp_event_supported = 1;
418 * Get host event wake mask, assume all events are wake events
421 ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
422 &ec_dev->host_event_wake_mask);
424 ec_dev->host_event_wake_mask = U32_MAX;
432 EXPORT_SYMBOL(cros_ec_query_all);
434 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
435 struct cros_ec_command *msg)
439 mutex_lock(&ec_dev->lock);
440 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
441 ret = cros_ec_query_all(ec_dev);
444 "EC version unknown and query failed; aborting command\n");
445 mutex_unlock(&ec_dev->lock);
450 if (msg->insize > ec_dev->max_response) {
451 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
452 msg->insize = ec_dev->max_response;
455 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
456 if (msg->outsize > ec_dev->max_request) {
458 "request of size %u is too big (max: %u)\n",
460 ec_dev->max_request);
461 mutex_unlock(&ec_dev->lock);
465 if (msg->outsize > ec_dev->max_passthru) {
467 "passthru rq of size %u is too big (max: %u)\n",
469 ec_dev->max_passthru);
470 mutex_unlock(&ec_dev->lock);
474 ret = send_command(ec_dev, msg);
475 mutex_unlock(&ec_dev->lock);
479 EXPORT_SYMBOL(cros_ec_cmd_xfer);
481 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
482 struct cros_ec_command *msg)
486 ret = cros_ec_cmd_xfer(ec_dev, msg);
488 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
489 } else if (msg->result != EC_RES_SUCCESS) {
490 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
496 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
498 static int get_next_event_xfer(struct cros_ec_device *ec_dev,
499 struct cros_ec_command *msg,
500 int version, uint32_t size)
504 msg->version = version;
505 msg->command = EC_CMD_GET_NEXT_EVENT;
509 ret = cros_ec_cmd_xfer(ec_dev, msg);
511 ec_dev->event_size = ret - 1;
512 memcpy(&ec_dev->event_data, msg->data, ret);
518 static int get_next_event(struct cros_ec_device *ec_dev)
520 u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
521 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
522 static int cmd_version = 1;
525 if (ec_dev->suspended) {
526 dev_dbg(ec_dev->dev, "Device suspended.\n");
530 if (cmd_version == 1) {
531 ret = get_next_event_xfer(ec_dev, msg, cmd_version,
532 sizeof(struct ec_response_get_next_event_v1));
533 if (ret < 0 || msg->result != EC_RES_INVALID_VERSION)
536 /* Fallback to version 0 for future send attempts */
540 ret = get_next_event_xfer(ec_dev, msg, cmd_version,
541 sizeof(struct ec_response_get_next_event));
546 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
548 u8 buffer[sizeof(struct cros_ec_command) +
549 sizeof(ec_dev->event_data.data)];
550 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
553 msg->command = EC_CMD_MKBP_STATE;
554 msg->insize = sizeof(ec_dev->event_data.data);
557 ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
558 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
559 memcpy(&ec_dev->event_data.data, msg->data,
560 sizeof(ec_dev->event_data.data));
562 return ec_dev->event_size;
565 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event)
571 if (!ec_dev->mkbp_event_supported) {
572 ret = get_keyboard_state_event(ec_dev);
582 ret = get_next_event(ec_dev);
587 event_type = ec_dev->event_data.event_type;
588 host_event = cros_ec_get_host_event(ec_dev);
591 * Sensor events need to be parsed by the sensor sub-device.
592 * Defer them, and don't report the wakeup here.
594 if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
596 /* Masked host-events should not count as wake events. */
597 else if (host_event &&
598 !(host_event & ec_dev->host_event_wake_mask))
600 /* Consider all other events as wake events. */
607 EXPORT_SYMBOL(cros_ec_get_next_event);
609 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
613 BUG_ON(!ec_dev->mkbp_event_supported);
615 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
618 if (ec_dev->event_size != sizeof(host_event)) {
619 dev_warn(ec_dev->dev, "Invalid host event size\n");
623 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
627 EXPORT_SYMBOL(cros_ec_get_host_event);