Skip to contentSkip to conversation

Add a driver for the ChromeOS snooping protection sensor (HPS)

PATCH Superseded Sami Kyöstilä 5 participants 15 messages Jan 27, 2022 – Feb 18, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Add a driver for the ChromeOS snooping protection sensor (HPS)

This series adds a driver for the ChromeOS snooping protection sensor (a.k.a. HPS) device, attached on the I2C bus. The sensor is a hardware-isolated module which reports a high-level presence signal, e.g., whether there is person in front of the computer or not.

The driver exposes the device to userspace as a character device, which can be used to send and receive messages to the sensor. The driver automatically turns power off to the sensor unless a process is

More information about HPS, its firmware and communication protocol can be found at
https://chromium.googlesource.com/chromiumos/platform/hps-firmware.

Sami Kyöstilä (2):
drivers/misc: add a driver for HPS
drivers/misc: add transfer ioctl for HPS

 MAINTAINERS              |   7 +
 drivers/misc/Kconfig     |  10 ++
 drivers/misc/Makefile    |   1 +
 drivers/misc/hps-i2c.c   | 304 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h |  20 +++
 5 files changed, 342 insertions(+)
 create mode 100644 drivers/misc/hps-i2c.c
 create mode 100644 include/uapi/linux/hps.h
Signature · 1 line
2.35.0.rc0.227.g00780c9af4-goog
#1 lore raw

PATCH 1/2 drivers/misc: add a driver for HPS

This patch introduces a driver for the ChromeOS snooping protection sensor (aka. HPS). The driver supports a sensor connected to the I2C bus and identified as "GOOG0020" in the ACPI tables.

When loaded, the driver exports the sensor to userspace through a character device. This initial version of the device only supports power management, i.e., communicating with the sensor must be done through I2C from userspace.

Power management is implemented by enabling the respective power GPIO while at least one userspace process holds an open fd on the character device. By default, the device is powered down if there are no active clients.

Note that the driver makes no effort to preserve the state of the sensor between power down and power up events. Userspace is responsible for reinitializing any needed state once power has been restored.

The device firmware, I2C protocol and other documentation is available at https://chromium.googlesource.com/chromiumos/platform/hps-firmware.

---

 MAINTAINERS            |   6 ++
 drivers/misc/Kconfig   |  10 ++
 drivers/misc/Makefile  |   1 +
 drivers/misc/hps-i2c.c | 223 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 240 insertions(+)
 create mode 100644 drivers/misc/hps-i2c.c
MAINTAINERS +6 -0
index ea3e6c914384..9dea4b8c2ab5 100644
@@ -8798,6 +8798,12 @@ S:	Maintained
 W:	http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi
 F:	fs/hpfs/
 
+HPS (ChromeOS snooping protection sensor) DRIVER
+M:	Sami Kyöstilä <[email protected]>
+R:	Evan Benn <[email protected]>
+S:	Maintained
+F:	drivers/misc/hps-i2c.c
+
 HSI SUBSYSTEM
 M:	Sebastian Reichel <[email protected]>
 S:	Maintained
drivers/misc/Kconfig +10 -0
index 0f5a49fc7c9e..b48b7803f537 100644
@@ -244,6 +244,16 @@ config HP_ILO
 	  To compile this driver as a module, choose M here: the
 	  module will be called hpilo.
 
+config HPS_I2C
+	tristate "ChromeOS HPS device support"
+	depends on HID && I2C && PM
+	help
+	  Say Y here if you want to enable support for the ChromeOS
+	  anti-snooping sensor (HPS), attached via I2C. The driver supports a
+	  sensor connected to the I2C bus and exposes it as a character device.
+	  To save power, the sensor is automatically powered down when no
+	  clients are accessing it.
+
 config QCOM_COINCELL
 	tristate "Qualcomm coincell charger support"
 	depends on MFD_SPMI_PMIC || COMPILE_TEST
drivers/misc/Makefile +1 -0
index a086197af544..162a7d530dab 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_SGI_GRU)		+= sgi-gru/
 obj-$(CONFIG_CS5535_MFGPT)	+= cs5535-mfgpt.o
 obj-$(CONFIG_GEHC_ACHC)		+= gehc-achc.o
 obj-$(CONFIG_HP_ILO)		+= hpilo.o
+obj-$(CONFIG_HPS_I2C)		+= hps-i2c.o
 obj-$(CONFIG_APDS9802ALS)	+= apds9802als.o
 obj-$(CONFIG_ISL29003)		+= isl29003.o
 obj-$(CONFIG_ISL29020)		+= isl29020.o
drivers/misc/hps-i2c.c new file +223 -0

223 lines not on this page · raw message

Signature · 1 line
2.35.0.rc0.227.g00780c9af4-goog
maintainer
#2 lore raw

