Skip to contentSkip to conversation

drivers: ddcci: upstream DDCCI driver

PATCH Superseded Yusuf Khan 3 participants 5 messages Mar 10, 2022 – Mar 11, 2022

Subsystems (4)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Yusuf Khan

author
#0 lore raw

PATCH v4 1/1 drivers: ddcci: upstream DDCCI driver

This patch upstreams the DDCCI driver by Christoph Grenz into the kernel. The original gitlab page is loacted at https://gitlab
.com/ddcci-driver-linux/ddcci-driver-linux/-/tree/master.

---
v2: Fix typos.

v3: Add documentation, move files around, replace semaphores with mutexes, and replaced <asm-generic/fcntl.h> with <linux/fcntl.h>. "imirkin"(which due to his involvement in the dri-devel irc channel I cant help but assume to be a DRM developer) said that the DDC/CI bus does not intefere with the buses that DRM is involved with.

v4: Move some documentation, fix grammer mistakes, remove usages of likely(), and clarify some documentation.

---
 Documentation/ABI/testing/sysfs-driver-ddcci |   37 +
 Documentation/driver-api/ddcci.rst           |  107 +
 drivers/char/Kconfig                         |   10 +
 drivers/char/Makefile                        |    1 +
 drivers/char/ddcci.c                         | 1887 ++++++++++++++++++
 drivers/video/backlight/Kconfig              |   10 +
 drivers/video/backlight/Makefile             |    1 +
 drivers/video/backlight/ddcci-backlight.c    |  411 ++++
 include/linux/ddcci.h                        |  164 ++
 9 files changed, 2628 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-ddcci
 create mode 100644 Documentation/driver-api/ddcci.rst
 create mode 100644 drivers/char/ddcci.c
 create mode 100644 drivers/video/backlight/ddcci-backlight.c
 create mode 100644 include/linux/ddcci.h
Documentation/ABI/testing/sysfs-driver-ddcci new file +37 -0
new file mode 100644
index 000000000000..2c160ab0797e
@@ -0,0 +1,37 @@
+What:		/sys/bus/ddcci/ddcci<I²C bus number>i<hex address>
+Date:		February 2022
+KernelVersion:	5.17
+Contact:	Yusuf Khan <[email protected]>
+Description:	This file is a internal dependent device on the I2C bus,
+		it exports the same data that the main device exports.
+
+What:		/sys/bus/ddcci/ddcci<I²C bus number>e<hex address>
+Date:		February 2022
+KernelVersion:	5.17
+Contact:	Yusuf Khan <[email protected]>
+Description:	This file is a external dependent device on the I2C bus,
+		it exports the same data that the main device exports.
+
+What:		/sys/bus/ddcci/ddcci<I²C bus number>
+Date:		February 2022
+KernelVersion:	5.17
+Contact:	Yusuf Khan <[email protected]>
+Description:	This file is the main device on the I2C bus, it exports
+		the following peices of information about the device, capabilities
+		which is a full ACCESS.bus capabilities string(see the ACCESS.bus
+		specification for more info), idProt, which is a full ACCESS.bus
+		protocol supported by the device(Usually "monitor"), idType,
+		a ACCESS.buss device subtype(Usually "LCD" or "CRT"), idModel, a
+		ACCESS.bus device model identifier(Usually a shortened form of the
+		device model name), idVendor, a ACCESS.bus device vendor identifier
+		(Empty if the Identification command is not supported), idModule,
+		a ACCESS.bus device module identifier(Empty if the Identification
+		command is not supported), idSerial, a 32 bit device number
+		a fixed serial number if it's positive, a temporary serial number
+		if negative and zero if the Identification command is not supported,
+		and modalias, a combined identifier for driver selection,
+		it has the form ddcci:<idProt>-<idType>-<idModel>-<idVendor>-<idModule>,
+		All non-alphanumeric characters (including whitespace) in the model,
+		vendor or module parts are replaced by underscores to prevent issues
+		with software like systemd-udevd. See Documentation/misc-devices/ddcci.rst
+		for more information on DDC/CI.
Documentation/driver-api/ddcci.rst new file +107 -0

