Skip to contentSkip to conversation

extcon: Rework initialization of i2c_device_id

PATCH In review Uwe Kleine-König (The Capable Hub) 1 participant 6 messages May 18 – Nov 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover extcon: Rework initialization of i2c_device_id

Hello,

I plan to do the following to the definition of i2c_device_id:

	diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
	index 23ff24080dfd..aebd3a5e90af 100644
	--- a/include/linux/mod_devicetable.h
	+++ b/include/linux/mod_devicetable.h
	@@ -477,7 +477,11 @@ struct rpmsg_device_id {

	 struct i2c_device_id {
		char name[I2C_NAME_SIZE];
	-	kernel_ulong_t driver_data;	/* Data private to the driver */
	+	union {
	+		/* Data private to the driver */
	+		kernel_ulong_t driver_data;
	+		const void *driver_data_ptr;
	+	};
	 };

	 /* pci_epf */

. This requires that .driver_data is assigned via a named initializer for static data. This requirement isn't a bad one because named initializers are also much better readable than list initializers.

The union added to struct i2c_device_id enables further cleanups like:

	diff --git a/drivers/regulator/ad5398.c b/drivers/regulator/ad5398.c
	index 0123ca8157a8..dfb0b07500a7 100644
	--- a/drivers/regulator/ad5398.c
	+++ b/drivers/regulator/ad5398.c
	@@ -207,8 +207,8 @@ struct ad5398_current_data_format {
	 static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};

	 static const struct i2c_device_id ad5398_id[] = {
	-	{ .name = "ad5398", .driver_data = (kernel_ulong_t)&df_10_4_120 },
	-	{ .name = "ad5821", .driver_data = (kernel_ulong_t)&df_10_4_120 },
	+	{ .name = "ad5398", .driver_data_ptr = &df_10_4_120 },
	+	{ .name = "ad5821", .driver_data_ptr = &df_10_4_120 },
	 	{ }
	 };
	 MODULE_DEVICE_TABLE(i2c, ad5398_id);
	@@ -219,8 +219,7 @@ static int ad5398_probe(struct i2c_client *client)
	 	struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
	 	struct regulator_config config = { };
	 	struct ad5398_chip_info *chip;
	-	const struct ad5398_current_data_format *df =
	-	                (struct ad5398_current_data_format *)id->driver_data;
	+	const struct ad5398_current_data_format *df = id->driver_data;

	 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
	 	if (!chip)

that are an improvement for readability (again!) and it keeps some properties of the pointers (here: being const) without having to pay attention for that. (I didn't find a extcon driver that benefits, so this is "only" a regulator driver example.)

My additional motivation for this effort is CHERI[1]. This is a hardware extension that uses 128 bit pointers but unsigned long is still 64 bit. So with CHERI you cannot store pointers in unsigned long variables.

The first patch simplifies one extcon driver to not use .driver_data, the second converts all extcon drivers to initialize i2c_device_ids using named initializers.

Best regards
Uwe

[1] https://cheri-alliance.org/discover-cheri/

    https://lwn.net/Articles/1037974/

Uwe Kleine-König (The Capable Hub) (2):
extcon: rt8973: Drop unused driver data
extcon: Use named initializers for arrays of i2c_device_data

 drivers/extcon/extcon-fsa9480.c    | 4 ++--
 drivers/extcon/extcon-lc824206xa.c | 2 +-
 drivers/extcon/extcon-max14526.c   | 2 +-
 drivers/extcon/extcon-ptn5150.c    | 2 +-
 drivers/extcon/extcon-rt8973a.c    | 2 +-
 drivers/extcon/extcon-rt8973a.h    | 4 ----
 drivers/extcon/extcon-sm5502.c     | 6 +++---
 7 files changed, 9 insertions(+), 13 deletions(-)

base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 extcon: rt8973: Drop unused driver data

The .driver_data member isn't used in this driver, so the explicit assignment can safely be dropped. Also drop the enum after its only user is gone.

While touching this i2c_device_id array, assign .name using a named initializer which makes it easier to understand and more robust to changes in the definition of struct i2c_device_id.

---
 drivers/extcon/extcon-rt8973a.c | 2 +-
 drivers/extcon/extcon-rt8973a.h | 4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
drivers/extcon/extcon-rt8973a.c +1 -1
index 19bb49f13fb0..dfeedb295c51 100644
@@ -684,7 +684,7 @@ static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops,
 			 rt8973a_muic_suspend, rt8973a_muic_resume);
 
 static const struct i2c_device_id rt8973a_i2c_id[] = {
-	{ "rt8973a", TYPE_RT8973A },
+	{ .name = "rt8973a" },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id);
drivers/extcon/extcon-rt8973a.h +0 -4
index 0d0d69fcac92..039af2d1cac6 100644
@@ -8,10 +8,6 @@
 #ifndef __LINUX_EXTCON_RT8973A_H
 #define __LINUX_EXTCON_RT8973A_H
 
-enum rt8973a_types {
-	TYPE_RT8973A,
-};
-
 /* RT8973A registers */
 enum rt8973A_reg {
 	RT8973A_REG_DEVICE_ID = 0x1,
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 extcon: Use named initializers for arrays of i2c_device_data

In reply to Uwe Kleine-König (The Capable Hub)

While being less compact, using named initializers allows to more easily see which members of the structs are assigned which value without having to lookup the declaration of the struct. And it's also more robust against changes to the struct definition.

The mentioned robustness is relevant for a planned change to struct i2c_device_id that replaces .driver_data by an anonymous union.

While touching all these arrays, unify usage of whitespace in the list terminator.

This patch doesn't modify the compiled arrays, only their representation in source form benefits. The former was confirmed with x86 and arm64 builds.

---
 drivers/extcon/extcon-fsa9480.c    | 4 ++--
 drivers/extcon/extcon-lc824206xa.c | 2 +-
 drivers/extcon/extcon-max14526.c   | 2 +-
 drivers/extcon/extcon-ptn5150.c    | 2 +-
 drivers/extcon/extcon-sm5502.c     | 6 +++---
 5 files changed, 8 insertions(+), 8 deletions(-)
drivers/extcon/extcon-fsa9480.c +2 -2
index a031eb0914a0..5de77ac5a294 100644
@@ -350,8 +350,8 @@ static const struct dev_pm_ops fsa9480_pm_ops = {
 };
 
 static const struct i2c_device_id fsa9480_id[] = {
-	{ "fsa9480" },
-	{}
+	{ .name = "fsa9480" },
+	{ }
 };
 MODULE_DEVICE_TABLE(i2c, fsa9480_id);
 
drivers/extcon/extcon-lc824206xa.c +1 -1
index 56938748aea8..a758f2955207 100644
@@ -475,7 +475,7 @@ static int lc824206xa_probe(struct i2c_client *client)
 }
 
 static const struct i2c_device_id lc824206xa_i2c_ids[] = {
-	{ "lc824206xa" },
+	{ .name = "lc824206xa" },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, lc824206xa_i2c_ids);
drivers/extcon/extcon-max14526.c +1 -1
index 3750a5c20612..2c337fd7214f 100644
@@ -281,7 +281,7 @@ static const struct of_device_id max14526_match[] = {
 MODULE_DEVICE_TABLE(of, max14526_match);
 
 static const struct i2c_device_id max14526_id[] = {
-	{ "max14526" },
+	{ .name = "max14526" },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, max14526_id);
drivers/extcon/extcon-ptn5150.c +1 -1
index eca1b140aeb0..bcbd38ed0d0d 100644
@@ -393,7 +393,7 @@ static const struct of_device_id ptn5150_dt_match[] = {
 MODULE_DEVICE_TABLE(of, ptn5150_dt_match);
 
 static const struct i2c_device_id ptn5150_i2c_id[] = {
-	{ "ptn5150" },
+	{ .name = "ptn5150" },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, ptn5150_i2c_id);
drivers/extcon/extcon-sm5502.c +3 -3
index c8c4b9ef72aa..5661b57cbb45 100644
@@ -827,9 +827,9 @@ static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
 			 sm5502_muic_suspend, sm5502_muic_resume);
 
 static const struct i2c_device_id sm5502_i2c_id[] = {
-	{ "sm5502", (kernel_ulong_t)&sm5502_data },
-	{ "sm5504", (kernel_ulong_t)&sm5504_data },
-	{ "sm5703-muic", (kernel_ulong_t)&sm5502_data },
+	{ .name = "sm5502", .driver_data = (kernel_ulong_t)&sm5502_data },
+	{ .name = "sm5504", .driver_data = (kernel_ulong_t)&sm5504_data },
+	{ .name = "sm5703-muic", .driver_data = (kernel_ulong_t)&sm5502_data },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
Signature · 1 line
2.47.3
#3 lore raw
In reply to Uwe Kleine-König (The Capable Hub)
Quoted 13 lines · Uwe Kleine-König (The Capable Hub)

Hello,

I plan to do the following to the definition of i2c_device_id:

	diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
	index 23ff24080dfd..aebd3a5e90af 100644
	--- a/include/linux/mod_devicetable.h
	+++ b/include/linux/mod_devicetable.h
	@@ -477,7 +477,11 @@ struct rpmsg_device_id {

	 struct i2c_device_id {
		char name[I2C_NAME_SIZE];
	-	kernel_ulong_t driver_data;	/* Data private to the driver */
Quoted 8 lines
	+	union {
	+		/* Data private to the driver */
	+		kernel_ulong_t driver_data;
	+		const void *driver_data_ptr;
	+	};
	 };

	 /* pci_epf */

I want to work on that, but I'd need this patch series to be in for it. Do you still have this patch on your radar?

Best regards
Uwe

#4 lore raw

Hello,

Quoted 21 lines · Uwe Kleine-König (The Capable Hub)
Quoted 20 lines · Uwe Kleine-König (The Capable Hub)

Hello,

I plan to do the following to the definition of i2c_device_id:

	diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
	index 23ff24080dfd..aebd3a5e90af 100644
	--- a/include/linux/mod_devicetable.h
	+++ b/include/linux/mod_devicetable.h
	@@ -477,7 +477,11 @@ struct rpmsg_device_id {

	 struct i2c_device_id {
		char name[I2C_NAME_SIZE];
	-	kernel_ulong_t driver_data;	/* Data private to the driver */
	+	union {
	+		/* Data private to the driver */
	+		kernel_ulong_t driver_data;
	+		const void *driver_data_ptr;
	+	};
	 };
Quoted 4 lines
Quoted 1 line
	 /* pci_epf */

I want to work on that, but I'd need this patch series to be in for it. Do you still have this patch on your radar?

While I still have to make several patches land before I can modify struct i2c_device_id, getting the patches from this thread in would at least make my queue smaller.

FTR: The series still applies fine to next-20260716.

Best regards
Uwe

#5 lore raw

Hello,

Quoted 24 lines · Uwe Kleine-König (The Capable Hub)
Quoted 23 lines · Uwe Kleine-König (The Capable Hub)
Quoted 21 lines · Uwe Kleine-König (The Capable Hub)

Hello,

I plan to do the following to the definition of i2c_device_id:

	diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
	index 23ff24080dfd..aebd3a5e90af 100644
	--- a/include/linux/mod_devicetable.h
	+++ b/include/linux/mod_devicetable.h
	@@ -477,7 +477,11 @@ struct rpmsg_device_id {

	 struct i2c_device_id {
		char name[I2C_NAME_SIZE];
	-	kernel_ulong_t driver_data;	/* Data private to the driver */
	+	union {
	+		/* Data private to the driver */
	+		kernel_ulong_t driver_data;
	+		const void *driver_data_ptr;
	+	};
	 };

	 /* pci_epf */
Quoted 8 lines
Quoted 2 lines

I want to work on that, but I'd need this patch series to be in for it. Do you still have this patch on your radar?

While I still have to make several patches land before I can modify struct i2c_device_id, getting the patches from this thread in would at least make my queue smaller.

FTR: The series still applies fine to next-20260716.

It's November and the series still applies to next-20261116. Does someone still have this patch set on their radar?

Best regards
Uwe