Re: [PATCH 1/2] drivers/misc: add a driver for HPS

Quoted 14 lines · Sami Kyöstilä

This patch introduces a driver for the ChromeOS snooping protection sensor (aka. HPS). The driver supports a sensor connected to the I2C bus and identified as "GOOG0020" in the ACPI tables.

When loaded, the driver exports the sensor to userspace through a character device. This initial version of the device only supports power management, i.e., communicating with the sensor must be done through I2C from userspace.

Power management is implemented by enabling the respective power GPIO while at least one userspace process holds an open fd on the character device. By default, the device is powered down if there are no active clients.

Quoted 6 lines

Note that the driver makes no effort to preserve the state of the sensor between power down and power up events. Userspace is responsible for reinitializing any needed state once power has been restored.

The device firmware, I2C protocol and other documentation is available at https://chromium.googlesource.com/chromiumos/platform/hps-firmware.

How about a userspace tool that interacts with this new ioctl interface as well so that we can understand how the driver is supposed to work?

Quoted 85 lines

---

 MAINTAINERS            |   6 ++
 drivers/misc/Kconfig   |  10 ++
 drivers/misc/Makefile  |   1 +
 drivers/misc/hps-i2c.c | 223 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 240 insertions(+)
 create mode 100644 drivers/misc/hps-i2c.c
MAINTAINERS +6 -0
index ea3e6c914384..9dea4b8c2ab5 100644
@@ -8798,6 +8798,12 @@ S:	Maintained
 W:	http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi
 F:	fs/hpfs/
 
+HPS (ChromeOS snooping protection sensor) DRIVER
+M:	Sami Kyöstilä <[email protected]>
+R:	Evan Benn <[email protected]>
+S:	Maintained
+F:	drivers/misc/hps-i2c.c
+
 HSI SUBSYSTEM
 M:	Sebastian Reichel <[email protected]>
 S:	Maintained
drivers/misc/Kconfig +10 -0
index 0f5a49fc7c9e..b48b7803f537 100644
@@ -244,6 +244,16 @@ config HP_ILO
 	  To compile this driver as a module, choose M here: the
 	  module will be called hpilo.
 
+config HPS_I2C
+	tristate "ChromeOS HPS device support"
+	depends on HID && I2C && PM
+	help
+	  Say Y here if you want to enable support for the ChromeOS
+	  anti-snooping sensor (HPS), attached via I2C. The driver supports a
+	  sensor connected to the I2C bus and exposes it as a character device.
+	  To save power, the sensor is automatically powered down when no
+	  clients are accessing it.
+
 config QCOM_COINCELL
 	tristate "Qualcomm coincell charger support"
 	depends on MFD_SPMI_PMIC || COMPILE_TEST
drivers/misc/Makefile +1 -0
index a086197af544..162a7d530dab 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_SGI_GRU)		+= sgi-gru/
 obj-$(CONFIG_CS5535_MFGPT)	+= cs5535-mfgpt.o
 obj-$(CONFIG_GEHC_ACHC)		+= gehc-achc.o
 obj-$(CONFIG_HP_ILO)		+= hpilo.o
+obj-$(CONFIG_HPS_I2C)		+= hps-i2c.o
 obj-$(CONFIG_APDS9802ALS)	+= apds9802als.o
 obj-$(CONFIG_ISL29003)		+= isl29003.o
 obj-$(CONFIG_ISL29020)		+= isl29020.o
drivers/misc/hps-i2c.c new file +18 -0
new file mode 100644
index 000000000000..fe9f073b0352
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for the ChromeOS anti-snooping sensor (HPS), attached via I2C.
+ *
+ * The driver exposes HPS as a character device, although currently no read or
+ * write operations are supported. Instead, the driver only controls the power
+ * state of the sensor, keeping it on only while userspace holds an open file
+ * descriptor to the HPS device.
+ *
+ * Copyright 2022 Google LLC.
+ */
+
+#include <linux/acpi.h>
+#include <linux/cdev.h>
+#include <linux/fs.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>

drivers/misc/hps-i2c.c line 28