108 lines not on this page · raw message

drivers/char/Kconfig +10 -0
index 740811893c57..ec36fae7ee38 100644
@@ -451,4 +451,14 @@ config RANDOM_TRUST_BOOTLOADER
 	pool. Otherwise, say N here so it will be regarded as device input that
 	only mixes the entropy pool.
 
+config DDCCI
+	tristate "DDCCI display protocol support"
+	help
+	  Display Data Channel Command Interface is a
+	  interface that allows the kernel to "talk"
+	  to most displays made after 2005. Check your
+	  display's specification to see if it has
+	  support for this. This depends on I2C to
+	  compile.
+
 endmenu
drivers/char/Makefile +1 -0
index 264eb398fdd4..eaa2d58d50df 100644
@@ -3,6 +3,7 @@
 # Makefile for the kernel character device drivers.
 #
 
+obj-$(CONFIG_DDCCI)				+= ddcci.o
 obj-y				+= mem.o random.o
 obj-$(CONFIG_TTY_PRINTK)	+= ttyprintk.o
 obj-y				+= misc.o
drivers/char/ddcci.c new file +1887 -0

1,887 lines not on this page · raw message

drivers/video/backlight/Kconfig +10 -0
index e32694c13da5..8b7efde28cfd 100644
@@ -289,6 +289,16 @@ config BACKLIGHT_QCOM_WLED
 	  If you have the Qualcomm PMIC, say Y to enable a driver for the
 	  WLED block. Currently it supports PM8941 and PMI8998.
 
+config BACKLIGHT_DDCCI
+	tristate "DDCCI Backlight Driver"
+	help
+	  If you have a DDC/CI supporing monitor, say Y to enable a driver
+	  to control its backlight using DDC/CI. This could be useful if
+	  your monitor does not include a backlight driver. For this to be
+	  useful you need to enable DDCCI support which can be found in
+	  Device Drivers -> Character devices and that further depends on
+	  I2C.
+
 config BACKLIGHT_RT4831
 	tristate "Richtek RT4831 Backlight Driver"
 	depends on MFD_RT4831
drivers/video/backlight/Makefile +1 -0
index cae2c83422ae..7bfb6e506b35 100644
@@ -58,3 +58,4 @@ obj-$(CONFIG_BACKLIGHT_WM831X)		+= wm831x_bl.o
 obj-$(CONFIG_BACKLIGHT_ARCXCNN) 	+= arcxcnn_bl.o
 obj-$(CONFIG_BACKLIGHT_RAVE_SP)		+= rave-sp-backlight.o
 obj-$(CONFIG_BACKLIGHT_LED)		+= led_bl.o
+obj-$(CONFIG_BACKLIGHT_DDCCI)		+= ddcci-backlight.o
drivers/video/backlight/ddcci-backlight.c new file +411 -0

411 lines not on this page · raw message

include/linux/ddcci.h new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 42 lines · Yusuf Khan

This patch upstreams the DDCCI driver by Christoph Grenz into the kernel. The original gitlab page is loacted at https://gitlab
.com/ddcci-driver-linux/ddcci-driver-linux/-/tree/master.

---
v2: Fix typos.

v3: Add documentation, move files around, replace semaphores with mutexes, and replaced <asm-generic/fcntl.h> with <linux/fcntl.h>. "imirkin"(which due to his involvement in the dri-devel irc channel I cant help but assume to be a DRM developer) said that the DDC/CI bus does not intefere with the buses that DRM is involved with.

v4: Move some documentation, fix grammer mistakes, remove usages of likely(), and clarify some documentation.

