Skip to contentSkip to conversation

cros_ec: Add support for Wilco EC

RFC PATCH Superseded Nick Crews 3 participants 16 messages Dec 15, 2018 – Dec 17, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nick Crews

author
#0 lore raw

cover cros_ec: Add support for Wilco EC

The Chromebook named wilco contains a different Embedded Controller than the rest of the chromebook series, and thus the kernel requires a different driver than the already existing and generalized cros_ec_* drivers. Specifically, this driver adds support for getting and setting the RTC on the EC, adding a binary sysfs attribute that receives ACPI events from the EC, adding a binary sysfs attribute to request telemetry data from the EC (useful for enterprise applications), and adding normal sysfs attributes to get/set various other properties on the EC. The core of the communication with the EC is implemented in wilco_ec_mailbox.c, using a simple byte-level protocol with a checksum, transmitted over an eSPI bus. For debugging purposes, a raw attribute is also provided which can write/read arbitrary bytes to/from the eSPI bus.

We attempted to adhere to the sysfs principles of "one piece of data per attribute" as much as possible, and mostly succeded. However, with the wilco_ec_adv_power.h attributes, which deal with scheduling power usage, we found it most elegant to bundle setting event times for an entire day into a single attribute, so at most you are using attributes formatted as "%d %d %d %d %d %d". With the telemetry attribute, we had to use a binary attribute, instead of the preferable human-readable ascii, in order to keep secure the information which is proprietary to the enterprise service provider. This opaque binary data will be read and sent using a proprietary daemon running on the OS. Finally, the "version" attribute returns a formatted result that looks something like:

Quoted 1 line

cat /sys/bus/platform/devices/GOOG000C\:00/version

Label : 95.00.06
SVN Revision : 5960a.06
Model Number : 08;8
Build Date : 11/29/18

The RTC driver is exposed as a standard RTC class driver with read/write functionality.

For event notification, the Wilco EC can return extended events that are not handled by standard ACPI objects. These events can include hotkeys which map to standard functions like brightness controls, or information about EC controlled features like the charger or battery. These events are triggered with an ACPI Notify(0x90) and the event data buffer is read through an ACPI method provided by the BIOS which reads the event buffer from EC RAM. These events are then processed, with hotkey events being sent to the input subsystem and other events put into a queue which can be read by a userspace daemon via a sysfs attribute.

The rest of the attributes are categorized as either "properties" or "legacy". "legacy" implies that the attribute existed on the EC before it was modified for ChromeOS, and "properties" implies that the attribute exposes functionality that was added to the EC specifically for ChromeOS. They are mostly boolean flags or percentages.

A full thread of the development of these patches can be found at
https://chromium-review.googlesource.com/c/1371034. This thread contains comments and revisions that could be helpful in understanding how the driver arrived at the state it is in now. The thread also contains some ChromeOS specific patches that actually enable the driver. If you want to test the patch yourself, you would have to install the ChromeOS SDK and cherry pick in these patches.

I also wrote some integration tests using the Tast testing framework that ChromeOS uses. It would require a full ChromeOS SDK to actually run the tests, but the source of the tests, written in Go, are useful for understanding what the desired behavior is. You can view the tests here:
https://chromium-review.googlesource.com/c/1372575

This is still an initial version of the driver, and we are sending it upstream for comments now, so that we can incorporate any requested changes such that it eventually can be merged. Thank you for your comments!

Duncan Laurie (6):
CHROMIUM: cros_ec: Remove cros_ec dependency in lpc_mec
CHROMIUM: wilco_ec: Add new driver for Wilco EC
CHROMIUM: wilco_ec: Add sysfs attributes
CHROMIUM: wilco_ec: Add support for raw commands in sysfs
CHROMIUM: wilco_ec: Add RTC class driver
CHROMIUM: wilco_ec: Add event handling

Nick Crews (4):
CHROMIUM: wilco_ec: Move legacy attributes to separate file
CHROMIUM: wilco_ec: Add EC properties
CHROMIUM: wilco_ec: Add peakshift and adv_batt_charging
CHROMIUM: wilco_ec: Add binary telemetry attributes

 drivers/platform/chrome/Kconfig               |  24 +-
 drivers/platform/chrome/Makefile              |   9 +-
 drivers/platform/chrome/cros_ec_lpc_mec.c     |  54 +-
 drivers/platform/chrome/cros_ec_lpc_mec.h     |  45 +-
 drivers/platform/chrome/cros_ec_lpc_reg.c     |  43 +-
 drivers/platform/chrome/wilco_ec.h            | 180 ++++++
 drivers/platform/chrome/wilco_ec_adv_power.c  | 533 ++++++++++++++++++
 drivers/platform/chrome/wilco_ec_adv_power.h  | 193 +++++++
 drivers/platform/chrome/wilco_ec_event.c      | 343 +++++++++++
 drivers/platform/chrome/wilco_ec_legacy.c     | 204 +++++++
 drivers/platform/chrome/wilco_ec_legacy.h     |  96 ++++
 drivers/platform/chrome/wilco_ec_mailbox.c    | 427 ++++++++++++++
 drivers/platform/chrome/wilco_ec_properties.c | 327 +++++++++++
 drivers/platform/chrome/wilco_ec_properties.h | 163 ++++++
 drivers/platform/chrome/wilco_ec_rtc.c        | 163 ++++++
 drivers/platform/chrome/wilco_ec_sysfs.c      | 253 +++++++++
 drivers/platform/chrome/wilco_ec_sysfs_util.h |  47 ++
 drivers/platform/chrome/wilco_ec_telemetry.c  |  66 +++
 drivers/platform/chrome/wilco_ec_telemetry.h  |  42 ++
 19 files changed, 3153 insertions(+), 59 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec.h
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.c
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.h
 create mode 100644 drivers/platform/chrome/wilco_ec_event.c
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.c
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.h
 create mode 100644 drivers/platform/chrome/wilco_ec_mailbox.c
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.c
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.h
 create mode 100644 drivers/platform/chrome/wilco_ec_rtc.c
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs.c
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs_util.h
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.c
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.h
Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#1 lore raw

RFC PATCH 1/10 CHROMIUM: cros_ec: Remove cros_ec dependency in lpc_mec

From: Duncan Laurie <[email protected]>

In order to allow this code to be re-used, remove the dependency on the rest of the cros_ec code from the cros_ec_lpc_mec functions.

Instead of using a hardcoded register base address of 0x800 have this be passed in to cros_ec_lpc_mec_init(). The existing cros_ec use case now passes in the 0x800 base address this way.

---

 drivers/platform/chrome/cros_ec_lpc_mec.c | 54 +++++++++++++++++++----
 drivers/platform/chrome/cros_ec_lpc_mec.h | 45 +++++++++++--------
 drivers/platform/chrome/cros_ec_lpc_reg.c | 43 +++++-------------
 3 files changed, 85 insertions(+), 57 deletions(-)
drivers/platform/chrome/cros_ec_lpc_mec.c +46 -8
index c4edfa83e493..18bd9f82be6c 100644
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/io.h>
 #include <linux/mfd/cros_ec_commands.h>
+#include <linux/mfd/cros_ec_lpc_mec.h>
 #include <linux/mutex.h>
 #include <linux/types.h>
 
@@ -34,6 +35,7 @@
  * EC mutex because memmap data may be accessed without it being held.
  */
 static struct mutex io_mutex;
+static u16 mec_emi_base, mec_emi_end;
 
 /*
  * cros_ec_lpc_mec_emi_write_address
@@ -46,10 +48,39 @@ static struct mutex io_mutex;
 static void cros_ec_lpc_mec_emi_write_address(u16 addr,
 			enum cros_ec_lpc_mec_emi_access_mode access_type)
 {
-	/* Address relative to start of EMI range */
-	addr -= MEC_EMI_RANGE_START;
-	outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0);
-	outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
+	outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
+	outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
+}
+
+/*
+ * cros_ec_lpc_mec_in_range
+ *
+ * Determine if requested addresses are in MEC EMI range.
+ *
+ * @offset:      Address offset
+ * @length: Number of bytes to check
+ *
+ * @return 1 if in range, 0 if not, and -1 if there is an error
+ *         such as the mec range not being initialized
+ */
+int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
+{
+	if (length == 0)
+		return -1;
+
+	if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
+		return -1;
+
+	if (offset >= mec_emi_base && offset < mec_emi_end) {
+		if (WARN_ON(offset + length - 1 <= mec_emi_end))
+			return -1;
+		return 1;
+	}
+
+	if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
+		return -1;
+
+	return 0;
 }
 
 /*
@@ -71,6 +102,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 	u8 sum = 0;
 	enum cros_ec_lpc_mec_emi_access_mode access, new_access;
 
+	/* Return checksum of 0 if window is not initialized */
+	WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
+	if (mec_emi_base == 0 || mec_emi_end == 0)
+		return 0;
+
 	/*
 	 * Long access cannot be used on misaligned data since reading B0 loads
 	 * the data register and writing B3 flushes.
@@ -86,9 +122,9 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 	cros_ec_lpc_mec_emi_write_address(offset, access);
 
 	/* Skip bytes in case of misaligned offset */
-	io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3);
+	io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
 	while (i < length) {
-		while (io_addr <= MEC_EMI_EC_DATA_B3) {
+		while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
 			if (io_type == MEC_IO_READ)
 				buf[i] = inb(io_addr++);
 			else
@@ -118,7 +154,7 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 		}
 
 		/* Access [B0, B3] on each loop pass */
-		io_addr = MEC_EMI_EC_DATA_B0;
+		io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
 	}
 
 done:
@@ -128,9 +164,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 }
 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
 
-void cros_ec_lpc_mec_init(void)
+void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
 {
 	mutex_init(&io_mutex);
+	mec_emi_base = base;
+	mec_emi_end = end;
 }
 EXPORT_SYMBOL(cros_ec_lpc_mec_init);
 
drivers/platform/chrome/cros_ec_lpc_mec.h +27 -18

70 lines not on this page · raw message

drivers/platform/chrome/cros_ec_lpc_reg.c +12 -31

63 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
#2 lore raw

Re: [RFC PATCH 01/10] CHROMIUM: cros_ec: Remove cros_ec dependency in lpc_mec

Hi Nick,

Many thanks for sending this upstream. Adding Guenter as also might be interested and some minor comments below.

Missatge de Nick Crews <[email protected]> del dia ds., 15 de des. 2018 a les 1:21:

Quoted 6 lines

From: Duncan Laurie <[email protected]>

In order to allow this code to be re-used, remove the dependency on the rest of the cros_ec code from the cros_ec_lpc_mec functions.

Quoted 7 lines

Instead of using a hardcoded register base address of 0x800 have this be passed in to cros_ec_lpc_mec_init(). The existing cros_ec use case now passes in the 0x800 base address this way.

---

For this patch and the rest of this series remove the CHROMIUM tag from the subject. Use the format 'platform/chrome: cros_ec: ...'

Quoted 11 lines
 drivers/platform/chrome/cros_ec_lpc_mec.c | 54 +++++++++++++++++++----
 drivers/platform/chrome/cros_ec_lpc_mec.h | 45 +++++++++++--------
 drivers/platform/chrome/cros_ec_lpc_reg.c | 43 +++++-------------
 3 files changed, 85 insertions(+), 57 deletions(-)
drivers/platform/chrome/cros_ec_lpc_mec.c +0 -0
index c4edfa83e493..18bd9f82be6c 100644

drivers/platform/chrome/cros_ec_lpc_mec.c line 27

Quoted 4 lines
drivers/platform/chrome/cros_ec_lpc_mec.c +1 -0
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/io.h>
 #include <linux/mfd/cros_ec_commands.h>
