1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC communication protocol helper functions
3 //
4 // Copyright (C) 2015 Google, Inc
5 
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>
12 
13 #define EC_COMMAND_RETRIES	50
14 
15 static int prepare_packet(struct cros_ec_device *ec_dev,
16 			  struct cros_ec_command *msg)
17 {
18 	struct ec_host_request *request;
19 	u8 *out;
20 	int i;
21 	u8 csum = 0;
22 
23 	BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
24 	BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
25 
26 	out = ec_dev->dout;
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;
34 
35 	for (i = 0; i < sizeof(*request); i++)
36 		csum += out[i];
37 
38 	/* Copy data and update checksum */
39 	memcpy(out + sizeof(*request), msg->data, msg->outsize);
40 	for (i = 0; i < msg->outsize; i++)
41 		csum += msg->data[i];
42 
43 	request->checksum = -csum;
44 
45 	return sizeof(*request) + msg->outsize;
46 }
47 
48 static int send_command(struct cros_ec_device *ec_dev,
49 			struct cros_ec_command *msg)
50 {
51 	int ret;
52 	int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
53 
54 	if (ec_dev->proto_version > 2)
55 		xfer_fxn = ec_dev->pkt_xfer;
56 	else
57 		xfer_fxn = ec_dev->cmd_xfer;
58 
59 	ret = (*xfer_fxn)(ec_dev, msg);
60 	if (msg->result == EC_RES_IN_PROGRESS) {
61 		int i;
62 		struct cros_ec_command *status_msg;
63 		struct ec_response_get_comms_status *status;
64 
65 		status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
66 				     GFP_KERNEL);
67 		if (!status_msg)
68 			return -ENOMEM;
69 
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;
74 
75 		/*
76 		 * Query the EC's status until it's no longer busy or
77 		 * we encounter an error.
78 		 */
79 		for (i = 0; i < EC_COMMAND_RETRIES; i++) {
80 			usleep_range(10000, 11000);
81 
82 			ret = (*xfer_fxn)(ec_dev, status_msg);
83 			if (ret == -EAGAIN)
84 				continue;
85 			if (ret < 0)
86 				break;
87 
88 			msg->result = status_msg->result;
89 			if (status_msg->result != EC_RES_SUCCESS)
90 				break;
91 
92 			status = (struct ec_response_get_comms_status *)
93 				 status_msg->data;
94 			if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
95 				break;
96 		}
97 
98 		kfree(status_msg);
99 	}
100 
101 	return ret;
102 }
103 
104 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
105 		       struct cros_ec_command *msg)
106 {
107 	u8 *out;
108 	u8 csum;
109 	int i;
110 
111 	if (ec_dev->proto_version > 2)
112 		return prepare_packet(ec_dev, msg);
113 
114 	BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
115 	out = ec_dev->dout;
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;
123 
124 	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
125 }
126 EXPORT_SYMBOL(cros_ec_prepare_tx);
127 
128 int cros_ec_check_result(struct cros_ec_device *ec_dev,
129 			 struct cros_ec_command *msg)
130 {
131 	switch (msg->result) {
132 	case EC_RES_SUCCESS:
133 		return 0;
134 	case EC_RES_IN_PROGRESS:
135 		dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
136 			msg->command);
137 		return -EAGAIN;
138 	default:
139 		dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
140 			msg->command, msg->result);
141 		return 0;
142 	}
143 }
144 EXPORT_SYMBOL(cros_ec_check_result);
145 
146 /*
147  * cros_ec_get_host_event_wake_mask
148  *
149  * Get the mask of host events that cause wake from suspend.
150  *
151  * @ec_dev: EC device to call
152  * @msg: message structure to use
153  * @mask: result when function returns >=0.
154  *
155  * LOCKING:
156  * the caller has ec_dev->lock mutex, or the caller knows there is
157  * no other command in progress.
158  */
159 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
160 					    struct cros_ec_command *msg,
161 					    uint32_t *mask)
162 {
163 	struct ec_response_host_event_mask *r;
164 	int ret;
165 
166 	msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
167 	msg->version = 0;
168 	msg->outsize = 0;
169 	msg->insize = sizeof(*r);
170 
171 	ret = send_command(ec_dev, msg);
172 	if (ret > 0) {
173 		r = (struct ec_response_host_event_mask *)msg->data;
174 		*mask = r->mask;
175 	}
176 
177 	return ret;
178 }
179 
180 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
181 					    int devidx,
182 					    struct cros_ec_command *msg)
183 {
184 	/*
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.
189 	 */
190 	int ret;
191 
192 	if (!ec_dev->pkt_xfer)
193 		return -EPROTONOSUPPORT;
194 
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);
198 
199 	ret = send_command(ec_dev, msg);
200 
201 	if (ret < 0) {
202 		dev_dbg(ec_dev->dev,
203 			"failed to check for EC[%d] protocol version: %d\n",
204 			devidx, ret);
205 		return ret;
206 	}
207 
208 	if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
209 		return -ENODEV;
210 	else if (msg->result != EC_RES_SUCCESS)
211 		return msg->result;
212 
213 	return 0;
214 }
215 
216 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
217 {
218 	struct cros_ec_command *msg;
219 	struct ec_params_hello *hello_params;
220 	struct ec_response_hello *hello_response;
221 	int ret;
222 	int len = max(sizeof(*hello_params), sizeof(*hello_response));
223 
224 	msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
225 	if (!msg)
226 		return -ENOMEM;
227 
228 	msg->version = 0;
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);
234 
235 	hello_params->in_data = 0xa0b0c0d0;
236 
237 	ret = send_command(ec_dev, msg);
238 
239 	if (ret < 0) {
240 		dev_dbg(ec_dev->dev,
241 			"EC failed to respond to v2 hello: %d\n",
242 			ret);
243 		goto exit;
244 	} else if (msg->result != EC_RES_SUCCESS) {
245 		dev_err(ec_dev->dev,
246 			"EC responded to v2 hello with error: %d\n",
247 			msg->result);
248 		ret = msg->result;
249 		goto exit;
250 	} else if (hello_response->out_data != 0xa1b2c3d4) {
251 		dev_err(ec_dev->dev,
252 			"EC responded to v2 hello with bad result: %u\n",
253 			hello_response->out_data);
254 		ret = -EBADMSG;
255 		goto exit;
256 	}
257 
258 	ret = 0;
259 
260  exit:
261 	kfree(msg);
262 	return ret;
263 }
264 
265 /*
266  * cros_ec_get_host_command_version_mask
267  *
268  * Get the version mask of a given command.
269  *
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.
274  *
275  * @return 0 on success, error code otherwise
276  *
277  * LOCKING:
278  * the caller has ec_dev->lock mutex or the caller knows there is
279  * no other command in progress.
280  */
281 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
282 	u16 cmd, u32 *mask)
283 {
284 	struct ec_params_get_cmd_versions *pver;
285 	struct ec_response_get_cmd_versions *rver;
286 	struct cros_ec_command *msg;
287 	int ret;
288 
289 	msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
290 		      GFP_KERNEL);
291 	if (!msg)
292 		return -ENOMEM;
293 
294 	msg->version = 0;
295 	msg->command = EC_CMD_GET_CMD_VERSIONS;
296 	msg->insize = sizeof(*rver);
297 	msg->outsize = sizeof(*pver);
298 
299 	pver = (struct ec_params_get_cmd_versions *)msg->data;
300 	pver->cmd = cmd;
301 
302 	ret = send_command(ec_dev, msg);
303 	if (ret > 0) {
304 		rver = (struct ec_response_get_cmd_versions *)msg->data;
305 		*mask = rver->version_mask;
306 	}
307 
308 	kfree(msg);
309 
310 	return ret;
311 }
312 
313 int cros_ec_query_all(struct cros_ec_device *ec_dev)
314 {
315 	struct device *dev = ec_dev->dev;
316 	struct cros_ec_command *proto_msg;
317 	struct ec_response_get_protocol_info *proto_info;
318 	u32 ver_mask = 0;
319 	int ret;
320 
321 	proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
322 			    GFP_KERNEL);
323 	if (!proto_msg)
324 		return -ENOMEM;
325 
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);
329 
330 	if (ret == 0) {
331 		proto_info = (struct ec_response_get_protocol_info *)
332 			proto_msg->data;
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);
340 		dev_dbg(ec_dev->dev,
341 			"using proto v%u\n",
342 			ec_dev->proto_version);
343 
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;
350 
351 		/*
352 		 * Check for PD
353 		 */
354 		ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
355 
356 		if (ret) {
357 			dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
358 			ec_dev->max_passthru = 0;
359 		} else {
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);
364 		}
365 	} else {
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);
369 
370 		if (ret == 0) {
371 			/* V2 hello succeeded. */
372 			dev_dbg(ec_dev->dev, "falling back to proto v2\n");
373 
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;
380 		} else {
381 			/*
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.
385 			 */
386 			ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
387 			dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
388 			goto exit;
389 		}
390 	}
391 
392 	devm_kfree(dev, ec_dev->din);
393 	devm_kfree(dev, ec_dev->dout);
394 
395 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
396 	if (!ec_dev->din) {
397 		ret = -ENOMEM;
398 		goto exit;
399 	}
400 
401 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
402 	if (!ec_dev->dout) {
403 		devm_kfree(dev, ec_dev->din);
404 		ret = -ENOMEM;
405 		goto exit;
406 	}
407 
408 	/* Probe if MKBP event is supported */
409 	ret = cros_ec_get_host_command_version_mask(ec_dev,
410 						    EC_CMD_GET_NEXT_EVENT,
411 						    &ver_mask);
412 	if (ret < 0 || ver_mask == 0)
413 		ec_dev->mkbp_event_supported = 0;
414 	else
415 		ec_dev->mkbp_event_supported = 1;
416 
417 	/*
418 	 * Get host event wake mask, assume all events are wake events
419 	 * if unavailable.
420 	 */
421 	ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
422 					       &ec_dev->host_event_wake_mask);
423 	if (ret < 0)
424 		ec_dev->host_event_wake_mask = U32_MAX;
425 
426 	ret = 0;
427 
428 exit:
429 	kfree(proto_msg);
430 	return ret;
431 }
432 EXPORT_SYMBOL(cros_ec_query_all);
433 
434 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
435 		     struct cros_ec_command *msg)
436 {
437 	int ret;
438 
439 	mutex_lock(&ec_dev->lock);
440 	if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
441 		ret = cros_ec_query_all(ec_dev);
442 		if (ret) {
443 			dev_err(ec_dev->dev,
444 				"EC version unknown and query failed; aborting command\n");
445 			mutex_unlock(&ec_dev->lock);
446 			return ret;
447 		}
448 	}
449 
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;
453 	}
454 
455 	if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
456 		if (msg->outsize > ec_dev->max_request) {
457 			dev_err(ec_dev->dev,
458 				"request of size %u is too big (max: %u)\n",
459 				msg->outsize,
460 				ec_dev->max_request);
461 			mutex_unlock(&ec_dev->lock);
462 			return -EMSGSIZE;
463 		}
464 	} else {
465 		if (msg->outsize > ec_dev->max_passthru) {
466 			dev_err(ec_dev->dev,
467 				"passthru rq of size %u is too big (max: %u)\n",
468 				msg->outsize,
469 				ec_dev->max_passthru);
470 			mutex_unlock(&ec_dev->lock);
471 			return -EMSGSIZE;
472 		}
473 	}
474 	ret = send_command(ec_dev, msg);
475 	mutex_unlock(&ec_dev->lock);
476 
477 	return ret;
478 }
479 EXPORT_SYMBOL(cros_ec_cmd_xfer);
480 
481 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
482 			    struct cros_ec_command *msg)
483 {
484 	int ret;
485 
486 	ret = cros_ec_cmd_xfer(ec_dev, msg);
487 	if (ret < 0) {
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);
491 		return -EPROTO;
492 	}
493 
494 	return ret;
495 }
496 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
497 
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)
501 {
502 	int ret;
503 
504 	msg->version = version;
505 	msg->command = EC_CMD_GET_NEXT_EVENT;
506 	msg->insize = size;
507 	msg->outsize = 0;
508 
509 	ret = cros_ec_cmd_xfer(ec_dev, msg);
510 	if (ret > 0) {
511 		ec_dev->event_size = ret - 1;
512 		memcpy(&ec_dev->event_data, msg->data, ret);
513 	}
514 
515 	return ret;
516 }
517 
518 static int get_next_event(struct cros_ec_device *ec_dev)
519 {
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;
523 	int ret;
524 
525 	if (ec_dev->suspended) {
526 		dev_dbg(ec_dev->dev, "Device suspended.\n");
527 		return -EHOSTDOWN;
528 	}
529 
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)
534 			return ret;
535 
536 		/* Fallback to version 0 for future send attempts */
537 		cmd_version = 0;
538 	}
539 
540 	ret = get_next_event_xfer(ec_dev, msg, cmd_version,
541 				  sizeof(struct ec_response_get_next_event));
542 
543 	return ret;
544 }
545 
546 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
547 {
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;
551 
552 	msg->version = 0;
553 	msg->command = EC_CMD_MKBP_STATE;
554 	msg->insize = sizeof(ec_dev->event_data.data);
555 	msg->outsize = 0;
556 
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));
561 
562 	return ec_dev->event_size;
563 }
564 
565 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event)
566 {
567 	u8 event_type;
568 	u32 host_event;
569 	int ret;
570 
571 	if (!ec_dev->mkbp_event_supported) {
572 		ret = get_keyboard_state_event(ec_dev);
573 		if (ret <= 0)
574 			return ret;
575 
576 		if (wake_event)
577 			*wake_event = true;
578 
579 		return ret;
580 	}
581 
582 	ret = get_next_event(ec_dev);
583 	if (ret <= 0)
584 		return ret;
585 
586 	if (wake_event) {
587 		event_type = ec_dev->event_data.event_type;
588 		host_event = cros_ec_get_host_event(ec_dev);
589 
590 		/*
591 		 * Sensor events need to be parsed by the sensor sub-device.
592 		 * Defer them, and don't report the wakeup here.
593 		 */
594 		if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
595 			*wake_event = false;
596 		/* Masked host-events should not count as wake events. */
597 		else if (host_event &&
598 			 !(host_event & ec_dev->host_event_wake_mask))
599 			*wake_event = false;
600 		/* Consider all other events as wake events. */
601 		else
602 			*wake_event = true;
603 	}
604 
605 	return ret;
606 }
607 EXPORT_SYMBOL(cros_ec_get_next_event);
608 
609 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
610 {
611 	u32 host_event;
612 
613 	BUG_ON(!ec_dev->mkbp_event_supported);
614 
615 	if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
616 		return 0;
617 
618 	if (ec_dev->event_size != sizeof(host_event)) {
619 		dev_warn(ec_dev->dev, "Invalid host event size\n");
620 		return 0;
621 	}
622 
623 	host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
624 
625 	return host_event;
626 }
627 EXPORT_SYMBOL(cros_ec_get_host_event);
628