---
 Documentation/ABI/testing/sysfs-driver-ddcci |   37 +
 Documentation/driver-api/ddcci.rst           |  107 +
 drivers/char/Kconfig                         |   10 +
 drivers/char/Makefile                        |    1 +
 drivers/char/ddcci.c                         | 1887 ++++++++++++++++++
 drivers/video/backlight/Kconfig              |   10 +
 drivers/video/backlight/Makefile             |    1 +
 drivers/video/backlight/ddcci-backlight.c    |  411 ++++
 include/linux/ddcci.h                        |  164 ++
 9 files changed, 2628 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-ddcci
 create mode 100644 Documentation/driver-api/ddcci.rst
 create mode 100644 drivers/char/ddcci.c
 create mode 100644 drivers/video/backlight/ddcci-backlight.c
 create mode 100644 include/linux/ddcci.h
Documentation/driver-api/ddcci.rst new file +2 -0
new file mode 100644
index 000000000000..b4a4b694d326
@@ -0,0 +1,107 @@
+.. SPDX-License-Identifier: GPL-2.0
+

...

Documentation/driver-api/ddcci.rst line 107

Quoted 4 lines
Documentation/driver-api/ddcci.rst new file +3 -0
+
+options ddcci dyndbg
+options ddcci-backlight dyndbg
\ No newline at end of file

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/char/Kconfig config RANDOM_TRUST_BOOTLOADER · line 455

Quoted 10 lines
drivers/char/Kconfig +2 -0
index 740811893c57..ec36fae7ee38 100644
@@ -451,4 +451,14 @@ config RANDOM_TRUST_BOOTLOADER
 	pool. Otherwise, say N here so it will be regarded as device input that
 	only mixes the entropy pool.
 
+config DDCCI
+	tristate "DDCCI display protocol support"

Add:
depends on I2C

drivers/char/Kconfig config RANDOM_TRUST_BOOTLOADER · line 457

Quoted 2 lines
drivers/char/Kconfig +2 -0
+	help
+	  Display Data Channel Command Interface is a

is an
(as in my previous comments)

Quoted 28 lines
drivers/char/Kconfig +6 -0
+	  interface that allows the kernel to "talk"
+	  to most displays made after 2005. Check your
+	  display's specification to see if it has
+	  support for this. This depends on I2C to
+	  compile.
+
 endmenu
drivers/char/ddcci.c new file +14 -0
new file mode 100644
index 000000000000..0a7bc9f63261
@@ -0,0 +1,1887 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  DDC/CI sub-bus driver
+ *
+ *  Copyright (c) 2015 Christoph Grenz
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+

...

Quoted 4 lines
drivers/char/ddcci.c new file +4 -0
+/* Write a message to the DDC/CI bus using i2c_smbus_write_byte() */
+static int __ddcci_write_bytewise(struct i2c_client *client, unsigned char addr,
+				  bool p_flag, const unsigned char *__restrict buf,
+				  unsigned char len)