Quoted 10 lines
drivers/misc/hps-i2c.c new file +10 -0
+#include <linux/pm_runtime.h>
+
+#define HPS_ACPI_ID		"GOOG0020"
+#define HPS_MAX_DEVICES		1
+
+struct hps_drvdata {
+	struct i2c_client *client;
+
+	struct cdev cdev;
+	struct class *cdev_class;

As you only have 1 device, please just use the miscdev interface, not the chardev interface. Makes your code much smaller and easier to review and maintain over time and does not "burn" a whole major number for this tiny driver.

thanks,

greg k-h

#3 lore raw

Re: [PATCH 1/2] drivers/misc: add a driver for HPS

to 27. tammik. 2022 klo 20.40 Greg KH ([email protected]) kirjoitti:

Quoted 20 lines · Sami Kyöstilä
Quoted 18 lines

This patch introduces a driver for the ChromeOS snooping protection sensor (aka. HPS). The driver supports a sensor connected to the I2C bus and identified as "GOOG0020" in the ACPI tables.

When loaded, the driver exports the sensor to userspace through a character device. This initial version of the device only supports power management, i.e., communicating with the sensor must be done through I2C from userspace.

Power management is implemented by enabling the respective power GPIO while at least one userspace process holds an open fd on the character device. By default, the device is powered down if there are no active clients.

Note that the driver makes no effort to preserve the state of the sensor between power down and power up events. Userspace is responsible for reinitializing any needed state once power has been restored.

Quoted 5 lines
Quoted 2 lines

The device firmware, I2C protocol and other documentation is available at https://chromium.googlesource.com/chromiumos/platform/hps-firmware.

How about a userspace tool that interacts with this new ioctl interface as well so that we can understand how the driver is supposed to work?

Sure, here's a small example that shows how to read a magic register value from the device:
https://gist.github.com/skyostil/13d60a288919d18d00b20e81dfe018cd

(Let me know if you'd prefer this to be checked into the tree somewhere.)

Here's also a patch that makes the production daemon use this
interface: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/3353640

Quoted 92 lines
Quoted 92 lines

---

 MAINTAINERS            |   6 ++
 drivers/misc/Kconfig   |  10 ++
 drivers/misc/Makefile  |   1 +
 drivers/misc/hps-i2c.c | 223 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 240 insertions(+)
 create mode 100644 drivers/misc/hps-i2c.c
MAINTAINERS +6 -0
index ea3e6c914384..9dea4b8c2ab5 100644
@@ -8798,6 +8798,12 @@ S:     Maintained
 W:   http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi
 F:   fs/hpfs/
 
+HPS (ChromeOS snooping protection sensor) DRIVER
+M:   Sami Kyöstilä <[email protected]>
+R:   Evan Benn <[email protected]>
+S:   Maintained
+F:   drivers/misc/hps-i2c.c
+
 HSI SUBSYSTEM
 M:   Sebastian Reichel <[email protected]>
 S:   Maintained
drivers/misc/Kconfig +10 -0
index 0f5a49fc7c9e..b48b7803f537 100644
@@ -244,6 +244,16 @@ config HP_ILO
        To compile this driver as a module, choose M here: the
        module will be called hpilo.
 
+config HPS_I2C
+     tristate "ChromeOS HPS device support"
+     depends on HID && I2C && PM
+     help
+       Say Y here if you want to enable support for the ChromeOS
+       anti-snooping sensor (HPS), attached via I2C. The driver supports a
+       sensor connected to the I2C bus and exposes it as a character device.
+       To save power, the sensor is automatically powered down when no
+       clients are accessing it.
+
 config QCOM_COINCELL
      tristate "Qualcomm coincell charger support"
      depends on MFD_SPMI_PMIC || COMPILE_TEST
drivers/misc/Makefile +1 -0
index a086197af544..162a7d530dab 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_SGI_GRU)               += sgi-gru/
 obj-$(CONFIG_CS5535_MFGPT)   += cs5535-mfgpt.o
 obj-$(CONFIG_GEHC_ACHC)              += gehc-achc.o
 obj-$(CONFIG_HP_ILO)         += hpilo.o
+obj-$(CONFIG_HPS_I2C)                += hps-i2c.o
 obj-$(CONFIG_APDS9802ALS)    += apds9802als.o
 obj-$(CONFIG_ISL29003)               += isl29003.o
 obj-$(CONFIG_ISL29020)               += isl29020.o
drivers/misc/hps-i2c.c new file +25 -0
new file mode 100644
index 000000000000..fe9f073b0352
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for the ChromeOS anti-snooping sensor (HPS), attached via I2C.
+ *
+ * The driver exposes HPS as a character device, although currently no read or
+ * write operations are supported. Instead, the driver only controls the power
+ * state of the sensor, keeping it on only while userspace holds an open file
+ * descriptor to the HPS device.
+ *
+ * Copyright 2022 Google LLC.
+ */
+
+#include <linux/acpi.h>
+#include <linux/cdev.h>
+#include <linux/fs.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+
+#define HPS_ACPI_ID          "GOOG0020"
+#define HPS_MAX_DEVICES              1
+
+struct hps_drvdata {
+     struct i2c_client *client;
Quoted 8 lines

drivers/misc/hps-i2c.c line 28

Quoted 3 lines
drivers/misc/hps-i2c.c new file +3 -0
+
+     struct cdev cdev;
+     struct class *cdev_class;

As you only have 1 device, please just use the miscdev interface, not the chardev interface. Makes your code much smaller and easier to review and maintain over time and does not "burn" a whole major number for this tiny driver.

Will do, thanks for the tip!

Quoted 4 lines

thanks,

greg k-h

- Sami

maintainer
#4 lore raw

Re: [PATCH 1/2] drivers/misc: add a driver for HPS

Quoted 24 lines · Sami Kyostila

to 27. tammik. 2022 klo 20.40 Greg KH ([email protected]) kirjoitti:

Quoted 23 lines · Sami Kyöstilä
Quoted 20 lines

This patch introduces a driver for the ChromeOS snooping protection sensor (aka. HPS). The driver supports a sensor connected to the I2C bus and identified as "GOOG0020" in the ACPI tables.

When loaded, the driver exports the sensor to userspace through a character device. This initial version of the device only supports power management, i.e., communicating with the sensor must be done through I2C from userspace.

Power management is implemented by enabling the respective power GPIO while at least one userspace process holds an open fd on the character device. By default, the device is powered down if there are no active clients.

Note that the driver makes no effort to preserve the state of the sensor between power down and power up events. Userspace is responsible for reinitializing any needed state once power has been restored.

The device firmware, I2C protocol and other documentation is available at https://chromium.googlesource.com/chromiumos/platform/hps-firmware.

Quoted 8 lines
Quoted 2 lines

How about a userspace tool that interacts with this new ioctl interface as well so that we can understand how the driver is supposed to work?

Sure, here's a small example that shows how to read a magic register value from the device:
https://gist.github.com/skyostil/13d60a288919d18d00b20e81dfe018cd

(Let me know if you'd prefer this to be checked into the tree somewhere.)

It should be somewhere so we know where to look and how this works and who to complain to when it needs to be changed :)

thanks,

greg k-h

#5 lore raw

Re: [PATCH 1/2] drivers/misc: add a driver for HPS

In reply to Sami Kyöstilä

Hi!

drivers/misc/Kconfig config HP_ILO · line 256

Quoted 10 lines
drivers/misc/Kconfig +10 -0
+config HPS_I2C
+	tristate "ChromeOS HPS device support"
+	depends on HID && I2C && PM
+	help
+	  Say Y here if you want to enable support for the ChromeOS
+	  anti-snooping sensor (HPS), attached via I2C. The driver supports a
+	  sensor connected to the I2C bus and exposes it as a character device.
+	  To save power, the sensor is automatically powered down when no
+	  clients are accessing it.
+

We don't need to know about power management here; but it would be good to have explanation what anti-snooping sensor actually does. Is it camera detecting user or what?

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#6 lore raw

PATCH 2/2 drivers/misc: add transfer ioctl for HPS

In reply to Sami Kyöstilä

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
MAINTAINERS +1 -0
index 9dea4b8c2ab5..d5fc066fdbc2 100644
@@ -8803,6 +8803,7 @@ M:	Sami Kyöstilä <[email protected]>
 R:	Evan Benn <[email protected]>
 S:	Maintained
 F:	drivers/misc/hps-i2c.c
+F:	include/uapi/linux/hps.h
 
 HSI SUBSYSTEM
 M:	Sebastian Reichel <[email protected]>
drivers/misc/hps-i2c.c +81 -0

106 lines not on this page · raw message

include/uapi/linux/hps.h new file +20 -0
new file mode 100644
index 000000000000..2c1bd174cd02
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+/*
+ *  Copyright 2022 Google LLC.
+ */
+
+#ifndef _UAPI_HPS_H
+#define _UAPI_HPS_H
+
+#include <linux/types.h>
+
+#define HPS_IOC_TRANSFER	_IOWR('h', 0x01, struct hps_transfer_ioctl_data)
+
+struct hps_transfer_ioctl_data {
+	__u32 isize;			/* Number of bytes to send */
+	unsigned char __user *ibuf;	/* Input buffer */
+	__u32 osize;			/* Number of bytes to receive */
+	unsigned char __user *obuf;	/* Output buffer */
+};
+
+#endif /* _UAPI_HPS_H */
Signature · 1 line
2.35.0.rc0.227.g00780c9af4-goog
maintainer
#7 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

Quoted 92 lines · Sami Kyöstilä

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
MAINTAINERS +1 -0
index 9dea4b8c2ab5..d5fc066fdbc2 100644
@@ -8803,6 +8803,7 @@ M:	Sami Kyöstilä <[email protected]>
 R:	Evan Benn <[email protected]>
 S:	Maintained
 F:	drivers/misc/hps-i2c.c
+F:	include/uapi/linux/hps.h
 
 HSI SUBSYSTEM
 M:	Sebastian Reichel <[email protected]>
drivers/misc/hps-i2c.c +36 -0
index fe9f073b0352..748ead49d678 100644
@@ -17,9 +17,11 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <uapi/linux/hps.h>
 
 #define HPS_ACPI_ID		"GOOG0020"
 #define HPS_MAX_DEVICES		1
+#define HPS_MAX_MSG_SIZE	8192
 
 struct hps_drvdata {
 	struct i2c_client *client;
@@ -60,6 +62,8 @@ static int hps_open(struct inode *inode, struct file *file)
 	ret = pm_runtime_get_sync(dev);
 	if (ret < 0)
 		goto pm_get_fail;
+
+	file->private_data = hps->client;
 	return 0;
 
 pm_get_fail:
@@ -84,10 +88,87 @@ static int hps_release(struct inode *inode, struct file *file)
 	return ret;
 }
 
+static int hps_do_ioctl_transfer(struct i2c_client *client,
+				 struct hps_transfer_ioctl_data *args)
+{
+	int ret;
+	int nmsg = 0;
+	struct i2c_msg msgs[2] = {
+		{
+			.addr = client->addr,
+			.flags = client->flags,
+		},
+		{
+			.addr = client->addr,
+			.flags = client->flags,
+		},
+	};
+
+	if (args->isize) {
+		msgs[nmsg].len = args->isize;
+		msgs[nmsg].buf = memdup_user(args->ibuf, args->isize);
+		if (IS_ERR(msgs[nmsg].buf)) {
+			ret = PTR_ERR(msgs[nmsg].buf);
+			goto memdup_fail;
+		}
+		nmsg++;
+	}
+
+	if (args->osize) {
+		msgs[nmsg].len = args->osize;
+		msgs[nmsg].buf = memdup_user(args->obuf, args->osize);
+		msgs[nmsg].flags |= I2C_M_RD;
+		if (IS_ERR(msgs[nmsg].buf)) {
+			ret = PTR_ERR(msgs[nmsg].buf);

drivers/misc/hps-i2c.c static int hps_release(struct inode *inode, struct file *file) · line 132

Quoted 10 lines
drivers/misc/hps-i2c.c +10 -0
+			goto memdup_fail;
+		}
+		nmsg++;
+	}
+
+	ret = i2c_transfer(client->adapter, &msgs[0], nmsg);
+	if (ret > 0 && args->osize) {
+		if (copy_to_user(args->obuf, msgs[nmsg - 1].buf, ret))
+			ret = -EFAULT;
+	}

Why can't you just do normal i2c transfers to/from userspace instead of requring a custom ioctl?

thanks,

greg k-h

#8 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

to 27. tammik. 2022 klo 20.41 Greg KH ([email protected]) kirjoitti:

Quoted 99 lines · Sami Kyöstilä
Quoted 97 lines

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
MAINTAINERS +1 -0
index 9dea4b8c2ab5..d5fc066fdbc2 100644
@@ -8803,6 +8803,7 @@ M:      Sami Kyöstilä <[email protected]>
 R:   Evan Benn <[email protected]>
 S:   Maintained
 F:   drivers/misc/hps-i2c.c
+F:   include/uapi/linux/hps.h
 
 HSI SUBSYSTEM
 M:   Sebastian Reichel <[email protected]>
drivers/misc/hps-i2c.c +41 -0
index fe9f073b0352..748ead49d678 100644
@@ -17,9 +17,11 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <uapi/linux/hps.h>
 
 #define HPS_ACPI_ID          "GOOG0020"
 #define HPS_MAX_DEVICES              1
+#define HPS_MAX_MSG_SIZE     8192
 
 struct hps_drvdata {
      struct i2c_client *client;
@@ -60,6 +62,8 @@ static int hps_open(struct inode *inode, struct file *file)
      ret = pm_runtime_get_sync(dev);
      if (ret < 0)
              goto pm_get_fail;
+
+     file->private_data = hps->client;
      return 0;
 
 pm_get_fail:
@@ -84,10 +88,87 @@ static int hps_release(struct inode *inode, struct file *file)
      return ret;
 }
 
+static int hps_do_ioctl_transfer(struct i2c_client *client,
+                              struct hps_transfer_ioctl_data *args)
+{
+     int ret;
+     int nmsg = 0;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+     };
+
+     if (args->isize) {
+             msgs[nmsg].len = args->isize;
+             msgs[nmsg].buf = memdup_user(args->ibuf, args->isize);
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
+     if (args->osize) {
+             msgs[nmsg].len = args->osize;
+             msgs[nmsg].buf = memdup_user(args->obuf, args->osize);
+             msgs[nmsg].flags |= I2C_M_RD;
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
Quoted 8 lines

drivers/misc/hps-i2c.c static int hps_release(struct inode *inode, struct file *file) · line 132

Quoted 5 lines
drivers/misc/hps-i2c.c +5 -0
+     ret = i2c_transfer(client->adapter, &msgs[0], nmsg);
+     if (ret > 0 && args->osize) {
+             if (copy_to_user(args->obuf, msgs[nmsg - 1].buf, ret))
+                     ret = -EFAULT;
+     }

Why can't you just do normal i2c transfers to/from userspace instead of requring a custom ioctl?

The main reason is security: without this driver, in order to talk to HPS the userspace daemon needs read/write access to the entire I2C controller (e.g., /dev/i2c-0). This means the daemon can also control any other I2C device which happens to be on the same bus. With a separate ioctl we can limit access to just HPS.

As far as I can tell, there's no way to restrict access on a per-device level with normal i2c, but I'd be happy to be proven wrong :)

Quoted 4 lines

thanks,

greg k-h

- Sami

maintainer
#9 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

Quoted 109 lines · Sami Kyostila

to 27. tammik. 2022 klo 20.41 Greg KH ([email protected]) kirjoitti:

Quoted 99 lines · Sami Kyöstilä
Quoted 97 lines

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
MAINTAINERS +1 -0
index 9dea4b8c2ab5..d5fc066fdbc2 100644
@@ -8803,6 +8803,7 @@ M:      Sami Kyöstilä <[email protected]>
 R:   Evan Benn <[email protected]>
 S:   Maintained
 F:   drivers/misc/hps-i2c.c
+F:   include/uapi/linux/hps.h
 
 HSI SUBSYSTEM
 M:   Sebastian Reichel <[email protected]>
drivers/misc/hps-i2c.c +41 -0
index fe9f073b0352..748ead49d678 100644
@@ -17,9 +17,11 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <uapi/linux/hps.h>
 
 #define HPS_ACPI_ID          "GOOG0020"
 #define HPS_MAX_DEVICES              1
+#define HPS_MAX_MSG_SIZE     8192
 
 struct hps_drvdata {
      struct i2c_client *client;
@@ -60,6 +62,8 @@ static int hps_open(struct inode *inode, struct file *file)
      ret = pm_runtime_get_sync(dev);
      if (ret < 0)
              goto pm_get_fail;
+
+     file->private_data = hps->client;
      return 0;
 
 pm_get_fail:
@@ -84,10 +88,87 @@ static int hps_release(struct inode *inode, struct file *file)
      return ret;
 }
 
+static int hps_do_ioctl_transfer(struct i2c_client *client,
+                              struct hps_transfer_ioctl_data *args)
+{
+     int ret;
+     int nmsg = 0;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+     };
+
+     if (args->isize) {
+             msgs[nmsg].len = args->isize;
+             msgs[nmsg].buf = memdup_user(args->ibuf, args->isize);
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
+     if (args->osize) {
+             msgs[nmsg].len = args->osize;
+             msgs[nmsg].buf = memdup_user(args->obuf, args->osize);
+             msgs[nmsg].flags |= I2C_M_RD;
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
Quoted 8 lines

drivers/misc/hps-i2c.c static int hps_release(struct inode *inode, struct file *file) · line 132

Quoted 5 lines
drivers/misc/hps-i2c.c +5 -0
+     ret = i2c_transfer(client->adapter, &msgs[0], nmsg);
+     if (ret > 0 && args->osize) {
+             if (copy_to_user(args->obuf, msgs[nmsg - 1].buf, ret))
+                     ret = -EFAULT;
+     }

Why can't you just do normal i2c transfers to/from userspace instead of requring a custom ioctl?

Quoted 5 lines

The main reason is security: without this driver, in order to talk to HPS the userspace daemon needs read/write access to the entire I2C controller (e.g., /dev/i2c-0). This means the daemon can also control any other I2C device which happens to be on the same bus. With a separate ioctl we can limit access to just HPS.

Then use seccomp for this?

Quoted 3 lines

As far as I can tell, there's no way to restrict access on a per-device level with normal i2c, but I'd be happy to be proven wrong :)

Selinux rules?

What else is on this bus for the device that you care about?

thanks,

greg k-h

#10 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

pe 28. tammik. 2022 klo 20.36 Greg KH ([email protected]) kirjoitti:

Quoted 111 lines · Sami Kyostila
Quoted 109 lines

to 27. tammik. 2022 klo 20.41 Greg KH ([email protected]) kirjoitti:

Quoted 107 lines · Sami Kyöstilä
Quoted 92 lines

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
MAINTAINERS +1 -0
index 9dea4b8c2ab5..d5fc066fdbc2 100644
@@ -8803,6 +8803,7 @@ M:      Sami Kyöstilä <[email protected]>
 R:   Evan Benn <[email protected]>
 S:   Maintained
 F:   drivers/misc/hps-i2c.c
+F:   include/uapi/linux/hps.h
 
 HSI SUBSYSTEM
 M:   Sebastian Reichel <[email protected]>
drivers/misc/hps-i2c.c +36 -0
index fe9f073b0352..748ead49d678 100644
@@ -17,9 +17,11 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <uapi/linux/hps.h>
 
 #define HPS_ACPI_ID          "GOOG0020"
 #define HPS_MAX_DEVICES              1
+#define HPS_MAX_MSG_SIZE     8192
 
 struct hps_drvdata {
      struct i2c_client *client;
@@ -60,6 +62,8 @@ static int hps_open(struct inode *inode, struct file *file)
      ret = pm_runtime_get_sync(dev);
      if (ret < 0)
              goto pm_get_fail;
+
+     file->private_data = hps->client;
      return 0;
 
 pm_get_fail:
@@ -84,10 +88,87 @@ static int hps_release(struct inode *inode, struct file *file)
      return ret;
 }
 
+static int hps_do_ioctl_transfer(struct i2c_client *client,
+                              struct hps_transfer_ioctl_data *args)
+{
+     int ret;
+     int nmsg = 0;
+     struct i2c_msg msgs[2] = {
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+             {
+                     .addr = client->addr,
+                     .flags = client->flags,
+             },
+     };
+
+     if (args->isize) {
+             msgs[nmsg].len = args->isize;
+             msgs[nmsg].buf = memdup_user(args->ibuf, args->isize);
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
+     if (args->osize) {
+             msgs[nmsg].len = args->osize;
+             msgs[nmsg].buf = memdup_user(args->obuf, args->osize);
+             msgs[nmsg].flags |= I2C_M_RD;
+             if (IS_ERR(msgs[nmsg].buf)) {
+                     ret = PTR_ERR(msgs[nmsg].buf);

drivers/misc/hps-i2c.c static int hps_release(struct inode *inode, struct file *file) · line 132

Quoted 10 lines
drivers/misc/hps-i2c.c +10 -0
+                     goto memdup_fail;
+             }
+             nmsg++;
+     }
+
+     ret = i2c_transfer(client->adapter, &msgs[0], nmsg);
+     if (ret > 0 && args->osize) {
+             if (copy_to_user(args->obuf, msgs[nmsg - 1].buf, ret))
+                     ret = -EFAULT;
+     }

Why can't you just do normal i2c transfers to/from userspace instead of requring a custom ioctl?

Quoted 6 lines
Quoted 5 lines

The main reason is security: without this driver, in order to talk to HPS the userspace daemon needs read/write access to the entire I2C controller (e.g., /dev/i2c-0). This means the daemon can also control any other I2C device which happens to be on the same bus. With a separate ioctl we can limit access to just HP

Then use seccomp for this?

As far as I can tell, seccomp can't do this because it can't read the device address field[1] which is behind two userspace pointer hops in the I2C_RDWR ioctl[2].

[1] https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/i2c.h#L74
[2] https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/i2c-dev.h#L51

Quoted 6 lines
Quoted 4 lines

As far as I can tell, there's no way to restrict access on a per-device level with normal i2c, but I'd be happy to be proven wrong :)

Selinux rules?

I guess we could add an LSM hook for I2C transfers, but that would require baking device addresses into the SELinux policy which seems a little unfortunate.

I think that leaves the options suggested by Arnd (thanks!):

a) Add a generic way to expose device nodes for individual I2C devices (something like /dev/i2c/by-id/NN?).

b) Make the ioctl interface more fully featured instead of just exposing I2C.

I think I'm leaning toward (a) since it's not yet totally clear what the right high level abstraction for this type of device is (e.g., should it be HID, in which case the protocol should probably become HID-I2C).

If this sounds okay, then I suggest we merge just the power control patch from this series (with comments addressed) and I'll follow-up with splitting out the i2c devices.

Quoted 1 line

What else is on this bus for the device that you care about?

That's really up to the device manufacturer; it could be anything from touchpads to cameras or fingerprint scanners.

Quoted 3 lines

thanks,

greg k-h

- Sami

maintainer
#11 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

Quoted 10 lines · Sami Kyostila

I guess we could add an LSM hook for I2C transfers, but that would require baking device addresses into the SELinux policy which seems a little unfortunate.

I think that leaves the options suggested by Arnd (thanks!):

a) Add a generic way to expose device nodes for individual I2C devices (something like /dev/i2c/by-id/NN?).

Quoted 6 lines

b) Make the ioctl interface more fully featured instead of just exposing I2C.

I think I'm leaning toward (a) since it's not yet totally clear what the right high level abstraction for this type of device is (e.g., should it be HID, in which case the protocol should probably become HID-I2C).

(adding i2c list to cc)

I think the implementation of the character device should be really straightforward, it can probably just use the exact same ioctls as the normal device, or a subset of them, and filter out any access that has the wrong address with ioctl(fd, I2C_SLAVE, ...) or other commands.

The tricky part is coming up with a sensible way of creating those character device nodes, as there generic method of knowing what is attached to the bus. I suppose this could be done either automatically based on the nodes in DT, or it could be done with user interaction like a new ioctl command on the normal device node or some sysfs interface to create the chardev for a particular slave device.

Arnd

maintainer
#12 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

In reply to Sami Kyostila
Quoted 12 lines · Sami Kyostila

to 27. tammik. 2022 klo 20.41 Greg KH ([email protected]) kirjoitti:

Quoted 3 lines

Why can't you just do normal i2c transfers to/from userspace instead of requring a custom ioctl?

The main reason is security: without this driver, in order to talk to HPS the userspace daemon needs read/write access to the entire I2C controller (e.g., /dev/i2c-0). This means the daemon can also control any other I2C device which happens to be on the same bus. With a separate ioctl we can limit access to just HPS.

Quoted 3 lines

As far as I can tell, there's no way to restrict access on a per-device level with normal i2c, but I'd be happy to be proven wrong :)

I don't know if is correct, but I trust your research on that. However, I would still argue that instead of adding a custom low-level interface to give user access to a single raw i2c device, we would be better off doing one of two other approaches:

a) add the generic interface you are missing -- it seems unlikely that you are the only user that has come across this issue, and adding a generic per-device interface to complement the per-controller one would likely help avoid others coming up with the same thing elsewhere, using yet another set of custom ioctls.

