xref: /linux/drivers/platform/chrome/cros_ec_proto.c (revision fcc8487d477a3452a1d0ccbdd4c5e0e1e3cb8bed)
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 
26 static 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 
59 static int send_command(struct cros_ec_device *ec_dev,
60 			struct cros_ec_command *msg)
61 {
62 	int ret;
63 
64 	if (ec_dev->proto_version > 2)
65 		ret = ec_dev->pkt_xfer(ec_dev, msg);
66 	else
67 		ret = ec_dev->cmd_xfer(ec_dev, msg);
68 
69 	if (msg->result == EC_RES_IN_PROGRESS) {
70 		int i;
71 		struct cros_ec_command *status_msg;
72 		struct ec_response_get_comms_status *status;
73 
74 		status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
75 				     GFP_KERNEL);
76 		if (!status_msg)
77 			return -ENOMEM;
78 
79 		status_msg->version = 0;
80 		status_msg->command = EC_CMD_GET_COMMS_STATUS;
81 		status_msg->insize = sizeof(*status);
82 		status_msg->outsize = 0;
83 
84 		/*
85 		 * Query the EC's status until it's no longer busy or
86 		 * we encounter an error.
87 		 */
88 		for (i = 0; i < EC_COMMAND_RETRIES; i++) {
89 			usleep_range(10000, 11000);
90 
91 			ret = ec_dev->cmd_xfer(ec_dev, status_msg);
92 			if (ret < 0)
93 				break;
94 
95 			msg->result = status_msg->result;
96 			if (status_msg->result != EC_RES_SUCCESS)
97 				break;
98 
99 			status = (struct ec_response_get_comms_status *)
100 				 status_msg->data;
101 			if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
102 				break;
103 		}
104 
105 		kfree(status_msg);
106 	}
107 
108 	return ret;
109 }
110 
111 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
112 		       struct cros_ec_command *msg)
113 {
114 	u8 *out;
115 	u8 csum;
116 	int i;
117 
118 	if (ec_dev->proto_version > 2)
119 		return prepare_packet(ec_dev, msg);
120 
121 	BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
122 	out = ec_dev->dout;
123 	out[0] = EC_CMD_VERSION0 + msg->version;
124 	out[1] = msg->command;
125 	out[2] = msg->outsize;
126 	csum = out[0] + out[1] + out[2];
127 	for (i = 0; i < msg->outsize; i++)
128 		csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
129 	out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
130 
131 	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
132 }
133 EXPORT_SYMBOL(cros_ec_prepare_tx);
134 
135 int cros_ec_check_result(struct cros_ec_device *ec_dev,
136 			 struct cros_ec_command *msg)
137 {
138 	switch (msg->result) {
139 	case EC_RES_SUCCESS:
140 		return 0;
141 	case EC_RES_IN_PROGRESS:
142 		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
143 			msg->command);
144 		return -EAGAIN;
145 	default:
146 		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
147 			msg->command, msg->result);
148 		return 0;
149 	}
150 }
151 EXPORT_SYMBOL(cros_ec_check_result);
152 
153 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
154 					    int devidx,
155 					    struct cros_ec_command *msg)
156 {
157 	/*
158 	 * Try using v3+ to query for supported protocols. If this
159 	 * command fails, fall back to v2. Returns the highest protocol
160 	 * supported by the EC.
161 	 * Also sets the max request/response/passthru size.
162 	 */
163 	int ret;
164 
165 	if (!ec_dev->pkt_xfer)
166 		return -EPROTONOSUPPORT;
167 
168 	memset(msg, 0, sizeof(*msg));
169 	msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
170 	msg->insize = sizeof(struct ec_response_get_protocol_info);
171 
172 	ret = send_command(ec_dev, msg);
173 
174 	if (ret < 0) {
175 		dev_dbg(ec_dev->dev,
176 			"failed to check for EC[%d] protocol version: %d\n",
177 			devidx, ret);
178 		return ret;
179 	}
180 
181 	if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
182 		return -ENODEV;
183 	else if (msg->result != EC_RES_SUCCESS)
184 		return msg->result;
185 
186 	return 0;
187 }
188 
189 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
190 {
191 	struct cros_ec_command *msg;
192 	struct ec_params_hello *hello_params;
193 	struct ec_response_hello *hello_response;
194 	int ret;
195 	int len = max(sizeof(*hello_params), sizeof(*hello_response));
196 
197 	msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
198 	if (!msg)
199 		return -ENOMEM;
200 
201 	msg->version = 0;
202 	msg->command = EC_CMD_HELLO;
203 	hello_params = (struct ec_params_hello *)msg->data;
204 	msg->outsize = sizeof(*hello_params);
205 	hello_response = (struct ec_response_hello *)msg->data;
206 	msg->insize = sizeof(*hello_response);
207 
208 	hello_params->in_data = 0xa0b0c0d0;
209 
210 	ret = send_command(ec_dev, msg);
211 
212 	if (ret < 0) {
213 		dev_dbg(ec_dev->dev,
214 			"EC failed to respond to v2 hello: %d\n",
215 			ret);
216 		goto exit;
217 	} else if (msg->result != EC_RES_SUCCESS) {
218 		dev_err(ec_dev->dev,
219 			"EC responded to v2 hello with error: %d\n",
220 			msg->result);
221 		ret = msg->result;
222 		goto exit;
223 	} else if (hello_response->out_data != 0xa1b2c3d4) {
224 		dev_err(ec_dev->dev,
225 			"EC responded to v2 hello with bad result: %u\n",
226 			hello_response->out_data);
227 		ret = -EBADMSG;
228 		goto exit;
229 	}
230 
231 	ret = 0;
232 
233  exit:
234 	kfree(msg);
235 	return ret;
236 }
237 
238 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
239 	u16 cmd, u32 *mask)
240 {
241 	struct ec_params_get_cmd_versions *pver;
242 	struct ec_response_get_cmd_versions *rver;
243 	struct cros_ec_command *msg;
244 	int ret;
245 
246 	msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
247 		      GFP_KERNEL);
248 	if (!msg)
249 		return -ENOMEM;
250 
251 	msg->version = 0;
252 	msg->command = EC_CMD_GET_CMD_VERSIONS;
253 	msg->insize = sizeof(*rver);
254 	msg->outsize = sizeof(*pver);
255 
256 	pver = (struct ec_params_get_cmd_versions *)msg->data;
257 	pver->cmd = cmd;
258 
259 	ret = cros_ec_cmd_xfer(ec_dev, msg);
260 	if (ret > 0) {
261 		rver = (struct ec_response_get_cmd_versions *)msg->data;
262 		*mask = rver->version_mask;
263 	}
264 
265 	kfree(msg);
266 
267 	return ret;
268 }
269 
270 int cros_ec_query_all(struct cros_ec_device *ec_dev)
271 {
272 	struct device *dev = ec_dev->dev;
273 	struct cros_ec_command *proto_msg;
274 	struct ec_response_get_protocol_info *proto_info;
275 	u32 ver_mask = 0;
276 	int ret;
277 
278 	proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
279 			    GFP_KERNEL);
280 	if (!proto_msg)
281 		return -ENOMEM;
282 
283 	/* First try sending with proto v3. */
284 	ec_dev->proto_version = 3;
285 	ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
286 
287 	if (ret == 0) {
288 		proto_info = (struct ec_response_get_protocol_info *)
289 			proto_msg->data;
290 		ec_dev->max_request = proto_info->max_request_packet_size -
291 			sizeof(struct ec_host_request);
292 		ec_dev->max_response = proto_info->max_response_packet_size -
293 			sizeof(struct ec_host_response);
294 		ec_dev->proto_version =
295 			min(EC_HOST_REQUEST_VERSION,
296 					fls(proto_info->protocol_versions) - 1);
297 		dev_dbg(ec_dev->dev,
298 			"using proto v%u\n",
299 			ec_dev->proto_version);
300 
301 		ec_dev->din_size = ec_dev->max_response +
302 			sizeof(struct ec_host_response) +
303 			EC_MAX_RESPONSE_OVERHEAD;
304 		ec_dev->dout_size = ec_dev->max_request +
305 			sizeof(struct ec_host_request) +
306 			EC_MAX_REQUEST_OVERHEAD;
307 
308 		/*
309 		 * Check for PD
310 		 */
311 		ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
312 
313 		if (ret) {
314 			dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
315 			ec_dev->max_passthru = 0;
316 		} else {
317 			dev_dbg(ec_dev->dev, "found PD chip\n");
318 			ec_dev->max_passthru =
319 				proto_info->max_request_packet_size -
320 				sizeof(struct ec_host_request);
321 		}
322 	} else {
323 		/* Try querying with a v2 hello message. */
324 		ec_dev->proto_version = 2;
325 		ret = cros_ec_host_command_proto_query_v2(ec_dev);
326 
327 		if (ret == 0) {
328 			/* V2 hello succeeded. */
329 			dev_dbg(ec_dev->dev, "falling back to proto v2\n");
330 
331 			ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
332 			ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
333 			ec_dev->max_passthru = 0;
334 			ec_dev->pkt_xfer = NULL;
335 			ec_dev->din_size = EC_PROTO2_MSG_BYTES;
336 			ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
337 		} else {
338 			/*
339 			 * It's possible for a test to occur too early when
340 			 * the EC isn't listening. If this happens, we'll
341 			 * test later when the first command is run.
342 			 */
343 			ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
344 			dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
345 			goto exit;
346 		}
347 	}
348 
349 	devm_kfree(dev, ec_dev->din);
350 	devm_kfree(dev, ec_dev->dout);
351 
352 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
353 	if (!ec_dev->din) {
354 		ret = -ENOMEM;
355 		goto exit;
356 	}
357 
358 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
359 	if (!ec_dev->dout) {
360 		devm_kfree(dev, ec_dev->din);
361 		ret = -ENOMEM;
362 		goto exit;
363 	}
364 
365 	/* Probe if MKBP event is supported */
366 	ret = cros_ec_get_host_command_version_mask(ec_dev,
367 						    EC_CMD_GET_NEXT_EVENT,
368 						    &ver_mask);
369 	if (ret < 0 || ver_mask == 0)
370 		ec_dev->mkbp_event_supported = 0;
371 	else
372 		ec_dev->mkbp_event_supported = 1;
373 
374 exit:
375 	kfree(proto_msg);
376 	return ret;
377 }
378 EXPORT_SYMBOL(cros_ec_query_all);
379 
380 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
381 		     struct cros_ec_command *msg)
382 {
383 	int ret;
384 
385 	mutex_lock(&ec_dev->lock);
386 	if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
387 		ret = cros_ec_query_all(ec_dev);
388 		if (ret) {
389 			dev_err(ec_dev->dev,
390 				"EC version unknown and query failed; aborting command\n");
391 			mutex_unlock(&ec_dev->lock);
392 			return ret;
393 		}
394 	}
395 
396 	if (msg->insize > ec_dev->max_response) {
397 		dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
398 		msg->insize = ec_dev->max_response;
399 	}
400 
401 	if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
402 		if (msg->outsize > ec_dev->max_request) {
403 			dev_err(ec_dev->dev,
404 				"request of size %u is too big (max: %u)\n",
405 				msg->outsize,
406 				ec_dev->max_request);
407 			mutex_unlock(&ec_dev->lock);
408 			return -EMSGSIZE;
409 		}
410 	} else {
411 		if (msg->outsize > ec_dev->max_passthru) {
412 			dev_err(ec_dev->dev,
413 				"passthru rq of size %u is too big (max: %u)\n",
414 				msg->outsize,
415 				ec_dev->max_passthru);
416 			mutex_unlock(&ec_dev->lock);
417 			return -EMSGSIZE;
418 		}
419 	}
420 	ret = send_command(ec_dev, msg);
421 	mutex_unlock(&ec_dev->lock);
422 
423 	return ret;
424 }
425 EXPORT_SYMBOL(cros_ec_cmd_xfer);
426 
427 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
428 			    struct cros_ec_command *msg)
429 {
430 	int ret;
431 
432 	ret = cros_ec_cmd_xfer(ec_dev, msg);
433 	if (ret < 0) {
434 		dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
435 	} else if (msg->result != EC_RES_SUCCESS) {
436 		dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
437 		return -EPROTO;
438 	}
439 
440 	return ret;
441 }
442 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
443 
444 static int get_next_event(struct cros_ec_device *ec_dev)
445 {
446 	u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
447 	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
448 	int ret;
449 
450 	if (ec_dev->suspended) {
451 		dev_dbg(ec_dev->dev, "Device suspended.\n");
452 		return -EHOSTDOWN;
453 	}
454 
455 	msg->version = 0;
456 	msg->command = EC_CMD_GET_NEXT_EVENT;
457 	msg->insize = sizeof(ec_dev->event_data);
458 	msg->outsize = 0;
459 
460 	ret = cros_ec_cmd_xfer(ec_dev, msg);
461 	if (ret > 0) {
462 		ec_dev->event_size = ret - 1;
463 		memcpy(&ec_dev->event_data, msg->data,
464 		       sizeof(ec_dev->event_data));
465 	}
466 
467 	return ret;
468 }
469 
470 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
471 {
472 	u8 buffer[sizeof(struct cros_ec_command) +
473 		  sizeof(ec_dev->event_data.data)];
474 	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
475 
476 	msg->version = 0;
477 	msg->command = EC_CMD_MKBP_STATE;
478 	msg->insize = sizeof(ec_dev->event_data.data);
479 	msg->outsize = 0;
480 
481 	ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
482 	ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
483 	memcpy(&ec_dev->event_data.data, msg->data,
484 	       sizeof(ec_dev->event_data.data));
485 
486 	return ec_dev->event_size;
487 }
488 
489 int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
490 {
491 	if (ec_dev->mkbp_event_supported)
492 		return get_next_event(ec_dev);
493 	else
494 		return get_keyboard_state_event(ec_dev);
495 }
496 EXPORT_SYMBOL(cros_ec_get_next_event);
497