linux/drivers/platform/chrome/cros_ec_proto.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2// ChromeOS EC communication protocol helper functions
   3//
   4// Copyright (C) 2015 Google, Inc
   5
   6#include <linux/delay.h>
   7#include <linux/device.h>
   8#include <linux/module.h>
   9#include <linux/platform_data/cros_ec_commands.h>
  10#include <linux/platform_data/cros_ec_proto.h>
  11#include <linux/slab.h>
  12#include <asm/unaligned.h>
  13
  14#include "cros_ec_trace.h"
  15
  16#define EC_COMMAND_RETRIES      50
  17
  18static const int cros_ec_error_map[] = {
  19        [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
  20        [EC_RES_ERROR] = -EIO,
  21        [EC_RES_INVALID_PARAM] = -EINVAL,
  22        [EC_RES_ACCESS_DENIED] = -EACCES,
  23        [EC_RES_INVALID_RESPONSE] = -EPROTO,
  24        [EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
  25        [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
  26        [EC_RES_IN_PROGRESS] = -EINPROGRESS,
  27        [EC_RES_UNAVAILABLE] = -ENODATA,
  28        [EC_RES_TIMEOUT] = -ETIMEDOUT,
  29        [EC_RES_OVERFLOW] = -EOVERFLOW,
  30        [EC_RES_INVALID_HEADER] = -EBADR,
  31        [EC_RES_REQUEST_TRUNCATED] = -EBADR,
  32        [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
  33        [EC_RES_BUS_ERROR] = -EFAULT,
  34        [EC_RES_BUSY] = -EBUSY,
  35        [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
  36        [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
  37        [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
  38        [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
  39};
  40
  41static int cros_ec_map_error(uint32_t result)
  42{
  43        int ret = 0;
  44
  45        if (result != EC_RES_SUCCESS) {
  46                if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
  47                        ret = cros_ec_error_map[result];
  48                else
  49                        ret = -EPROTO;
  50        }
  51
  52        return ret;
  53}
  54
  55static int prepare_packet(struct cros_ec_device *ec_dev,
  56                          struct cros_ec_command *msg)
  57{
  58        struct ec_host_request *request;
  59        u8 *out;
  60        int i;
  61        u8 csum = 0;
  62
  63        BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
  64        BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
  65
  66        out = ec_dev->dout;
  67        request = (struct ec_host_request *)out;
  68        request->struct_version = EC_HOST_REQUEST_VERSION;
  69        request->checksum = 0;
  70        request->command = msg->command;
  71        request->command_version = msg->version;
  72        request->reserved = 0;
  73        request->data_len = msg->outsize;
  74
  75        for (i = 0; i < sizeof(*request); i++)
  76                csum += out[i];
  77
  78        /* Copy data and update checksum */
  79        memcpy(out + sizeof(*request), msg->data, msg->outsize);
  80        for (i = 0; i < msg->outsize; i++)
  81                csum += msg->data[i];
  82
  83        request->checksum = -csum;
  84
  85        return sizeof(*request) + msg->outsize;
  86}
  87
  88static int send_command(struct cros_ec_device *ec_dev,
  89                        struct cros_ec_command *msg)
  90{
  91        int ret;
  92        int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
  93
  94        if (ec_dev->proto_version > 2)
  95                xfer_fxn = ec_dev->pkt_xfer;
  96        else
  97                xfer_fxn = ec_dev->cmd_xfer;
  98
  99        if (!xfer_fxn) {
 100                /*
 101                 * This error can happen if a communication error happened and
 102                 * the EC is trying to use protocol v2, on an underlying
 103                 * communication mechanism that does not support v2.
 104                 */
 105                dev_err_once(ec_dev->dev,
 106                             "missing EC transfer API, cannot send command\n");
 107                return -EIO;
 108        }
 109
 110        trace_cros_ec_request_start(msg);
 111        ret = (*xfer_fxn)(ec_dev, msg);
 112        trace_cros_ec_request_done(msg, ret);
 113        if (msg->result == EC_RES_IN_PROGRESS) {
 114                int i;
 115                struct cros_ec_command *status_msg;
 116                struct ec_response_get_comms_status *status;
 117
 118                status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
 119                                     GFP_KERNEL);
 120                if (!status_msg)
 121                        return -ENOMEM;
 122
 123                status_msg->version = 0;
 124                status_msg->command = EC_CMD_GET_COMMS_STATUS;
 125                status_msg->insize = sizeof(*status);
 126                status_msg->outsize = 0;
 127
 128                /*
 129                 * Query the EC's status until it's no longer busy or
 130                 * we encounter an error.
 131                 */
 132                for (i = 0; i < EC_COMMAND_RETRIES; i++) {
 133                        usleep_range(10000, 11000);
 134
 135                        trace_cros_ec_request_start(status_msg);
 136                        ret = (*xfer_fxn)(ec_dev, status_msg);
 137                        trace_cros_ec_request_done(status_msg, ret);
 138                        if (ret == -EAGAIN)
 139                                continue;
 140                        if (ret < 0)
 141                                break;
 142
 143                        msg->result = status_msg->result;
 144                        if (status_msg->result != EC_RES_SUCCESS)
 145                                break;
 146
 147                        status = (struct ec_response_get_comms_status *)
 148                                 status_msg->data;
 149                        if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
 150                                break;
 151                }
 152
 153                kfree(status_msg);
 154        }
 155
 156        return ret;
 157}
 158
 159/**
 160 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
 161 * @ec_dev: Device to register.
 162 * @msg: Message to write.
 163 *
 164 * This is intended to be used by all ChromeOS EC drivers, but at present
 165 * only SPI uses it. Once LPC uses the same protocol it can start using it.
 166 * I2C could use it now, with a refactor of the existing code.
 167 *
 168 * Return: 0 on success or negative error code.
 169 */
 170int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
 171                       struct cros_ec_command *msg)
 172{
 173        u8 *out;
 174        u8 csum;
 175        int i;
 176
 177        if (ec_dev->proto_version > 2)
 178                return prepare_packet(ec_dev, msg);
 179
 180        BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
 181        out = ec_dev->dout;
 182        out[0] = EC_CMD_VERSION0 + msg->version;
 183        out[1] = msg->command;
 184        out[2] = msg->outsize;
 185        csum = out[0] + out[1] + out[2];
 186        for (i = 0; i < msg->outsize; i++)
 187                csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
 188        out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
 189
 190        return EC_MSG_TX_PROTO_BYTES + msg->outsize;
 191}
 192EXPORT_SYMBOL(cros_ec_prepare_tx);
 193
 194/**
 195 * cros_ec_check_result() - Check ec_msg->result.
 196 * @ec_dev: EC device.
 197 * @msg: Message to check.
 198 *
 199 * This is used by ChromeOS EC drivers to check the ec_msg->result for
 200 * errors and to warn about them.
 201 *
 202 * Return: 0 on success or negative error code.
 203 */
 204int cros_ec_check_result(struct cros_ec_device *ec_dev,
 205                         struct cros_ec_command *msg)
 206{
 207        switch (msg->result) {
 208        case EC_RES_SUCCESS:
 209                return 0;
 210        case EC_RES_IN_PROGRESS:
 211                dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
 212                        msg->command);
 213                return -EAGAIN;
 214        default:
 215                dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
 216                        msg->command, msg->result);
 217                return 0;
 218        }
 219}
 220EXPORT_SYMBOL(cros_ec_check_result);
 221
 222/*
 223 * cros_ec_get_host_event_wake_mask
 224 *
 225 * Get the mask of host events that cause wake from suspend.
 226 *
 227 * @ec_dev: EC device to call
 228 * @msg: message structure to use
 229 * @mask: result when function returns >=0.
 230 *
 231 * LOCKING:
 232 * the caller has ec_dev->lock mutex, or the caller knows there is
 233 * no other command in progress.
 234 */
 235static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
 236                                            struct cros_ec_command *msg,
 237                                            uint32_t *mask)
 238{
 239        struct ec_response_host_event_mask *r;
 240        int ret;
 241
 242        msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
 243        msg->version = 0;
 244        msg->outsize = 0;
 245        msg->insize = sizeof(*r);
 246
 247        ret = send_command(ec_dev, msg);
 248        if (ret >= 0) {
 249                if (msg->result == EC_RES_INVALID_COMMAND)
 250                        return -EOPNOTSUPP;
 251                if (msg->result != EC_RES_SUCCESS)
 252                        return -EPROTO;
 253        }
 254        if (ret > 0) {
 255                r = (struct ec_response_host_event_mask *)msg->data;
 256                *mask = r->mask;
 257        }
 258
 259        return ret;
 260}
 261
 262static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
 263                                            int devidx,
 264                                            struct cros_ec_command *msg)
 265{
 266        /*
 267         * Try using v3+ to query for supported protocols. If this
 268         * command fails, fall back to v2. Returns the highest protocol
 269         * supported by the EC.
 270         * Also sets the max request/response/passthru size.
 271         */
 272        int ret;
 273
 274        if (!ec_dev->pkt_xfer)
 275                return -EPROTONOSUPPORT;
 276
 277        memset(msg, 0, sizeof(*msg));
 278        msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
 279        msg->insize = sizeof(struct ec_response_get_protocol_info);
 280
 281        ret = send_command(ec_dev, msg);
 282
 283        if (ret < 0) {
 284                dev_dbg(ec_dev->dev,
 285                        "failed to check for EC[%d] protocol version: %d\n",
 286                        devidx, ret);
 287                return ret;
 288        }
 289
 290        if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
 291                return -ENODEV;
 292        else if (msg->result != EC_RES_SUCCESS)
 293                return msg->result;
 294
 295        return 0;
 296}
 297
 298static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
 299{
 300        struct cros_ec_command *msg;
 301        struct ec_params_hello *hello_params;
 302        struct ec_response_hello *hello_response;
 303        int ret;
 304        int len = max(sizeof(*hello_params), sizeof(*hello_response));
 305
 306        msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
 307        if (!msg)
 308                return -ENOMEM;
 309
 310        msg->version = 0;
 311        msg->command = EC_CMD_HELLO;
 312        hello_params = (struct ec_params_hello *)msg->data;
 313        msg->outsize = sizeof(*hello_params);
 314        hello_response = (struct ec_response_hello *)msg->data;
 315        msg->insize = sizeof(*hello_response);
 316
 317        hello_params->in_data = 0xa0b0c0d0;
 318
 319        ret = send_command(ec_dev, msg);
 320
 321        if (ret < 0) {
 322                dev_dbg(ec_dev->dev,
 323                        "EC failed to respond to v2 hello: %d\n",
 324                        ret);
 325                goto exit;
 326        } else if (msg->result != EC_RES_SUCCESS) {
 327                dev_err(ec_dev->dev,
 328                        "EC responded to v2 hello with error: %d\n",
 329                        msg->result);
 330                ret = msg->result;
 331                goto exit;
 332        } else if (hello_response->out_data != 0xa1b2c3d4) {
 333                dev_err(ec_dev->dev,
 334                        "EC responded to v2 hello with bad result: %u\n",
 335                        hello_response->out_data);
 336                ret = -EBADMSG;
 337                goto exit;
 338        }
 339
 340        ret = 0;
 341
 342 exit:
 343        kfree(msg);
 344        return ret;
 345}
 346
 347/*
 348 * cros_ec_get_host_command_version_mask
 349 *
 350 * Get the version mask of a given command.
 351 *
 352 * @ec_dev: EC device to call
 353 * @msg: message structure to use
 354 * @cmd: command to get the version of.
 355 * @mask: result when function returns 0.
 356 *
 357 * @return 0 on success, error code otherwise
 358 *
 359 * LOCKING:
 360 * the caller has ec_dev->lock mutex or the caller knows there is
 361 * no other command in progress.
 362 */
 363static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
 364        u16 cmd, u32 *mask)
 365{
 366        struct ec_params_get_cmd_versions *pver;
 367        struct ec_response_get_cmd_versions *rver;
 368        struct cros_ec_command *msg;
 369        int ret;
 370
 371        msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
 372                      GFP_KERNEL);
 373        if (!msg)
 374                return -ENOMEM;
 375
 376        msg->version = 0;
 377        msg->command = EC_CMD_GET_CMD_VERSIONS;
 378        msg->insize = sizeof(*rver);
 379        msg->outsize = sizeof(*pver);
 380
 381        pver = (struct ec_params_get_cmd_versions *)msg->data;
 382        pver->cmd = cmd;
 383
 384        ret = send_command(ec_dev, msg);
 385        if (ret > 0) {
 386                rver = (struct ec_response_get_cmd_versions *)msg->data;
 387                *mask = rver->version_mask;
 388        }
 389
 390        kfree(msg);
 391
 392        return ret;
 393}
 394
 395/**
 396 * cros_ec_query_all() -  Query the protocol version supported by the
 397 *         ChromeOS EC.
 398 * @ec_dev: Device to register.
 399 *
 400 * Return: 0 on success or negative error code.
 401 */
 402int cros_ec_query_all(struct cros_ec_device *ec_dev)
 403{
 404        struct device *dev = ec_dev->dev;
 405        struct cros_ec_command *proto_msg;
 406        struct ec_response_get_protocol_info *proto_info;
 407        u32 ver_mask = 0;
 408        int ret;
 409
 410        proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
 411                            GFP_KERNEL);
 412        if (!proto_msg)
 413                return -ENOMEM;
 414
 415        /* First try sending with proto v3. */
 416        ec_dev->proto_version = 3;
 417        ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
 418
 419        if (ret == 0) {
 420                proto_info = (struct ec_response_get_protocol_info *)
 421                        proto_msg->data;
 422                ec_dev->max_request = proto_info->max_request_packet_size -
 423                        sizeof(struct ec_host_request);
 424                ec_dev->max_response = proto_info->max_response_packet_size -
 425                        sizeof(struct ec_host_response);
 426                ec_dev->proto_version =
 427                        min(EC_HOST_REQUEST_VERSION,
 428                                        fls(proto_info->protocol_versions) - 1);
 429                dev_dbg(ec_dev->dev,
 430                        "using proto v%u\n",
 431                        ec_dev->proto_version);
 432
 433                ec_dev->din_size = ec_dev->max_response +
 434                        sizeof(struct ec_host_response) +
 435                        EC_MAX_RESPONSE_OVERHEAD;
 436                ec_dev->dout_size = ec_dev->max_request +
 437                        sizeof(struct ec_host_request) +
 438                        EC_MAX_REQUEST_OVERHEAD;
 439
 440                /*
 441                 * Check for PD
 442                 */
 443                ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
 444
 445                if (ret) {
 446                        dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
 447                        ec_dev->max_passthru = 0;
 448                } else {
 449                        dev_dbg(ec_dev->dev, "found PD chip\n");
 450                        ec_dev->max_passthru =
 451                                proto_info->max_request_packet_size -
 452                                sizeof(struct ec_host_request);
 453                }
 454        } else {
 455                /* Try querying with a v2 hello message. */
 456                ec_dev->proto_version = 2;
 457                ret = cros_ec_host_command_proto_query_v2(ec_dev);
 458
 459                if (ret == 0) {
 460                        /* V2 hello succeeded. */
 461                        dev_dbg(ec_dev->dev, "falling back to proto v2\n");
 462
 463                        ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
 464                        ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
 465                        ec_dev->max_passthru = 0;
 466                        ec_dev->pkt_xfer = NULL;
 467                        ec_dev->din_size = EC_PROTO2_MSG_BYTES;
 468                        ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
 469                } else {
 470                        /*
 471                         * It's possible for a test to occur too early when
 472                         * the EC isn't listening. If this happens, we'll
 473                         * test later when the first command is run.
 474                         */
 475                        ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
 476                        dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
 477                        goto exit;
 478                }
 479        }
 480
 481        devm_kfree(dev, ec_dev->din);
 482        devm_kfree(dev, ec_dev->dout);
 483
 484        ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
 485        if (!ec_dev->din) {
 486                ret = -ENOMEM;
 487                goto exit;
 488        }
 489
 490        ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
 491        if (!ec_dev->dout) {
 492                devm_kfree(dev, ec_dev->din);
 493                ret = -ENOMEM;
 494                goto exit;
 495        }
 496
 497        /* Probe if MKBP event is supported */
 498        ret = cros_ec_get_host_command_version_mask(ec_dev,
 499                                                    EC_CMD_GET_NEXT_EVENT,
 500                                                    &ver_mask);
 501        if (ret < 0 || ver_mask == 0)
 502                ec_dev->mkbp_event_supported = 0;
 503        else
 504                ec_dev->mkbp_event_supported = fls(ver_mask);
 505
 506        dev_dbg(ec_dev->dev, "MKBP support version %u\n",
 507                ec_dev->mkbp_event_supported - 1);
 508
 509        /* Probe if host sleep v1 is supported for S0ix failure detection. */
 510        ret = cros_ec_get_host_command_version_mask(ec_dev,
 511                                                    EC_CMD_HOST_SLEEP_EVENT,
 512                                                    &ver_mask);
 513        ec_dev->host_sleep_v1 = (ret >= 0 && (ver_mask & EC_VER_MASK(1)));
 514
 515        /* Get host event wake mask. */
 516        ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
 517                                               &ec_dev->host_event_wake_mask);
 518        if (ret < 0) {
 519                /*
 520                 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
 521                 * use a reasonable default. Note that we ignore various
 522                 * battery, AC status, and power-state events, because (a)
 523                 * those can be quite common (e.g., when sitting at full
 524                 * charge, on AC) and (b) these are not actionable wake events;
 525                 * if anything, we'd like to continue suspending (to save
 526                 * power), not wake up.
 527                 */
 528                ec_dev->host_event_wake_mask = U32_MAX &
 529                        ~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
 530                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
 531                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
 532                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
 533                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
 534                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
 535                          EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
 536                /*
 537                 * Old ECs may not support this command. Complain about all
 538                 * other errors.
 539                 */
 540                if (ret != -EOPNOTSUPP)
 541                        dev_err(ec_dev->dev,
 542                                "failed to retrieve wake mask: %d\n", ret);
 543        }
 544
 545        ret = 0;
 546
 547exit:
 548        kfree(proto_msg);
 549        return ret;
 550}
 551EXPORT_SYMBOL(cros_ec_query_all);
 552
 553/**
 554 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
 555 * @ec_dev: EC device.
 556 * @msg: Message to write.
 557 *
 558 * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
 559 * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
 560 * successfully and the EC replied with success status.
 561 *
 562 * Return:
 563 * >=0 - The number of bytes transferred
 564 * <0 - Linux error code
 565 */
 566int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
 567                            struct cros_ec_command *msg)
 568{
 569        int ret, mapped;
 570
 571        mutex_lock(&ec_dev->lock);
 572        if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
 573                ret = cros_ec_query_all(ec_dev);
 574                if (ret) {
 575                        dev_err(ec_dev->dev,
 576                                "EC version unknown and query failed; aborting command\n");
 577                        mutex_unlock(&ec_dev->lock);
 578                        return ret;
 579                }
 580        }
 581
 582        if (msg->insize > ec_dev->max_response) {
 583                dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
 584                msg->insize = ec_dev->max_response;
 585        }
 586
 587        if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
 588                if (msg->outsize > ec_dev->max_request) {
 589                        dev_err(ec_dev->dev,
 590                                "request of size %u is too big (max: %u)\n",
 591                                msg->outsize,
 592                                ec_dev->max_request);
 593                        mutex_unlock(&ec_dev->lock);
 594                        return -EMSGSIZE;
 595                }
 596        } else {
 597                if (msg->outsize > ec_dev->max_passthru) {
 598                        dev_err(ec_dev->dev,
 599                                "passthru rq of size %u is too big (max: %u)\n",
 600                                msg->outsize,
 601                                ec_dev->max_passthru);
 602                        mutex_unlock(&ec_dev->lock);
 603                        return -EMSGSIZE;
 604                }
 605        }
 606
 607        ret = send_command(ec_dev, msg);
 608        mutex_unlock(&ec_dev->lock);
 609
 610        mapped = cros_ec_map_error(msg->result);
 611        if (mapped) {
 612                dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
 613                        msg->result, mapped);
 614                ret = mapped;
 615        }
 616
 617        return ret;
 618}
 619EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
 620
 621static int get_next_event_xfer(struct cros_ec_device *ec_dev,
 622                               struct cros_ec_command *msg,
 623                               struct ec_response_get_next_event_v1 *event,
 624                               int version, uint32_t size)
 625{
 626        int ret;
 627
 628        msg->version = version;
 629        msg->command = EC_CMD_GET_NEXT_EVENT;
 630        msg->insize = size;
 631        msg->outsize = 0;
 632
 633        ret = cros_ec_cmd_xfer_status(ec_dev, msg);
 634        if (ret > 0) {
 635                ec_dev->event_size = ret - 1;
 636                ec_dev->event_data = *event;
 637        }
 638
 639        return ret;
 640}
 641
 642static int get_next_event(struct cros_ec_device *ec_dev)
 643{
 644        struct {
 645                struct cros_ec_command msg;
 646                struct ec_response_get_next_event_v1 event;
 647        } __packed buf;
 648        struct cros_ec_command *msg = &buf.msg;
 649        struct ec_response_get_next_event_v1 *event = &buf.event;
 650        const int cmd_version = ec_dev->mkbp_event_supported - 1;
 651
 652        memset(msg, 0, sizeof(*msg));
 653        if (ec_dev->suspended) {
 654                dev_dbg(ec_dev->dev, "Device suspended.\n");
 655                return -EHOSTDOWN;
 656        }
 657
 658        if (cmd_version == 0)
 659                return get_next_event_xfer(ec_dev, msg, event, 0,
 660                                  sizeof(struct ec_response_get_next_event));
 661
 662        return get_next_event_xfer(ec_dev, msg, event, cmd_version,
 663                                sizeof(struct ec_response_get_next_event_v1));
 664}
 665
 666static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
 667{
 668        u8 buffer[sizeof(struct cros_ec_command) +
 669                  sizeof(ec_dev->event_data.data)];
 670        struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
 671
 672        msg->version = 0;
 673        msg->command = EC_CMD_MKBP_STATE;
 674        msg->insize = sizeof(ec_dev->event_data.data);
 675        msg->outsize = 0;
 676
 677        ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
 678        ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
 679        memcpy(&ec_dev->event_data.data, msg->data,
 680               sizeof(ec_dev->event_data.data));
 681
 682        return ec_dev->event_size;
 683}
 684
 685/**
 686 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
 687 * @ec_dev: Device to fetch event from.
 688 * @wake_event: Pointer to a bool set to true upon return if the event might be
 689 *              treated as a wake event. Ignored if null.
 690 * @has_more_events: Pointer to bool set to true if more than one event is
 691 *              pending.
 692 *              Some EC will set this flag to indicate cros_ec_get_next_event()
 693 *              can be called multiple times in a row.
 694 *              It is an optimization to prevent issuing a EC command for
 695 *              nothing or wait for another interrupt from the EC to process
 696 *              the next message.
 697 *              Ignored if null.
 698 *
 699 * Return: negative error code on errors; 0 for no data; or else number of
 700 * bytes received (i.e., an event was retrieved successfully). Event types are
 701 * written out to @ec_dev->event_data.event_type on success.
 702 */
 703int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
 704                           bool *wake_event,
 705                           bool *has_more_events)
 706{
 707        u8 event_type;
 708        u32 host_event;
 709        int ret;
 710
 711        /*
 712         * Default value for wake_event.
 713         * Wake up on keyboard event, wake up for spurious interrupt or link
 714         * error to the EC.
 715         */
 716        if (wake_event)
 717                *wake_event = true;
 718
 719        /*
 720         * Default value for has_more_events.
 721         * EC will raise another interrupt if AP does not process all events
 722         * anyway.
 723         */
 724        if (has_more_events)
 725                *has_more_events = false;
 726
 727        if (!ec_dev->mkbp_event_supported)
 728                return get_keyboard_state_event(ec_dev);
 729
 730        ret = get_next_event(ec_dev);
 731        if (ret <= 0)
 732                return ret;
 733
 734        if (has_more_events)
 735                *has_more_events = ec_dev->event_data.event_type &
 736                        EC_MKBP_HAS_MORE_EVENTS;
 737        ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
 738
 739        if (wake_event) {
 740                event_type = ec_dev->event_data.event_type;
 741                host_event = cros_ec_get_host_event(ec_dev);
 742
 743                /*
 744                 * Sensor events need to be parsed by the sensor sub-device.
 745                 * Defer them, and don't report the wakeup here.
 746                 */
 747                if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
 748                        *wake_event = false;
 749                } else if (host_event) {
 750                        /* rtc_update_irq() already handles wakeup events. */
 751                        if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
 752                                *wake_event = false;
 753                        /* Masked host-events should not count as wake events. */
 754                        if (!(host_event & ec_dev->host_event_wake_mask))
 755                                *wake_event = false;
 756                }
 757        }
 758
 759        return ret;
 760}
 761EXPORT_SYMBOL(cros_ec_get_next_event);
 762
 763/**
 764 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
 765 * @ec_dev: Device to fetch event from.
 766 *
 767 * When MKBP is supported, when the EC raises an interrupt, we collect the
 768 * events raised and call the functions in the ec notifier. This function
 769 * is a helper to know which events are raised.
 770 *
 771 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
 772 */
 773u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
 774{
 775        u32 host_event;
 776
 777        BUG_ON(!ec_dev->mkbp_event_supported);
 778
 779        if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
 780                return 0;
 781
 782        if (ec_dev->event_size != sizeof(host_event)) {
 783                dev_warn(ec_dev->dev, "Invalid host event size\n");
 784                return 0;
 785        }
 786
 787        host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
 788
 789        return host_event;
 790}
 791EXPORT_SYMBOL(cros_ec_get_host_event);
 792
 793/**
 794 * cros_ec_check_features() - Test for the presence of EC features
 795 *
 796 * @ec: EC device, does not have to be connected directly to the AP,
 797 *      can be daisy chained through another device.
 798 * @feature: One of ec_feature_code bit.
 799 *
 800 * Call this function to test whether the ChromeOS EC supports a feature.
 801 *
 802 * Return: 1 if supported, 0 if not
 803 */
 804int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
 805{
 806        struct cros_ec_command *msg;
 807        int ret;
 808
 809        if (ec->features[0] == -1U && ec->features[1] == -1U) {
 810                /* features bitmap not read yet */
 811                msg = kzalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
 812                if (!msg)
 813                        return -ENOMEM;
 814
 815                msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
 816                msg->insize = sizeof(ec->features);
 817
 818                ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
 819                if (ret < 0) {
 820                        dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
 821                                 ret, msg->result);
 822                        memset(ec->features, 0, sizeof(ec->features));
 823                } else {
 824                        memcpy(ec->features, msg->data, sizeof(ec->features));
 825                }
 826
 827                dev_dbg(ec->dev, "EC features %08x %08x\n",
 828                        ec->features[0], ec->features[1]);
 829
 830                kfree(msg);
 831        }
 832
 833        return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
 834}
 835EXPORT_SYMBOL_GPL(cros_ec_check_features);
 836
 837/**
 838 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
 839 *
 840 * @ec: EC device, does not have to be connected directly to the AP,
 841 *      can be daisy chained through another device.
 842 * Return: < 0 in case of error.
 843 */
 844int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
 845{
 846        /*
 847         * Issue a command to get the number of sensor reported.
 848         * If not supported, check for legacy mode.
 849         */
 850        int ret, sensor_count;
 851        struct ec_params_motion_sense *params;
 852        struct ec_response_motion_sense *resp;
 853        struct cros_ec_command *msg;
 854        struct cros_ec_device *ec_dev = ec->ec_dev;
 855        u8 status;
 856
 857        msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
 858                      GFP_KERNEL);
 859        if (!msg)
 860                return -ENOMEM;
 861
 862        msg->version = 1;
 863        msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
 864        msg->outsize = sizeof(*params);
 865        msg->insize = sizeof(*resp);
 866
 867        params = (struct ec_params_motion_sense *)msg->data;
 868        params->cmd = MOTIONSENSE_CMD_DUMP;
 869
 870        ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
 871        if (ret < 0) {
 872                sensor_count = ret;
 873        } else {
 874                resp = (struct ec_response_motion_sense *)msg->data;
 875                sensor_count = resp->dump.sensor_count;
 876        }
 877        kfree(msg);
 878
 879        /*
 880         * Check legacy mode: Let's find out if sensors are accessible
 881         * via LPC interface.
 882         */
 883        if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
 884                ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
 885                                1, &status);
 886                if (ret >= 0 &&
 887                    (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
 888                        /*
 889                         * We have 2 sensors, one in the lid, one in the base.
 890                         */
 891                        sensor_count = 2;
 892                } else {
 893                        /*
 894                         * EC uses LPC interface and no sensors are presented.
 895                         */
 896                        sensor_count = 0;
 897                }
 898        }
 899        return sensor_count;
 900}
 901EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
 902