+#include <linux/mfd/cros_ec_lpc_mec.h>

This file does not exist, you'll get a build error with only this patch applied. I think the file is already included.

Quoted 16 lines
drivers/platform/chrome/cros_ec_lpc_mec.c +1 -1
 #include <linux/mutex.h>
 #include <linux/types.h>
 
@@ -34,6 +35,7 @@
  * EC mutex because memmap data may be accessed without it being held.
  */
 static struct mutex io_mutex;
+static u16 mec_emi_base, mec_emi_end;
 
 /*
  * cros_ec_lpc_mec_emi_write_address
@@ -46,10 +48,39 @@ static struct mutex io_mutex;
 static void cros_ec_lpc_mec_emi_write_address(u16 addr,
                        enum cros_ec_lpc_mec_emi_access_mode access_type)
 {
-       /* Address relative to start of EMI range */

drivers/platform/chrome/cros_ec_lpc_mec.c static struct mutex io_mutex; · line 57

Quoted 10 lines
drivers/platform/chrome/cros_ec_lpc_mec.c +7 -3
-       addr -= MEC_EMI_RANGE_START;
-       outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0);
-       outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
+       outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
+       outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
+}
+
+/*
+ * cros_ec_lpc_mec_in_range
+ *

For this and all other functions that you are documenting below use the standard kernel-doc format, please. See:
https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html

drivers/platform/chrome/cros_ec_lpc_mec.c static struct mutex io_mutex; · line 65

Quoted 8 lines
drivers/platform/chrome/cros_ec_lpc_mec.c +8 -0
+ * Determine if requested addresses are in MEC EMI range.
+ *
+ * @offset:      Address offset
+ * @length: Number of bytes to check
+ *
+ * @return 1 if in range, 0 if not, and -1 if there is an error
+ *         such as the mec range not being initialized
+ */

Can't you use 0 if it's ok and -errno if it fails here? Will you handle 0 and -1 errors in a different way?

Quoted 105 lines
drivers/platform/chrome/cros_ec_lpc_mec.c +29 -4
+int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
+{
+       if (length == 0)
+               return -1;
+
+       if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
+               return -1;
+
+       if (offset >= mec_emi_base && offset < mec_emi_end) {
+               if (WARN_ON(offset + length - 1 <= mec_emi_end))
+                       return -1;
+               return 1;
+       }
+
+       if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
+               return -1;
+
+       return 0;
 }
 
 /*
@@ -71,6 +102,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
        u8 sum = 0;
        enum cros_ec_lpc_mec_emi_access_mode access, new_access;
 
+       /* Return checksum of 0 if window is not initialized */
+       WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
+       if (mec_emi_base == 0 || mec_emi_end == 0)
+               return 0;
+
        /*
         * Long access cannot be used on misaligned data since reading B0 loads
         * the data register and writing B3 flushes.
@@ -86,9 +122,9 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
        cros_ec_lpc_mec_emi_write_address(offset, access);
 
        /* Skip bytes in case of misaligned offset */
-       io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3);
+       io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
        while (i < length) {
-               while (io_addr <= MEC_EMI_EC_DATA_B3) {
+               while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
                        if (io_type == MEC_IO_READ)
                                buf[i] = inb(io_addr++);
                        else
@@ -118,7 +154,7 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
                }
 
                /* Access [B0, B3] on each loop pass */
-               io_addr = MEC_EMI_EC_DATA_B0;
+               io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
        }
 
 done:
@@ -128,9 +164,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 }
 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
 
-void cros_ec_lpc_mec_init(void)
+void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
 {
        mutex_init(&io_mutex);
+       mec_emi_base = base;
+       mec_emi_end = end;
 }
 EXPORT_SYMBOL(cros_ec_lpc_mec_init);
 
drivers/platform/chrome/cros_ec_lpc_mec.h +7 -15
index 105068c0e919..a81cc6a8b621 100644
@@ -24,8 +24,6 @@
 #ifndef __CROS_EC_LPC_MEC_H
 #define __CROS_EC_LPC_MEC_H
 
-#include <linux/mfd/cros_ec_commands.h>
-
 enum cros_ec_lpc_mec_emi_access_mode {
        /* 8-bit access */
        ACCESS_TYPE_BYTE = 0x0,
@@ -45,35 +43,46 @@ enum cros_ec_lpc_mec_io_type {
        MEC_IO_WRITE,
 };
 
-/* Access IO ranges 0x800 thru 0x9ff using EMI interface instead of LPC */
-#define MEC_EMI_RANGE_START EC_HOST_CMD_REGION0
-#define MEC_EMI_RANGE_END   (EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE)
-
 /* EMI registers are relative to base */
-#define MEC_EMI_BASE 0x800
-#define MEC_EMI_HOST_TO_EC (MEC_EMI_BASE + 0)
-#define MEC_EMI_EC_TO_HOST (MEC_EMI_BASE + 1)
-#define MEC_EMI_EC_ADDRESS_B0 (MEC_EMI_BASE + 2)
-#define MEC_EMI_EC_ADDRESS_B1 (MEC_EMI_BASE + 3)
-#define MEC_EMI_EC_DATA_B0 (MEC_EMI_BASE + 4)
-#define MEC_EMI_EC_DATA_B1 (MEC_EMI_BASE + 5)
-#define MEC_EMI_EC_DATA_B2 (MEC_EMI_BASE + 6)
-#define MEC_EMI_EC_DATA_B3 (MEC_EMI_BASE + 7)
+#define MEC_EMI_HOST_TO_EC(MEC_EMI_BASE)       (MEC_EMI_BASE + 0)
+#define MEC_EMI_EC_TO_HOST(MEC_EMI_BASE)       (MEC_EMI_BASE + 1)
+#define MEC_EMI_EC_ADDRESS_B0(MEC_EMI_BASE)    (MEC_EMI_BASE + 2)
+#define MEC_EMI_EC_ADDRESS_B1(MEC_EMI_BASE)    (MEC_EMI_BASE + 3)
+#define MEC_EMI_EC_DATA_B0(MEC_EMI_BASE)       (MEC_EMI_BASE + 4)
+#define MEC_EMI_EC_DATA_B1(MEC_EMI_BASE)       (MEC_EMI_BASE + 5)
+#define MEC_EMI_EC_DATA_B2(MEC_EMI_BASE)       (MEC_EMI_BASE + 6)

drivers/platform/chrome/cros_ec_lpc_mec.h enum cros_ec_lpc_mec_io_type { · line 62

Quoted 10 lines
drivers/platform/chrome/cros_ec_lpc_mec.h +5 -1
+#define MEC_EMI_EC_DATA_B3(MEC_EMI_BASE)       (MEC_EMI_BASE + 7)
 
-/*
+/**
  * cros_ec_lpc_mec_init
  *
  * Initialize MEC I/O.
+ *
+ * @base: MEC EMI Base address
+ * @end:  MEC EMI End address

Since you're touching this function, please fix the documentation format also. Thanks :)

drivers/platform/chrome/cros_ec_lpc_mec.h enum cros_ec_lpc_mec_io_type { · line 66

Quoted 6 lines
drivers/platform/chrome/cros_ec_lpc_mec.h +2 -2
  */
-void cros_ec_lpc_mec_init(void);
+void cros_ec_lpc_mec_init(unsigned int base, unsigned int end);
 
-/*
+/**

Unrelated change.

Quoted 91 lines
drivers/platform/chrome/cros_ec_lpc_mec.h +13 -0
  * cros_ec_lpc_mec_destroy
  *
  * Cleanup MEC I/O.
  */
 void cros_ec_lpc_mec_destroy(void);
 
+/**
+ * cros_ec_lpc_mec_in_range
+ *
+ * Determine if requested addresses are in MEC EMI range.
+ *
+ * @offset: Address offset
+ * @length: Number of bytes to check
+ *
+ * @return 1 if in range, 0 if not, and -1 if there is an error
+ *         such as the mec range not being initialized
+ */
+int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length);
+
 /**
  * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port
  *
drivers/platform/chrome/cros_ec_lpc_reg.c +12 -32
index fc23d535c404..4cba259b5b1e 100644
@@ -59,51 +59,32 @@ static u8 lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
 
 u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
 {
-       if (length == 0)
-               return 0;
-
-       /* Access desired range through EMI interface */
-       if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
-               /* Ensure we don't straddle EMI region */
-               if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
-                       return 0;
+       int in_range = cros_ec_lpc_mec_in_range(offset, length);
 
-               return cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length,
-                                               dest);
-       }
-
-       if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
-                   offset < MEC_EMI_RANGE_START))
+       if (in_range < 0)
                return 0;
 
-       return lpc_read_bytes(offset, length, dest);
+       return in_range ?
+               cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length, dest) :
+               lpc_read_bytes(offset, length, dest);
 }
 
 u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
 {
-       if (length == 0)
-               return 0;
-
-       /* Access desired range through EMI interface */
-       if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
-               /* Ensure we don't straddle EMI region */
-               if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
-                       return 0;
+       int in_range = cros_ec_lpc_mec_in_range(offset, length);
 
-               return cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length,
-                                               msg);
-       }
-
-       if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
-                   offset < MEC_EMI_RANGE_START))
+       if (in_range < 0)
                return 0;
 
-       return lpc_write_bytes(offset, length, msg);
+       return in_range ?
+               cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length, msg) :
+               lpc_write_bytes(offset, length, msg);
 }
 
 void cros_ec_lpc_reg_init(void)
 {
-       cros_ec_lpc_mec_init();
+       cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
+                            EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
 }
 
 void cros_ec_lpc_reg_destroy(void)
--
Quoted 2 lines

2.20.0.405.gbc1bbc6f85-goog

Thanks,
Enric

author
#3 lore raw

RFC PATCH 2/10 CHROMIUM: wilco_ec: Add new driver for Wilco EC

In reply to Nick Crews

From: Duncan Laurie <[email protected]>

This EC is an incompatible variant of the typical Chrome OS embedded controller. It uses the same low-level communication and a similar protocol with some significant differences. The EC firmware does not support the same mailbox commands so it is not registered as a cros_ec device type.

---

 drivers/platform/chrome/Kconfig            |  14 +-
 drivers/platform/chrome/Makefile           |   3 +
 drivers/platform/chrome/wilco_ec.h         |  97 +++++
 drivers/platform/chrome/wilco_ec_mailbox.c | 395 +++++++++++++++++++++
 4 files changed, 508 insertions(+), 1 deletion(-)
 create mode 100644 drivers/platform/chrome/wilco_ec.h
 create mode 100644 drivers/platform/chrome/wilco_ec_mailbox.c
drivers/platform/chrome/Kconfig +13 -1
index 16b1615958aa..4168d5e6bedc 100644
@@ -49,6 +49,18 @@ config CHROMEOS_TBMC
 	  To compile this driver as a module, choose M here: the
 	  module will be called chromeos_tbmc.
 
+config WILCO_EC
+	tristate "ChromeOS Wilco Embedded Controller"
+	depends on ACPI && (X86 || COMPILE_TEST)
+	select CROS_EC_LPC_MEC
+	help
+	  If you say Y here, you get support for talking to the Chrome OS
+	  Wilco EC over an eSPI bus. This uses a simple byte-level protocol
+	  with a checksum.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called wilco_ec.
+
 config CROS_EC_CTL
         tristate
 
@@ -86,7 +98,7 @@ config CROS_EC_LPC
 
 config CROS_EC_LPC_MEC
 	bool "ChromeOS Embedded Controller LPC Microchip EC (MEC) variant"