drivers/char/ddcci.c line 94

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+{
+	int ret = 0;
+	unsigned char outer_addr = (unsigned char)(client->addr << 1);
+	unsigned int xor = outer_addr; /* initial xor value */
+
+	/* Consistency checks */
+	if (len > 127)
+		return -EINVAL;
+
+	/* Special case: sender to 0x6E is always 0x51 */

What does that comment mean?

Quoted 50 lines
drivers/char/ddcci.c new file +50 -0
+	if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
+		addr = DDCCI_HOST_ADDR_ODD;
+	} else {
+		/* When sending the odd address is used */
+		addr = addr | 1;
+	}
+
+	/* first byte: sender address */
+	xor ^= addr;
+	ret = i2c_smbus_write_byte(client, addr);
+	if (ret < 0)
+		return ret;
+
+	/* second byte: protocol flag and message size */
+	xor ^= ((p_flag << 7) | len);
+	ret = i2c_smbus_write_byte(client, (p_flag << 7)|len);
+	if (ret < 0)
+		return ret;
+
+	/* send payload */
+	while (len--) {
+		xor ^= (*buf);
+		ret = i2c_smbus_write_byte(client, (*buf));
+		if (ret < 0)
+			return ret;
+		buf++;
+	}
+
+	/* send checksum */
+	ret = i2c_smbus_write_byte(client, xor);
+	return ret;
+}
+
+/* Write a message to the DDC/CI bus using i2c_master_send() */
+static int __ddcci_write_block(struct i2c_client *client, unsigned char addr,
+			       unsigned char *sendbuf, bool p_flag,
+			       const unsigned char *data, unsigned char len)
+{
+	unsigned char outer_addr = (unsigned char)(client->addr << 1);
+	unsigned int xor = outer_addr;	/* initial xor value */
+	unsigned char *ptr = sendbuf;
+
+	/* Consistency checks */
+	if (len > 127)
+		return -EINVAL;
+
+	/* Special case: sender to 0x6E is always 0x51 */
+	if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
+		addr = DDCCI_HOST_ADDR_ODD;
+	} else {

drivers/char/ddcci.c line 154

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+		/* When sending the odd address is used */
+		addr = addr | 1;
+	}
+
+	/* first byte: sender address */
+	xor ^= addr;
+	*(ptr++) = addr;
+	/* second byte: protocol flag and message size */
+	xor ^= ((p_flag << 7) | len);
+	*(ptr++) = (p_flag << 7)|len;

Fix spacing above. (add some spaces)

Quoted 104 lines
drivers/char/ddcci.c new file +104 -0
+	/* payload */
+	while (len--) {
+		xor ^= (*data);
+		*(ptr++) = (*data);
+		data++;
+	}
+	/* checksum */
+	(*ptr) = xor;
+
+	/* Send it */
+	return i2c_master_send(client, sendbuf, ptr - sendbuf + 1);
+}
+
+/*
+ * Write a message to the DDC/CI bus.
+ *
+ * You must hold the bus mutex when calling this function.
+ */
+static int ddcci_write(struct i2c_client *client, unsigned char addr,
+		       bool p_flag, const unsigned char *data,
+		       unsigned char len)
+{
+	struct ddcci_bus_drv_data *drv_data;
+	unsigned char *sendbuf;
+	int ret;
+
+	drv_data = i2c_get_clientdata(client);
+
+
+	pr_debug("sending to %d:%02x:%02x: %*ph\n", client->adapter->nr,
+		 client->addr << 1, addr, len, data);
+	if (drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE) {
+		ret = __ddcci_write_bytewise(client, addr, p_flag, data, len);
+	} else {
+		sendbuf = drv_data->recv_buffer;
+		ret = __ddcci_write_block(client, addr, sendbuf, p_flag, data, len);
+	}
+
+	return ret;
+}
+
+/*
+ * Read a response from the DDC/CI bus with headers directly into a buffer.
+ * Always check for DDCCI_QUIRK_SKIP_FIRST_BYTE when using this function.
+ * The returned length contains the whole unmodified response.
+ * If -EMSGSIZE is returned, the buffer contains the response up to `len`.
+ * If any other negative error code is returned, the buffer content is
+ * unspecified.
+ */
+static int __ddcci_read(struct i2c_client *client, unsigned char addr,
+			bool p_flag, unsigned long quirks, unsigned char *buf,
+			unsigned char len)
+{
+	int i, payload_len, packet_length, ret;
+	unsigned char xor = DDCCI_HOST_ADDR_EVEN;
+
+	/* Consistency checks */
+	if (len < 3)
+		return -EINVAL;
+
+	/* Read frame */
+	ret = i2c_master_recv(client, buf, len);
+	if (ret < 0)
+		goto out_err;
+	packet_length = ret;
+
+	/* Skip first byte if quirk active */
+	if ((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) && ret > 0 && len > 0) {
+		ret--;
+		len--;
+		buf++;
+	}
+
+	/* If answer too short (= incomplete) break out */
+	if (ret < 3) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* validate first byte */
+	if (unlikely(buf[0] != addr)) {
+		ret = (buf[0] == '\0') ? -EAGAIN : -EIO;
+		goto out_err;
+	}
+
+	/* validate second byte (protocol flag) */
+	if (unlikely((buf[1] & 0x80) != (p_flag << 7))) {
+		if (!p_flag || !(quirks & DDCCI_QUIRK_NO_PFLAG)) {
+			ret = -EIO;
+			goto out_err;
+		}
+	}
+
+	/* get and check payload length */
+	payload_len = buf[1] & 0x7F;
+	if (3+payload_len > packet_length)
+		return -EBADMSG;
+	if (3+payload_len > len)
+		return -EMSGSIZE;
+
+	/* calculate checksum */
+	for (i = 0; i < 3+payload_len; i++)
+		xor ^= buf[i];
+

drivers/char/ddcci.c line 268

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+	/* verify checksum */
+	if (xor != 0) {
+		dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
+			xor, payload_len);
+		ret = -EBADMSG;
+		goto out_err;
+	}
+
+	/* return result */
+	ret = payload_len+3+((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)?1:0);

