static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev, int devidx, struct cros_ec_command *msg) { /* * Try using v3+ to query for supported protocols. If this * command fails, fall back to v2. Returns the highest protocol * supported by the EC. * Also sets the max request/response/passthru size. */ int ret; if (!ec_dev->pkt_xfer) return -EPROTONOSUPPORT; memset(msg, 0, sizeof(*msg)); msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO; msg->insize = sizeof(struct ec_response_get_protocol_info); ret = send_command(ec_dev, msg); if (ret < 0) { dev_dbg(ec_dev->dev, "failed to check for EC[%d] protocol version: %d\n", devidx, ret); return ret; } if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND) return -ENODEV; else if (msg->result != EC_RES_SUCCESS) return msg->result; return 0; }
int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg) { int ret; mutex_lock(&ec_dev->lock); if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) { ret = cros_ec_query_all(ec_dev); if (ret) { dev_err(ec_dev->dev, "EC version unknown and query failed; aborting command\n"); mutex_unlock(&ec_dev->lock); return ret; } } if (msg->insize > ec_dev->max_response) { dev_dbg(ec_dev->dev, "clamping message receive buffer\n"); msg->insize = ec_dev->max_response; } if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) { if (msg->outsize > ec_dev->max_request) { dev_err(ec_dev->dev, "request of size %u is too big (max: %u)\n", msg->outsize, ec_dev->max_request); mutex_unlock(&ec_dev->lock); return -EMSGSIZE; } } else { if (msg->outsize > ec_dev->max_passthru) { dev_err(ec_dev->dev, "passthru rq of size %u is too big (max: %u)\n", msg->outsize, ec_dev->max_passthru); mutex_unlock(&ec_dev->lock); return -EMSGSIZE; } } ret = send_command(ec_dev, msg); mutex_unlock(&ec_dev->lock); return ret; }
static int google_chromeec_command_v3(struct chromeec_command *cec_command) { struct ec_host_request rq; struct ec_host_response rs; const u8 *d; u8 csum = 0; int i; if (cec_command->cmd_size_in + sizeof(rq) > EC_LPC_HOST_PACKET_SIZE) { printk(BIOS_ERR, "EC cannot send %zu bytes\n", cec_command->cmd_size_in + sizeof(rq)); return -1; } if (cec_command->cmd_size_out > EC_LPC_HOST_PACKET_SIZE) { printk(BIOS_ERR, "EC cannot receive %d bytes\n", cec_command->cmd_size_out); return -1; } if (google_chromeec_wait_ready(EC_LPC_ADDR_HOST_CMD)) { printk(BIOS_ERR, "Timeout waiting for EC start command %d!\n", cec_command->cmd_code); return -1; } /* Fill in request packet */ rq.struct_version = EC_HOST_REQUEST_VERSION; rq.checksum = 0; rq.command = cec_command->cmd_code | EC_CMD_PASSTHRU_OFFSET(cec_command->cmd_dev_index); rq.command_version = cec_command->cmd_version; rq.reserved = 0; rq.data_len = cec_command->cmd_size_in; /* Copy data and start checksum */ write_bytes(EC_LPC_ADDR_HOST_PACKET + sizeof(rq), cec_command->cmd_size_in, (u8*)cec_command->cmd_data_in, &csum); /* Finish checksum */ for (i = 0, d = (const u8 *)&rq; i < sizeof(rq); i++, d++) csum += *d; /* Write checksum field so the entire packet sums to 0 */ rq.checksum = -csum; /* Copy header */ write_bytes(EC_LPC_ADDR_HOST_PACKET, sizeof(rq), (u8*)&rq, NULL); /* Start the command */ write_byte(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD); if (google_chromeec_wait_ready(EC_LPC_ADDR_HOST_CMD)) { printk(BIOS_ERR, "Timeout waiting for EC process command %d!\n", cec_command->cmd_code); return -1; } /* Check result */ cec_command->cmd_code = read_byte(EC_LPC_ADDR_HOST_DATA); if (cec_command->cmd_code) { printk(BIOS_ERR, "EC returned error result code %d\n", cec_command->cmd_code); return -i; } /* Read back response header and start checksum */ csum = 0; read_bytes(EC_LPC_ADDR_HOST_PACKET, sizeof(rs), (u8*)&rs, &csum); if (rs.struct_version != EC_HOST_RESPONSE_VERSION) { printk(BIOS_ERR, "EC response version mismatch (%d != %d)\n", rs.struct_version, EC_HOST_RESPONSE_VERSION); return -1; } if (rs.reserved) { printk(BIOS_ERR, "EC response reserved is %d, should be 0\n", rs.reserved); return -1; } if (rs.data_len > cec_command->cmd_size_out) { printk(BIOS_ERR, "EC returned too much data (%d > %d)\n", rs.data_len, cec_command->cmd_size_out); return -1; } /* Read back data and update checksum */ read_bytes(EC_LPC_ADDR_HOST_PACKET + sizeof(rs), rs.data_len, cec_command->cmd_data_out, &csum); /* Verify checksum */ if (csum) { printk(BIOS_ERR, "EC response has invalid checksum\n"); return -1; } return 0; }
* battery charging and regulator control, firmware update. */ #include <linux/of_platform.h> #include <linux/interrupt.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/mfd/core.h> #include <linux/mfd/cros_ec.h> #define CROS_EC_DEV_EC_INDEX 0 #define CROS_EC_DEV_PD_INDEX 1 static struct cros_ec_platform ec_p = { .ec_name = CROS_EC_DEV_NAME, .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX), }; static struct cros_ec_platform pd_p = { .ec_name = CROS_EC_DEV_PD_NAME, .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX), }; static const struct mfd_cell ec_cell = { .name = "cros-ec-ctl", .platform_data = &ec_p, .pdata_size = sizeof(ec_p), }; static const struct mfd_cell ec_pd_cell = { .name = "cros-ec-ctl",