[alsa-devel] [PATCH v2 00/17] platform/chrome: Replace cros_ec_cmd_xfer_status

Many callers of cros_ec_cmd_xfer_status() use similar setup and cleanup code, including setting up the cros_ec_command message struct and copying the received buffer.
This series introduces a replacement function cros_ec_cmd() that performs this setup and teardown, and then updates all call sites that used xfer_status() to use the new function instead.
The final patch in the series drops cros_ec_cmd_xfer_status() altogether.
Changes in v2: - Renamed new function to cros_ec_cmd() - Added a "result" pointer argument, for call-sites that are interested in the EC command result. - Updated subsequent patches to new function name and parameter list. - Used C99 element setting to initialize some structs directly rather than zeroing them, then setting them.
v1: https://lkml.org/lkml/2020/1/30/802
Prashant Malani (17): platform/chrome: Add EC command msg wrapper platform/chrome: chardev: Use cros_ec_cmd() platform/chrome: proto: Use cros_ec_cmd() platform/chrome: usbpd_logger: Use cros_ec_cmd() platform/chrome: sensorhub: Use cros_ec_cmd() platform/chrome: debugfs: Use cros_ec_cmd() platform/chrome: sysfs: Use cros_ec_cmd() extcon: cros_ec: Use cros_ec_cmd() hid: google-hammer: Use cros_ec_cmd() iio: cros_ec: Use cros_ec_cmd() ASoC: cros_ec_codec: Use cros_ec_cmd() power: supply: cros: Use cros_ec_cmd() pwm: cros-ec: Remove cros_ec_cmd_xfer_status() rtc: cros-ec: Use cros_ec_cmd() media: cros-ec-cec: Use cros_ec_cmd() i2c: cros-ec-tunnel: Use cros_ec_cmd() platform/chrome: Drop cros_ec_cmd_xfer_status()
drivers/extcon/extcon-usbc-cros-ec.c | 61 ++------ drivers/hid/hid-google-hammer.c | 23 +-- drivers/i2c/busses/i2c-cros-ec-tunnel.c | 22 ++- .../cros_ec_sensors/cros_ec_sensors_core.c | 25 ++-- .../media/platform/cros-ec-cec/cros-ec-cec.c | 45 +++--- drivers/platform/chrome/cros_ec_chardev.c | 17 +-- drivers/platform/chrome/cros_ec_debugfs.c | 131 ++++++------------ drivers/platform/chrome/cros_ec_proto.c | 76 ++++++---- drivers/platform/chrome/cros_ec_sensorhub.c | 30 ++-- drivers/platform/chrome/cros_ec_sysfs.c | 103 ++++++-------- drivers/platform/chrome/cros_usbpd_logger.c | 12 +- drivers/power/supply/cros_usbpd-charger.c | 58 ++------ drivers/pwm/pwm-cros-ec.c | 59 +++----- drivers/rtc/rtc-cros-ec.c | 27 ++-- include/linux/platform_data/cros_ec_proto.h | 5 +- sound/soc/codecs/cros_ec_codec.c | 119 ++++++---------- 16 files changed, 277 insertions(+), 536 deletions(-)