ugly :(

drivers/char/ddcci.c line 273

Quoted 5 lines
drivers/char/ddcci.c new file +5 -0
+
+out_err:
+	return ret;
+}
+

and it still contains too much use of likely() and unlikely().

drivers/video/backlight/Kconfig config BACKLIGHT_QCOM_WLED · line 293

Quoted 10 lines
drivers/video/backlight/Kconfig +2 -0
index e32694c13da5..8b7efde28cfd 100644
@@ -289,6 +289,16 @@ config BACKLIGHT_QCOM_WLED
 	  If you have the Qualcomm PMIC, say Y to enable a driver for the
 	  WLED block. Currently it supports PM8941 and PMI8998.
 
+config BACKLIGHT_DDCCI
+	tristate "DDCCI Backlight Driver"

add line:
depends on I2C

drivers/video/backlight/Kconfig config BACKLIGHT_QCOM_WLED · line 295

Quoted 2 lines
drivers/video/backlight/Kconfig +2 -0
+	help
+	  If you have a DDC/CI supporing monitor, say Y to enable a driver

supporting

Quoted 9 lines
drivers/video/backlight/Kconfig +6 -0
+	  to control its backlight using DDC/CI. This could be useful if
+	  your monitor does not include a backlight driver. For this to be
+	  useful you need to enable DDCCI support which can be found in
+	  Device Drivers -> Character devices and that further depends on
+	  I2C.
+
 config BACKLIGHT_RT4831
 	tristate "Richtek RT4831 Backlight Driver"
 	depends on MFD_RT4831
Signature · 1 line
~Randy
author
#2 lore raw

I (hopefully) fulfilled those requests in my subsequent patch, turns out I forgot to git add some of my changes. I did not remove the unlikely()s as I think they look good and point to where error checking is that many may miss.

Quoted 349 lines · Randy Dunlap
Quoted 42 lines · Yusuf Khan

This patch upstreams the DDCCI driver by Christoph Grenz into the kernel. The original gitlab page is loacted at https://gitlab
.com/ddcci-driver-linux/ddcci-driver-linux/-/tree/master.

---
v2: Fix typos.

v3: Add documentation, move files around, replace semaphores with mutexes, and replaced <asm-generic/fcntl.h> with <linux/fcntl.h>. "imirkin"(which due to his involvement in the dri-devel irc channel I cant help but assume to be a DRM developer) said that the DDC/CI bus does not intefere with the buses that DRM is involved with.

v4: Move some documentation, fix grammer mistakes, remove usages of likely(), and clarify some documentation.

