2 * ChromeOS EC communication protocol helper functions
4 * Copyright (C) 2015 Google, Inc
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.
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.
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>
24 #define EC_COMMAND_RETRIES 50
26 static int prepare_packet(struct cros_ec_device *ec_dev,
27 struct cros_ec_command *msg)
29 struct ec_host_request *request;
34 BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
35 BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
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;
46 for (i = 0; i < sizeof(*request); i++)
49 /* Copy data and update checksum */
50 memcpy(out + sizeof(*request), msg->data, msg->outsize);
51 for (i = 0; i < msg->outsize; i++)
54 request->checksum = -csum;
56 return sizeof(*request) + msg->outsize;
59 static int send_command(struct cros_ec_device *ec_dev,
60 struct cros_ec_command *msg)
63 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
65 if (ec_dev->proto_version > 2)
66 xfer_fxn = ec_dev->pkt_xfer;
68 xfer_fxn = ec_dev->cmd_xfer;
72 * This error can happen if a communication error happened and
73 * the EC is trying to use protocol v2, on an underlying
74 * communication mechanism that does not support v2.
76 dev_err_once(ec_dev->dev,
77 "missing EC transfer API, cannot send command\n");
81 ret = (*xfer_fxn)(ec_dev, msg);
82 if (msg->result == EC_RES_IN_PROGRESS) {
84 struct cros_ec_command *status_msg;
85 struct ec_response_get_comms_status *status;
87 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
92 status_msg->version = 0;
93 status_msg->command = EC_CMD_GET_COMMS_STATUS;
94 status_msg->insize = sizeof(*status);
95 status_msg->outsize = 0;
98 * Query the EC's status until it's no longer busy or
99 * we encounter an error.
101 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
102 usleep_range(10000, 11000);
104 ret = (*xfer_fxn)(ec_dev, status_msg);
108 msg->result = status_msg->result;
109 if (status_msg->result != EC_RES_SUCCESS)
112 status = (struct ec_response_get_comms_status *)
114 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
124 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
125 struct cros_ec_command *msg)
131 if (ec_dev->proto_version > 2)
132 return prepare_packet(ec_dev, msg);
134 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
136 out[0] = EC_CMD_VERSION0 + msg->version;
137 out[1] = msg->command;
138 out[2] = msg->outsize;
139 csum = out[0] + out[1] + out[2];
140 for (i = 0; i < msg->outsize; i++)
141 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
142 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
144 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
146 EXPORT_SYMBOL(cros_ec_prepare_tx);
148 int cros_ec_check_result(struct cros_ec_device *ec_dev,
149 struct cros_ec_command *msg)
151 switch (msg->result) {
154 case EC_RES_IN_PROGRESS:
155 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
159 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
160 msg->command, msg->result);
164 EXPORT_SYMBOL(cros_ec_check_result);
166 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
168 struct cros_ec_command *msg)
171 * Try using v3+ to query for supported protocols. If this
172 * command fails, fall back to v2. Returns the highest protocol
173 * supported by the EC.
174 * Also sets the max request/response/passthru size.
178 if (!ec_dev->pkt_xfer)
179 return -EPROTONOSUPPORT;
181 memset(msg, 0, sizeof(*msg));
182 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
183 msg->insize = sizeof(struct ec_response_get_protocol_info);
185 ret = send_command(ec_dev, msg);
189 "failed to check for EC[%d] protocol version: %d\n",
194 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
196 else if (msg->result != EC_RES_SUCCESS)
202 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
204 struct cros_ec_command *msg;
205 struct ec_params_hello *hello_params;
206 struct ec_response_hello *hello_response;
208 int len = max(sizeof(*hello_params), sizeof(*hello_response));
210 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
215 msg->command = EC_CMD_HELLO;
216 hello_params = (struct ec_params_hello *)msg->data;
217 msg->outsize = sizeof(*hello_params);
218 hello_response = (struct ec_response_hello *)msg->data;
219 msg->insize = sizeof(*hello_response);
221 hello_params->in_data = 0xa0b0c0d0;
223 ret = send_command(ec_dev, msg);
227 "EC failed to respond to v2 hello: %d\n",
230 } else if (msg->result != EC_RES_SUCCESS) {
232 "EC responded to v2 hello with error: %d\n",
236 } else if (hello_response->out_data != 0xa1b2c3d4) {
238 "EC responded to v2 hello with bad result: %u\n",
239 hello_response->out_data);
251 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
254 struct ec_params_get_cmd_versions *pver;
255 struct ec_response_get_cmd_versions *rver;
256 struct cros_ec_command *msg;
259 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
265 msg->command = EC_CMD_GET_CMD_VERSIONS;
266 msg->insize = sizeof(*rver);
267 msg->outsize = sizeof(*pver);
269 pver = (struct ec_params_get_cmd_versions *)msg->data;
272 ret = cros_ec_cmd_xfer(ec_dev, msg);
274 rver = (struct ec_response_get_cmd_versions *)msg->data;
275 *mask = rver->version_mask;
283 int cros_ec_query_all(struct cros_ec_device *ec_dev)
285 struct device *dev = ec_dev->dev;
286 struct cros_ec_command *proto_msg;
287 struct ec_response_get_protocol_info *proto_info;
291 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
296 /* First try sending with proto v3. */
297 ec_dev->proto_version = 3;
298 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
301 proto_info = (struct ec_response_get_protocol_info *)
303 ec_dev->max_request = proto_info->max_request_packet_size -
304 sizeof(struct ec_host_request);
305 ec_dev->max_response = proto_info->max_response_packet_size -
306 sizeof(struct ec_host_response);
307 ec_dev->proto_version =
308 min(EC_HOST_REQUEST_VERSION,
309 fls(proto_info->protocol_versions) - 1);
312 ec_dev->proto_version);
314 ec_dev->din_size = ec_dev->max_response +
315 sizeof(struct ec_host_response) +
316 EC_MAX_RESPONSE_OVERHEAD;
317 ec_dev->dout_size = ec_dev->max_request +
318 sizeof(struct ec_host_request) +
319 EC_MAX_REQUEST_OVERHEAD;
324 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
327 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
328 ec_dev->max_passthru = 0;
330 dev_dbg(ec_dev->dev, "found PD chip\n");
331 ec_dev->max_passthru =
332 proto_info->max_request_packet_size -
333 sizeof(struct ec_host_request);
336 /* Try querying with a v2 hello message. */
337 ec_dev->proto_version = 2;
338 ret = cros_ec_host_command_proto_query_v2(ec_dev);
341 /* V2 hello succeeded. */
342 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
344 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
345 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
346 ec_dev->max_passthru = 0;
347 ec_dev->pkt_xfer = NULL;
348 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
349 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
352 * It's possible for a test to occur too early when
353 * the EC isn't listening. If this happens, we'll
354 * test later when the first command is run.
356 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
357 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
362 devm_kfree(dev, ec_dev->din);
363 devm_kfree(dev, ec_dev->dout);
365 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
371 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
373 devm_kfree(dev, ec_dev->din);
378 /* Probe if MKBP event is supported */
379 ret = cros_ec_get_host_command_version_mask(ec_dev,
380 EC_CMD_GET_NEXT_EVENT,
382 if (ret < 0 || ver_mask == 0)
383 ec_dev->mkbp_event_supported = 0;
385 ec_dev->mkbp_event_supported = 1;
391 EXPORT_SYMBOL(cros_ec_query_all);
393 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
394 struct cros_ec_command *msg)
398 mutex_lock(&ec_dev->lock);
399 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
400 ret = cros_ec_query_all(ec_dev);
403 "EC version unknown and query failed; aborting command\n");
404 mutex_unlock(&ec_dev->lock);
409 if (msg->insize > ec_dev->max_response) {
410 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
411 msg->insize = ec_dev->max_response;
414 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
415 if (msg->outsize > ec_dev->max_request) {
417 "request of size %u is too big (max: %u)\n",
419 ec_dev->max_request);
420 mutex_unlock(&ec_dev->lock);
424 if (msg->outsize > ec_dev->max_passthru) {
426 "passthru rq of size %u is too big (max: %u)\n",
428 ec_dev->max_passthru);
429 mutex_unlock(&ec_dev->lock);
433 ret = send_command(ec_dev, msg);
434 mutex_unlock(&ec_dev->lock);
438 EXPORT_SYMBOL(cros_ec_cmd_xfer);
440 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
441 struct cros_ec_command *msg)
445 ret = cros_ec_cmd_xfer(ec_dev, msg);
447 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
448 } else if (msg->result != EC_RES_SUCCESS) {
449 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
455 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
457 static int get_next_event(struct cros_ec_device *ec_dev)
459 u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
460 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
464 msg->command = EC_CMD_GET_NEXT_EVENT;
465 msg->insize = sizeof(ec_dev->event_data);
468 ret = cros_ec_cmd_xfer(ec_dev, msg);
470 ec_dev->event_size = ret - 1;
471 memcpy(&ec_dev->event_data, msg->data,
472 sizeof(ec_dev->event_data));
478 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
480 u8 buffer[sizeof(struct cros_ec_command) +
481 sizeof(ec_dev->event_data.data)];
482 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
485 msg->command = EC_CMD_MKBP_STATE;
486 msg->insize = sizeof(ec_dev->event_data.data);
489 ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
490 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
491 memcpy(&ec_dev->event_data.data, msg->data,
492 sizeof(ec_dev->event_data.data));
494 return ec_dev->event_size;
497 int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
499 if (ec_dev->mkbp_event_supported)
500 return get_next_event(ec_dev);
502 return get_keyboard_state_event(ec_dev);
504 EXPORT_SYMBOL(cros_ec_get_next_event);