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
prepare_packet(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)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
send_command(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)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
cros_ec_prepare_tx(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)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
cros_ec_check_result(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)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
cros_ec_host_command_proto_query(struct cros_ec_device * ec_dev,int devidx,struct cros_ec_command * msg)155 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
156 int devidx,
157 struct cros_ec_command *msg)
158 {
159 /*
160 * Try using v3+ to query for supported protocols. If this
161 * command fails, fall back to v2. Returns the highest protocol
162 * supported by the EC.
163 * Also sets the max request/response/passthru size.
164 */
165 int ret;
166
167 if (!ec_dev->pkt_xfer)
168 return -EPROTONOSUPPORT;
169
170 memset(msg, 0, sizeof(*msg));
171 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
172 msg->insize = sizeof(struct ec_response_get_protocol_info);
173
174 ret = send_command(ec_dev, msg);
175
176 if (ret < 0) {
177 dev_dbg(ec_dev->dev,
178 "failed to check for EC[%d] protocol version: %d\n",
179 devidx, ret);
180 return ret;
181 }
182
183 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
184 return -ENODEV;
185 else if (msg->result != EC_RES_SUCCESS)
186 return msg->result;
187
188 return 0;
189 }
190
cros_ec_host_command_proto_query_v2(struct cros_ec_device * ec_dev)191 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
192 {
193 struct cros_ec_command *msg;
194 struct ec_params_hello *hello_params;
195 struct ec_response_hello *hello_response;
196 int ret;
197 int len = max(sizeof(*hello_params), sizeof(*hello_response));
198
199 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
200 if (!msg)
201 return -ENOMEM;
202
203 msg->version = 0;
204 msg->command = EC_CMD_HELLO;
205 hello_params = (struct ec_params_hello *)msg->data;
206 msg->outsize = sizeof(*hello_params);
207 hello_response = (struct ec_response_hello *)msg->data;
208 msg->insize = sizeof(*hello_response);
209
210 hello_params->in_data = 0xa0b0c0d0;
211
212 ret = send_command(ec_dev, msg);
213
214 if (ret < 0) {
215 dev_dbg(ec_dev->dev,
216 "EC failed to respond to v2 hello: %d\n",
217 ret);
218 goto exit;
219 } else if (msg->result != EC_RES_SUCCESS) {
220 dev_err(ec_dev->dev,
221 "EC responded to v2 hello with error: %d\n",
222 msg->result);
223 ret = msg->result;
224 goto exit;
225 } else if (hello_response->out_data != 0xa1b2c3d4) {
226 dev_err(ec_dev->dev,
227 "EC responded to v2 hello with bad result: %u\n",
228 hello_response->out_data);
229 ret = -EBADMSG;
230 goto exit;
231 }
232
233 ret = 0;
234
235 exit:
236 kfree(msg);
237 return ret;
238 }
239
cros_ec_get_host_command_version_mask(struct cros_ec_device * ec_dev,u16 cmd,u32 * mask)240 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
241 u16 cmd, u32 *mask)
242 {
243 struct ec_params_get_cmd_versions *pver;
244 struct ec_response_get_cmd_versions *rver;
245 struct cros_ec_command *msg;
246 int ret;
247
248 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
249 GFP_KERNEL);
250 if (!msg)
251 return -ENOMEM;
252
253 msg->version = 0;
254 msg->command = EC_CMD_GET_CMD_VERSIONS;
255 msg->insize = sizeof(*rver);
256 msg->outsize = sizeof(*pver);
257
258 pver = (struct ec_params_get_cmd_versions *)msg->data;
259 pver->cmd = cmd;
260
261 ret = cros_ec_cmd_xfer(ec_dev, msg);
262 if (ret > 0) {
263 rver = (struct ec_response_get_cmd_versions *)msg->data;
264 *mask = rver->version_mask;
265 }
266
267 kfree(msg);
268
269 return ret;
270 }
271
cros_ec_query_all(struct cros_ec_device * ec_dev)272 int cros_ec_query_all(struct cros_ec_device *ec_dev)
273 {
274 struct device *dev = ec_dev->dev;
275 struct cros_ec_command *proto_msg;
276 struct ec_response_get_protocol_info *proto_info;
277 u32 ver_mask = 0;
278 int ret;
279
280 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
281 GFP_KERNEL);
282 if (!proto_msg)
283 return -ENOMEM;
284
285 /* First try sending with proto v3. */
286 ec_dev->proto_version = 3;
287 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
288
289 if (ret == 0) {
290 proto_info = (struct ec_response_get_protocol_info *)
291 proto_msg->data;
292 ec_dev->max_request = proto_info->max_request_packet_size -
293 sizeof(struct ec_host_request);
294 ec_dev->max_response = proto_info->max_response_packet_size -
295 sizeof(struct ec_host_response);
296 ec_dev->proto_version =
297 min(EC_HOST_REQUEST_VERSION,
298 fls(proto_info->protocol_versions) - 1);
299 dev_dbg(ec_dev->dev,
300 "using proto v%u\n",
301 ec_dev->proto_version);
302
303 ec_dev->din_size = ec_dev->max_response +
304 sizeof(struct ec_host_response) +
305 EC_MAX_RESPONSE_OVERHEAD;
306 ec_dev->dout_size = ec_dev->max_request +
307 sizeof(struct ec_host_request) +
308 EC_MAX_REQUEST_OVERHEAD;
309
310 /*
311 * Check for PD
312 */
313 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
314
315 if (ret) {
316 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
317 ec_dev->max_passthru = 0;
318 } else {
319 dev_dbg(ec_dev->dev, "found PD chip\n");
320 ec_dev->max_passthru =
321 proto_info->max_request_packet_size -
322 sizeof(struct ec_host_request);
323 }
324 } else {
325 /* Try querying with a v2 hello message. */
326 ec_dev->proto_version = 2;
327 ret = cros_ec_host_command_proto_query_v2(ec_dev);
328
329 if (ret == 0) {
330 /* V2 hello succeeded. */
331 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
332
333 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
334 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
335 ec_dev->max_passthru = 0;
336 ec_dev->pkt_xfer = NULL;
337 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
338 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
339 } else {
340 /*
341 * It's possible for a test to occur too early when
342 * the EC isn't listening. If this happens, we'll
343 * test later when the first command is run.
344 */
345 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
346 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
347 goto exit;
348 }
349 }
350
351 devm_kfree(dev, ec_dev->din);
352 devm_kfree(dev, ec_dev->dout);
353
354 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
355 if (!ec_dev->din) {
356 ret = -ENOMEM;
357 goto exit;
358 }
359
360 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
361 if (!ec_dev->dout) {
362 devm_kfree(dev, ec_dev->din);
363 ret = -ENOMEM;
364 goto exit;
365 }
366
367 /* Probe if MKBP event is supported */
368 ret = cros_ec_get_host_command_version_mask(ec_dev,
369 EC_CMD_GET_NEXT_EVENT,
370 &ver_mask);
371 if (ret < 0 || ver_mask == 0)
372 ec_dev->mkbp_event_supported = 0;
373 else
374 ec_dev->mkbp_event_supported = 1;
375
376 exit:
377 kfree(proto_msg);
378 return ret;
379 }
380 EXPORT_SYMBOL(cros_ec_query_all);
381
cros_ec_cmd_xfer(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)382 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
383 struct cros_ec_command *msg)
384 {
385 int ret;
386
387 mutex_lock(&ec_dev->lock);
388 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
389 ret = cros_ec_query_all(ec_dev);
390 if (ret) {
391 dev_err(ec_dev->dev,
392 "EC version unknown and query failed; aborting command\n");
393 mutex_unlock(&ec_dev->lock);
394 return ret;
395 }
396 }
397
398 if (msg->insize > ec_dev->max_response) {
399 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
400 msg->insize = ec_dev->max_response;
401 }
402
403 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
404 if (msg->outsize > ec_dev->max_request) {
405 dev_err(ec_dev->dev,
406 "request of size %u is too big (max: %u)\n",
407 msg->outsize,
408 ec_dev->max_request);
409 mutex_unlock(&ec_dev->lock);
410 return -EMSGSIZE;
411 }
412 } else {
413 if (msg->outsize > ec_dev->max_passthru) {
414 dev_err(ec_dev->dev,
415 "passthru rq of size %u is too big (max: %u)\n",
416 msg->outsize,
417 ec_dev->max_passthru);
418 mutex_unlock(&ec_dev->lock);
419 return -EMSGSIZE;
420 }
421 }
422 ret = send_command(ec_dev, msg);
423 mutex_unlock(&ec_dev->lock);
424
425 return ret;
426 }
427 EXPORT_SYMBOL(cros_ec_cmd_xfer);
428
cros_ec_cmd_xfer_status(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)429 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
430 struct cros_ec_command *msg)
431 {
432 int ret;
433
434 ret = cros_ec_cmd_xfer(ec_dev, msg);
435 if (ret < 0) {
436 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
437 } else if (msg->result != EC_RES_SUCCESS) {
438 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
439 return -EPROTO;
440 }
441
442 return ret;
443 }
444 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
445
get_next_event(struct cros_ec_device * ec_dev)446 static int get_next_event(struct cros_ec_device *ec_dev)
447 {
448 u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
449 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
450 int ret;
451
452 msg->version = 0;
453 msg->command = EC_CMD_GET_NEXT_EVENT;
454 msg->insize = sizeof(ec_dev->event_data);
455 msg->outsize = 0;
456
457 ret = cros_ec_cmd_xfer(ec_dev, msg);
458 if (ret > 0) {
459 ec_dev->event_size = ret - 1;
460 memcpy(&ec_dev->event_data, msg->data,
461 sizeof(ec_dev->event_data));
462 }
463
464 return ret;
465 }
466
get_keyboard_state_event(struct cros_ec_device * ec_dev)467 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
468 {
469 u8 buffer[sizeof(struct cros_ec_command) +
470 sizeof(ec_dev->event_data.data)];
471 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
472
473 msg->version = 0;
474 msg->command = EC_CMD_MKBP_STATE;
475 msg->insize = sizeof(ec_dev->event_data.data);
476 msg->outsize = 0;
477
478 ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
479 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
480 memcpy(&ec_dev->event_data.data, msg->data,
481 sizeof(ec_dev->event_data.data));
482
483 return ec_dev->event_size;
484 }
485
cros_ec_get_next_event(struct cros_ec_device * ec_dev)486 int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
487 {
488 if (ec_dev->mkbp_event_supported)
489 return get_next_event(ec_dev);
490 else
491 return get_keyboard_state_event(ec_dev);
492 }
493 EXPORT_SYMBOL(cros_ec_get_next_event);
494