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>
23 #define EC_COMMAND_RETRIES 50
25 static int prepare_packet(struct cros_ec_device *ec_dev,
26 struct cros_ec_command *msg)
28 struct ec_host_request *request;
33 BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
34 BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
37 request = (struct ec_host_request *)out;
38 request->struct_version = EC_HOST_REQUEST_VERSION;
39 request->checksum = 0;
40 request->command = msg->command;
41 request->command_version = msg->version;
42 request->reserved = 0;
43 request->data_len = msg->outsize;
45 for (i = 0; i < sizeof(*request); i++)
48 /* Copy data and update checksum */
49 memcpy(out + sizeof(*request), msg->data, msg->outsize);
50 for (i = 0; i < msg->outsize; i++)
53 request->checksum = -csum;
55 return sizeof(*request) + msg->outsize;
58 static int send_command(struct cros_ec_device *ec_dev,
59 struct cros_ec_command *msg)
62 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
64 if (ec_dev->proto_version > 2)
65 xfer_fxn = ec_dev->pkt_xfer;
67 xfer_fxn = ec_dev->cmd_xfer;
71 * This error can happen if a communication error happened and
72 * the EC is trying to use protocol v2, on an underlying
73 * communication mechanism that does not support v2.
75 dev_err_once(ec_dev->dev,
76 "missing EC transfer API, cannot send command\n");
80 ret = (*xfer_fxn)(ec_dev, msg);
81 if (msg->result == EC_RES_IN_PROGRESS) {
83 struct cros_ec_command *status_msg;
84 struct ec_response_get_comms_status *status;
86 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
91 status_msg->version = 0;
92 status_msg->command = EC_CMD_GET_COMMS_STATUS;
93 status_msg->insize = sizeof(*status);
94 status_msg->outsize = 0;
97 * Query the EC's status until it's no longer busy or
98 * we encounter an error.
100 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
101 usleep_range(10000, 11000);
103 ret = (*xfer_fxn)(ec_dev, status_msg);
107 msg->result = status_msg->result;
108 if (status_msg->result != EC_RES_SUCCESS)
111 status = (struct ec_response_get_comms_status *)
113 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
123 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
124 struct cros_ec_command *msg)
130 if (ec_dev->proto_version > 2)
131 return prepare_packet(ec_dev, msg);
133 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
135 out[0] = EC_CMD_VERSION0 + msg->version;
136 out[1] = msg->command;
137 out[2] = msg->outsize;
138 csum = out[0] + out[1] + out[2];
139 for (i = 0; i < msg->outsize; i++)
140 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
141 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
143 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
145 EXPORT_SYMBOL(cros_ec_prepare_tx);
147 int cros_ec_check_result(struct cros_ec_device *ec_dev,
148 struct cros_ec_command *msg)
150 switch (msg->result) {
153 case EC_RES_IN_PROGRESS:
154 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
158 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
159 msg->command, msg->result);
163 EXPORT_SYMBOL(cros_ec_check_result);
165 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
167 struct cros_ec_command *msg)
170 * Try using v3+ to query for supported protocols. If this
171 * command fails, fall back to v2. Returns the highest protocol
172 * supported by the EC.
173 * Also sets the max request/response/passthru size.
177 if (!ec_dev->pkt_xfer)
178 return -EPROTONOSUPPORT;
180 memset(msg, 0, sizeof(*msg));
181 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
182 msg->insize = sizeof(struct ec_response_get_protocol_info);
184 ret = send_command(ec_dev, msg);
186 * Send command once again when timeout occurred.
187 * Fingerprint MCU (FPMCU) is restarted during system boot which
188 * introduces small window in which FPMCU won't respond for any
189 * messages sent by kernel. There is no need to wait before next
190 * attempt because we waited at least EC_MSG_DEADLINE_MS.
192 if (ret == -ETIMEDOUT)
193 ret = send_command(ec_dev, msg);
197 "failed to check for EC[%d] protocol version: %d\n",
202 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
204 else if (msg->result != EC_RES_SUCCESS)
210 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
212 struct cros_ec_command *msg;
213 struct ec_params_hello *hello_params;
214 struct ec_response_hello *hello_response;
216 int len = max(sizeof(*hello_params), sizeof(*hello_response));
218 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
223 msg->command = EC_CMD_HELLO;
224 hello_params = (struct ec_params_hello *)msg->data;
225 msg->outsize = sizeof(*hello_params);
226 hello_response = (struct ec_response_hello *)msg->data;
227 msg->insize = sizeof(*hello_response);
229 hello_params->in_data = 0xa0b0c0d0;
231 ret = send_command(ec_dev, msg);
235 "EC failed to respond to v2 hello: %d\n",
238 } else if (msg->result != EC_RES_SUCCESS) {
240 "EC responded to v2 hello with error: %d\n",
244 } else if (hello_response->out_data != 0xa1b2c3d4) {
246 "EC responded to v2 hello with bad result: %u\n",
247 hello_response->out_data);
259 int cros_ec_query_all(struct cros_ec_device *ec_dev)
261 struct device *dev = ec_dev->dev;
262 struct cros_ec_command *proto_msg;
263 struct ec_response_get_protocol_info *proto_info;
266 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
271 /* First try sending with proto v3. */
272 ec_dev->proto_version = 3;
273 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
276 proto_info = (struct ec_response_get_protocol_info *)
278 ec_dev->max_request = proto_info->max_request_packet_size -
279 sizeof(struct ec_host_request);
280 ec_dev->max_response = proto_info->max_response_packet_size -
281 sizeof(struct ec_host_response);
282 ec_dev->proto_version =
283 min(EC_HOST_REQUEST_VERSION,
284 fls(proto_info->protocol_versions) - 1);
287 ec_dev->proto_version);
289 ec_dev->din_size = ec_dev->max_response +
290 sizeof(struct ec_host_response) +
291 EC_MAX_RESPONSE_OVERHEAD;
292 ec_dev->dout_size = ec_dev->max_request +
293 sizeof(struct ec_host_request) +
294 EC_MAX_REQUEST_OVERHEAD;
299 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
302 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
303 ec_dev->max_passthru = 0;
305 dev_dbg(ec_dev->dev, "found PD chip\n");
306 ec_dev->max_passthru =
307 proto_info->max_request_packet_size -
308 sizeof(struct ec_host_request);
311 /* Try querying with a v2 hello message. */
312 ec_dev->proto_version = 2;
313 ret = cros_ec_host_command_proto_query_v2(ec_dev);
316 /* V2 hello succeeded. */
317 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
319 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
320 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
321 ec_dev->max_passthru = 0;
322 ec_dev->pkt_xfer = NULL;
323 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
324 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
327 * It's possible for a test to occur too early when
328 * the EC isn't listening. If this happens, we'll
329 * test later when the first command is run.
331 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
332 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
337 devm_kfree(dev, ec_dev->din);
338 devm_kfree(dev, ec_dev->dout);
340 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
346 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
348 devm_kfree(dev, ec_dev->din);
357 EXPORT_SYMBOL(cros_ec_query_all);
359 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
360 struct cros_ec_command *msg)
364 mutex_lock(&ec_dev->lock);
365 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
366 ret = cros_ec_query_all(ec_dev);
369 "EC version unknown and query failed; aborting command\n");
370 mutex_unlock(&ec_dev->lock);
375 if (msg->insize > ec_dev->max_response) {
376 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
377 msg->insize = ec_dev->max_response;
380 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
381 if (msg->outsize > ec_dev->max_request) {
383 "request of size %u is too big (max: %u)\n",
385 ec_dev->max_request);
386 mutex_unlock(&ec_dev->lock);
390 if (msg->outsize > ec_dev->max_passthru) {
392 "passthru rq of size %u is too big (max: %u)\n",
394 ec_dev->max_passthru);
395 mutex_unlock(&ec_dev->lock);
399 ret = send_command(ec_dev, msg);
400 mutex_unlock(&ec_dev->lock);
404 EXPORT_SYMBOL(cros_ec_cmd_xfer);
406 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
407 struct cros_ec_command *msg)
411 ret = cros_ec_cmd_xfer(ec_dev, msg);
413 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
414 } else if (msg->result != EC_RES_SUCCESS) {
415 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
421 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);