platform/chrome: Add cros_ec_readmem() helpers for I2C/SPI based ECs
PATCH v3 1/1 platform/chrome: Add cros_ec_readmem() helpers for I2C/SPI based ECs
Add cros_ec_readmem() helpers for I2C/SPI based ECs. Unlike the LPC based ECs the I2C/SPI based ones cannot just directly read the mapped region, but have to resort to EC_CMD_READ_MEMMAP.
This is useful for things like accessing fan speeds or temperatures.
---
Hi all,
This can be used for a hwmon or thermal driver such as [1], but also alone can be used with 'ectool' via the ioctl() interface.
Thanks,
Moritz
Changes from v2:
- Addressed KBuild complaint about max() with size_t vs unsigned int parameters
Changes from v1:
- Moved to drivers/platform/proto
[1] https://patchwork.kernel.org/patch/9670517/ (needs work)
--- drivers/platform/chrome/cros_ec_i2c.c | 1 + drivers/platform/chrome/cros_ec_proto.c | 39 +++++++++++++++++++++++++ drivers/platform/chrome/cros_ec_spi.c | 1 + include/linux/mfd/cros_ec.h | 12 ++++++++ 4 files changed, 53 insertions(+)
drivers/platform/chrome/cros_ec_i2c.c +1 -0
@@ -303,6 +303,7 @@ static int cros_ec_i2c_probe(struct i2c_client *client, ec_dev->irq = client->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c; ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c; + ec_dev->cmd_readmem = cros_ec_readmem; ec_dev->phys_name = client->adapter->name; ec_dev->din_size =sizeof (struct ec_host_response_i2c ) +sizeof (struct ec_response_get_protocol_info );
drivers/platform/chrome/cros_ec_proto.c +39 -0
@@ -636,3 +636,42 @@ u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)return host_event; }EXPORT_SYMBOL (cros_ec_get_host_event); + +int cros_ec_readmem (struct cros_ec_device *ec,unsigned int offset, +unsigned int bytes,void *dest) +{ +struct ec_params_read_memmap *params; +struct cros_ec_command *msg; +int ret; + +if (offset >=EC_MEMMAP_SIZE - bytes) +return -EINVAL ; + + msg =kzalloc (sizeof (*msg) +max_t (size_t,sizeof (*params), bytes), +GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg->version =0 ; + msg->command =EC_CMD_READ_MEMMAP ; + msg->insize = bytes; + msg->outsize =sizeof (*params); + + params = (struct ec_params_read_memmap *)msg->data; + params->offset = offset; + params->size = bytes; + + ret =cros_ec_cmd_xfer_status (ec, msg); +if (ret <0 ) { +dev_warn (ec->dev,"cannot read mapped reg: %d/%d\n" , + ret, msg->result); + goto out_free; + } + +memcpy (dest, msg->data, bytes); + +out_free: +kfree (msg); +return ret; +} +EXPORT_SYMBOL_GPL (cros_ec_readmem);
drivers/platform/chrome/cros_ec_spi.c +1 -0
@@ -666,6 +666,7 @@ static int cros_ec_spi_probe(struct spi_device *spi) ec_dev->irq = spi->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; + ec_dev->cmd_readmem = cros_ec_readmem; ec_dev->phys_name =dev_name (&ec_spi->spi->dev); ec_dev->din_size =EC_MSG_PREAMBLE_COUNT + sizeof(struct ec_host_response) +
include/linux/mfd/cros_ec.h +12 -0
@@ -335,6 +335,18 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);*/ u32 cros_ec_get_host_event (struct cros_ec_device *ec_dev); +/** +* cros_ec_readmem - Read mapped memory in the ChromeOS EC +* +* @ec: Device to read from +* @offset: Offset to read within the mapped region +* @bytes: number of bytes to read +* @data: Return data +* @return: 0 if Ok, -ve on error +*/ +int cros_ec_readmem (struct cros_ec_device *ec,unsigned int offset, +unsigned int bytes,void *dest); +/* sysfs stuff */ extern struct attribute_group cros_ec_attr_group;extern struct attribute_group cros_ec_lightbar_attr_group;
Signature · 1 line
2.20.1
Hi Moritz,
Missatge de Moritz Fischer <[email protected]> del dia dj., 10 de gen. 2019 a les 5:40:
Quoted 7 lines
Add
cros_ec_readmem()helpers for I2C/SPI based ECs. Unlike the LPC based ECs the I2C/SPI based ones cannot just directly read the mapped region, but have to resort toEC_CMD_READ_MEMMAP.This is useful for things like accessing fan speeds or temperatures.
Quoted 8 lines
Signed-off-by Moritz Fischer <[email protected]>---
Hi all,
This can be used for a hwmon or thermal driver such as [1], but also alone can be used with 'ectool' via the
ioctl()interface.
ooi, did you try this with a Chromebook? Or only with your device?
I was a bit worried on the side effects to expose this command to userspace, so I did a quick test, and to be honest I didn't like the result. Issuing the ioctl resulted on a reboot on the Chromebook I tested, which is not good. I didn't investigate more but I can say that this change, at least, will need some rework. I'll try to investigate a bit more on what is happening.
Thanks,
Enric
Quoted 117 lines
Thanks,
MoritzChanges from v2:
- Addressed KBuild complaint aboutmax()withsize_tvs unsigned int parametersChanges from v1:
- Moved todrivers/platform/proto[1] https://patchwork.kernel.org/patch/9670517/ (needs work)
--- drivers/platform/chrome/cros_ec_i2c.c | 1 + drivers/platform/chrome/cros_ec_proto.c | 39 +++++++++++++++++++++++++ drivers/platform/chrome/cros_ec_spi.c | 1 + include/linux/mfd/cros_ec.h | 12 ++++++++ 4 files changed, 53 insertions(+)drivers/platform/chrome/cros_ec_i2c.c +1
-0@@ -303,6 +303,7 @@ static int cros_ec_i2c_probe(struct i2c_client *client, ec_dev->irq = client->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c; ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c; + ec_dev->cmd_readmem = cros_ec_readmem; ec_dev->phys_name = client->adapter->name; ec_dev->din_size =sizeof (struct ec_host_response_i2c ) +sizeof (struct ec_response_get_protocol_info );drivers/platform/chrome/cros_ec_proto.c +39
-0@@ -636,3 +636,42 @@ u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)return host_event; }EXPORT_SYMBOL (cros_ec_get_host_event); + +int cros_ec_readmem (struct cros_ec_device *ec,unsigned int offset, +unsigned int bytes,void *dest) +{ +struct ec_params_read_memmap *params; +struct cros_ec_command *msg; +int ret; + +if (offset >=EC_MEMMAP_SIZE - bytes) +return -EINVAL ; + + msg =kzalloc (sizeof (*msg) +max_t (size_t,sizeof (*params), bytes), +GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + msg->version =0 ; + msg->command =EC_CMD_READ_MEMMAP ; + msg->insize = bytes; + msg->outsize =sizeof (*params); + + params = (struct ec_params_read_memmap *)msg->data; + params->offset = offset; + params->size = bytes; + + ret =cros_ec_cmd_xfer_status (ec, msg); +if (ret <0 ) { +dev_warn (ec->dev,"cannot read mapped reg: %d/%d\n" , + ret, msg->result); + goto out_free; + } + +memcpy (dest, msg->data, bytes); + +out_free: +kfree (msg); +return ret; +} +EXPORT_SYMBOL_GPL (cros_ec_readmem);drivers/platform/chrome/cros_ec_spi.c +1
-0@@ -666,6 +666,7 @@ static int cros_ec_spi_probe(struct spi_device *spi) ec_dev->irq = spi->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; + ec_dev->cmd_readmem = cros_ec_readmem; ec_dev->phys_name =dev_name (&ec_spi->spi->dev); ec_dev->din_size =EC_MSG_PREAMBLE_COUNT + sizeof(struct ec_host_response) +include/linux/mfd/cros_ec.h +12
-1@@ -335,6 +335,18 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);*/ u32 cros_ec_get_host_event (struct cros_ec_device *ec_dev); +/** +* cros_ec_readmem - Read mapped memory in the ChromeOS EC +* +* @ec: Device to read from +* @offset: Offset to read within the mapped region +* @bytes: number of bytes to read +* @data: Return data +* @return: 0 if Ok, -ve on error +*/ +int cros_ec_readmem (struct cros_ec_device *ec,unsigned int offset, +unsigned int bytes,void *dest); +/* sysfs stuff */ extern struct attribute_group cros_ec_attr_group;extern struct attribute_group cros_ec_lightbar_attr_group; --2.20.1
Hi Enrico,
Quoted 14 lines · Enric Balletbo Serra
Hi Moritz,
Missatge de Moritz Fischer <[email protected]> del dia dj., 10 de gen. 2019 a les 5:40:
Quoted 10 lines
Add
cros_ec_readmem()helpers for I2C/SPI based ECs. Unlike the LPC based ECs the I2C/SPI based ones cannot just directly read the mapped region, but have to resort toEC_CMD_READ_MEMMAP.This is useful for things like accessing fan speeds or temperatures.
Signed-off-by Moritz Fischer <[email protected]>---
Quoted 7 lines
Quoted 5 lines
Hi all,
This can be used for a hwmon or thermal driver such as [1], but also alone can be used with 'ectool' via the
ioctl()interface.ooi, did you try this with a Chromebook? Or only with your device?
Nope, mine has a dead battery (and no fans ... :-/)
Quoted 7 lines
I was a bit worried on the side effects to expose this command to userspace, so I did a quick test, and to be honest I didn't like the result. Issuing the ioctl resulted on a reboot on the Chromebook I tested, which is not good. I didn't investigate more but I can say that this change, at least, will need some rework. I'll try to investigate a bit more on what is happening.
That sounds bad :-/
What chromebook did you run this on (so I can look at the EC code it is running and figure out what's the difference). Maybe it has a different configuration. It wasn't an x86 chromebook, right?
What was the ioctl you ran?
Thanks for testing,
Moritz
Hi Moritz,
Missatge de Moritz Fischer <[email protected]> del dia dl., 21 de gen. 2019 a les 19:40:
Quoted 35 lines
Hi Enrico,
Quoted 14 lines · Enric Balletbo Serra
Hi Moritz,
Missatge de Moritz Fischer <[email protected]> del dia dj., 10 de gen. 2019 a les 5:40:
Quoted 10 lines
Add
cros_ec_readmem()helpers for I2C/SPI based ECs. Unlike the LPC based ECs the I2C/SPI based ones cannot just directly read the mapped region, but have to resort toEC_CMD_READ_MEMMAP.This is useful for things like accessing fan speeds or temperatures.
Signed-off-by Moritz Fischer <[email protected]>---
Quoted 7 lines
Quoted 5 lines
Hi all,
This can be used for a hwmon or thermal driver such as [1], but also alone can be used with 'ectool' via the
ioctl()interface.ooi, did you try this with a Chromebook? Or only with your device?
Nope, mine has a dead battery (and no fans ... :-/)
Quoted 7 lines
I was a bit worried on the side effects to expose this command to userspace, so I did a quick test, and to be honest I didn't like the result. Issuing the ioctl resulted on a reboot on the Chromebook I tested, which is not good. I didn't investigate more but I can say that this change, at least, will need some rework. I'll try to investigate a bit more on what is happening.
Quoted 6 lines
That sounds bad :-/
What chromebook did you run this on (so I can look at the EC code it is running and figure out what's the difference). Maybe it has a different configuration. It wasn't an x86 chromebook, right?
I tried with a Samsung Chromebook Plus (arm64 Chromebook)
Quoted 2 lines
What was the ioctl you ran?
The code that I ran is this: https://hastebin.com/cupecuvowi.py
No guarantee :) I'll try to look deeper tomorrow.
Thanks,
Enric
Quoted 2 lines
Thanks for testing,
Moritz
Hi Enrico,
Quoted 7 lines · Enric Balletbo Serra
I tried with a Samsung Chromebook Plus (arm64 Chromebook)
Quoted 2 lines
What was the ioctl you ran?
The code that I ran is this: https://hastebin.com/cupecuvowi.py
No guarantee :) I'll try to look deeper tomorrow.
I fixed it up to run with python3, but on my device I got either a protocol error if len is > 120:
root@ni-n3xx-313ABDC:~# python3 foo Testing EC_DEV_IOCRDMEM cmd I/O error(71): Protocol error
or it worked for lengths <= 120:
root@ni-n3xx-313ABDC:~# python3 foo Testing EC_DEV_IOCRDMEM cmd 6b 6f ff ff ff ff ff ff ff ff ff ff ff ff ff ff f0 0f f0 0f ff ff ff ff ff ff ff ff ff ff ff ff 45 43 01 02 00 01 01 00 00 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [...]
Did you try smaller reads? If that works maybe we'll have to filter in the kernel to prevent large reads from crashing the EC?
Thanks,
Moritz
Missatge de Moritz Fischer <[email protected]> del dia dt., 22 de gen. 2019 a les 22:09:
Quoted 28 lines
Hi Enrico,
Quoted 7 lines · Enric Balletbo Serra
I tried with a Samsung Chromebook Plus (arm64 Chromebook)
Quoted 2 lines
What was the ioctl you ran?
The code that I ran is this: https://hastebin.com/cupecuvowi.py
No guarantee :) I'll try to look deeper tomorrow.I fixed it up to run with python3, but on my device I got either a protocol error if len is > 120:
root@ni-n3xx-313ABDC:~# python3 foo Testing EC_DEV_IOCRDMEM cmd I/O error(71): Protocol erroror it worked for lengths <= 120:
root@ni-n3xx-313ABDC:~# python3 foo Testing EC_DEV_IOCRDMEM cmd 6b 6f ff ff ff ff ff ff ff ff ff ff ff ff ff ff f0 0f f0 0f ff ff ff ff ff ff ff ff ff ff ff ff 45 43 01 02 00 01 01 00 00 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [...]
Quoted 3 lines
Did you try smaller reads? If that works maybe we'll have to filter in the kernel to prevent large reads from crashing the EC?
So looks like I can read up to 161 bytes, if I read 162 to 169 bytes the EC fails to respond and if I read more than 169 bytes it simply resets the machine. I didn't look the code yet.
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 160 Testing EC_DEV_IOCRDMEM cmd (size 160) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 45 43 01 00 01 01 01 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9f 21 00 00 0e 01 00 00 59 13 00 00 0b 00 00 00 14 14 00 00 b0 1d 00 00 9a 13 00 00 35 01 00 00 53 44 49 00 00 00 00 00 34 33 35 32 44 35 31 00 30 46 38 32 00 00 00 00 4c 49 4f 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 161 Testing EC_DEV_IOCRDMEM cmd (size 161) [ 425.053205] cros-ec-spi spi2.0: cannot read mapped reg: -71/14 I/O error(71): Protocol error root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 162 Testing EC_DEV_IOCRDMEM cmd (size 162) [ 427.208065] cros-ec-spi spi2.0: cannot read mapped reg: -71/14 I/O error(71): Protocol error
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 163 Testing EC_DEV_IOCRDMEM cmd (size 163) [ 429.845069] cros-ec-spi spi2.0: cannot read mapped reg: -71/14 I/O error(71): Protocol error root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 164 Testing EC_DEV_IOCRDMEM cmd (size 164)[ 431.968014] cros-ec-spi spi2.0: cannot read mapped reg: -71/14
I/O error(71): Protocol error
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 165 Testing EC_DEV_IOCRDMEM cmd (size 165) [ 440.289340] cros-ec-spi spi2.0: EC failed to respond in time [ 440.296271] cros-ec-spi spi2.0: Command xfer error (err:-110) [ 440.302732] cros-ec-spi spi2.0: cannot read mapped reg: -110/0 I/O error(110): Connection timed out
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 166 Testing EC_DEV_IOCRDMEM cmd (size 166) [ 443.953212] cros-ec-spi spi2.0: EC failed to respond in time [ 443.960095] cros-ec-spi spi2.0: Command xfer error (err:-110) [ 443.966554] cros-ec-spi spi2.0: cannot read mapped reg: -110/0 I/O error(110): Connection timed out
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 167 Testing EC_DEV_IOCRDMEM cmd (size 167) [ 446.585416] cros-ec-spi spi2.0: EC failed to respond in time [ 446.592335] cros-ec-spi spi2.0: Command xfer error (err:-110) [ 446.598798] cros-ec-spi spi2.0: cannot read mapped reg: -110/0 I/O error(110): Connection timed out
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 168 Testing EC_DEV_IOCRDMEM cmd (size 168) [ 449.337255] cros-ec-spi spi2.0: EC failed to respond in time [ 449.344140] cros-ec-spi spi2.0: Command xfer error (err:-110) [ 449.350588] cros-ec-spi spi2.0: cannot read mapped reg: -110/0 I/O error(110): Connection timed out
root@debian:~# python /home/debian/Tests/cros_ec/scripts/cros-ec-readmem.py 169
Testing EC_DEV_IOCRDMEM cmd (size 169)
l%(NAPEK@ @%vZ__|+~/g?sڬ}mj}u??UwWo;_ۻI6go{]uw]{m}w.ͼ�ߵAnd the machine reboots ...
Quoted 3 lines
Thanks,
Moritz