1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef EC_GOOGLE_WILCO_EC_H
4 #define EC_GOOGLE_WILCO_EC_H
5
6 #include <stddef.h>
7 #include <stdint.h>
8
9 /* Different supported message types */
10 enum wilco_ec_msg_type {
11 WILCO_EC_MSG_RAW, /* Raw message, do not skip any data */
12 WILCO_EC_MSG_DEFAULT, /* Skip 1 byte of response data */
13 WILCO_EC_MSG_NO_RESPONSE, /* EC does not respond to command */
14 };
15
16 /**
17 * wilco_ec_mailbox
18 *
19 * Send a command request to the EC mailbox and receive the response.
20 *
21 * @type: Mailbox message type, see enum above
22 * @command: Command to execute
23 * @request_data: Request data buffer
24 * @request_size: Number of bytes in request data buffer (max 32)
25 * @response_data: Response data buffer
26 * @response_size: Number of bytes in response data buffer (max 32)
27 *
28 * @return number of bytes received, negative error code on failure
29 */
30 int wilco_ec_mailbox(enum wilco_ec_msg_type type, uint8_t command,
31 const void *request_data, size_t request_size,
32 void *response_data, size_t response_size);
33
34 /**
35 * wilco_ec_send
36 *
37 * Send a basic EC command with a one byte parameter with no
38 * returned data;
39 *
40 * @command: Command to execute
41 * @param: Command parameter to send
42 *
43 * @return negative error code on failure
44 */
wilco_ec_send(uint8_t command,uint8_t param)45 static inline int wilco_ec_send(uint8_t command, uint8_t param)
46 {
47 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, command,
48 ¶m, sizeof(param), NULL, 0);
49 }
50
51 /**
52 * wilco_ec_send_noargs
53 *
54 * Send a basic EC command with no parameters and no returned data.
55 *
56 * @command: Command to execute
57 *
58 * @return negative error code on failure
59 */
wilco_ec_send_noargs(uint8_t command)60 static inline int wilco_ec_send_noargs(uint8_t command)
61 {
62 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, command,
63 NULL, 0, NULL, 0);
64 }
65
66 /**
67 * wilco_ec_sendrecv
68 *
69 * Send a basic EC command with a one byte parameter, ignoring the
70 * first byte of returned data to match the common behavior.
71 * The maximum response size is 31 due to the ignored byte.
72 *
73 * @command: Command to execute
74 * @param: Command parameter to send
75 * @data: Response data buffer
76 * @size: Number of bytes in response data buffer (max 31)
77 *
78 * @return number of bytes received, negative error code on failure
79 */
wilco_ec_sendrecv(uint8_t command,uint8_t param,void * data,size_t size)80 static inline int wilco_ec_sendrecv(uint8_t command, uint8_t param,
81 void *data, size_t size)
82 {
83 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, command,
84 ¶m, sizeof(param), data, size);
85 }
86
87 /**
88 * wilco_ec_sendrecv_noargs
89 *
90 * Send a basic EC command with no parameters, ignoring the
91 * first byte of returned data to match the common behavior.
92 * The maximum response size is 31 due to the ignored byte.
93 *
94 * @command: Command to execute
95 * @data: Response data buffer
96 * @size: Number of bytes in response data buffer (max 31)
97 *
98 * @return number of bytes received, negative error code on failure
99 */
wilco_ec_sendrecv_noargs(uint8_t command,void * data,size_t size)100 static inline int wilco_ec_sendrecv_noargs(uint8_t command,
101 void *data, size_t size)
102 {
103 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, command,
104 NULL, 0, data, size);
105 }
106
107 #endif /* EC_GOOGLE_WILCO_EC_H */
108