• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #define EC_COMMAND_RETRIES	50
24 
prepare_packet(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)25 static int prepare_packet(struct cros_ec_device *ec_dev,
26 			  struct cros_ec_command *msg)
27 {
28 	struct ec_host_request *request;
29 	u8 *out;
30 	int i;
31 	u8 csum = 0;
32 
33 	BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
34 	BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
35 
36 	out = ec_dev->dout;
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;
44 
45 	for (i = 0; i < sizeof(*request); i++)
46 		csum += out[i];
47 
48 	/* Copy data and update checksum */
49 	memcpy(out + sizeof(*request), msg->data, msg->outsize);
50 	for (i = 0; i < msg->outsize; i++)
51 		csum += msg->data[i];
52 
53 	request->checksum = -csum;
54 
55 	return sizeof(*request) + msg->outsize;
56 }
57 
send_command(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)58 static int send_command(struct cros_ec_device *ec_dev,
59 			struct cros_ec_command *msg)
60 {
61 	int ret;
62 	int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
63 
64 	if (ec_dev->proto_version > 2)
65 		xfer_fxn = ec_dev->pkt_xfer;
66 	else
67 		xfer_fxn = ec_dev->cmd_xfer;
68 
69 	if (!xfer_fxn) {
70 		/*
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.
74 		 */
75 		dev_err_once(ec_dev->dev,
76 			     "missing EC transfer API, cannot send command\n");
77 		return -EIO;
78 	}
79 
80 	ret = (*xfer_fxn)(ec_dev, msg);
81 	if (msg->result == EC_RES_IN_PROGRESS) {
82 		int i;
83 		struct cros_ec_command *status_msg;
84 		struct ec_response_get_comms_status *status;
85 
86 		status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
87 				     GFP_KERNEL);
88 		if (!status_msg)
89 			return -ENOMEM;
90 
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;
95 
96 		/*
97 		 * Query the EC's status until it's no longer busy or
98 		 * we encounter an error.
99 		 */
100 		for (i = 0; i < EC_COMMAND_RETRIES; i++) {
101 			usleep_range(10000, 11000);
102 
103 			ret = (*xfer_fxn)(ec_dev, status_msg);
104 			if (ret < 0)
105 				break;
106 
107 			msg->result = status_msg->result;
108 			if (status_msg->result != EC_RES_SUCCESS)
109 				break;
110 
111 			status = (struct ec_response_get_comms_status *)
112 				 status_msg->data;
113 			if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
114 				break;
115 		}
116 
117 		kfree(status_msg);
118 	}
119 
120 	return ret;
121 }
122 
cros_ec_prepare_tx(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)123 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
124 		       struct cros_ec_command *msg)
125 {
126 	u8 *out;
127 	u8 csum;
128 	int i;
129 
130 	if (ec_dev->proto_version > 2)
131 		return prepare_packet(ec_dev, msg);
132 
133 	BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
134 	out = ec_dev->dout;
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;
142 
143 	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
144 }
145 EXPORT_SYMBOL(cros_ec_prepare_tx);
146 
cros_ec_check_result(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)147 int cros_ec_check_result(struct cros_ec_device *ec_dev,
148 			 struct cros_ec_command *msg)
149 {
150 	switch (msg->result) {
151 	case EC_RES_SUCCESS:
152 		return 0;
153 	case EC_RES_IN_PROGRESS:
154 		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
155 			msg->command);
156 		return -EAGAIN;
157 	default:
158 		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
159 			msg->command, msg->result);
160 		return 0;
161 	}
162 }
163 EXPORT_SYMBOL(cros_ec_check_result);
164 
cros_ec_host_command_proto_query(struct cros_ec_device * ec_dev,int devidx,struct cros_ec_command * msg)165 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
166 					    int devidx,
167 					    struct cros_ec_command *msg)
168 {
169 	/*
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.
174 	 */
175 	int ret;
176 
177 	if (!ec_dev->pkt_xfer)
178 		return -EPROTONOSUPPORT;
179 
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);
183 
184 	ret = send_command(ec_dev, msg);
185 	/*
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.
191 	 */
192 	if (ret == -ETIMEDOUT)
193 		ret = send_command(ec_dev, msg);
194 
195 	if (ret < 0) {
196 		dev_dbg(ec_dev->dev,
197 			"failed to check for EC[%d] protocol version: %d\n",
198 			devidx, ret);
199 		return ret;
200 	}
201 
202 	if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
203 		return -ENODEV;
204 	else if (msg->result != EC_RES_SUCCESS)
205 		return msg->result;
206 
207 	return 0;
208 }
209 
cros_ec_host_command_proto_query_v2(struct cros_ec_device * ec_dev)210 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
211 {
212 	struct cros_ec_command *msg;
213 	struct ec_params_hello *hello_params;
214 	struct ec_response_hello *hello_response;
215 	int ret;
216 	int len = max(sizeof(*hello_params), sizeof(*hello_response));
217 
218 	msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
219 	if (!msg)
220 		return -ENOMEM;
221 
222 	msg->version = 0;
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);
228 
229 	hello_params->in_data = 0xa0b0c0d0;
230 
231 	ret = send_command(ec_dev, msg);
232 
233 	if (ret < 0) {
234 		dev_dbg(ec_dev->dev,
235 			"EC failed to respond to v2 hello: %d\n",
236 			ret);
237 		goto exit;
238 	} else if (msg->result != EC_RES_SUCCESS) {
239 		dev_err(ec_dev->dev,
240 			"EC responded to v2 hello with error: %d\n",
241 			msg->result);
242 		ret = msg->result;
243 		goto exit;
244 	} else if (hello_response->out_data != 0xa1b2c3d4) {
245 		dev_err(ec_dev->dev,
246 			"EC responded to v2 hello with bad result: %u\n",
247 			hello_response->out_data);
248 		ret = -EBADMSG;
249 		goto exit;
250 	}
251 
252 	ret = 0;
253 
254  exit:
255 	kfree(msg);
256 	return ret;
257 }
258 
cros_ec_query_all(struct cros_ec_device * ec_dev)259 int cros_ec_query_all(struct cros_ec_device *ec_dev)
260 {
261 	struct device *dev = ec_dev->dev;
262 	struct cros_ec_command *proto_msg;
263 	struct ec_response_get_protocol_info *proto_info;
264 	int ret;
265 
266 	proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
267 			    GFP_KERNEL);
268 	if (!proto_msg)
269 		return -ENOMEM;
270 
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);
274 
275 	if (ret == 0) {
276 		proto_info = (struct ec_response_get_protocol_info *)
277 			proto_msg->data;
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);
285 		dev_dbg(ec_dev->dev,
286 			"using proto v%u\n",
287 			ec_dev->proto_version);
288 
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;
295 
296 		/*
297 		 * Check for PD
298 		 */
299 		ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
300 
301 		if (ret) {
302 			dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
303 			ec_dev->max_passthru = 0;
304 		} else {
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);
309 		}
310 	} else {
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);
314 
315 		if (ret == 0) {
316 			/* V2 hello succeeded. */
317 			dev_dbg(ec_dev->dev, "falling back to proto v2\n");
318 
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;
325 		} else {
326 			/*
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.
330 			 */
331 			ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
332 			dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
333 			goto exit;
334 		}
335 	}
336 
337 	devm_kfree(dev, ec_dev->din);
338 	devm_kfree(dev, ec_dev->dout);
339 
340 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
341 	if (!ec_dev->din) {
342 		ret = -ENOMEM;
343 		goto exit;
344 	}
345 
346 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
347 	if (!ec_dev->dout) {
348 		devm_kfree(dev, ec_dev->din);
349 		ret = -ENOMEM;
350 		goto exit;
351 	}
352 
353 exit:
354 	kfree(proto_msg);
355 	return ret;
356 }
357 EXPORT_SYMBOL(cros_ec_query_all);
358 
cros_ec_cmd_xfer(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)359 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
360 		     struct cros_ec_command *msg)
361 {
362 	int ret;
363 
364 	mutex_lock(&ec_dev->lock);
365 	if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
366 		ret = cros_ec_query_all(ec_dev);
367 		if (ret) {
368 			dev_err(ec_dev->dev,
369 				"EC version unknown and query failed; aborting command\n");
370 			mutex_unlock(&ec_dev->lock);
371 			return ret;
372 		}
373 	}
374 
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;
378 	}
379 
380 	if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
381 		if (msg->outsize > ec_dev->max_request) {
382 			dev_err(ec_dev->dev,
383 				"request of size %u is too big (max: %u)\n",
384 				msg->outsize,
385 				ec_dev->max_request);
386 			mutex_unlock(&ec_dev->lock);
387 			return -EMSGSIZE;
388 		}
389 	} else {
390 		if (msg->outsize > ec_dev->max_passthru) {
391 			dev_err(ec_dev->dev,
392 				"passthru rq of size %u is too big (max: %u)\n",
393 				msg->outsize,
394 				ec_dev->max_passthru);
395 			mutex_unlock(&ec_dev->lock);
396 			return -EMSGSIZE;
397 		}
398 	}
399 	ret = send_command(ec_dev, msg);
400 	mutex_unlock(&ec_dev->lock);
401 
402 	return ret;
403 }
404 EXPORT_SYMBOL(cros_ec_cmd_xfer);
405 
cros_ec_cmd_xfer_status(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)406 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
407 			    struct cros_ec_command *msg)
408 {
409 	int ret;
410 
411 	ret = cros_ec_cmd_xfer(ec_dev, msg);
412 	if (ret < 0) {
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);
416 		return -EPROTO;
417 	}
418 
419 	return ret;
420 }
421 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
422