-	depends on CROS_EC_LPC
+	depends on CROS_EC_LPC || WILCO_EC
 	default n
 	help
 	  If you say Y here, a variant LPC protocol for the Microchip EC
drivers/platform/chrome/Makefile +3 -0
index cd591bf872bb..b132ba5b3e3d 100644
@@ -12,4 +12,7 @@ cros_ec_lpcs-objs			:= cros_ec_lpc.o cros_ec_lpc_reg.o
 cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)	+= cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
+
 obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)	+= cros_kbd_led_backlight.o
+wilco_ec-objs				:= wilco_ec_mailbox.o
+obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec.h new file +97 -0

97 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_mailbox.c new file +395 -0

395 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
#4 lore raw

Re: [RFC PATCH 02/10] CHROMIUM: wilco_ec: Add new driver for Wilco EC

Hi Nick,

Many thanks for sending this upstream. Adding Guenter as also might be interested, and some minor comments below.

Missatge de Nick Crews <[email protected]> del dia ds., 15 de des. 2018 a les 1:22:

Quoted 26 lines

From: Duncan Laurie <[email protected]>

This EC is an incompatible variant of the typical Chrome OS embedded controller. It uses the same low-level communication and a similar protocol with some significant differences. The EC firmware does not support the same mailbox commands so it is not registered as a cros_ec device type.

---

 drivers/platform/chrome/Kconfig            |  14 +-
 drivers/platform/chrome/Makefile           |   3 +
 drivers/platform/chrome/wilco_ec.h         |  97 +++++
 drivers/platform/chrome/wilco_ec_mailbox.c | 395 +++++++++++++++++++++
 4 files changed, 508 insertions(+), 1 deletion(-)
 create mode 100644 drivers/platform/chrome/wilco_ec.h
 create mode 100644 drivers/platform/chrome/wilco_ec_mailbox.c
drivers/platform/chrome/Kconfig +0 -0
index 16b1615958aa..4168d5e6bedc 100644

drivers/platform/chrome/Kconfig config CHROMEOS_TBMC · line 57

Quoted 9 lines
drivers/platform/chrome/Kconfig +6 -0
@@ -49,6 +49,18 @@ config CHROMEOS_TBMC
          To compile this driver as a module, choose M here: the
          module will be called chromeos_tbmc.
 
+config WILCO_EC
+       tristate "ChromeOS Wilco Embedded Controller"
+       depends on ACPI && (X86 || COMPILE_TEST)
+       select CROS_EC_LPC_MEC
+       help
+         If you say Y here, you get support for talking to the Chrome OS

s/Chrome OS/ChromeOS/ like you did in the config description. For coherence try to use the same form.

Quoted 23 lines
drivers/platform/chrome/Kconfig +7 -1
+         Wilco EC over an eSPI bus. This uses a simple byte-level protocol
+         with a checksum.
+
+         To compile this driver as a module, choose M here: the
+         module will be called wilco_ec.
+
 config CROS_EC_CTL
         tristate
 
@@ -86,7 +98,7 @@ config CROS_EC_LPC
 
 config CROS_EC_LPC_MEC
        bool "ChromeOS Embedded Controller LPC Microchip EC (MEC) variant"
-       depends on CROS_EC_LPC
+       depends on CROS_EC_LPC || WILCO_EC
        default n
        help
          If you say Y here, a variant LPC protocol for the Microchip EC
drivers/platform/chrome/Makefile +0 -0
index cd591bf872bb..b132ba5b3e3d 100644

drivers/platform/chrome/Makefile cros_ec_lpcs-objs := cros_ec_lpc.o cros_ec_lpc_reg.o · line 15

Quoted 4 lines
drivers/platform/chrome/Makefile +1 -0
@@ -12,4 +12,7 @@ cros_ec_lpcs-objs                     := cros_ec_lpc.o cros_ec_lpc_reg.o
 cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC) += cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)              += cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)            += cros_ec_proto.o
+

Unrelated change.

drivers/platform/chrome/wilco_ec.h line 3

Quoted 12 lines
drivers/platform/chrome/Makefile +2 -0
 obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)   += cros_kbd_led_backlight.o
+wilco_ec-objs                          := wilco_ec_mailbox.o
+obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
drivers/platform/chrome/wilco_ec.h new file +3 -0
new file mode 100644
index 000000000000..ba16fcff87c4
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * wilco_ec - Chrome OS Wilco Embedded Controller

Remove 'wilco_ec -' and s/Chrome OS/ChromeOS/

drivers/platform/chrome/wilco_ec.h line 14

Quoted 11 lines
drivers/platform/chrome/wilco_ec.h new file +11 -0
+ *
+ * Copyright 2018 Google LLC
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

You already added the SPDX-identifier so can remove the license boiler-plate.

Quoted 16 lines
drivers/platform/chrome/wilco_ec.h new file +16 -0
+ */
+
+#ifndef WILCO_EC_H
+#define WILCO_EC_H
+
+#include <linux/device.h>
+#include <linux/kernel.h>
+
+#define WILCO_EC_FLAG_NO_RESPONSE      BIT(0) /* EC does not respond */
+#define WILCO_EC_FLAG_EXTENDED_DATA    BIT(1) /* EC returns 256 data bytes */
+#define WILCO_EC_FLAG_RAW_REQUEST      BIT(2) /* Do not trim request data */
+#define WILCO_EC_FLAG_RAW_RESPONSE     BIT(3) /* Do not trim response data */
+#define WILCO_EC_FLAG_RAW              (WILCO_EC_FLAG_RAW_REQUEST | \
+                                        WILCO_EC_FLAG_RAW_RESPONSE)
+
+/**

drivers/platform/chrome/wilco_ec.h line 40

Quoted 10 lines
drivers/platform/chrome/wilco_ec.h new file +10 -0
+ * enum wilco_ec_msg_type - Message type to select a set of command codes.
+ * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior.
+ * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property.
+ * @WILCO_EC_MSG_TELEMETRY: Telemetry data provided by the EC.
+ */
+enum wilco_ec_msg_type {
+       WILCO_EC_MSG_LEGACY = 0x00f0,
+       WILCO_EC_MSG_PROPERTY = 0x00f2,
+       WILCO_EC_MSG_TELEMETRY = 0x00f5,
+};

I think that this is not used for now, remove and introduce it when you use it.

Quoted 63 lines
drivers/platform/chrome/wilco_ec.h new file +57 -0
+
+/**
+ * struct wilco_ec_device - Wilco Embedded Controller handle.
+ * @dev: Device handle.
+ * @mailbox_lock: Mutex to ensure one mailbox command at a time.
+ * @io_command: I/O port for mailbox command.  Provided by ACPI.
+ * @io_data: I/O port for mailbox data.  Provided by ACPI.
+ * @io_packet: I/O port for mailbox packet data.  Provided by ACPI.
+ * @data_buffer: Buffer used for EC communication.  The same buffer
+ *               is used to hold the request and the response.
+ * @data_size: Size of the data buffer used for EC communication.
+ */
+struct wilco_ec_device {
+       struct device *dev;
+       struct mutex mailbox_lock;
+       struct resource *io_command;
+       struct resource *io_data;
+       struct resource *io_packet;
+       void *data_buffer;
+       size_t data_size;
+};
+
+/**
+ * struct wilco_ec_message - Request and response message.
+ * @type: Mailbox message type.
+ * @flags: Message flags.
+ * @command: Mailbox command code.
+ * @result: Result code from the EC.  Non-zero indicates an error.
+ * @request_size: Number of bytes to send to the EC.
+ * @request_data: Buffer containing the request data.
+ * @response_size: Number of bytes expected from the EC.
+ *                 This is 32 by default and 256 if the flag
+ *                 is set for %WILCO_EC_FLAG_EXTENDED_DATA
+ * @response_data: Buffer containing the response data, should be
+ *                 response_size bytes and allocated by caller.
+ */
+struct wilco_ec_message {
+       enum wilco_ec_msg_type type;
+       u8 flags;
+       u8 command;
+       u8 result;
+       size_t request_size;
+       void *request_data;
+       size_t response_size;
+       void *response_data;
+};
+
+/**
+ * wilco_ec_mailbox() - Send request to the EC and receive the response.
+ * @ec: Wilco EC device.
+ * @msg: Wilco EC message.
+ *
+ * Return: Number of bytes received or negative error code on failure.
+ */
+int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
+
+#endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_mailbox.c new file +0 -0
new file mode 100644
index 000000000000..6613c18c2a82

drivers/platform/chrome/wilco_ec_mailbox.c line 3

Quoted 3 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +3 -0
@@ -0,0 +1,395 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * wilco_ec_mailbox - Mailbox interface for Wilco Embedded Controller

Remove 'wilco_ec_mailbox -'

drivers/platform/chrome/wilco_ec_mailbox.c line 14

Quoted 11 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +11 -0
+ *
+ * Copyright 2018 Google LLC
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

Remove the license boiler-plate.

Quoted 9 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +9 -0
+ */
+
+/*
+ * The Wilco EC is similar to a typical Chrome OS embedded controller.
+ * It uses the same MEC based low-level communication and a similar
+ * protocol, but with some important differences.  The EC firmware does
+ * not support the same mailbox commands so it is not registered as a
+ * cros_ec device type.
+ *

drivers/platform/chrome/wilco_ec_mailbox.c line 33

Quoted 10 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +10 -0
+ * Most messages follow a standard format, but there are some exceptions
+ * and an interface is provided to do direct/raw transactions that do not
+ * make assumptions about byte placement.
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/io.h>
+#include <linux/mfd/cros_ec_lpc_mec.h>

This file does not exists.

#include "cros_ec_lpc_mec.h" ?

Quoted 214 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +214 -0
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include "wilco_ec.h"
+
+/* Version of mailbox interface */
+#define EC_MAILBOX_VERSION             0
+
+/* Command to start mailbox transaction */
+#define EC_MAILBOX_START_COMMAND       0xda
+
+/* Version of EC protocol */
+#define EC_MAILBOX_PROTO_VERSION       3
+
+/* Normal commands have a maximum 32 bytes of data */
+#define EC_MAILBOX_DATA_SIZE           32
+
+/* Extended commands have 256 bytes of response data */
+#define EC_MAILBOX_DATA_SIZE_EXTENDED  256
+
+/* Number of header bytes to be counted as data bytes */
+#define EC_MAILBOX_DATA_EXTRA          2
+
+/* Maximum timeout */
+#define EC_MAILBOX_TIMEOUT             HZ
+
+/* EC response flags */
+#define EC_CMDR_DATA           BIT(0)  /* Data ready for host to read */
+#define EC_CMDR_PENDING                BIT(1)  /* Write pending to EC */
+#define EC_CMDR_BUSY           BIT(2)  /* EC is busy processing a command */
+#define EC_CMDR_CMD            BIT(3)  /* Last host write was a command */
+
+/**
+ * struct wilco_ec_request - Mailbox request message format.
+ * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
+ * @checksum: Sum of all bytes must be 0.
+ * @mailbox_id: Mailbox identifier, specifies the command set.
+ * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION
+ * @reserved: Set to zero.
+ * @data_size: Length of request, data + last 2 bytes of the header.
+ * @command: Mailbox command code, unique for each mailbox_id set.
+ * @reserved_raw: Set to zero for most commands, but is used by
+ *                some command types and for raw commands.
+ */
+struct wilco_ec_request {
+       u8 struct_version;
+       u8 checksum;
+       u16 mailbox_id;
+       u8 mailbox_version;
+       u8 reserved;
+       u16 data_size;
+       u8 command;
+       u8 reserved_raw;
+} __packed;
+
+/**
+ * struct wilco_ec_response - Mailbox response message format.
+ * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
+ * @checksum: Sum of all bytes must be 0.
+ * @result: Result code from the EC.  Non-zero indicates an error.
+ * @data_size: Length of the response data buffer.
+ * @reserved: Set to zero.
+ * @mbox0: EC returned data at offset 0 is unused (always 0) so this byte
+ *         is treated as part of the header instead of the data.
+ * @data: Response data buffer.  Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED.
+ */
+struct wilco_ec_response {
+       u8 struct_version;
+       u8 checksum;
+       u16 result;
+       u16 data_size;
+       u8 reserved[2];
+       u8 mbox0;
+       u8 data[0];
+} __packed;
+
+/**
+ * wilco_ec_response_timed_out() - Wait for EC response.
+ * @ec: EC device.
+ *
+ * Return: true if EC timed out, false if EC did not time out.
+ */
+static bool wilco_ec_response_timed_out(struct wilco_ec_device *ec)
+{
+       unsigned long timeout = jiffies + EC_MAILBOX_TIMEOUT;
+
+       usleep_range(200, 300);
+       do {
+               if (!(inb(ec->io_command->start) &
+                     (EC_CMDR_PENDING | EC_CMDR_BUSY)))
+                       return false;
+               usleep_range(100, 200);
+       } while (time_before(jiffies, timeout));
+
+       return true;
+}
+
+/**
+ * wilco_ec_checksum() - Compute 8bit checksum over data range.
+ * @data: Data to checksum.
+ * @size: Number of bytes to checksum.
+ *
+ * Return: 8bit checksum of provided data.
+ */
+static u8 wilco_ec_checksum(const void *data, size_t size)
+{
+       u8 *data_bytes = (u8 *)data;
+       u8 checksum = 0;
+       size_t i;
+
+       for (i = 0; i < size; i++)
+               checksum += data_bytes[i];
+
+       return checksum;
+}
+
+/**
+ * wilco_ec_prepare() - Prepare the request structure for the EC.
+ * @msg: EC message with request information.
+ * @rq: EC request structure to fill.
+ */
+static void wilco_ec_prepare(struct wilco_ec_message *msg,
+                            struct wilco_ec_request *rq)
+{
+       memset(rq, 0, sizeof(*rq));
+
+       /* Handle messages without trimming bytes from the request */
+       if (msg->request_size && msg->flags & WILCO_EC_FLAG_RAW_REQUEST) {
+               rq->reserved_raw = *(u8 *)msg->request_data;
+               msg->request_size--;
+               memmove(msg->request_data, msg->request_data + 1,
+                       msg->request_size);
+       }
+
+       /* Fill in request packet */
+       rq->struct_version = EC_MAILBOX_PROTO_VERSION;
+       rq->mailbox_id = msg->type;
+       rq->mailbox_version = EC_MAILBOX_VERSION;
+       rq->data_size = msg->request_size + EC_MAILBOX_DATA_EXTRA;
+       rq->command = msg->command;
+
+       /* Checksum header and data */
+       rq->checksum = wilco_ec_checksum(rq, sizeof(*rq));
+       rq->checksum += wilco_ec_checksum(msg->request_data, msg->request_size);
+       rq->checksum = -rq->checksum;
+}
+
+/**
+ * wilco_ec_transfer() - Send EC request and receive EC response.
+ * @ec: EC device.
+ * @msg: EC message data for request and response.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+static int wilco_ec_transfer(struct wilco_ec_device *ec,
+                            struct wilco_ec_message *msg)
+{
+       struct wilco_ec_request *rq;
+       struct wilco_ec_response *rs;
+       u8 checksum;
+       size_t size;
+
+       /* Prepare request packet */
+       rq = ec->data_buffer;
+       wilco_ec_prepare(msg, rq);
+
+       /* Write request header */
+       cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
+
+       /* Write request data */
+       cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
+                                msg->request_data);
+
+       /* Start the command */
+       outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
+
+       /* Wait for it to complete */
+       if (wilco_ec_response_timed_out(ec)) {
+               dev_err(ec->dev, "response timed out\n");
+               return -ETIMEDOUT;
+       }
+
+       /* Some commands will put the EC into a state where it cannot respond */
+       if (msg->flags & WILCO_EC_FLAG_NO_RESPONSE) {
+               dev_dbg(ec->dev, "EC does not respond to this command\n");
+               return 0;
+       }
+
+       /* Check result */
+       msg->result = inb(ec->io_data->start);
+       if (msg->result) {
+               dev_err(ec->dev, "bad response: 0x%02x\n", msg->result);
+               return -EIO;
+       }
+
+       if (msg->flags & WILCO_EC_FLAG_EXTENDED_DATA) {
+               /* Extended commands return 256 bytes of data */
+               size = EC_MAILBOX_DATA_SIZE_EXTENDED;
+       } else {
+               /* Otherwise EC commands return 32 bytes of data */
+               size = EC_MAILBOX_DATA_SIZE;
+       }
+
+       /* Read back response */
+       rs = ec->data_buffer;
+       checksum = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
+                                           sizeof(*rs) + size, (u8 *)rs);
+       if (checksum) {
+               dev_err(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
+               return -EBADMSG;
+       }
+       msg->result = rs->result;
+
+       /* Check the returned data size, skipping the header */
+       if (rs->data_size != size) {

drivers/platform/chrome/wilco_ec_mailbox.c line 257

Quoted 10 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +10 -0
+               dev_err(ec->dev, "unexpected packet size (%u != %zu)",
+                       rs->data_size, size);
+               return -EMSGSIZE;
+       }
+
+       /* Skip 1 response data byte unless specified */
+       size = (msg->flags & WILCO_EC_FLAG_RAW_RESPONSE) ? 0 : 1;
+
+       if (msg->response_size > rs->data_size - size) {
+               dev_err(ec->dev, "response data too short (%u > %zu)",

Fix the following compiler warning:

warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘size_t’ {aka ‘l
ong unsigned int’} [-Wformat=]

Quoted 10 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +10 -0
+                       rs->data_size - size, msg->response_size);
+               return -EMSGSIZE;
+       }
+
+       /* Ignore response data bytes as requested */
+       memcpy(msg->response_data, rs->data + size, msg->response_size);
+
+       /* Return actual amount of data received */
+       return msg->response_size;
+}

drivers/platform/chrome/wilco_ec_mailbox.c line 277

Quoted 10 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +10 -0
+
+int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg)
+{
+       size_t size = EC_MAILBOX_DATA_SIZE;
+       int ret;
+
+       dev_dbg(ec->dev, "cmd=%02x type=%04x flags=%02x rslen=%d rqlen=%d\n",
+               msg->command, msg->type, msg->flags, msg->response_size,
+               msg->request_size);
+

warning: format ‘%d’ expects argument of type ‘int’, but argument 7 has type ‘size_t’ {aka ‘long unsigned int’} [-Wformat=]

Quoted 106 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +106 -0
+       if (msg->request_size > size) {
+               dev_err(ec->dev, "provided request too large: %zu > %zu\n",
+                       msg->request_size, size);
+               return -EINVAL;
+       }
+
+       /* Check for extended size on response data if flag is set */
+       if (msg->flags & WILCO_EC_FLAG_EXTENDED_DATA)
+               size = EC_MAILBOX_DATA_SIZE_EXTENDED;
+
+       if (msg->response_size > size) {
+               dev_err(ec->dev, "expected response too large: %zu > %zu\n",
+                       msg->response_size, size);
+               return -EINVAL;
+       }
+       if (msg->request_size && !msg->request_data) {
+               dev_err(ec->dev, "request data missing\n");
+               return -EINVAL;
+       }
+       if (msg->response_size && !msg->response_data) {
+               dev_err(ec->dev, "response data missing\n");
+               return -EINVAL;
+       }
+
+       mutex_lock(&ec->mailbox_lock);
+
+       ret = wilco_ec_transfer(ec, msg);
+       if (ret >= 0 && msg->result)
+               ret = -EBADMSG;
+
+       mutex_unlock(&ec->mailbox_lock);
+
+       return ret;
+}
+
+static struct resource *wilco_get_resource(struct platform_device *pdev,
+                                          int index)
+{
+       struct device *dev = &pdev->dev;
+       struct resource *res;
+
+       res = platform_get_resource(pdev, IORESOURCE_IO, index);
+       if (!res) {
+               dev_err(dev, "couldn't find IO resource %d\n", index);
+               return NULL;
+       }
+
+       if (!devm_request_region(dev, res->start, resource_size(res),
+                                dev_name(dev))) {
+               dev_err(dev, "couldn't reserve IO region %d\n", index);
+               return NULL;
+       }
+
+       return res;
+}
+
+static int wilco_ec_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct wilco_ec_device *ec;
+
+       ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
+       if (!ec)
+               return -ENOMEM;
+       platform_set_drvdata(pdev, ec);
+       ec->dev = dev;
+       mutex_init(&ec->mailbox_lock);
+
+       /* Largest data buffer size requirement is extended data response */
+       ec->data_size = sizeof(struct wilco_ec_response) +
+               EC_MAILBOX_DATA_SIZE_EXTENDED;
+       ec->data_buffer = devm_kzalloc(dev, ec->data_size, GFP_KERNEL);
+       if (!ec->data_buffer)
+               return -ENOMEM;
+
+       /* Prepare access to IO regions provided by ACPI */
+       ec->io_data = wilco_get_resource(pdev, 0);      /* Host Data */
+       ec->io_command = wilco_get_resource(pdev, 1);   /* Host Command */
+       ec->io_packet = wilco_get_resource(pdev, 2);    /* MEC EMI */
+       if (!ec->io_data || !ec->io_command || !ec->io_packet)
+               return -ENODEV;
+
+       /* Initialize cros_ec register interface for communication */
+       cros_ec_lpc_mec_init(ec->io_packet->start,
+                            ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
+
+       return 0;
+}
+
+static int wilco_ec_remove(struct platform_device *pdev)
+{
+       /* Teardown cros_ec interface */
+       cros_ec_lpc_mec_destroy();
+
+       return 0;
+}
+
+static const struct acpi_device_id wilco_ec_acpi_device_ids[] = {
+       { "GOOG000C", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(acpi, wilco_ec_acpi_device_ids);
+
+static struct platform_driver wilco_ec_driver = {
+       .driver = {
+               .name = "wilco_ec",

drivers/platform/chrome/wilco_ec_mailbox.c line 393

Quoted 10 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +10 -0
+               .acpi_match_table = wilco_ec_acpi_device_ids,
+       },
+       .probe = wilco_ec_probe,
+       .remove = wilco_ec_remove,
+};
+
+module_platform_driver(wilco_ec_driver);
+
+MODULE_AUTHOR("Duncan Laurie <[email protected]>");
+MODULE_LICENSE("GPL");

License mismatch, according to the SPDX identifier you want GPL v2 here.

drivers/platform/chrome/wilco_ec_mailbox.c line 394

Quoted 1 line
drivers/platform/chrome/wilco_ec_mailbox.c new file +1 -0
+MODULE_DESCRIPTION("Chrome OS Wilco Embedded Controller driver");

s/Chrome OS/ChromeOS/

Quoted 4 lines
drivers/platform/chrome/wilco_ec_mailbox.c new file +1 -1
+MODULE_ALIAS("platform:wilco-ec");
--

2.20.0.405.gbc1bbc6f85-goog

Thanks,
Enric

author
#5 lore raw

RFC PATCH 3/10 CHROMIUM: wilco_ec: Add sysfs attributes

In reply to Nick Crews

From: Duncan Laurie <[email protected]>

Add some sample sysfs attributes for the Wilco EC that show how the mailbox interface works.

Quoted 1 line

cat /sys/bus/platform/devices/GOOG000C\:00/version

Label : 99.99.99
SVN Revision : 738ed.99
Model Number : 08;8
Build Date : 08/30/18

---

 drivers/platform/chrome/Makefile           |   3 +-
 drivers/platform/chrome/wilco_ec.h         |  14 +++
 drivers/platform/chrome/wilco_ec_mailbox.c |  12 ++
 drivers/platform/chrome/wilco_ec_sysfs.c   | 121 +++++++++++++++++++++
 4 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs.c
drivers/platform/chrome/Makefile +1 -2
index b132ba5b3e3d..e8603bc5b095 100644
@@ -13,6 +13,5 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)	+= cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
-obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)	+= cros_kbd_led_backlight.o
-wilco_ec-objs				:= wilco_ec_mailbox.o
+wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec.h +14 -0
index ba16fcff87c4..699f4cf744dc 100644
@@ -94,4 +94,18 @@ struct wilco_ec_message {
  */
 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
 
+/**
+ * wilco_ec_sysfs_init() - Create sysfs attributes.
+ * @ec: EC device.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_sysfs_init(struct wilco_ec_device *ec);
+
+/**
+ * wilco_ec_sysfs_remove() - Remove sysfs attributes.
+ * @ec: EC device.
+ */
+void wilco_ec_sysfs_remove(struct wilco_ec_device *ec);
+
 #endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_mailbox.c +12 -0
index 6613c18c2a82..414ea0a8ad03 100644
@@ -361,11 +361,23 @@ static int wilco_ec_probe(struct platform_device *pdev)
 	cros_ec_lpc_mec_init(ec->io_packet->start,
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+	/* Create sysfs attributes for userspace interaction */
+	if (wilco_ec_sysfs_init(ec) < 0) {
+		dev_err(dev, "Failed to create sysfs attributes\n");
+		cros_ec_lpc_mec_destroy();
+		return -ENODEV;
+	}
+
 	return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+	/* Remove sysfs attributes */
+	wilco_ec_sysfs_remove(ec);
+
 	/* Teardown cros_ec interface */
 	cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec_sysfs.c new file +121 -0

121 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
#6 lore raw

Re: [RFC PATCH 03/10] CHROMIUM: wilco_ec: Add sysfs attributes

Hi Nick,

Many thanks for sending this upstream. Adding Guenter as also might be interested, and some comments and questions below.

Missatge de Nick Crews <[email protected]> del dia ds., 15 de des. 2018 a les 1:20:

Quoted 12 lines

From: Duncan Laurie <[email protected]>

Add some sample sysfs attributes for the Wilco EC that show how the mailbox interface works.

Quoted 1 line

cat /sys/bus/platform/devices/GOOG000C\:00/version

Label : 99.99.99
SVN Revision : 738ed.99
Model Number : 08;8
Build Date : 08/30/18

Quoted 8 lines

---

 drivers/platform/chrome/Makefile           |   3 +-
 drivers/platform/chrome/wilco_ec.h         |  14 +++
 drivers/platform/chrome/wilco_ec_mailbox.c |  12 ++
 drivers/platform/chrome/wilco_ec_sysfs.c   | 121 +++++++++++++++++++++

I am wondering if this should have its own driver (like we recently did for cros_ec) or if it should be integrated into the wilco core. Also, I am wondering if this should be attached to the cros_ec_class or not ( /sys/class/chromeos/wilco_ec ? ). Guenter and Benson, any opinion here?

Anyway, you should add the documentation of the sysfs entries, please.

drivers/platform/chrome/Makefile cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC) += cros_ec_lpc_mec.o · line 16

Quoted 12 lines
 4 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs.c
drivers/platform/chrome/Makefile +0 -1
index b132ba5b3e3d..e8603bc5b095 100644
@@ -13,6 +13,5 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)        += cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)              += cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)            += cros_ec_proto.o
 
-obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)   += cros_kbd_led_backlight.o

I guess that you don't want to do this :)

Quoted 60 lines
drivers/platform/chrome/Makefile +1 -1
-wilco_ec-objs                          := wilco_ec_mailbox.o
+wilco_ec-objs                          := wilco_ec_mailbox.o wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
drivers/platform/chrome/wilco_ec.h +14 -0
index ba16fcff87c4..699f4cf744dc 100644
@@ -94,4 +94,18 @@ struct wilco_ec_message {
  */
 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
 
+/**
+ * wilco_ec_sysfs_init() - Create sysfs attributes.
+ * @ec: EC device.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_sysfs_init(struct wilco_ec_device *ec);
+
+/**
+ * wilco_ec_sysfs_remove() - Remove sysfs attributes.
+ * @ec: EC device.
+ */
+void wilco_ec_sysfs_remove(struct wilco_ec_device *ec);
+
 #endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_mailbox.c +12 -0
index 6613c18c2a82..414ea0a8ad03 100644
@@ -361,11 +361,23 @@ static int wilco_ec_probe(struct platform_device *pdev)
        cros_ec_lpc_mec_init(ec->io_packet->start,
                             ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+       /* Create sysfs attributes for userspace interaction */
+       if (wilco_ec_sysfs_init(ec) < 0) {
+               dev_err(dev, "Failed to create sysfs attributes\n");
+               cros_ec_lpc_mec_destroy();
+               return -ENODEV;
+       }
+
        return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+       struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+       /* Remove sysfs attributes */
+       wilco_ec_sysfs_remove(ec);
+
        /* Teardown cros_ec interface */
        cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec_sysfs.c new file +0 -0
new file mode 100644
index 000000000000..f9ae6cef6169

drivers/platform/chrome/wilco_ec_sysfs.c line 3

Quoted 3 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +3 -0
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * wilco_ec_sysfs - Sysfs attributes for Wilco Embedded Controller

Remove 'wilco_ec_sysfs -'.

drivers/platform/chrome/wilco_ec_sysfs.c line 14

Quoted 11 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +11 -0
+ *
+ * Copyright 2018 Google LLC
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

Remove the license boiler-plate.

Quoted 108 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +107 -1
+ */
+
+#include <linux/ctype.h>
+#include <linux/platform_device.h>
+#include <linux/sysfs.h>
+#include "wilco_ec.h"
+
+#define EC_COMMAND_EC_INFO             0x38
+#define EC_INFO_SIZE                    9
+#define EC_COMMAND_STEALTH_MODE                0xfc
+
+struct ec_info {
+       u8 index;
+       const char *label;
+};
+
+static ssize_t wilco_ec_show_info(struct wilco_ec_device *ec, char *buf,
+                                 ssize_t count, struct ec_info *info)
+{
+       char result[EC_INFO_SIZE];
+       struct wilco_ec_message msg = {
+               .type = WILCO_EC_MSG_LEGACY,
+               .command = EC_COMMAND_EC_INFO,
+               .request_data = &info->index,
+               .request_size = sizeof(info->index),
+               .response_data = result,
+               .response_size = EC_INFO_SIZE,
+       };
+       int ret;
+
+       ret = wilco_ec_mailbox(ec, &msg);
+       if (ret != EC_INFO_SIZE)
+               return scnprintf(buf + count, PAGE_SIZE - count,
+                                "%-12s : ERROR %d\n", info->label, ret);
+
+       return scnprintf(buf + count, PAGE_SIZE - count,
+                        "%-12s : %s\n", info->label, result);
+}
+
+static ssize_t version_show(struct device *dev,
+                           struct device_attribute *attr, char *buf)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(dev);
+       struct ec_info wilco_ec_info[] = {
+               { 0, "Label" },
+               { 1, "SVN Revision" },
+               { 2, "Model Number" },
+               { 3, "Build Date" },
+               { 0xff, NULL },
+       };
+       struct ec_info *info = wilco_ec_info;
+       ssize_t c = 0;
+
+       for (info = wilco_ec_info; info->label; info++)
+               c += wilco_ec_show_info(ec, buf, c, info);
+
+       return c;
+}
+
+static ssize_t stealth_mode_store(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(dev);
+       u8 param;
+       struct wilco_ec_message msg = {
+               .type = WILCO_EC_MSG_LEGACY,
+               .command = EC_COMMAND_STEALTH_MODE,
+               .request_data = &param,
+               .request_size = sizeof(param),
+       };
+       int ret;
+       bool enable;
+
+       ret = kstrtobool(buf, &enable);
+       if (ret)
+               return ret;
+
+       /* Invert input parameter, EC expects 0=on and 1=off */
+       param = enable ? 0 : 1;
+
+       ret = wilco_ec_mailbox(ec, &msg);
+       if (ret < 0)
+               return ret;
+
+       return count;
+}
+
+static DEVICE_ATTR_RO(version);
+static DEVICE_ATTR_WO(stealth_mode);
+
+static struct attribute *wilco_ec_attrs[] = {
+       &dev_attr_version.attr,
+       &dev_attr_stealth_mode.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(wilco_ec);
+
+int wilco_ec_sysfs_init(struct wilco_ec_device *ec)
+{
+       return sysfs_create_groups(&ec->dev->kobj, wilco_ec_groups);
+}
+
+void wilco_ec_sysfs_remove(struct wilco_ec_device *ec)
+{
+       sysfs_remove_groups(&ec->dev->kobj, wilco_ec_groups);
+}
--
Quoted 2 lines

2.20.0.405.gbc1bbc6f85-goog

Thanks,
Enric

#7 lore raw

Re: [RFC PATCH 03/10] CHROMIUM: wilco_ec: Add sysfs attributes

Quoted 29 lines · Enric Balletbo Serra

Hi Nick,

Many thanks for sending this upstream. Adding Guenter as also might be interested, and some comments and questions below.

Missatge de Nick Crews <[email protected]> del dia ds., 15 de des. 2018 a les 1:20:

Quoted 12 lines

From: Duncan Laurie <[email protected]>

Add some sample sysfs attributes for the Wilco EC that show how the mailbox interface works.

Quoted 1 line

cat /sys/bus/platform/devices/GOOG000C\:00/version

Label : 99.99.99
SVN Revision : 738ed.99
Model Number : 08;8
Build Date : 08/30/18

Quoted 8 lines

---

 drivers/platform/chrome/Makefile           |   3 +-
 drivers/platform/chrome/wilco_ec.h         |  14 +++
 drivers/platform/chrome/wilco_ec_mailbox.c |  12 ++
 drivers/platform/chrome/wilco_ec_sysfs.c   | 121 +++++++++++++++++++++
Quoted 6 lines

I am wondering if this should have its own driver (like we recently did for cros_ec) or if it should be integrated into the wilco core. Also, I am wondering if this should be attached to the cros_ec_class or not ( /sys/class/chromeos/wilco_ec ? ). Guenter and Benson, any opinion here?

I would agree.

Quoted 139 lines

Anyway, you should add the documentation of the sysfs entries, please.

drivers/platform/chrome/Makefile cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC) += cros_ec_lpc_mec.o · line 16

Quoted 12 lines
 4 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs.c
drivers/platform/chrome/Makefile +0 -1
index b132ba5b3e3d..e8603bc5b095 100644
@@ -13,6 +13,5 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)        += cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)              += cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)            += cros_ec_proto.o
 
-obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)   += cros_kbd_led_backlight.o

I guess that you don't want to do this :)