---
 Documentation/ABI/testing/sysfs-driver-ddcci |   37 +
 Documentation/driver-api/ddcci.rst           |  107 +
 drivers/char/Kconfig                         |   10 +
 drivers/char/Makefile                        |    1 +
 drivers/char/ddcci.c                         | 1887 ++++++++++++++++++
 drivers/video/backlight/Kconfig              |   10 +
 drivers/video/backlight/Makefile             |    1 +
 drivers/video/backlight/ddcci-backlight.c    |  411 ++++
 include/linux/ddcci.h                        |  164 ++
 9 files changed, 2628 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-ddcci
 create mode 100644 Documentation/driver-api/ddcci.rst
 create mode 100644 drivers/char/ddcci.c
 create mode 100644 drivers/video/backlight/ddcci-backlight.c
 create mode 100644 include/linux/ddcci.h
Documentation/driver-api/ddcci.rst new file +2 -0
new file mode 100644
index 000000000000..b4a4b694d326
@@ -0,0 +1,107 @@
+.. SPDX-License-Identifier: GPL-2.0
+

...

Documentation/driver-api/ddcci.rst line 107

Quoted 4 lines
Documentation/driver-api/ddcci.rst new file +3 -0
+
+options ddcci dyndbg
+options ddcci-backlight dyndbg
\ No newline at end of file

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/char/Kconfig config RANDOM_TRUST_BOOTLOADER · line 455

Quoted 10 lines
drivers/char/Kconfig +2 -0
index 740811893c57..ec36fae7ee38 100644
@@ -451,4 +451,14 @@ config RANDOM_TRUST_BOOTLOADER
      pool. Otherwise, say N here so it will be regarded as device input that
      only mixes the entropy pool.
 
+config DDCCI
+     tristate "DDCCI display protocol support"

Add:
depends on I2C

drivers/char/Kconfig config RANDOM_TRUST_BOOTLOADER · line 457

Quoted 2 lines
drivers/char/Kconfig +2 -0
+     help
+       Display Data Channel Command Interface is a

is an
(as in my previous comments)

Quoted 28 lines
drivers/char/Kconfig +6 -0
+       interface that allows the kernel to "talk"
+       to most displays made after 2005. Check your
+       display's specification to see if it has
+       support for this. This depends on I2C to
+       compile.
+
 endmenu
drivers/char/ddcci.c new file +14 -0
new file mode 100644
index 000000000000..0a7bc9f63261
@@ -0,0 +1,1887 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  DDC/CI sub-bus driver
+ *
+ *  Copyright (c) 2015 Christoph Grenz
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+

...

Quoted 4 lines
drivers/char/ddcci.c new file +4 -0
+/* Write a message to the DDC/CI bus using i2c_smbus_write_byte() */
+static int __ddcci_write_bytewise(struct i2c_client *client, unsigned char addr,
+                               bool p_flag, const unsigned char *__restrict buf,
+                               unsigned char len)