b) move part of your application into the kernel, and provide a high-level abstraction for your device in place of the low-level i2c access.

Arnd

#13 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

In reply to Sami Kyöstilä
Quoted 10 lines · Sami Kyöstilä

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

Quoted 9 lines

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h

Hi--

If your next patch version continues to use an ioctl, its magic "number" ('h') should be documented in Documentation/userspace-api/ioctl/ioctl-number.rst.

thanks.

Quoted 26 lines
include/uapi/linux/hps.h new file +20 -0
new file mode 100644
index 000000000000..2c1bd174cd02
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+/*
+ *  Copyright 2022 Google LLC.
+ */
+
+#ifndef _UAPI_HPS_H
+#define _UAPI_HPS_H
+
+#include <linux/types.h>
+
+#define HPS_IOC_TRANSFER	_IOWR('h', 0x01, struct hps_transfer_ioctl_data)
+
+struct hps_transfer_ioctl_data {
+	__u32 isize;			/* Number of bytes to send */
+	unsigned char __user *ibuf;	/* Input buffer */
+	__u32 osize;			/* Number of bytes to receive */
+	unsigned char __user *obuf;	/* Output buffer */
+};
+
+#endif /* _UAPI_HPS_H */
Signature · 1 line
~Randy
#14 lore raw