Quoted 60 lines
drivers/platform/chrome/Makefile +1 -1
-wilco_ec-objs                          := wilco_ec_mailbox.o
+wilco_ec-objs                          := wilco_ec_mailbox.o wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
drivers/platform/chrome/wilco_ec.h +14 -0
index ba16fcff87c4..699f4cf744dc 100644
@@ -94,4 +94,18 @@ struct wilco_ec_message {
  */
 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
 
+/**
+ * wilco_ec_sysfs_init() - Create sysfs attributes.
+ * @ec: EC device.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_sysfs_init(struct wilco_ec_device *ec);
+
+/**
+ * wilco_ec_sysfs_remove() - Remove sysfs attributes.
+ * @ec: EC device.
+ */
+void wilco_ec_sysfs_remove(struct wilco_ec_device *ec);
+
 #endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_mailbox.c +12 -0
index 6613c18c2a82..414ea0a8ad03 100644
@@ -361,11 +361,23 @@ static int wilco_ec_probe(struct platform_device *pdev)
        cros_ec_lpc_mec_init(ec->io_packet->start,
                             ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+       /* Create sysfs attributes for userspace interaction */
+       if (wilco_ec_sysfs_init(ec) < 0) {
+               dev_err(dev, "Failed to create sysfs attributes\n");
+               cros_ec_lpc_mec_destroy();
+               return -ENODEV;
+       }
+
        return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+       struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+       /* Remove sysfs attributes */
+       wilco_ec_sysfs_remove(ec);
+
        /* Teardown cros_ec interface */
        cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec_sysfs.c new file +0 -0
new file mode 100644
index 000000000000..f9ae6cef6169

drivers/platform/chrome/wilco_ec_sysfs.c line 3

Quoted 3 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +3 -0
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * wilco_ec_sysfs - Sysfs attributes for Wilco Embedded Controller

Remove 'wilco_ec_sysfs -'.

drivers/platform/chrome/wilco_ec_sysfs.c line 14

Quoted 11 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +11 -0
+ *
+ * Copyright 2018 Google LLC
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

Remove the license boiler-plate.

Quoted 42 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +42 -0
+ */
+
+#include <linux/ctype.h>
+#include <linux/platform_device.h>
+#include <linux/sysfs.h>
+#include "wilco_ec.h"
+
+#define EC_COMMAND_EC_INFO             0x38
+#define EC_INFO_SIZE                    9
+#define EC_COMMAND_STEALTH_MODE                0xfc
+
+struct ec_info {
+       u8 index;
+       const char *label;
+};
+
+static ssize_t wilco_ec_show_info(struct wilco_ec_device *ec, char *buf,
+                                 ssize_t count, struct ec_info *info)
+{
+       char result[EC_INFO_SIZE];
+       struct wilco_ec_message msg = {
+               .type = WILCO_EC_MSG_LEGACY,
+               .command = EC_COMMAND_EC_INFO,
+               .request_data = &info->index,
+               .request_size = sizeof(info->index),
+               .response_data = result,
+               .response_size = EC_INFO_SIZE,
+       };
+       int ret;
+
+       ret = wilco_ec_mailbox(ec, &msg);
+       if (ret != EC_INFO_SIZE)
+               return scnprintf(buf + count, PAGE_SIZE - count,
+                                "%-12s : ERROR %d\n", info->label, ret);
+
+       return scnprintf(buf + count, PAGE_SIZE - count,
+                        "%-12s : %s\n", info->label, result);
+}
+
+static ssize_t version_show(struct device *dev,
+                           struct device_attribute *attr, char *buf)
+{
Quoted 8 lines

drivers/platform/chrome/wilco_ec_sysfs.c line 64

Quoted 8 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +8 -0
+       struct wilco_ec_device *ec = dev_get_drvdata(dev);
+       struct ec_info wilco_ec_info[] = {
+               { 0, "Label" },
+               { 1, "SVN Revision" },
+               { 2, "Model Number" },
+               { 3, "Build Date" },
+               { 0xff, NULL },
+       };

sysfs conventions ask for separate attributes for each of those.

Quoted 22 lines
Quoted 22 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +22 -0
+       struct ec_info *info = wilco_ec_info;
+       ssize_t c = 0;
+
+       for (info = wilco_ec_info; info->label; info++)
+               c += wilco_ec_show_info(ec, buf, c, info);
+
+       return c;
+}
+
+static ssize_t stealth_mode_store(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(dev);
+       u8 param;
+       struct wilco_ec_message msg = {
+               .type = WILCO_EC_MSG_LEGACY,
+               .command = EC_COMMAND_STEALTH_MODE,
+               .request_data = &param,
+               .request_size = sizeof(param),
+       };
+       int ret;
Quoted 8 lines

drivers/platform/chrome/wilco_ec_sysfs.c line 94

Quoted 8 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +8 -0
+       bool enable;
+
+       ret = kstrtobool(buf, &enable);
+       if (ret)
+               return ret;
+
+       /* Invert input parameter, EC expects 0=on and 1=off */
+       param = enable ? 0 : 1;

Why not just !enable ?

Quoted 33 lines
Quoted 28 lines
drivers/platform/chrome/wilco_ec_sysfs.c new file +27 -1
+
+       ret = wilco_ec_mailbox(ec, &msg);
+       if (ret < 0)
+               return ret;
+
+       return count;
+}
+
+static DEVICE_ATTR_RO(version);
+static DEVICE_ATTR_WO(stealth_mode);
+
+static struct attribute *wilco_ec_attrs[] = {
+       &dev_attr_version.attr,
+       &dev_attr_stealth_mode.attr,
+       NULL
+};
+ATTRIBUTE_GROUPS(wilco_ec);
+
+int wilco_ec_sysfs_init(struct wilco_ec_device *ec)
+{
+       return sysfs_create_groups(&ec->dev->kobj, wilco_ec_groups);
+}
+
+void wilco_ec_sysfs_remove(struct wilco_ec_device *ec)
+{
+       sysfs_remove_groups(&ec->dev->kobj, wilco_ec_groups);
+}
--
Quoted 2 lines

2.20.0.405.gbc1bbc6f85-goog

Thanks,
Enric

author
#8 lore raw

RFC PATCH 4/10 CHROMIUM: wilco_ec: Add support for raw commands in sysfs

In reply to Nick Crews

From: Duncan Laurie <[email protected]>

Add a sysfs attribute that allows sending raw commands to the EC. This is useful for development and debug but should not be enabled in a production environment.

Quoted 2 lines

echo 00 f0 38 00 03 00 > /sys/bus/platform/devices/GOOG000C\:00/raw cat /sys/bus/platform/devices/GOOG000C\:00/raw

00 37 33 38 65 64 00...

---

 drivers/platform/chrome/Kconfig            |  10 ++
 drivers/platform/chrome/wilco_ec.h         |   6 +
 drivers/platform/chrome/wilco_ec_mailbox.c |   6 -
 drivers/platform/chrome/wilco_ec_sysfs.c   | 126 +++++++++++++++++++++
 4 files changed, 142 insertions(+), 6 deletions(-)
drivers/platform/chrome/Kconfig +10 -0
index 4168d5e6bedc..05c6d9a00395 100644
@@ -61,6 +61,16 @@ config WILCO_EC
 	  To compile this driver as a module, choose M here: the
 	  module will be called wilco_ec.
 
+config WILCO_EC_SYSFS_RAW
+	bool "Enable raw access to EC via sysfs"
+	depends on WILCO_EC
+	default n
+	help
+	  If you say Y here, you get support for sending raw commands to
+	  the Wilco EC via sysfs.  These commands do not do any byte
+	  manipulation and allow for testing arbitrary commands.  This
+	  interface is intended for debug only and is disabled by default.
+
 config CROS_EC_CTL
         tristate
 
drivers/platform/chrome/wilco_ec.h +6 -0
index 699f4cf744dc..0b3dec4e2830 100644
@@ -20,6 +20,12 @@
 #include <linux/device.h>
 #include <linux/kernel.h>
 
+/* Normal commands have a maximum 32 bytes of data */
+#define EC_MAILBOX_DATA_SIZE		32
+
+/* Extended commands have 256 bytes of response data */
+#define EC_MAILBOX_DATA_SIZE_EXTENDED	256
+
 #define WILCO_EC_FLAG_NO_RESPONSE	BIT(0) /* EC does not respond */
 #define WILCO_EC_FLAG_EXTENDED_DATA	BIT(1) /* EC returns 256 data bytes */
 #define WILCO_EC_FLAG_RAW_REQUEST	BIT(2) /* Do not trim request data */
drivers/platform/chrome/wilco_ec_mailbox.c +0 -6
index 414ea0a8ad03..1cb34b7280fd 100644
@@ -44,12 +44,6 @@
 /* Version of EC protocol */
 #define EC_MAILBOX_PROTO_VERSION	3
 
-/* Normal commands have a maximum 32 bytes of data */
-#define EC_MAILBOX_DATA_SIZE		32
-
-/* Extended commands have 256 bytes of response data */
-#define EC_MAILBOX_DATA_SIZE_EXTENDED	256
-
 /* Number of header bytes to be counted as data bytes */
 #define EC_MAILBOX_DATA_EXTRA		2
 
drivers/platform/chrome/wilco_ec_sysfs.c +126 -0

142 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#9 lore raw

RFC PATCH 5/10 CHROMIUM: wilco_ec: Add RTC class driver

In reply to Nick Crews

From: Duncan Laurie <[email protected]>

This Embedded Controller has an internal RTC that is exposed as a standard RTC class driver with read/write functionality.

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00

---

 drivers/platform/chrome/Makefile           |   3 +-
 drivers/platform/chrome/wilco_ec.h         |  29 ++++
 drivers/platform/chrome/wilco_ec_mailbox.c |  15 ++
 drivers/platform/chrome/wilco_ec_rtc.c     | 163 +++++++++++++++++++++
 4 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_rtc.c
drivers/platform/chrome/Makefile +2 -1
index e8603bc5b095..5ca484c2d0d7 100644
@@ -13,5 +13,6 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)	+= cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
-wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_sysfs.o
+wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_rtc.o \
+					   wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec.h +29 -0
index 0b3dec4e2830..eee5c514e720 100644
@@ -19,6 +19,7 @@
 
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/rtc.h>
 
 /* Normal commands have a maximum 32 bytes of data */
 #define EC_MAILBOX_DATA_SIZE		32
@@ -55,6 +56,7 @@ enum wilco_ec_msg_type {
  * @data_buffer: Buffer used for EC communication.  The same buffer
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
+ * @rtc: RTC device handler.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -64,6 +66,7 @@ struct wilco_ec_device {
 	struct resource *io_packet;
 	void *data_buffer;
 	size_t data_size;
+	struct rtc_device *rtc;
 };
 
 /**
@@ -114,4 +117,30 @@ int wilco_ec_sysfs_init(struct wilco_ec_device *ec);
  */
 void wilco_ec_sysfs_remove(struct wilco_ec_device *ec);
 
+/**
+ * wilco_ec_rtc_read() - Fill RTC time structure with values from the EC.
+ * @dev: EC device.
+ * @tm: Returns RTC time from EC.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm);
+
+/**
+ * wilco_ec_rtc_write() - Write EC time/date from RTC time structure.
+ * @dev: EC device.
+ * @tm: RTC time to write to EC.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm);
+
+/**
+ * wilco_ec_rtc_sync() - Write EC time/date from system time.
+ * @dev: EC device.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_rtc_sync(struct device *dev);
+
 #endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_mailbox.c +15 -0
index 1cb34b7280fd..2f093a281a30 100644
@@ -33,6 +33,7 @@
 #include <linux/mfd/cros_ec_lpc_mec.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/rtc.h>
 #include "wilco_ec.h"
 
 /* Version of mailbox interface */
@@ -325,6 +326,11 @@ static struct resource *wilco_get_resource(struct platform_device *pdev,
 	return res;
 }
 
+static const struct rtc_class_ops wilco_ec_rtc_ops = {
+	.read_time = wilco_ec_rtc_read,
+	.set_time = wilco_ec_rtc_write,
+};
+
 static int wilco_ec_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -355,6 +361,15 @@ static int wilco_ec_probe(struct platform_device *pdev)
 	cros_ec_lpc_mec_init(ec->io_packet->start,
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+	/* Install RTC driver */
+	ec->rtc = devm_rtc_device_register(ec->dev, "wilco_ec",
+					   &wilco_ec_rtc_ops, THIS_MODULE);
+	if (IS_ERR(ec->rtc)) {
+		dev_err(dev, "Failed to install RTC driver\n");
+		cros_ec_lpc_mec_destroy();
+		return PTR_ERR(ec->rtc);
+	}
+
 	/* Create sysfs attributes for userspace interaction */
 	if (wilco_ec_sysfs_init(ec) < 0) {
 		dev_err(dev, "Failed to create sysfs attributes\n");
drivers/platform/chrome/wilco_ec_rtc.c new file +163 -0

163 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#10 lore raw

RFC PATCH 6/10 CHROMIUM: wilco_ec: Add event handling

In reply to Nick Crews

From: Duncan Laurie <[email protected]>

The Wilco Embedded Controller can return extended events that are not handled by standard ACPI objects. These events can include hotkeys which map to standard functions like brightness controls, or information about EC controlled features like the charger or battery.

These events are triggered with an ACPI Notify(0x90) and the event data buffer is read through an ACPI method provided by the BIOS which reads the event buffer from EC RAM.

These events are then processed, with hotkey events being sent to the input subsystem and other events put into a queue which can be read by a userspace daemon via a sysfs attribute.

Quoted 1 line

evtest /dev/input/event6

Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x0 product 0x0 version 0x0 Input device name: "Wilco EC hotkeys"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 224 (KEY_BRIGHTNESSDOWN)
Event code 225 (KEY_BRIGHTNESSUP)
Event code 240 (KEY_UNKNOWN)
Event type 4 (EV_MSC)
Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit)
Event: type 4 (EV_MSC), code 4 (MSC_SCAN), value 57
Event: type 1 (EV_KEY), code 224 (KEY_BRIGHTNESSDOWN), value 1
Event: -------------- SYN_REPORT ------------
Event: type 1 (EV_KEY), code 224 (KEY_BRIGHTNESSDOWN), value 0
Event: -------------- SYN_REPORT ------------
Event: type 4 (EV_MSC), code 4 (MSC_SCAN), value 58
Event: type 1 (EV_KEY), code 225 (KEY_BRIGHTNESSUP), value 1
Event: -------------- SYN_REPORT ------------
Event: type 1 (EV_KEY), code 225 (KEY_BRIGHTNESSUP), value 0
Event: -------------- SYN_REPORT ------------

---

 drivers/platform/chrome/Makefile           |   4 +-
 drivers/platform/chrome/wilco_ec.h         |  34 ++
 drivers/platform/chrome/wilco_ec_event.c   | 343 +++++++++++++++++++++
 drivers/platform/chrome/wilco_ec_mailbox.c |  11 +
 4 files changed, 390 insertions(+), 2 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_event.c
drivers/platform/chrome/Makefile +2 -2
index 5ca484c2d0d7..60b19190dba1 100644
@@ -13,6 +13,6 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)	+= cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
-wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_rtc.o \
-					   wilco_ec_sysfs.o
+wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_event.o \
+					   wilco_ec_rtc.o wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec.h +34 -0
index eee5c514e720..6f084188faa1 100644
@@ -18,7 +18,9 @@
 #define WILCO_EC_H
 
 #include <linux/device.h>
+#include <linux/input.h>
 #include <linux/kernel.h>
+#include <linux/list.h>
 #include <linux/rtc.h>
 
 /* Normal commands have a maximum 32 bytes of data */
@@ -46,6 +48,22 @@ enum wilco_ec_msg_type {
 	WILCO_EC_MSG_TELEMETRY = 0x00f5,
 };
 
+/**
+ * struct wilco_ec_event - EC extended events.
+ * @lock: Mutex to guard the list of events.
+ * @list: Queue of EC events to be provided to userspace.
+ * @attr: Sysfs attribute for userspace to read events.
+ * @count: Count of events in the queue.
+ * @input: Input device for hotkey events.
+ */
+struct wilco_ec_event {
+	struct mutex lock;
+	struct list_head list;
+	struct bin_attribute attr;
+	size_t count;
+	struct input_dev *input;
+};
+
 /**
  * struct wilco_ec_device - Wilco Embedded Controller handle.
  * @dev: Device handle.
@@ -57,6 +75,7 @@ enum wilco_ec_msg_type {
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @rtc: RTC device handler.
+ * @event: EC extended event handler.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -67,6 +86,7 @@ struct wilco_ec_device {
 	void *data_buffer;
 	size_t data_size;
 	struct rtc_device *rtc;
+	struct wilco_ec_event event;
 };
 
 /**
@@ -143,4 +163,18 @@ int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm);
  */
 int wilco_ec_rtc_sync(struct device *dev);
 
+/**
+ * wilco_ec_event_init() - Prepare to handle EC events.
+ * @ec: EC device.
+ *
+ * Return: 0 for success or negative error code on failure.
+ */
+int wilco_ec_event_init(struct wilco_ec_device *ec);
+
+/**
+ * wilco_ec_event_remove() - Remove EC event handler.
+ * @ec: EC device.
+ */
+void wilco_ec_event_remove(struct wilco_ec_device *ec);
+
 #endif /* WILCO_EC_H */
drivers/platform/chrome/wilco_ec_event.c new file +343 -0

343 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_mailbox.c +11 -0
index 2f093a281a30..25c2c539c813 100644
@@ -377,6 +377,14 @@ static int wilco_ec_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
+	/* Prepare to handle events */
+	if (wilco_ec_event_init(ec) < 0) {
+		dev_err(dev, "Failed to setup event handling\n");
+		wilco_ec_sysfs_remove(ec);
+		cros_ec_lpc_mec_destroy();
+		return -ENODEV;
+	}
+
 	return 0;
 }
 
@@ -384,6 +392,9 @@ static int wilco_ec_remove(struct platform_device *pdev)
 {
 	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+	/* Stop handling EC events */
+	wilco_ec_event_remove(ec);
+
 	/* Remove sysfs attributes */
 	wilco_ec_sysfs_remove(ec);
 
Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#11 lore raw

RFC PATCH 7/10 CHROMIUM: wilco_ec: Move legacy attributes to separate file

In reply to Nick Crews

Legacy attributes are EC properties that are non-chromebook specific, ones which existed before the EC was modified for Chromebooks (as I understand it at least). This adds no new behavior, but just refactors the existing legacy attributes so adding more attributes in the future will work in an elegant way.

wilco_sysfs.c should just contain the information you need to understand how to use the sysfs interface. It will only specify the names of the attributes and their location within the directory structure. The API and the implementation for all the attributes will be located in individual external files, so if you want to see how to use an attribute, you can look that that specific file. This should help with the problem of this sysfs file becoming way too big to understand.

With this new structure, it should be easy to add new attributes by making new implementation files, #include-ing them in wilco_ec_sysfs.c, and then defining where to actually place the attributes in wilco_ec_sysfs.c

---

 drivers/platform/chrome/Makefile          |   3 +-
 drivers/platform/chrome/wilco_ec_legacy.c | 204 ++++++++++++++++++
 drivers/platform/chrome/wilco_ec_legacy.h |  96 +++++++++
 drivers/platform/chrome/wilco_ec_sysfs.c  | 248 ++++------------------
 4 files changed, 343 insertions(+), 208 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.c
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.h
drivers/platform/chrome/Makefile +2 -1
index 60b19190dba1..56c39de8e5f5 100644
@@ -14,5 +14,6 @@ obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
 wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_event.o \
-					   wilco_ec_rtc.o wilco_ec_sysfs.o
+					   wilco_ec_rtc.o wilco_ec_legacy.o \
+					   wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec_legacy.c new file +204 -0

204 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_legacy.h new file +96 -0

96 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_sysfs.c +41 -207

271 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#12 lore raw

RFC PATCH 8/10 CHROMIUM: wilco_ec: Add EC properties

In reply to Nick Crews

A Property is typically a data item that is stored to NVRAM. Each of these data items has an index associated with it known as the Property ID (PID). The Property ID is used by the system BIOS (and EC) to refer to the Property. Properties may have variable lengths. Many features are implemented primarily by EC Firmware with system BIOS just supporting user configuration via BIOS SETUP and/or SMBIOS changes. In order to implement many of these types of features the user configuration information is saved to and retrieved from the EC. The EC stores this configuration information to NVRAM and then can use it while the system BIOS is not running or during early boot. Although this is a typical scenario there may be other reasons to store information in the EC NVRAM instead of the System NVRAM. Most of the property services do not have a valid failure condition, so this field can be ignored. For items that are write once, a failure is returned when a second write is attempted.

Add a get and set interface for EC properties. properties live within the "properties" directory. Most of the added properties are boolean, but this also provides the interface for non-boolean properties, which will be used late for scheduling power routines.

The wilco_ec_sysfs_util.h stuff will be used for
future attributes as well.

Quoted 2 lines

cd /sys/bus/platform/devices/GOOG000C\:00/
echo 0 > properties/global_mic_mute_led

[mic mute led on keyboard turns off]

Quoted 1 line

cat

0

Quoted 1 line

echo 1 > properties/global_mic_mute_led

[mic mute led on keyboard turns on]

Quoted 1 line

cat properties/global_mic_mute_led

1

Quoted 1 line

cat properties/wireless_sw_wlan

cat: wireless_sw_wlan: Permission denied
[Good, that is supposed to be write-only]

Quoted 1 line

echo 0 > properties/wireless_sw_wlan

---

 drivers/platform/chrome/Makefile              |   1 +
 drivers/platform/chrome/wilco_ec_properties.c | 327 ++++++++++++++++++
 drivers/platform/chrome/wilco_ec_properties.h | 163 +++++++++
 drivers/platform/chrome/wilco_ec_sysfs.c      |  66 +++-
 drivers/platform/chrome/wilco_ec_sysfs_util.h |  47 +++
 5 files changed, 598 insertions(+), 6 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.c
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.h
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs_util.h
drivers/platform/chrome/Makefile +1 -0
index 56c39de8e5f5..eefb75e5e69c 100644
@@ -15,5 +15,6 @@ obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
 wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_event.o \
 					   wilco_ec_rtc.o wilco_ec_legacy.o \
+					   wilco_ec_properties.o \
 					   wilco_ec_sysfs.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec_properties.c new file +327 -0

327 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_properties.h new file +163 -0

163 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_sysfs.c +60 -6

94 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_sysfs_util.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#13 lore raw

RFC PATCH 9/10 CHROMIUM: wilco_ec: Add peakshift and adv_batt_charging

In reply to Nick Crews

Create "peakshift" and "advanced_battery_charging" directories within the "properties" directory, and create the relevant attributes within these. These properties have to do with configuring some of the advanced power management options that prolong battery health and reduce energy use at peak hours of the day.

Scheduling events uses a 24 hour clock, and only supports time intervals of 15 minutes. For example, to set advanced_battery_charging to start at 4:15pm and to last for 6 hours and 45 minutes, you would use the argument "16 15 6 45".

Quoted 3 lines

cd /sys/bus/platform/devices/GOOG000C\:00 cat properties/peakshift/peakshift_battery_threshold 015

[means 15 percent]

Quoted 1 line

cat properties/peakshift/peakshift_monday

16 00 20 30 00 00
[starts at 4:00 pm, ends at 8:30, charging resumes at midnight]

Quoted 1 line

echo "16 00 20 31 00 00" > properties/peakshift/peakshift_monday

-bash: echo: write error: Invalid argument

Quoted 1 line

dmesg | tail -n1

[40.34534] wilco_ec GOOG00C:00: minutes must be at the quarter hour
Quoted 2 lines

echo "16 0 20 45 0 0" > properties/peakshift/peakshift_monday cat properties/peakshift/peakshift_monday

16 00 20 45 00 00

---

 drivers/platform/chrome/Makefile             |   3 +-
 drivers/platform/chrome/wilco_ec_adv_power.c | 533 +++++++++++++++++++
 drivers/platform/chrome/wilco_ec_adv_power.h | 193 +++++++
 drivers/platform/chrome/wilco_ec_sysfs.c     | 103 ++++
 4 files changed, 831 insertions(+), 1 deletion(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.c
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.h
drivers/platform/chrome/Makefile +2 -1
index eefb75e5e69c..c9d3d44098f9 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 
 wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_event.o \
 					   wilco_ec_rtc.o wilco_ec_legacy.o \
+					   wilco_ec_sysfs.o \
 					   wilco_ec_properties.o \
-					   wilco_ec_sysfs.o
+					   wilco_ec_adv_power.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec_adv_power.c new file +533 -0

533 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_adv_power.h new file +193 -0

193 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_sysfs.c +103 -0

130 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
author
#14 lore raw

RFC PATCH 10/10 CHROMIUM: wilco_ec: Add binary telemetry attributes

In reply to Nick Crews

The Wilco Embedded Controller is able to send telemetry data which is useful for enterprise applications. A daemon running on the OS sends a command (possibly with args) to the EC via this sysfs interface, and the EC responds over the sysfs interface with the response. Both the request and the response are in an opaque binary format so that information which is proprietary to the enterprise service provider is secure.

At this point, the Wilco EC does not implement this telemetry functionality, so any request using the WILCO_EC_MSG_TELEMETRY command returns an error. This is just an initial change for comments, until the EC code is implemented.

---

 drivers/platform/chrome/Makefile             |  3 +-
 drivers/platform/chrome/wilco_ec_sysfs.c     | 17 ++++-
 drivers/platform/chrome/wilco_ec_telemetry.c | 66 ++++++++++++++++++++
 drivers/platform/chrome/wilco_ec_telemetry.h | 42 +++++++++++++
 4 files changed, 126 insertions(+), 2 deletions(-)
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.c
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.h
drivers/platform/chrome/Makefile +2 -1
index c9d3d44098f9..62444887ce18 100644
@@ -17,5 +17,6 @@ wilco_ec-objs				:= wilco_ec_mailbox.o wilco_ec_event.o \
 					   wilco_ec_rtc.o wilco_ec_legacy.o \
 					   wilco_ec_sysfs.o \
 					   wilco_ec_properties.o \
-					   wilco_ec_adv_power.o
+					   wilco_ec_adv_power.o  \
+					   wilco_ec_telemetry.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec_sysfs.c +16 -1
index 81dded951058..0019d17c0cf9 100644
@@ -19,6 +19,7 @@
 #include "wilco_ec_legacy.h"
 #include "wilco_ec_properties.h"
 #include "wilco_ec_adv_power.h"
+#include "wilco_ec_telemetry.h"
 
 #define WILCO_EC_ATTR_RO(_name)						\
 __ATTR(_name, 0444, wilco_ec_##_name##_show, NULL)
@@ -46,7 +47,21 @@ static struct attribute *wilco_ec_toplevel_attrs[] = {
 	NULL
 };
 
-ATTRIBUTE_GROUPS(wilco_ec_toplevel);
+static struct bin_attribute telem_attr = TELEMETRY_BIN_ATTR(telemetry);
+static struct bin_attribute *telem_attrs[] = {
+	&telem_attr,
+	NULL
+};
+
+static const struct attribute_group wilco_ec_toplevel_group = {
+	.attrs = wilco_ec_toplevel_attrs,
+	.bin_attrs = telem_attrs,
+};
+
+static const struct attribute_group *wilco_ec_toplevel_groups[] = {
+	&wilco_ec_toplevel_group,
+	NULL,
+};
 
 /* Make property attributes, which will live inside GOOG000C:00/properties/  */
 
drivers/platform/chrome/wilco_ec_telemetry.c new file +66 -0

66 lines not on this page · raw message

drivers/platform/chrome/wilco_ec_telemetry.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.20.0.405.gbc1bbc6f85-goog
#15 lore raw
In reply to Nick Crews

Hi Nick,

Many thanks for sending this upstream. Adding Guenter as also might be interested and some few questions regarding the driver structure.

Missatge de Nick Crews <[email protected]> del dia ds., 15 de des. 2018 a les 1:21:

Quoted 87 lines

The Chromebook named wilco contains a different Embedded Controller than the rest of the chromebook series, and thus the kernel requires a different driver than the already existing and generalized cros_ec_* drivers. Specifically, this driver adds support for getting and setting the RTC on the EC, adding a binary sysfs attribute that receives ACPI events from the EC, adding a binary sysfs attribute to request telemetry data from the EC (useful for enterprise applications), and adding normal sysfs attributes to get/set various other properties on the EC. The core of the communication with the EC is implemented in wilco_ec_mailbox.c, using a simple byte-level protocol with a checksum, transmitted over an eSPI bus. For debugging purposes, a raw attribute is also provided which can write/read arbitrary bytes to/from the eSPI bus.

We attempted to adhere to the sysfs principles of "one piece of data per attribute" as much as possible, and mostly succeded. However, with the wilco_ec_adv_power.h attributes, which deal with scheduling power usage, we found it most elegant to bundle setting event times for an entire day into a single attribute, so at most you are using attributes formatted as "%d %d %d %d %d %d". With the telemetry attribute, we had to use a binary attribute, instead of the preferable human-readable ascii, in order to keep secure the information which is proprietary to the enterprise service provider. This opaque binary data will be read and sent using a proprietary daemon running on the OS. Finally, the "version" attribute returns a formatted result that looks something like:

Quoted 1 line

cat /sys/bus/platform/devices/GOOG000C\:00/version

Label : 95.00.06
SVN Revision : 5960a.06
Model Number : 08;8
Build Date : 11/29/18

The RTC driver is exposed as a standard RTC class driver with read/write functionality.

For event notification, the Wilco EC can return extended events that are not handled by standard ACPI objects. These events can include hotkeys which map to standard functions like brightness controls, or information about EC controlled features like the charger or battery. These events are triggered with an ACPI Notify(0x90) and the event data buffer is read through an ACPI method provided by the BIOS which reads the event buffer from EC RAM. These events are then processed, with hotkey events being sent to the input subsystem and other events put into a queue which can be read by a userspace daemon via a sysfs attribute.

The rest of the attributes are categorized as either "properties" or "legacy". "legacy" implies that the attribute existed on the EC before it was modified for ChromeOS, and "properties" implies that the attribute exposes functionality that was added to the EC specifically for ChromeOS. They are mostly boolean flags or percentages.

A full thread of the development of these patches can be found at
https://chromium-review.googlesource.com/c/1371034. This thread contains comments and revisions that could be helpful in understanding how the driver arrived at the state it is in now. The thread also contains some ChromeOS specific patches that actually enable the driver. If you want to test the patch yourself, you would have to install the ChromeOS SDK and cherry pick in these patches.

I also wrote some integration tests using the Tast testing framework that ChromeOS uses. It would require a full ChromeOS SDK to actually run the tests, but the source of the tests, written in Go, are useful for understanding what the desired behavior is. You can view the tests here:
https://chromium-review.googlesource.com/c/1372575

This is still an initial version of the driver, and we are sending it upstream for comments now, so that we can incorporate any requested changes such that it eventually can be merged. Thank you for your comments!

Duncan Laurie (6):
CHROMIUM: cros_ec: Remove cros_ec dependency in lpc_mec
CHROMIUM: wilco_ec: Add new driver for Wilco EC
CHROMIUM: wilco_ec: Add sysfs attributes
CHROMIUM: wilco_ec: Add support for raw commands in sysfs
CHROMIUM: wilco_ec: Add RTC class driver
CHROMIUM: wilco_ec: Add event handling

Nick Crews (4):
CHROMIUM: wilco_ec: Move legacy attributes to separate file
CHROMIUM: wilco_ec: Add EC properties
CHROMIUM: wilco_ec: Add peakshift and adv_batt_charging
CHROMIUM: wilco_ec: Add binary telemetry attributes

Quoted 15 lines
 drivers/platform/chrome/Kconfig               |  24 +-
 drivers/platform/chrome/Makefile              |   9 +-
 drivers/platform/chrome/cros_ec_lpc_mec.c     |  54 +-
 drivers/platform/chrome/cros_ec_lpc_mec.h     |  45 +-
 drivers/platform/chrome/cros_ec_lpc_reg.c     |  43 +-
 drivers/platform/chrome/wilco_ec.h            | 180 ++++++
 drivers/platform/chrome/wilco_ec_adv_power.c  | 533 ++++++++++++++++++
 drivers/platform/chrome/wilco_ec_adv_power.h  | 193 +++++++
 drivers/platform/chrome/wilco_ec_event.c      | 343 +++++++++++
 drivers/platform/chrome/wilco_ec_legacy.c     | 204 +++++++
 drivers/platform/chrome/wilco_ec_legacy.h     |  96 ++++
 drivers/platform/chrome/wilco_ec_mailbox.c    | 427 ++++++++++++++
 drivers/platform/chrome/wilco_ec_properties.c | 327 +++++++++++
 drivers/platform/chrome/wilco_ec_properties.h | 163 ++++++
 drivers/platform/chrome/wilco_ec_rtc.c        | 163 ++++++

Shouldn't this go to the RTC subsystem? Not sure if there are more drivers that can go to a specific subsystem.

Quoted 5 lines
 drivers/platform/chrome/wilco_ec_sysfs.c      | 253 +++++++++
 drivers/platform/chrome/wilco_ec_sysfs_util.h |  47 ++
 drivers/platform/chrome/wilco_ec_telemetry.c  |  66 +++
 drivers/platform/chrome/wilco_ec_telemetry.h  |  42 ++
 19 files changed, 3153 insertions(+), 59 deletions(-)

Wow, seems that this is going to be big, might make sense add a wilco_ec subdirectory for all this?

Quoted 15 lines
 create mode 100644 drivers/platform/chrome/wilco_ec.h
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.c
 create mode 100644 drivers/platform/chrome/wilco_ec_adv_power.h
 create mode 100644 drivers/platform/chrome/wilco_ec_event.c
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.c
 create mode 100644 drivers/platform/chrome/wilco_ec_legacy.h
 create mode 100644 drivers/platform/chrome/wilco_ec_mailbox.c
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.c
 create mode 100644 drivers/platform/chrome/wilco_ec_properties.h
 create mode 100644 drivers/platform/chrome/wilco_ec_rtc.c
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs.c
 create mode 100644 drivers/platform/chrome/wilco_ec_sysfs_util.h
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.c
 create mode 100644 drivers/platform/chrome/wilco_ec_telemetry.h
Quoted 3 lines

--
2.20.0.405.gbc1bbc6f85-goog

Thanks,
Enric