Skip to content

Conversation

@avolmat-st
Copy link

@avolmat-st avolmat-st commented Sep 18, 2025

This PR correct incorrects (clock_control_subsys_t *) cast done when calling some clock_control_ apis in some stm32 drivers.

@avolmat-st avolmat-st force-pushed the pr-stm32-pclken-const-fixes branch from b7c9c90 to 036a247 Compare September 19, 2025 11:15
@avolmat-st avolmat-st changed the title video: stm32: dcmipp: remove const for clock structs drivers: stm32: correct wrong clock_control_subsys_t cast in clock_control calls Sep 19, 2025
@avolmat-st avolmat-st force-pushed the pr-stm32-pclken-const-fixes branch from 3ff6c22 to 036a247 Compare September 19, 2025 12:02
@avolmat-st avolmat-st marked this pull request as ready for review September 19, 2025 12:03
@zephyrbot zephyrbot added area: mbox area: USB Universal Serial Bus area: Video Video subsystem platform: STM32 ST Micro STM32 labels Sep 19, 2025
mathieuchopstm
mathieuchopstm previously approved these changes Sep 19, 2025
Copy link
Contributor

@mathieuchopstm mathieuchopstm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How ironic. Makes you wonder whether casts should even be there in the first place...

ngphibang
ngphibang previously approved these changes Sep 19, 2025
if (DT_INST_NUM_CLOCKS(0) > 1) {
if (clock_control_configure(clk, (clock_control_subsys_t *)&pclken[1],
NULL) != 0) {
if (clock_control_configure(clk, (clock_control_subsys_t)&pclken[1], NULL) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not a correct fix, explicit type conversion is the bug here, and we had too many of them last time.

diff --git a/drivers/usb/udc/udc_stm32.c b/drivers/usb/udc/udc_stm32.c
index 6d4f14af70c..fd067a77b57 100644
--- a/drivers/usb/udc/udc_stm32.c
+++ b/drivers/usb/udc/udc_stm32.c
@@ -1034,7 +1034,7 @@ static void priv_pcd_prepare(const struct device *dev)
 #endif /* USB_OTG_HS_EMB_PHY */
 }
 
-static const struct stm32_pclken pclken[] = STM32_DT_INST_CLOCKS(0);
+static struct stm32_pclken pclken[] = STM32_DT_INST_CLOCKS(0);
 
 static int priv_clock_enable(void)
 {
@@ -1108,14 +1108,13 @@ static int priv_clock_enable(void)
 #endif
 
        if (DT_INST_NUM_CLOCKS(0) > 1) {
-               if (clock_control_configure(clk, (clock_control_subsys_t *)&pclken[1],
-                                                                       NULL) != 0) {
+               if (clock_control_configure(clk, &pclken[1], NULL) != 0) {
                        LOG_ERR("Could not select USB domain clock");
                        return -EIO;
                }
        }
 
-       if (clock_control_on(clk, (clock_control_subsys_t *)&pclken[0]) != 0) {
+       if (clock_control_on(clk, &pclken[0]) != 0) {
                LOG_ERR("Unable to enable USB clock");
                return -EIO;
        }
@@ -1124,7 +1123,7 @@ static int priv_clock_enable(void)
                uint32_t usb_clock_rate;
 
                if (clock_control_get_rate(clk,
-                                          (clock_control_subsys_t *)&pclken[1],
+                                          &pclken[1],
                                           &usb_clock_rate) != 0) {
                        LOG_ERR("Failed to get USB domain clock rate");
                        return -EIO;
@@ -1195,7 +1194,7 @@ static int priv_clock_disable(void)
 {
        const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
 
-       if (clock_control_off(clk, (clock_control_subsys_t *)&pclken[0]) != 0) {
+       if (clock_control_off(clk, &pclken[0]) != 0) {
                LOG_ERR("Unable to disable USB clock");
                return -EIO;
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we remove const from pclken? Doing so unnecessarily increases the RAM footprint since the structure is never written to.

Copy link
Author

@avolmat-st avolmat-st Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfischer-no, the issue if we remove the (clock_control_subsys_t) is that we would be affecting a const to a non-const void * which will lead to a warning. Hence doing the (clock_control_subsys_t) cast.
It is very common to have the clock related information as const within the driver config structure since anyway those information are populated at build time by looking at the device-tree information.

Ex:

struct stm32_dcmipp_config {
        const struct stm32_pclken dcmipp_pclken;
        const struct stm32_pclken dcmipp_pclken_ker;
        irq_config_func_t irq_config;
        const struct pinctrl_dev_config *pctrl;
        const struct device *source_dev;
        const struct reset_dt_spec reset_dcmipp;
...

Unless I mistake, if we remove the whole cast, it would mean that we need to remove the const of the config structure as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand that. My question may have been poorly worded, what I meant to say is "why should we remove const and move the pclken into RAM just to avoid a cast?"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my reply wasn't clear enough in fact. This was a reply targetting @jfischer-no about the proposal to remove const.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfischer-no, could you clarify the point about my 2nd question ? As I wrote above, very very often (most of the case ?) the struct stm32_pclken is stored within the _config structure of the driver. As far as I understand, it is not only a matter of removing the const in front of the struct stm32_pclken within that structure but also removing the const in front of the whole _config struct otherwise this would still be a const pointer. (cf the example I have few comments above).

Regarding the fact that clock_control_subsys_t might be a nonsense, maybe yes, but shouldn't this be a treewide operation to do ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And while we are on it, something like typedef void *clock_control_subsys_t; is nonsense.

@jfischer-no This question relates to the clock_control api definition, which is out of scope here.

So, as I can understand (but not made explicit despite the clarification questions), the complain here is only on cases where pclken is not in a struct and in which the proposed fix works.

Let's fix this case and move on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now. @jfischer-no PTAL

Copy link
Contributor

@jfischer-no jfischer-no Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfischer-no, could you clarify the point about my 2nd question ? As I wrote above, very very often (most of the case ?) the struct stm32_pclken is stored within the _config structure of the driver.

Well it looks like it should be placed in driver's data instead. I guess in worst case the whole config would be placed in RAM (for example video_stm32_dcmi_config_0 is placed in RAM). However, I did not ask for these changes.

As far as I understand, it is not only a matter of removing the const in front of the struct stm32_pclken within that structure but also removing the const in front of the whole _config struct otherwise this would still be a const pointer. (cf the example I have few comments above).

I explicitly commented on the changes in drivers/usb/udc/udc_stm32.c. pclken is not a member of shim driver config. The "const pointer" does not do what you expect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfischer-no Your comment has been addressed. Your fix has actually been taken in extenso, so if you want, you could claim co-authorship or sign-off, or if you have other comments on the scope of this PR, please do so, but can we move on, please ?

@avolmat-st avolmat-st force-pushed the pr-stm32-pclken-const-fixes branch from 036a247 to b442f17 Compare September 30, 2025 19:30
Alain Volmat added 4 commits October 1, 2025 09:29
Correct the clock_control_subsys_t cast in calls of the
clock_control_ framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Correct the clock_control_subsys_t cast in calls of the
clock_control_ framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Avoid incorrect (clock_control_subsys_t *) incorrect cast and
remove the cast of pclken in calls to the clock_control_
framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Correct the clock_control_subsys_t cast in calls of the
clock_control_ framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
@avolmat-st avolmat-st force-pushed the pr-stm32-pclken-const-fixes branch from b442f17 to 0dd4c8a Compare October 1, 2025 07:33
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 1, 2025

Copy link
Contributor

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UDC changes LGTM. Perhaps someone has to move all the pclken to driver data as they are in no way placed in flash.

@kartben kartben merged commit e7aa49c into zephyrproject-rtos:main Oct 2, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mbox area: USB Universal Serial Bus area: Video Video subsystem platform: STM32 ST Micro STM32

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants