2024-02-18 19:17:44

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH 0/4] usb: constify struct device_type usage

This is a simple and straight forward cleanup series that makes all device
types in the net subsystem constants. This has been possible since 2011 [1]
but not all occurrences were cleaned.

---
[1] https://lore.kernel.org/all/[email protected]/

Signed-off-by: Ricardo B. Marliere <[email protected]>

---
Ricardo B. Marliere (4):
usb: typec: constify the struct device_type usage
usb: phy: constify the struct device_type usage
usb: gadget: constify the struct device_type usage
usb: core: constify the struct device_type usage

drivers/usb/core/endpoint.c | 2 +-
drivers/usb/core/message.c | 2 +-
drivers/usb/core/port.c | 2 +-
drivers/usb/core/usb.c | 2 +-
drivers/usb/core/usb.h | 8 ++++----
drivers/usb/gadget/function/u_ether.c | 2 +-
drivers/usb/phy/phy.c | 2 +-
drivers/usb/typec/pd.c | 30 +++++++++++++++---------------
8 files changed, 25 insertions(+), 25 deletions(-)
---
base-commit: cf92b9df3dcf45eab0fe3fcf9e97ce55c31c48c0
change-id: 20240218-device_cleanup-usb-98fac2ef0160

Best regards,
--
Ricardo B. Marliere <[email protected]>



2024-02-18 19:18:01

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH 1/4] usb: typec: constify the struct device_type usage

Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
core can properly handle constant struct device_type. Move all the
device_type variables in use by the driver to be constant structures as
well, placing it into read-only memory which can not be modified at
runtime.

Signed-off-by: Ricardo B. Marliere <[email protected]>
---
drivers/usb/typec/pd.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/usb/typec/pd.c b/drivers/usb/typec/pd.c
index b9cca2be76fc..d78c04a421bc 100644
--- a/drivers/usb/typec/pd.c
+++ b/drivers/usb/typec/pd.c
@@ -157,7 +157,7 @@ static const struct attribute_group source_fixed_supply_group = {
};
__ATTRIBUTE_GROUPS(source_fixed_supply);