drivers/char/ddcci.c line 94

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+{
+     int ret = 0;
+     unsigned char outer_addr = (unsigned char)(client->addr << 1);
+     unsigned int xor = outer_addr; /* initial xor value */
+
+     /* Consistency checks */
+     if (len > 127)
+             return -EINVAL;
+
+     /* Special case: sender to 0x6E is always 0x51 */

What does that comment mean?

Quoted 50 lines
drivers/char/ddcci.c new file +50 -0
+     if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
+             addr = DDCCI_HOST_ADDR_ODD;
+     } else {
+             /* When sending the odd address is used */
+             addr = addr | 1;
+     }
+
+     /* first byte: sender address */
+     xor ^= addr;
+     ret = i2c_smbus_write_byte(client, addr);
+     if (ret < 0)
+             return ret;
+
+     /* second byte: protocol flag and message size */
+     xor ^= ((p_flag << 7) | len);
+     ret = i2c_smbus_write_byte(client, (p_flag << 7)|len);
+     if (ret < 0)
+             return ret;
+
+     /* send payload */
+     while (len--) {
+             xor ^= (*buf);
+             ret = i2c_smbus_write_byte(client, (*buf));
+             if (ret < 0)
+                     return ret;
+             buf++;
+     }
+
+     /* send checksum */
+     ret = i2c_smbus_write_byte(client, xor);
+     return ret;
+}
+
+/* Write a message to the DDC/CI bus using i2c_master_send() */
+static int __ddcci_write_block(struct i2c_client *client, unsigned char addr,
+                            unsigned char *sendbuf, bool p_flag,
+                            const unsigned char *data, unsigned char len)
+{
+     unsigned char outer_addr = (unsigned char)(client->addr << 1);
+     unsigned int xor = outer_addr;  /* initial xor value */
+     unsigned char *ptr = sendbuf;
+
+     /* Consistency checks */
+     if (len > 127)
+             return -EINVAL;
+
+     /* Special case: sender to 0x6E is always 0x51 */
+     if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
+             addr = DDCCI_HOST_ADDR_ODD;
+     } else {

drivers/char/ddcci.c line 154

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+             /* When sending the odd address is used */
+             addr = addr | 1;
+     }
+
+     /* first byte: sender address */
+     xor ^= addr;
+     *(ptr++) = addr;
+     /* second byte: protocol flag and message size */
+     xor ^= ((p_flag << 7) | len);
+     *(ptr++) = (p_flag << 7)|len;

Fix spacing above. (add some spaces)

Quoted 104 lines
drivers/char/ddcci.c new file +104 -0
+     /* payload */
+     while (len--) {
+             xor ^= (*data);
+             *(ptr++) = (*data);
+             data++;
+     }
+     /* checksum */
+     (*ptr) = xor;
+
+     /* Send it */
+     return i2c_master_send(client, sendbuf, ptr - sendbuf + 1);
+}
+
+/*
+ * Write a message to the DDC/CI bus.
+ *
+ * You must hold the bus mutex when calling this function.
+ */
+static int ddcci_write(struct i2c_client *client, unsigned char addr,
+                    bool p_flag, const unsigned char *data,
+                    unsigned char len)
+{
+     struct ddcci_bus_drv_data *drv_data;
+     unsigned char *sendbuf;
+     int ret;
+
+     drv_data = i2c_get_clientdata(client);
+
+
+     pr_debug("sending to %d:%02x:%02x: %*ph\n", client->adapter->nr,
+              client->addr << 1, addr, len, data);
+     if (drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE) {
+             ret = __ddcci_write_bytewise(client, addr, p_flag, data, len);
+     } else {
+             sendbuf = drv_data->recv_buffer;
+             ret = __ddcci_write_block(client, addr, sendbuf, p_flag, data, len);
+     }
+
+     return ret;
+}
+
+/*
+ * Read a response from the DDC/CI bus with headers directly into a buffer.
+ * Always check for DDCCI_QUIRK_SKIP_FIRST_BYTE when using this function.
+ * The returned length contains the whole unmodified response.
+ * If -EMSGSIZE is returned, the buffer contains the response up to `len`.
+ * If any other negative error code is returned, the buffer content is
+ * unspecified.
+ */
+static int __ddcci_read(struct i2c_client *client, unsigned char addr,
+                     bool p_flag, unsigned long quirks, unsigned char *buf,
+                     unsigned char len)
+{
+     int i, payload_len, packet_length, ret;
+     unsigned char xor = DDCCI_HOST_ADDR_EVEN;
+
+     /* Consistency checks */
+     if (len < 3)
+             return -EINVAL;
+
+     /* Read frame */
+     ret = i2c_master_recv(client, buf, len);
+     if (ret < 0)
+             goto out_err;
+     packet_length = ret;
+
+     /* Skip first byte if quirk active */
+     if ((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) && ret > 0 && len > 0) {
+             ret--;
+             len--;
+             buf++;
+     }
+
+     /* If answer too short (= incomplete) break out */
+     if (ret < 3) {
+             ret = -EIO;
+             goto out_err;
+     }
+
+     /* validate first byte */
+     if (unlikely(buf[0] != addr)) {
+             ret = (buf[0] == '\0') ? -EAGAIN : -EIO;
+             goto out_err;
+     }
+
+     /* validate second byte (protocol flag) */
+     if (unlikely((buf[1] & 0x80) != (p_flag << 7))) {
+             if (!p_flag || !(quirks & DDCCI_QUIRK_NO_PFLAG)) {
+                     ret = -EIO;
+                     goto out_err;
+             }
+     }
+
+     /* get and check payload length */
+     payload_len = buf[1] & 0x7F;
+     if (3+payload_len > packet_length)
+             return -EBADMSG;
+     if (3+payload_len > len)
+             return -EMSGSIZE;
+
+     /* calculate checksum */
+     for (i = 0; i < 3+payload_len; i++)
+             xor ^= buf[i];
+

drivers/char/ddcci.c line 268

Quoted 10 lines
drivers/char/ddcci.c new file +10 -0
+     /* verify checksum */
+     if (xor != 0) {
+             dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
+                     xor, payload_len);
+             ret = -EBADMSG;
+             goto out_err;
+     }
+
+     /* return result */
+     ret = payload_len+3+((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)?1:0);

