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