-static struct device_type source_fixed_supply_type = {
+static const struct device_type source_fixed_supply_type = {
.name = "pdo",
.release = pdo_release,
.groups = source_fixed_supply_groups,
@@ -182,7 +182,7 @@ static const struct attribute_group sink_fixed_supply_group = {
};
__ATTRIBUTE_GROUPS(sink_fixed_supply);

-static struct device_type sink_fixed_supply_type = {
+static const struct device_type sink_fixed_supply_type = {
.name = "pdo",
.release = pdo_release,
.groups = sink_fixed_supply_groups,
@@ -213,7 +213,7 @@ static struct attribute *source_variable_supply_attrs[] = {
};
ATTRIBUTE_GROUPS(source_variable_supply);

-static struct device_type source_variable_supply_type = {
+static const struct device_type source_variable_supply_type = {
.name = "pdo",
.release = pdo_release,
.groups = source_variable_supply_groups,
@@ -227,7 +227,7 @@ static struct attribute *sink_variable_supply_attrs[] = {
};
ATTRIBUTE_GROUPS(sink_variable_supply);

-static struct device_type sink_variable_supply_type = {
+static const struct device_type sink_variable_supply_type = {
.name = "pdo",
.release = pdo_release,
.groups = sink_variable_supply_groups,
@@ -258,7 +258,7 @@ static struct attribute *source_battery_attrs[] = {
};
ATTRIBUTE_GROUPS(source_battery);

-static struct device_type source_battery_type = {
+static const struct device_type source_battery_type = {
.name = "pdo",
.release = pdo_release,
.groups = source_battery_groups,
@@ -272,7 +272,7 @@ static struct attribute *sink_battery_attrs[] = {
};
ATTRIBUTE_GROUPS(sink_battery);

-static struct device_type sink_battery_type = {
+static const struct device_type sink_battery_type = {
.name = "pdo",
.release = pdo_release,
.groups = sink_battery_groups,
@@ -339,7 +339,7 @@ static struct attribute *source_pps_attrs[] = {
};
ATTRIBUTE_GROUPS(source_pps);

-static struct device_type source_pps_type = {
+static const struct device_type source_pps_type = {
.name = "pdo",
.release = pdo_release,
.groups = source_pps_groups,
@@ -353,7 +353,7 @@ static struct attribute *sink_pps_attrs[] = {
};
ATTRIBUTE_GROUPS(sink_pps);

-static struct device_type sink_pps_type = {
+static const struct device_type sink_pps_type = {
.name = "pdo",
.release = pdo_release,
.groups = sink_pps_groups,
@@ -371,30 +371,30 @@ static const char * const apdo_supply_name[] = {
[APDO_TYPE_PPS] = "programmable_supply",
};

-static struct device_type *source_type[] = {
+static const struct device_type *source_type[] = {
[PDO_TYPE_FIXED] = &source_fixed_supply_type,
[PDO_TYPE_BATT] = &source_battery_type,
[PDO_TYPE_VAR] = &source_variable_supply_type,
};

-static struct device_type *source_apdo_type[] = {
+static const struct device_type *source_apdo_type[] = {
[APDO_TYPE_PPS] = &source_pps_type,
};

-static struct device_type *sink_type[] = {
+static const struct device_type *sink_type[] = {
[PDO_TYPE_FIXED] = &sink_fixed_supply_type,
[PDO_TYPE_BATT] = &sink_battery_type,
[PDO_TYPE_VAR] = &sink_variable_supply_type,
};

-static struct device_type *sink_apdo_type[] = {
+static const struct device_type *sink_apdo_type[] = {
[APDO_TYPE_PPS] = &sink_pps_type,
};

/* REVISIT: Export when EPR_*_Capabilities need to be supported. */
static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
{
- struct device_type *type;
+ const struct device_type *type;
const char *name;
struct pdo *p;
int ret;
@@ -460,7 +460,7 @@ static void pd_capabilities_release(struct device *dev)
kfree(to_usb_power_delivery_capabilities(dev));
}

-static struct device_type pd_capabilities_type = {
+static const struct device_type pd_capabilities_type = {
.name = "capabilities",
.release = pd_capabilities_release,
};
@@ -575,7 +575,7 @@ static void pd_release(struct device *dev)
kfree(pd);
}

-static struct device_type pd_type = {
+static const struct device_type pd_type = {
.name = "usb_power_delivery",
.release = pd_release,
.groups = pd_groups,

--
2.43.0


2024-02-18 19:18:14

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH 2/4] usb: phy: constify the struct device_type usage

Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
core can properly handle constant struct device_type. Move the
usb_phy_dev_type variable to be a constant structure as well, placing it
into read-only memory which can not be modified at runtime.

Signed-off-by: Ricardo B. Marliere <[email protected]>
---
drivers/usb/phy/phy.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 4b468bde19cf..06e0fb23566c 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -699,7 +699,7 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
}
EXPORT_SYMBOL_GPL(usb_add_phy);

-static struct device_type usb_phy_dev_type = {
+static const struct device_type usb_phy_dev_type = {
.name = "usb_phy",
.uevent = usb_phy_uevent,
};

--
2.43.0


2024-02-18 19:18:28

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH 3/4] usb: gadget: constify the struct device_type usage

Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
core can properly handle constant struct device_type. Move the gadget_type
variable to be a constant structure as well, placing it into read-only
memory which can not be modified at runtime.

Signed-off-by: Ricardo B. Marliere <[email protected]>
---
drivers/usb/gadget/function/u_ether.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/function/u_ether.c
index 3c5a6f6ac341..444212c0b5a9 100644
--- a/drivers/usb/gadget/function/u_ether.c
+++ b/drivers/usb/gadget/function/u_ether.c
@@ -718,7 +718,7 @@ static const struct net_device_ops eth_netdev_ops = {
.ndo_validate_addr = eth_validate_addr,
};

-static struct device_type gadget_type = {
+static const struct device_type gadget_type = {
.name = "gadget",
};


--
2.43.0


2024-02-18 19:18:44

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH 4/4] usb: core: constify the struct device_type usage

Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
core can properly handle constant struct device_type. Move the
usb_device_type, usb_if_device_type, usb_ep_device_type and
usb_port_device_type variables to be constant structures as well, placing
it into read-only memory which can not be modified at runtime.

Signed-off-by: Ricardo B. Marliere <[email protected]>
---
drivers/usb/core/endpoint.c | 2 +-
drivers/usb/core/message.c | 2 +-
drivers/usb/core/port.c | 2 +-
drivers/usb/core/usb.c | 2 +-
drivers/usb/core/usb.h | 8 ++++----
5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c
index a2530811cf7d..4b38b87a1343 100644
--- a/drivers/usb/core/endpoint.c
+++ b/drivers/usb/core/endpoint.c
@@ -141,7 +141,7 @@ static void ep_device_release(struct device *dev)
kfree(ep_dev);
}

-struct device_type usb_ep_device_type = {
+const struct device_type usb_ep_device_type = {
.name = "usb_endpoint",
.release = ep_device_release,
};
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 077dfe48d01c..67316d271596 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1849,7 +1849,7 @@ static int usb_if_uevent(const struct device *dev, struct kobj_uevent_env *env)
return 0;
}

-struct device_type usb_if_device_type = {
+const struct device_type usb_if_device_type = {
.name = "usb_interface",
.release = usb_release_interface,
.uevent = usb_if_uevent,
diff --git a/drivers/usb/core/port.c b/drivers/usb/core/port.c
index c628c1abc907..84d36172b040 100644
--- a/drivers/usb/core/port.c
+++ b/drivers/usb/core/port.c
@@ -429,7 +429,7 @@ static const struct dev_pm_ops usb_port_pm_ops = {
#endif
};

-struct device_type usb_port_device_type = {
+const struct device_type usb_port_device_type = {
.name = "usb_port",
.release = usb_port_device_release,
.pm = &usb_port_pm_ops,
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index dc8d9228a5e7..a0c432b14b20 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -592,7 +592,7 @@ static char *usb_devnode(const struct device *dev,
usb_dev->bus->busnum, usb_dev->devnum);
}

-struct device_type usb_device_type = {
+const struct device_type usb_device_type = {
.name = "usb_device",
.release = usb_release_dev,
.uevent = usb_dev_uevent,
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index bfecb50773b6..b8324ea05b20 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -144,10 +144,10 @@ static inline int usb_disable_usb2_hardware_lpm(struct usb_device *udev)
extern const struct class usbmisc_class;
extern const struct bus_type usb_bus_type;
extern struct mutex usb_port_peer_mutex;
-extern struct device_type usb_device_type;
-extern struct device_type usb_if_device_type;
-extern struct device_type usb_ep_device_type;
-extern struct device_type usb_port_device_type;
+extern const struct device_type usb_device_type;
+extern const struct device_type usb_if_device_type;
+extern const struct device_type usb_ep_device_type;
+extern const struct device_type usb_port_device_type;
extern struct usb_device_driver usb_generic_driver;

static inline int is_usb_device(const struct device *dev)

--
2.43.0


2024-02-26 11:51:06

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [PATCH 1/4] usb: typec: constify the struct device_type usage

On Sun, Feb 18, 2024 at 04:18:09PM -0300, Ricardo B. Marliere wrote:
> Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
> core can properly handle constant struct device_type. Move all the
> device_type variables in use by the driver to be constant structures as
> well, placing it into read-only memory which can not be modified at
> runtime.
>
> Signed-off-by: Ricardo B. Marliere <[email protected]>

Acked-by: Heikki Krogerus <[email protected]>

> ---
> drivers/usb/typec/pd.c | 30 +++++++++++++++---------------
> 1 file changed, 15 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/usb/typec/pd.c b/drivers/usb/typec/pd.c
> index b9cca2be76fc..d78c04a421bc 100644
> --- a/drivers/usb/typec/pd.c
> +++ b/drivers/usb/typec/pd.c
> @@ -157,7 +157,7 @@ static const struct attribute_group source_fixed_supply_group = {
> };
> __ATTRIBUTE_GROUPS(source_fixed_supply);
>
> -static struct device_type source_fixed_supply_type = {
> +static const struct device_type source_fixed_supply_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = source_fixed_supply_groups,
> @@ -182,7 +182,7 @@ static const struct attribute_group sink_fixed_supply_group = {
> };
> __ATTRIBUTE_GROUPS(sink_fixed_supply);
>
> -static struct device_type sink_fixed_supply_type = {
> +static const struct device_type sink_fixed_supply_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = sink_fixed_supply_groups,
> @@ -213,7 +213,7 @@ static struct attribute *source_variable_supply_attrs[] = {
> };
> ATTRIBUTE_GROUPS(source_variable_supply);
>
> -static struct device_type source_variable_supply_type = {
> +static const struct device_type source_variable_supply_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = source_variable_supply_groups,
> @@ -227,7 +227,7 @@ static struct attribute *sink_variable_supply_attrs[] = {
> };
> ATTRIBUTE_GROUPS(sink_variable_supply);
>
> -static struct device_type sink_variable_supply_type = {
> +static const struct device_type sink_variable_supply_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = sink_variable_supply_groups,
> @@ -258,7 +258,7 @@ static struct attribute *source_battery_attrs[] = {
> };
> ATTRIBUTE_GROUPS(source_battery);
>
> -static struct device_type source_battery_type = {
> +static const struct device_type source_battery_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = source_battery_groups,
> @@ -272,7 +272,7 @@ static struct attribute *sink_battery_attrs[] = {
> };
> ATTRIBUTE_GROUPS(sink_battery);
>
> -static struct device_type sink_battery_type = {
> +static const struct device_type sink_battery_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = sink_battery_groups,
> @@ -339,7 +339,7 @@ static struct attribute *source_pps_attrs[] = {
> };
> ATTRIBUTE_GROUPS(source_pps);
>
> -static struct device_type source_pps_type = {
> +static const struct device_type source_pps_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = source_pps_groups,
> @@ -353,7 +353,7 @@ static struct attribute *sink_pps_attrs[] = {
> };
> ATTRIBUTE_GROUPS(sink_pps);
>
> -static struct device_type sink_pps_type = {
> +static const struct device_type sink_pps_type = {
> .name = "pdo",
> .release = pdo_release,
> .groups = sink_pps_groups,
> @@ -371,30 +371,30 @@ static const char * const apdo_supply_name[] = {
> [APDO_TYPE_PPS] = "programmable_supply",
> };
>
> -static struct device_type *source_type[] = {
> +static const struct device_type *source_type[] = {
> [PDO_TYPE_FIXED] = &source_fixed_supply_type,
> [PDO_TYPE_BATT] = &source_battery_type,
> [PDO_TYPE_VAR] = &source_variable_supply_type,
> };
>
> -static struct device_type *source_apdo_type[] = {
> +static const struct device_type *source_apdo_type[] = {
> [APDO_TYPE_PPS] = &source_pps_type,
> };
>
> -static struct device_type *sink_type[] = {
> +static const struct device_type *sink_type[] = {
> [PDO_TYPE_FIXED] = &sink_fixed_supply_type,
> [PDO_TYPE_BATT] = &sink_battery_type,
> [PDO_TYPE_VAR] = &sink_variable_supply_type,
> };
>
> -static struct device_type *sink_apdo_type[] = {
> +static const struct device_type *sink_apdo_type[] = {
> [APDO_TYPE_PPS] = &sink_pps_type,
> };
>
> /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
> static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
> {
> - struct device_type *type;
> + const struct device_type *type;
> const char *name;
> struct pdo *p;
> int ret;
> @@ -460,7 +460,7 @@ static void pd_capabilities_release(struct device *dev)
> kfree(to_usb_power_delivery_capabilities(dev));
> }
>
> -static struct device_type pd_capabilities_type = {
> +static const struct device_type pd_capabilities_type = {
> .name = "capabilities",
> .release = pd_capabilities_release,
> };
> @@ -575,7 +575,7 @@ static void pd_release(struct device *dev)
> kfree(pd);
> }
>
> -static struct device_type pd_type = {
> +static const struct device_type pd_type = {
> .name = "usb_power_delivery",
> .release = pd_release,
> .groups = pd_groups,
>
> --
> 2.43.0

--
heikki