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>
22 #include <asm/unaligned.h>
24 #define EC_COMMAND_RETRIES 50
26 static int prepare_packet(struct cros_ec_device
*ec_dev
,
27 struct cros_ec_command
*msg
)
29 struct ec_host_request
*request
;
34 BUG_ON(ec_dev
->proto_version
!= EC_HOST_REQUEST_VERSION
);
35 BUG_ON(msg
->outsize
+ sizeof(*request
) > ec_dev
->dout_size
);
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
;
46 for (i
= 0; i
< sizeof(*request
); i
++)
49 /* Copy data and update checksum */
50 memcpy(out
+ sizeof(*request
), msg
->data
, msg
->outsize
);
51 for (i
= 0; i
< msg
->outsize
; i
++)
54 request
->checksum
= -csum
;
56 return sizeof(*request
) + msg
->outsize
;
59 static int send_command(struct cros_ec_device
*ec_dev
,
60 struct cros_ec_command
*msg
)
63 int (*xfer_fxn
)(struct cros_ec_device
*ec
, struct cros_ec_command
*msg
);
65 if (ec_dev
->proto_version
> 2)
66 xfer_fxn
= ec_dev
->pkt_xfer
;
68 xfer_fxn
= ec_dev
->cmd_xfer
;
70 ret
= (*xfer_fxn
)(ec_dev
, msg
);
71 if (msg
->result
== EC_RES_IN_PROGRESS
) {
73 struct cros_ec_command
*status_msg
;
74 struct ec_response_get_comms_status
*status
;
76 status_msg
= kmalloc(sizeof(*status_msg
) + sizeof(*status
),
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;
87 * Query the EC's status until it's no longer busy or
88 * we encounter an error.
90 for (i
= 0; i
< EC_COMMAND_RETRIES
; i
++) {
91 usleep_range(10000, 11000);
93 ret
= (*xfer_fxn
)(ec_dev
, status_msg
);
97 msg
->result
= status_msg
->result
;
98 if (status_msg
->result
!= EC_RES_SUCCESS
)
101 status
= (struct ec_response_get_comms_status
*)
103 if (!(status
->flags
& EC_COMMS_STATUS_PROCESSING
))
113 int cros_ec_prepare_tx(struct cros_ec_device
*ec_dev
,
114 struct cros_ec_command
*msg
)
120 if (ec_dev
->proto_version
> 2)
121 return prepare_packet(ec_dev
, msg
);
123 BUG_ON(msg
->outsize
> EC_PROTO2_MAX_PARAM_SIZE
);
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
;
133 return EC_MSG_TX_PROTO_BYTES
+ msg
->outsize
;
135 EXPORT_SYMBOL(cros_ec_prepare_tx
);
137 int cros_ec_check_result(struct cros_ec_device
*ec_dev
,
138 struct cros_ec_command
*msg
)
140 switch (msg
->result
) {
143 case EC_RES_IN_PROGRESS
:
144 dev_dbg(ec_dev
->dev
, "command 0x%02x in progress\n",
148 dev_dbg(ec_dev
->dev
, "command 0x%02x returned %d\n",
149 msg
->command
, msg
->result
);
153 EXPORT_SYMBOL(cros_ec_check_result
);
156 * cros_ec_get_host_event_wake_mask
158 * Get the mask of host events that cause wake from suspend.
160 * @ec_dev: EC device to call
161 * @msg: message structure to use
162 * @mask: result when function returns >=0.
165 * the caller has ec_dev->lock mutex, or the caller knows there is
166 * no other command in progress.
168 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device
*ec_dev
,
169 struct cros_ec_command
*msg
,
172 struct ec_response_host_event_mask
*r
;
175 msg
->command
= EC_CMD_HOST_EVENT_GET_WAKE_MASK
;
178 msg
->insize
= sizeof(*r
);
180 ret
= send_command(ec_dev
, msg
);
182 r
= (struct ec_response_host_event_mask
*)msg
->data
;
189 static int cros_ec_host_command_proto_query(struct cros_ec_device
*ec_dev
,
191 struct cros_ec_command
*msg
)
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.
201 if (!ec_dev
->pkt_xfer
)
202 return -EPROTONOSUPPORT
;
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
);
208 ret
= send_command(ec_dev
, msg
);
212 "failed to check for EC[%d] protocol version: %d\n",
217 if (devidx
> 0 && msg
->result
== EC_RES_INVALID_COMMAND
)
219 else if (msg
->result
!= EC_RES_SUCCESS
)
225 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device
*ec_dev
)
227 struct cros_ec_command
*msg
;
228 struct ec_params_hello
*hello_params
;
229 struct ec_response_hello
*hello_response
;
231 int len
= max(sizeof(*hello_params
), sizeof(*hello_response
));
233 msg
= kmalloc(sizeof(*msg
) + len
, GFP_KERNEL
);
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
);
244 hello_params
->in_data
= 0xa0b0c0d0;
246 ret
= send_command(ec_dev
, msg
);
250 "EC failed to respond to v2 hello: %d\n",
253 } else if (msg
->result
!= EC_RES_SUCCESS
) {
255 "EC responded to v2 hello with error: %d\n",
259 } else if (hello_response
->out_data
!= 0xa1b2c3d4) {
261 "EC responded to v2 hello with bad result: %u\n",
262 hello_response
->out_data
);
275 * cros_ec_get_host_command_version_mask
277 * Get the version mask of a given command.
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.
284 * @return 0 on success, error code otherwise
287 * the caller has ec_dev->lock mutex or the caller knows there is
288 * no other command in progress.
290 static int cros_ec_get_host_command_version_mask(struct cros_ec_device
*ec_dev
,
293 struct ec_params_get_cmd_versions
*pver
;
294 struct ec_response_get_cmd_versions
*rver
;
295 struct cros_ec_command
*msg
;
298 msg
= kmalloc(sizeof(*msg
) + max(sizeof(*rver
), sizeof(*pver
)),
304 msg
->command
= EC_CMD_GET_CMD_VERSIONS
;
305 msg
->insize
= sizeof(*rver
);
306 msg
->outsize
= sizeof(*pver
);
308 pver
= (struct ec_params_get_cmd_versions
*)msg
->data
;
311 ret
= send_command(ec_dev
, msg
);
313 rver
= (struct ec_response_get_cmd_versions
*)msg
->data
;
314 *mask
= rver
->version_mask
;
322 int cros_ec_query_all(struct cros_ec_device
*ec_dev
)
324 struct device
*dev
= ec_dev
->dev
;
325 struct cros_ec_command
*proto_msg
;
326 struct ec_response_get_protocol_info
*proto_info
;
330 proto_msg
= kzalloc(sizeof(*proto_msg
) + sizeof(*proto_info
),
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
);
340 proto_info
= (struct ec_response_get_protocol_info
*)
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);
351 ec_dev
->proto_version
);
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
;
363 ret
= cros_ec_host_command_proto_query(ec_dev
, 1, proto_msg
);
366 dev_dbg(ec_dev
->dev
, "no PD chip found: %d\n", ret
);
367 ec_dev
->max_passthru
= 0;
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
);
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
);
380 /* V2 hello succeeded. */
381 dev_dbg(ec_dev
->dev
, "falling back to proto v2\n");
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
;
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.
395 ec_dev
->proto_version
= EC_PROTO_VERSION_UNKNOWN
;
396 dev_dbg(ec_dev
->dev
, "EC query failed: %d\n", ret
);
401 devm_kfree(dev
, ec_dev
->din
);
402 devm_kfree(dev
, ec_dev
->dout
);
404 ec_dev
->din
= devm_kzalloc(dev
, ec_dev
->din_size
, GFP_KERNEL
);
410 ec_dev
->dout
= devm_kzalloc(dev
, ec_dev
->dout_size
, GFP_KERNEL
);
412 devm_kfree(dev
, ec_dev
->din
);
417 /* Probe if MKBP event is supported */
418 ret
= cros_ec_get_host_command_version_mask(ec_dev
,
419 EC_CMD_GET_NEXT_EVENT
,
421 if (ret
< 0 || ver_mask
== 0)
422 ec_dev
->mkbp_event_supported
= 0;
424 ec_dev
->mkbp_event_supported
= 1;
427 * Get host event wake mask, assume all events are wake events
430 ret
= cros_ec_get_host_event_wake_mask(ec_dev
, proto_msg
,
431 &ec_dev
->host_event_wake_mask
);
433 ec_dev
->host_event_wake_mask
= U32_MAX
;
441 EXPORT_SYMBOL(cros_ec_query_all
);
443 int cros_ec_cmd_xfer(struct cros_ec_device
*ec_dev
,
444 struct cros_ec_command
*msg
)
448 mutex_lock(&ec_dev
->lock
);
449 if (ec_dev
->proto_version
== EC_PROTO_VERSION_UNKNOWN
) {
450 ret
= cros_ec_query_all(ec_dev
);
453 "EC version unknown and query failed; aborting command\n");
454 mutex_unlock(&ec_dev
->lock
);
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
;
464 if (msg
->command
< EC_CMD_PASSTHRU_OFFSET(1)) {
465 if (msg
->outsize
> ec_dev
->max_request
) {
467 "request of size %u is too big (max: %u)\n",
469 ec_dev
->max_request
);
470 mutex_unlock(&ec_dev
->lock
);
474 if (msg
->outsize
> ec_dev
->max_passthru
) {
476 "passthru rq of size %u is too big (max: %u)\n",
478 ec_dev
->max_passthru
);
479 mutex_unlock(&ec_dev
->lock
);
483 ret
= send_command(ec_dev
, msg
);
484 mutex_unlock(&ec_dev
->lock
);
488 EXPORT_SYMBOL(cros_ec_cmd_xfer
);
490 int cros_ec_cmd_xfer_status(struct cros_ec_device
*ec_dev
,
491 struct cros_ec_command
*msg
)
495 ret
= cros_ec_cmd_xfer(ec_dev
, msg
);
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
);
505 EXPORT_SYMBOL(cros_ec_cmd_xfer_status
);
507 static int get_next_event(struct cros_ec_device
*ec_dev
)
509 u8 buffer
[sizeof(struct cros_ec_command
) + sizeof(ec_dev
->event_data
)];
510 struct cros_ec_command
*msg
= (struct cros_ec_command
*)&buffer
;
513 if (ec_dev
->suspended
) {
514 dev_dbg(ec_dev
->dev
, "Device suspended.\n");
519 msg
->command
= EC_CMD_GET_NEXT_EVENT
;
520 msg
->insize
= sizeof(ec_dev
->event_data
);
523 ret
= cros_ec_cmd_xfer(ec_dev
, msg
);
525 ec_dev
->event_size
= ret
- 1;
526 memcpy(&ec_dev
->event_data
, msg
->data
,
527 sizeof(ec_dev
->event_data
));
533 static int get_keyboard_state_event(struct cros_ec_device
*ec_dev
)
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
;
540 msg
->command
= EC_CMD_MKBP_STATE
;
541 msg
->insize
= sizeof(ec_dev
->event_data
.data
);
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
));
549 return ec_dev
->event_size
;
552 int cros_ec_get_next_event(struct cros_ec_device
*ec_dev
, bool *wake_event
)
557 if (!ec_dev
->mkbp_event_supported
) {
558 ret
= get_keyboard_state_event(ec_dev
);
568 ret
= get_next_event(ec_dev
);
573 host_event
= cros_ec_get_host_event(ec_dev
);
575 /* Consider non-host_event as wake event */
576 *wake_event
= !host_event
||
577 !!(host_event
& ec_dev
->host_event_wake_mask
);
582 EXPORT_SYMBOL(cros_ec_get_next_event
);
584 u32
cros_ec_get_host_event(struct cros_ec_device
*ec_dev
)
588 BUG_ON(!ec_dev
->mkbp_event_supported
);
590 if (ec_dev
->event_data
.event_type
!= EC_MKBP_EVENT_HOST_EVENT
)
593 if (ec_dev
->event_size
!= sizeof(host_event
)) {
594 dev_warn(ec_dev
->dev
, "Invalid host event size\n");
598 host_event
= get_unaligned_le32(&ec_dev
->event_data
.data
.host_event
);
602 EXPORT_SYMBOL(cros_ec_get_host_event
);