Re: [PATCH 2/2] drivers/misc: add transfer ioctl for HPS

pe 28. tammik. 2022 klo 9.39 Randy Dunlap ([email protected]) kirjoitti:

Quoted 24 lines · Sami Kyöstilä
Quoted 10 lines

This patch adds an ioctl operation for sending and receiving data from the ChromeOS snooping protection sensor (a.k.a., HPS). This allows userspace programs to perform a combined read/write I2C transaction through a single syscall.

The I2C wire protocol for the device is documented at:

https://chromium.googlesource.com/chromiumos/platform/hps-firmware/+/
refs/heads/main/docs/host_device_i2c_protocol.md

Quoted 9 lines

---

 MAINTAINERS              |  1 +
 drivers/misc/hps-i2c.c   | 81 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/hps.h | 20 ++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 include/uapi/linux/hps.h
Quoted 4 lines

Hi--

If your next patch version continues to use an ioctl, its magic "number" ('h') should be documented in Documentation/userspace-api/ioctl/ioctl-number.rst.

Ah, thanks for the pointer. Will do.

- Sami

Quoted 32 lines

thanks.

Quoted 16 lines
include/uapi/linux/hps.h new file +10 -0
new file mode 100644
index 000000000000..2c1bd174cd02
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+/*
+ *  Copyright 2022 Google LLC.
+ */
+
+#ifndef _UAPI_HPS_H
+#define _UAPI_HPS_H
+
+#include <linux/types.h>
+

include/uapi/linux/hps.h line 20

Quoted 10 lines
include/uapi/linux/hps.h new file +10 -0
+#define HPS_IOC_TRANSFER     _IOWR('h', 0x01, struct hps_transfer_ioctl_data)
+
+struct hps_transfer_ioctl_data {
+     __u32 isize;                    /* Number of bytes to send */
+     unsigned char __user *ibuf;     /* Input buffer */
+     __u32 osize;                    /* Number of bytes to receive */
+     unsigned char __user *obuf;     /* Output buffer */
+};
+
+#endif /* _UAPI_HPS_H */

--
~Randy