GNU Linux-libre 4.9.308-gnu1
[releases.git] / drivers / platform / chrome / cros_ec_proto.c
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         if (!xfer_fxn) {
71                 /*
72                  * This error can happen if a communication error happened and
73                  * the EC is trying to use protocol v2, on an underlying
74                  * communication mechanism that does not support v2.
75                  */
76                 dev_err_once(ec_dev->dev,
77                              "missing EC transfer API, cannot send command\n");
78                 return -EIO;
79         }
80
81         ret = (*xfer_fxn)(ec_dev, msg);
82         if (msg->result == EC_RES_IN_PROGRESS) {
83                 int i;
84                 struct cros_ec_command *status_msg;
85                 struct ec_response_get_comms_status *status;
86
87                 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
88                                      GFP_KERNEL);
89                 if (!status_msg)
90                         return -ENOMEM;
91
92                 status_msg->version = 0;
93                 status_msg->command = EC_CMD_GET_COMMS_STATUS;
94                 status_msg->insize = sizeof(*status);
95                 status_msg->outsize = 0;
96
97                 /*
98                  * Query the EC's status until it's no longer busy or
99                  * we encounter an error.
100                  */
101                 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
102                         usleep_range(10000, 11000);
103
104                         ret = (*xfer_fxn)(ec_dev, status_msg);
105                         if (ret < 0)
106                                 break;
107
108                         msg->result = status_msg->result;
109                         if (status_msg->result != EC_RES_SUCCESS)
110                                 break;
111
112                         status = (struct ec_response_get_comms_status *)
113                                  status_msg->data;
114                         if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
115                                 break;
116                 }
117
118                 kfree(status_msg);
119         }
120
121         return ret;
122 }
123
124 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
125                        struct cros_ec_command *msg)
126 {
127         u8 *out;
128         u8 csum;
129         int i;
130
131         if (ec_dev->proto_version > 2)
132                 return prepare_packet(ec_dev, msg);
133
134         BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
135         out = ec_dev->dout;
136         out[0] = EC_CMD_VERSION0 + msg->version;
137         out[1] = msg->command;
138         out[2] = msg->outsize;
139         csum = out[0] + out[1] + out[2];
140         for (i = 0; i < msg->outsize; i++)
141                 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
142         out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
143
144         return EC_MSG_TX_PROTO_BYTES + msg->outsize;
145 }
146 EXPORT_SYMBOL(cros_ec_prepare_tx);
147
148 int cros_ec_check_result(struct cros_ec_device *ec_dev,
149                          struct cros_ec_command *msg)
150 {
151         switch (msg->result) {
152         case EC_RES_SUCCESS:
153                 return 0;
154         case EC_RES_IN_PROGRESS:
155                 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
156                         msg->command);
157                 return -EAGAIN;
158         default:
159                 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
160                         msg->command, msg->result);
161                 return 0;
162         }
163 }
164 EXPORT_SYMBOL(cros_ec_check_result);
165
166 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
167                                             int devidx,
168                                             struct cros_ec_command *msg)
169 {
170         /*
171          * Try using v3+ to query for supported protocols. If this
172          * command fails, fall back to v2. Returns the highest protocol
173          * supported by the EC.
174          * Also sets the max request/response/passthru size.
175          */
176         int ret;
177
178         if (!ec_dev->pkt_xfer)
179                 return -EPROTONOSUPPORT;
180
181         memset(msg, 0, sizeof(*msg));
182         msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
183         msg->insize = sizeof(struct ec_response_get_protocol_info);
184
185         ret = send_command(ec_dev, msg);
186         /*
187          * Send command once again when timeout occurred.
188          * Fingerprint MCU (FPMCU) is restarted during system boot which
189          * introduces small window in which FPMCU won't respond for any
190          * messages sent by kernel. There is no need to wait before next
191          * attempt because we waited at least EC_MSG_DEADLINE_MS.
192          */
193         if (ret == -ETIMEDOUT)
194                 ret = send_command(ec_dev, msg);
195
196         if (ret < 0) {
197                 dev_dbg(ec_dev->dev,
198                         "failed to check for EC[%d] protocol version: %d\n",
199                         devidx, ret);
200                 return ret;
201         }
202
203         if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
204                 return -ENODEV;
205         else if (msg->result != EC_RES_SUCCESS)
206                 return msg->result;
207
208         return 0;
209 }
210
211 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
212 {
213         struct cros_ec_command *msg;
214         struct ec_params_hello *hello_params;
215         struct ec_response_hello *hello_response;
216         int ret;
217         int len = max(sizeof(*hello_params), sizeof(*hello_response));
218
219         msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
220         if (!msg)
221                 return -ENOMEM;
222
223         msg->version = 0;
224         msg->command = EC_CMD_HELLO;
225         hello_params = (struct ec_params_hello *)msg->data;
226         msg->outsize = sizeof(*hello_params);
227         hello_response = (struct ec_response_hello *)msg->data;
228         msg->insize = sizeof(*hello_response);
229
230         hello_params->in_data = 0xa0b0c0d0;
231
232         ret = send_command(ec_dev, msg);
233
234         if (ret < 0) {
235                 dev_dbg(ec_dev->dev,
236                         "EC failed to respond to v2 hello: %d\n",
237                         ret);
238                 goto exit;
239         } else if (msg->result != EC_RES_SUCCESS) {
240                 dev_err(ec_dev->dev,
241                         "EC responded to v2 hello with error: %d\n",
242                         msg->result);
243                 ret = msg->result;
244                 goto exit;
245         } else if (hello_response->out_data != 0xa1b2c3d4) {
246                 dev_err(ec_dev->dev,
247                         "EC responded to v2 hello with bad result: %u\n",
248                         hello_response->out_data);
249                 ret = -EBADMSG;
250                 goto exit;
251         }
252
253         ret = 0;
254
255  exit:
256         kfree(msg);
257         return ret;
258 }
259
260 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
261         u16 cmd, u32 *mask)
262 {
263         struct ec_params_get_cmd_versions *pver;
264         struct ec_response_get_cmd_versions *rver;
265         struct cros_ec_command *msg;
266         int ret;
267
268         msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
269                       GFP_KERNEL);
270         if (!msg)
271                 return -ENOMEM;
272
273         msg->version = 0;
274         msg->command = EC_CMD_GET_CMD_VERSIONS;
275         msg->insize = sizeof(*rver);
276         msg->outsize = sizeof(*pver);
277
278         pver = (struct ec_params_get_cmd_versions *)msg->data;
279         pver->cmd = cmd;
280
281         ret = cros_ec_cmd_xfer(ec_dev, msg);
282         if (ret > 0) {
283                 rver = (struct ec_response_get_cmd_versions *)msg->data;
284                 *mask = rver->version_mask;
285         }
286
287         kfree(msg);
288
289         return ret;
290 }
291
292 int cros_ec_query_all(struct cros_ec_device *ec_dev)
293 {
294         struct device *dev = ec_dev->dev;
295         struct cros_ec_command *proto_msg;
296         struct ec_response_get_protocol_info *proto_info;
297         u32 ver_mask = 0;
298         int ret;
299
300         proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
301                             GFP_KERNEL);
302         if (!proto_msg)
303                 return -ENOMEM;
304
305         /* First try sending with proto v3. */
306         ec_dev->proto_version = 3;
307         ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
308
309         if (ret == 0) {
310                 proto_info = (struct ec_response_get_protocol_info *)
311                         proto_msg->data;
312                 ec_dev->max_request = proto_info->max_request_packet_size -
313                         sizeof(struct ec_host_request);
314                 ec_dev->max_response = proto_info->max_response_packet_size -
315                         sizeof(struct ec_host_response);
316                 ec_dev->proto_version =
317                         min(EC_HOST_REQUEST_VERSION,
318                                         fls(proto_info->protocol_versions) - 1);
319                 dev_dbg(ec_dev->dev,
320                         "using proto v%u\n",
321                         ec_dev->proto_version);
322
323                 ec_dev->din_size = ec_dev->max_response +
324                         sizeof(struct ec_host_response) +
325                         EC_MAX_RESPONSE_OVERHEAD;
326                 ec_dev->dout_size = ec_dev->max_request +
327                         sizeof(struct ec_host_request) +
328                         EC_MAX_REQUEST_OVERHEAD;
329
330                 /*
331                  * Check for PD
332                  */
333                 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
334
335                 if (ret) {
336                         dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
337                         ec_dev->max_passthru = 0;
338                 } else {
339                         dev_dbg(ec_dev->dev, "found PD chip\n");
340                         ec_dev->max_passthru =
341                                 proto_info->max_request_packet_size -
342                                 sizeof(struct ec_host_request);
343                 }
344         } else {
345                 /* Try querying with a v2 hello message. */
346                 ec_dev->proto_version = 2;
347                 ret = cros_ec_host_command_proto_query_v2(ec_dev);
348
349                 if (ret == 0) {
350                         /* V2 hello succeeded. */
351                         dev_dbg(ec_dev->dev, "falling back to proto v2\n");
352
353                         ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
354                         ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
355                         ec_dev->max_passthru = 0;
356                         ec_dev->pkt_xfer = NULL;
357                         ec_dev->din_size = EC_PROTO2_MSG_BYTES;
358                         ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
359                 } else {
360                         /*
361                          * It's possible for a test to occur too early when
362                          * the EC isn't listening. If this happens, we'll
363                          * test later when the first command is run.
364                          */
365                         ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
366                         dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
367                         goto exit;
368                 }
369         }
370
371         devm_kfree(dev, ec_dev->din);
372         devm_kfree(dev, ec_dev->dout);
373
374         ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
375         if (!ec_dev->din) {
376                 ret = -ENOMEM;
377                 goto exit;
378         }
379
380         ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
381         if (!ec_dev->dout) {
382                 devm_kfree(dev, ec_dev->din);
383                 ret = -ENOMEM;
384                 goto exit;
385         }
386
387         /* Probe if MKBP event is supported */
388         ret = cros_ec_get_host_command_version_mask(ec_dev,
389                                                     EC_CMD_GET_NEXT_EVENT,
390                                                     &ver_mask);
391         if (ret < 0 || ver_mask == 0)
392                 ec_dev->mkbp_event_supported = 0;
393         else
394                 ec_dev->mkbp_event_supported = 1;
395
396 exit:
397         kfree(proto_msg);
398         return ret;
399 }
400 EXPORT_SYMBOL(cros_ec_query_all);
401
402 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
403                      struct cros_ec_command *msg)
404 {
405         int ret;
406
407         mutex_lock(&ec_dev->lock);
408         if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
409                 ret = cros_ec_query_all(ec_dev);
410                 if (ret) {
411                         dev_err(ec_dev->dev,
412                                 "EC version unknown and query failed; aborting command\n");
413                         mutex_unlock(&ec_dev->lock);
414                         return ret;
415                 }
416         }
417
418         if (msg->insize > ec_dev->max_response) {
419                 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
420                 msg->insize = ec_dev->max_response;
421         }
422
423         if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
424                 if (msg->outsize > ec_dev->max_request) {
425                         dev_err(ec_dev->dev,
426                                 "request of size %u is too big (max: %u)\n",
427                                 msg->outsize,
428                                 ec_dev->max_request);
429                         mutex_unlock(&ec_dev->lock);
430                         return -EMSGSIZE;
431                 }
432         } else {
433                 if (msg->outsize > ec_dev->max_passthru) {
434                         dev_err(ec_dev->dev,
435                                 "passthru rq of size %u is too big (max: %u)\n",
436                                 msg->outsize,
437                                 ec_dev->max_passthru);
438                         mutex_unlock(&ec_dev->lock);
439                         return -EMSGSIZE;
440                 }
441         }
442         ret = send_command(ec_dev, msg);
443         mutex_unlock(&ec_dev->lock);
444
445         return ret;
446 }
447 EXPORT_SYMBOL(cros_ec_cmd_xfer);
448
449 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
450                             struct cros_ec_command *msg)
451 {
452         int ret;
453
454         ret = cros_ec_cmd_xfer(ec_dev, msg);
455         if (ret < 0) {
456                 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
457         } else if (msg->result != EC_RES_SUCCESS) {
458                 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
459                 return -EPROTO;
460         }
461
462         return ret;
463 }
464 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
465
466 static int get_next_event(struct cros_ec_device *ec_dev)
467 {
468         u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
469         struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
470         int ret;
471
472         msg->version = 0;
473         msg->command = EC_CMD_GET_NEXT_EVENT;
474         msg->insize = sizeof(ec_dev->event_data);
475         msg->outsize = 0;
476
477         ret = cros_ec_cmd_xfer(ec_dev, msg);
478         if (ret > 0) {
479                 ec_dev->event_size = ret - 1;
480                 memcpy(&ec_dev->event_data, msg->data,
481                        sizeof(ec_dev->event_data));
482         }
483
484         return ret;
485 }
486
487 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
488 {
489         u8 buffer[sizeof(struct cros_ec_command) +
490                   sizeof(ec_dev->event_data.data)];
491         struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
492
493         msg->version = 0;
494         msg->command = EC_CMD_MKBP_STATE;
495         msg->insize = sizeof(ec_dev->event_data.data);
496         msg->outsize = 0;
497
498         ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
499         ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
500         memcpy(&ec_dev->event_data.data, msg->data,
501                sizeof(ec_dev->event_data.data));
502
503         return ec_dev->event_size;
504 }
505
506 int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
507 {
508         if (ec_dev->mkbp_event_supported)
509                 return get_next_event(ec_dev);
510         else
511                 return get_keyboard_state_event(ec_dev);
512 }
513 EXPORT_SYMBOL(cros_ec_get_next_event);