ugly :(

drivers/char/ddcci.c line 273

Quoted 5 lines
drivers/char/ddcci.c new file +5 -0
+
+out_err:
+     return ret;
+}
+

and it still contains too much use of likely() and unlikely().

drivers/video/backlight/Kconfig config BACKLIGHT_QCOM_WLED · line 293

Quoted 10 lines
drivers/video/backlight/Kconfig +2 -0
index e32694c13da5..8b7efde28cfd 100644
@@ -289,6 +289,16 @@ config BACKLIGHT_QCOM_WLED
        If you have the Qualcomm PMIC, say Y to enable a driver for the
        WLED block. Currently it supports PM8941 and PMI8998.
 
+config BACKLIGHT_DDCCI
+     tristate "DDCCI Backlight Driver"

add line:
depends on I2C

drivers/video/backlight/Kconfig config BACKLIGHT_QCOM_WLED · line 295

Quoted 2 lines
drivers/video/backlight/Kconfig +2 -0
+     help
+       If you have a DDC/CI supporing monitor, say Y to enable a driver

supporting

drivers/video/backlight/Kconfig config BACKLIGHT_QCOM_WLED · line 304

Quoted 9 lines
drivers/video/backlight/Kconfig +6 -0
+       to control its backlight using DDC/CI. This could be useful if
+       your monitor does not include a backlight driver. For this to be
+       useful you need to enable DDCCI support which can be found in
+       Device Drivers -> Character devices and that further depends on
+       I2C.
+
 config BACKLIGHT_RT4831
      tristate "Richtek RT4831 Backlight Driver"
      depends on MFD_RT4831

--
~Randy

maintainer
#3 lore raw
Quoted 4 lines · Yusuf Khan

I (hopefully) fulfilled those requests in my subsequent patch, turns out I forgot to git add some of my changes. I did not remove the unlikely()s as I think they look good and point to where error checking is that many may miss.

Never use a unlikely() or likely() macro unless you can prove, with a benchmark, that it makes the code go faster. If not, just remove them as the compiler and CPU will always do a better job at this (we have proof of it in the past.)

thanks,

greg k-h

author
#4 lore raw

Fine

Quoted 15 lines · gregkh
Quoted 4 lines · Yusuf Khan

I (hopefully) fulfilled those requests in my subsequent patch, turns out I forgot to git add some of my changes. I did not remove the unlikely()s as I think they look good and point to where error checking is that many may miss.

Never use a unlikely() or likely() macro unless you can prove, with a benchmark, that it makes the code go faster. If not, just remove them as the compiler and CPU will always do a better job at this (we have proof of it in the past.)

thanks,

greg k-h