Many callers of cros_ec_cmd_xfer_status() use a similar set up of allocating and filling a message buffer and then copying any received data to a target buffer.
Create a utility function cros_ec_cmd() that performs this setup so that callers can use this function instead. Subsequent patches will convert callers of cros_ec_cmd_xfer_status() to the new function instead.
Signed-off-by: Prashant Malani pmalani@chromium.org ---
Changes in v2: - Renamed function to cros_ec_cmd() - Added result pointer parameter. - Removed references to cros_ec_cmd_xfer() or cros_ec_cmd_xfer_status() from documentation.
drivers/platform/chrome/cros_ec_proto.c | 58 +++++++++++++++++++++ include/linux/platform_data/cros_ec_proto.h | 4 ++ 2 files changed, 62 insertions(+)
diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c index 3cfa643f1d073d..b3d5368f596813 100644 --- a/drivers/platform/chrome/cros_ec_proto.c +++ b/drivers/platform/chrome/cros_ec_proto.c @@ -572,6 +572,64 @@ int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev, } EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
+/** + * cros_ec_cmd() - Utility function to send commands to ChromeOS EC. + * @ec: EC device struct. + * @version: Command version number (often 0). + * @command: Command ID including offset. + * @outdata: Data to be sent to the EC. + * @outsize: Size of the &outdata buffer. + * @indata: Data to be received from the EC. + * @insize: Size of the &indata buffer. + * @result: Result of the transfer command. + * + * This function sends a command to the EC and also performs some of the common + * setup involved in doing so. This includes allocating and filling up a + * &struct cros_ec_command message buffer, and copying the received data to + * another buffer. + * + * Return: The number of bytes transferred on success or negative error code. + */ +int cros_ec_cmd(struct cros_ec_device *ec, u32 version, u32 command, + void *outdata, u32 outsize, void *indata, + u32 insize, u32 *result) +{ + struct cros_ec_command *msg; + int ret; + + msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL); + if (!msg) + return -ENOMEM; + + msg->version = version; + msg->command = command; + msg->outsize = outsize; + msg->insize = insize; + + if (outdata && outsize > 0) + memcpy(msg->data, outdata, outsize); + + ret = cros_ec_cmd_xfer(ec, msg); + if (result) + *result = msg->result; + if (ret < 0) { + dev_err(ec->dev, "Command xfer error (err:%d)\n", ret); + goto cleanup; + } else if (msg->result != EC_RES_SUCCESS) { + dev_dbg(ec->dev, "Command result (err: %d)\n", msg->result); + ret = -EPROTO; + goto cleanup; + } + + if (insize) + memcpy(indata, msg->data, insize); + +cleanup: + kfree(msg); + return ret; +} +EXPORT_SYMBOL(cros_ec_cmd); + static int get_next_event_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg, struct ec_response_get_next_event_v1 *event, diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h index ba591477019180..54b9bbf9a07c0c 100644 --- a/include/linux/platform_data/cros_ec_proto.h +++ b/include/linux/platform_data/cros_ec_proto.h @@ -218,6 +218,10 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
+int cros_ec_cmd(struct cros_ec_device *ec_dev, u32 version, u32 command, + void *outdata, u32 outsize, void *indata, u32 insize, + u32 *result); + int cros_ec_query_all(struct cros_ec_device *ec_dev);
int cros_ec_get_next_event(struct cros_ec_device *ec_dev,

Replace send_ec_host_command() with cros_ec_cmd() which does the same thing, but is defined in a common location in platform/chrome and exposed for other modules to use. This allows us to remove send_ec_host_command() entirely.
Signed-off-by: Prashant Malani pmalani@chromium.org ---
Changes in v2: - Updated to use new function name and parameter list.
sound/soc/codecs/cros_ec_codec.c | 119 +++++++++++-------------------- 1 file changed, 42 insertions(+), 77 deletions(-)
diff --git a/sound/soc/codecs/cros_ec_codec.c b/sound/soc/codecs/cros_ec_codec.c index 6a24f570c5e86f..8516ba5f7624f8 100644 --- a/sound/soc/codecs/cros_ec_codec.c +++ b/sound/soc/codecs/cros_ec_codec.c @@ -69,38 +69,6 @@ static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap) return priv->ec_capabilities & BIT(cap); }
-static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd, - uint8_t *out, size_t outsize, - uint8_t *in, size_t insize) -{ - int ret; - struct cros_ec_command *msg; - - msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL); - if (!msg) - return -ENOMEM; - - msg->version = 0; - msg->command = cmd; - msg->outsize = outsize; - msg->insize = insize; - - if (outsize) - memcpy(msg->data, out, outsize); - - ret = cros_ec_cmd_xfer_status(ec_dev, msg); - if (ret < 0) - goto error; - - if (insize) - memcpy(in, msg->data, insize); - - ret = 0; -error: - kfree(msg); - return ret; -} - static int calculate_sha256(struct cros_ec_codec_priv *priv, uint8_t *buf, uint32_t size, uint8_t *digest) { @@ -149,18 +117,18 @@ static int dmic_get_gain(struct snd_kcontrol *kcontrol,
p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX; p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_DMIC, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, sizeof(r), + NULL); if (ret < 0) return ret; ucontrol->value.integer.value[0] = r.gain;
p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX; p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_DMIC, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, sizeof(r), + NULL); if (ret < 0) return ret; ucontrol->value.integer.value[1] = r.gain; @@ -191,16 +159,16 @@ static int dmic_put_gain(struct snd_kcontrol *kcontrol, p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX; p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0; p.set_gain_idx_param.gain = left; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, - (uint8_t *)&p, sizeof(p), NULL, 0); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_DMIC, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); if (ret < 0) return ret;
p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX; p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1; p.set_gain_idx_param.gain = right; - return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, - (uint8_t *)&p, sizeof(p), NULL, 0); + return cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_DMIC, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); }
static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0); @@ -231,9 +199,9 @@ static int dmic_probe(struct snd_soc_component *component)
p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;
- ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_DMIC, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, sizeof(r), + NULL); if (ret < 0) { dev_warn(dev, "get_max_gain() unsupported\n"); return 0; @@ -279,8 +247,8 @@ static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH; p.set_sample_depth_param.depth = depth; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, - (uint8_t *)&p, sizeof(p), NULL, 0); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_I2S_RX, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); if (ret < 0) return ret;
@@ -289,8 +257,8 @@ static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
p.cmd = EC_CODEC_I2S_RX_SET_BCLK; p.set_bclk_param.bclk = snd_soc_params_to_bclk(params); - return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, - (uint8_t *)&p, sizeof(p), NULL, 0); + return cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_I2S_RX, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); }
static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) @@ -333,8 +301,8 @@ static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT; p.set_daifmt_param.daifmt = daifmt; - return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, - (uint8_t *)&p, sizeof(p), NULL, 0); + return cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_I2S_RX, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); }
static const struct snd_soc_dai_ops i2s_rx_dai_ops = { @@ -364,8 +332,8 @@ static int i2s_rx_event(struct snd_soc_dapm_widget *w, return 0; }
- return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, - (uint8_t *)&p, sizeof(p), NULL, 0); + return cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_I2S_RX, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); }
static struct snd_soc_dapm_widget i2s_rx_dapm_widgets[] = { @@ -415,9 +383,8 @@ static void *wov_map_shm(struct cros_ec_codec_priv *priv,
p.cmd = EC_CODEC_GET_SHM_ADDR; p.get_shm_addr_param.shm_id = shm_id; - if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)) < 0) { + if (cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC, (uint8_t *)&p, + sizeof(p), (uint8_t *)&r, sizeof(r), NULL) < 0) { dev_err(priv->dev, "failed to EC_CODEC_GET_SHM_ADDR\n"); return NULL; } @@ -453,9 +420,8 @@ static void *wov_map_shm(struct cros_ec_codec_priv *priv, p.set_shm_addr_param.phys_addr = priv->ap_shm_last_alloc; p.set_shm_addr_param.len = req; p.set_shm_addr_param.shm_id = shm_id; - if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, - (uint8_t *)&p, sizeof(p), - NULL, 0) < 0) { + if (cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL) < 0) { dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n"); return NULL; } @@ -571,9 +537,9 @@ static int wov_read_audio_shm(struct cros_ec_codec_priv *priv) int ret;
p.cmd = EC_CODEC_WOV_READ_AUDIO_SHM; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, sizeof(r), + NULL); if (ret) { dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n"); return ret; @@ -596,9 +562,9 @@ static int wov_read_audio(struct cros_ec_codec_priv *priv)
while (remain >= 0) { p.cmd = EC_CODEC_WOV_READ_AUDIO; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, + sizeof(r), NULL); if (ret) { dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO\n"); @@ -669,8 +635,8 @@ static int wov_enable_put(struct snd_kcontrol *kcontrol, else p.cmd = EC_CODEC_WOV_DISABLE;
- ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), NULL, 0); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); if (ret) { dev_err(priv->dev, "failed to %s wov\n", enabled ? "enable" : "disable"); @@ -716,8 +682,8 @@ static int wov_set_lang_shm(struct cros_ec_codec_priv *priv, p.cmd = EC_CODEC_WOV_SET_LANG_SHM; memcpy(pp->hash, digest, SHA256_DIGEST_SIZE); pp->total_len = size; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), NULL, 0); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); if (ret) { dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n"); return ret; @@ -743,8 +709,8 @@ static int wov_set_lang(struct cros_ec_codec_priv *priv, pp->offset = i; memcpy(pp->buf, buf + i, req); pp->len = req; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), NULL, 0); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), NULL, 0, NULL); if (ret) { dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n"); return ret; @@ -782,9 +748,9 @@ static int wov_hotword_model_put(struct snd_kcontrol *kcontrol, goto leave;
p.cmd = EC_CODEC_WOV_GET_LANG; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC_WOV, + (uint8_t *)&p, sizeof(p), (uint8_t *)&r, sizeof(r), + NULL); if (ret) goto leave;
@@ -1020,9 +986,8 @@ static int cros_ec_codec_platform_probe(struct platform_device *pdev) atomic_set(&priv->dmic_probed, 0);
p.cmd = EC_CODEC_GET_CAPABILITIES; - ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, - (uint8_t *)&p, sizeof(p), - (uint8_t *)&r, sizeof(r)); + ret = cros_ec_cmd(priv->ec_device, 0, EC_CMD_EC_CODEC, (uint8_t *)&p, + sizeof(p), (uint8_t *)&r, sizeof(r), NULL); if (ret) { dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n"); return ret;

On Wed, Feb 05, 2020 at 11:00:16AM -0800, Prashant Malani wrote:
Replace send_ec_host_command() with cros_ec_cmd() which does the same thing, but is defined in a common location in platform/chrome and exposed for other modules to use. This allows us to remove send_ec_host_command() entirely.
Acked-by: Mark Brown broonie@kernel.org
participants (2)
-
Mark Brown
-
Prashant Malani