* [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
@ 2024-07-31 20:14 Rob Herring (Arm)
2024-08-27 14:23 ` Thierry Reding
0 siblings, 1 reply; 7+ messages in thread
From: Rob Herring (Arm) @ 2024-07-31 20:14 UTC (permalink / raw)
To: Laxman Dewangan, Dmitry Torokhov, Thierry Reding, Jonathan Hunter
Cc: linux-input, linux-tegra, linux-kernel
There's no need to get the length of an DT array property before
parsing the array. of_property_read_variable_u32_array() takes a
minimum and maximum length and returns the actual length (or error
code).
This is part of a larger effort to remove callers of of_get_property()
and similar functions. of_get_property() leaks the DT property data
pointer which is a problem for dynamically allocated nodes which may
be freed.
---
drivers/input/keyboard/tegra-kbc.c | 72 +++++++++++-------------------
1 file changed, 27 insertions(+), 45 deletions(-)
diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
index a1765ed8c825..53f39fc155ea 100644
--- a/drivers/input/keyboard/tegra-kbc.c
+++ b/drivers/input/keyboard/tegra-kbc.c
@@ -491,12 +491,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
struct device_node *np = kbc->dev->of_node;
u32 prop;
int i;
- u32 num_rows = 0;
- u32 num_cols = 0;
+ int num_rows;
+ int num_cols;
u32 cols_cfg[KBC_MAX_GPIO];
u32 rows_cfg[KBC_MAX_GPIO];
- int proplen;
- int ret;
if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
kbc->debounce_cnt = prop;
@@ -510,56 +508,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
kbc->wakeup = true;
- if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
- dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
- return -ENOENT;
- }
- num_rows = proplen / sizeof(u32);
-
- if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
- dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
- return -ENOENT;
- }
- num_cols = proplen / sizeof(u32);
-
- if (num_rows > kbc->hw_support->max_rows) {
- dev_err(kbc->dev,
- "Number of rows is more than supported by hardware\n");
- return -EINVAL;
- }
-
- if (num_cols > kbc->hw_support->max_columns) {
- dev_err(kbc->dev,
- "Number of cols is more than supported by hardware\n");
- return -EINVAL;
- }
-
- if (!of_get_property(np, "linux,keymap", &proplen)) {
+ if (!of_property_present(np, "linux,keymap")) {
dev_err(kbc->dev, "property linux,keymap not found\n");
return -ENOENT;
}
- if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
- dev_err(kbc->dev,
- "keypad rows/columns not properly specified\n");
- return -EINVAL;
- }
-
/* Set all pins as non-configured */
for (i = 0; i < kbc->num_rows_and_columns; i++)
kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
- ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
- rows_cfg, num_rows);
- if (ret < 0) {
+ num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
+ rows_cfg, 1, KBC_MAX_GPIO);
+ if (num_rows < 0) {
dev_err(kbc->dev, "Rows configurations are not proper\n");
- return -EINVAL;
- }
-
- ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
- cols_cfg, num_cols);
- if (ret < 0) {
- dev_err(kbc->dev, "Cols configurations are not proper\n");
+ return num_rows;
+ } else if (num_rows > kbc->hw_support->max_rows) {
+ dev_err(kbc->dev,
+ "Number of rows is more than supported by hardware\n");
return -EINVAL;
}
@@ -568,11 +533,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
kbc->pin_cfg[rows_cfg[i]].num = i;
}
+ num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
+ cols_cfg, 1, KBC_MAX_GPIO);
+ if (num_cols < 0) {
+ dev_err(kbc->dev, "Cols configurations are not proper\n");
+ return num_cols;
+ } else if (num_cols > kbc->hw_support->max_columns) {
+ dev_err(kbc->dev,
+ "Number of cols is more than supported by hardware\n");
+ return -EINVAL;
+ }
+
for (i = 0; i < num_cols; i++) {
kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
kbc->pin_cfg[cols_cfg[i]].num = i;
}
+ if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
+ dev_err(kbc->dev,
+ "keypad rows/columns not properly specified\n");
+ return -EINVAL;
+ }
+
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-07-31 20:14 [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present() Rob Herring (Arm)
@ 2024-08-27 14:23 ` Thierry Reding
2024-08-27 15:36 ` Dmitry Torokhov
0 siblings, 1 reply; 7+ messages in thread
From: Thierry Reding @ 2024-08-27 14:23 UTC (permalink / raw)
To: Rob Herring (Arm)
Cc: Laxman Dewangan, Dmitry Torokhov, Jonathan Hunter, linux-input,
linux-tegra, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 4702 bytes --]
On Wed, Jul 31, 2024 at 02:14:01PM GMT, Rob Herring (Arm) wrote:
> There's no need to get the length of an DT array property before
> parsing the array. of_property_read_variable_u32_array() takes a
> minimum and maximum length and returns the actual length (or error
> code).
>
> This is part of a larger effort to remove callers of of_get_property()
> and similar functions. of_get_property() leaks the DT property data
> pointer which is a problem for dynamically allocated nodes which may
> be freed.
> ---
> drivers/input/keyboard/tegra-kbc.c | 72 +++++++++++-------------------
> 1 file changed, 27 insertions(+), 45 deletions(-)
>
> diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
> index a1765ed8c825..53f39fc155ea 100644
> --- a/drivers/input/keyboard/tegra-kbc.c
> +++ b/drivers/input/keyboard/tegra-kbc.c
> @@ -491,12 +491,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> struct device_node *np = kbc->dev->of_node;
> u32 prop;
> int i;
> - u32 num_rows = 0;
> - u32 num_cols = 0;
> + int num_rows;
> + int num_cols;
> u32 cols_cfg[KBC_MAX_GPIO];
> u32 rows_cfg[KBC_MAX_GPIO];
> - int proplen;
> - int ret;
>
> if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
> kbc->debounce_cnt = prop;
> @@ -510,56 +508,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
> kbc->wakeup = true;
>
> - if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
> - dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
> - return -ENOENT;
> - }
> - num_rows = proplen / sizeof(u32);
> -
> - if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
> - dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
> - return -ENOENT;
> - }
> - num_cols = proplen / sizeof(u32);
> -
> - if (num_rows > kbc->hw_support->max_rows) {
> - dev_err(kbc->dev,
> - "Number of rows is more than supported by hardware\n");
> - return -EINVAL;
> - }
> -
> - if (num_cols > kbc->hw_support->max_columns) {
> - dev_err(kbc->dev,
> - "Number of cols is more than supported by hardware\n");
> - return -EINVAL;
> - }
> -
> - if (!of_get_property(np, "linux,keymap", &proplen)) {
> + if (!of_property_present(np, "linux,keymap")) {
> dev_err(kbc->dev, "property linux,keymap not found\n");
> return -ENOENT;
> }
>
> - if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> - dev_err(kbc->dev,
> - "keypad rows/columns not properly specified\n");
> - return -EINVAL;
> - }
> -
> /* Set all pins as non-configured */
> for (i = 0; i < kbc->num_rows_and_columns; i++)
> kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
>
> - ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
> - rows_cfg, num_rows);
> - if (ret < 0) {
> + num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
> + rows_cfg, 1, KBC_MAX_GPIO);
> + if (num_rows < 0) {
> dev_err(kbc->dev, "Rows configurations are not proper\n");
> - return -EINVAL;
> - }
> -
> - ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
> - cols_cfg, num_cols);
> - if (ret < 0) {
> - dev_err(kbc->dev, "Cols configurations are not proper\n");
> + return num_rows;
> + } else if (num_rows > kbc->hw_support->max_rows) {
> + dev_err(kbc->dev,
> + "Number of rows is more than supported by hardware\n");
> return -EINVAL;
> }
>
> @@ -568,11 +533,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> kbc->pin_cfg[rows_cfg[i]].num = i;
> }
>
> + num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
> + cols_cfg, 1, KBC_MAX_GPIO);
> + if (num_cols < 0) {
> + dev_err(kbc->dev, "Cols configurations are not proper\n");
> + return num_cols;
> + } else if (num_cols > kbc->hw_support->max_columns) {
> + dev_err(kbc->dev,
> + "Number of cols is more than supported by hardware\n");
> + return -EINVAL;
> + }
> +
> for (i = 0; i < num_cols; i++) {
> kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
> kbc->pin_cfg[cols_cfg[i]].num = i;
> }
>
> + if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> + dev_err(kbc->dev,
> + "keypad rows/columns not properly specified\n");
> + return -EINVAL;
> + }
Previously we wouldn't try to initialize the columns when the
rows/columns were invalid, so this block could move before the last for
loop above.
But it doesn't really matter given that these are exceptions and really
shouldn't happen, so:
Acked-by: Thierry Reding <treding@nvidia.com>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-08-27 14:23 ` Thierry Reding
@ 2024-08-27 15:36 ` Dmitry Torokhov
2024-08-27 17:53 ` Dmitry Torokhov
2024-08-28 15:05 ` Thierry Reding
0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Torokhov @ 2024-08-27 15:36 UTC (permalink / raw)
To: Thierry Reding
Cc: Rob Herring (Arm), Laxman Dewangan, Jonathan Hunter, linux-input,
linux-tegra, linux-kernel
On Tue, Aug 27, 2024 at 04:23:48PM +0200, Thierry Reding wrote:
> On Wed, Jul 31, 2024 at 02:14:01PM GMT, Rob Herring (Arm) wrote:
> > There's no need to get the length of an DT array property before
> > parsing the array. of_property_read_variable_u32_array() takes a
> > minimum and maximum length and returns the actual length (or error
> > code).
> >
> > This is part of a larger effort to remove callers of of_get_property()
> > and similar functions. of_get_property() leaks the DT property data
> > pointer which is a problem for dynamically allocated nodes which may
> > be freed.
> > ---
> > drivers/input/keyboard/tegra-kbc.c | 72 +++++++++++-------------------
> > 1 file changed, 27 insertions(+), 45 deletions(-)
> >
> > diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
> > index a1765ed8c825..53f39fc155ea 100644
> > --- a/drivers/input/keyboard/tegra-kbc.c
> > +++ b/drivers/input/keyboard/tegra-kbc.c
> > @@ -491,12 +491,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > struct device_node *np = kbc->dev->of_node;
> > u32 prop;
> > int i;
> > - u32 num_rows = 0;
> > - u32 num_cols = 0;
> > + int num_rows;
> > + int num_cols;
> > u32 cols_cfg[KBC_MAX_GPIO];
> > u32 rows_cfg[KBC_MAX_GPIO];
> > - int proplen;
> > - int ret;
> >
> > if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
> > kbc->debounce_cnt = prop;
> > @@ -510,56 +508,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
> > kbc->wakeup = true;
> >
> > - if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
> > - dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
> > - return -ENOENT;
> > - }
> > - num_rows = proplen / sizeof(u32);
> > -
> > - if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
> > - dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
> > - return -ENOENT;
> > - }
> > - num_cols = proplen / sizeof(u32);
> > -
> > - if (num_rows > kbc->hw_support->max_rows) {
> > - dev_err(kbc->dev,
> > - "Number of rows is more than supported by hardware\n");
> > - return -EINVAL;
> > - }
> > -
> > - if (num_cols > kbc->hw_support->max_columns) {
> > - dev_err(kbc->dev,
> > - "Number of cols is more than supported by hardware\n");
> > - return -EINVAL;
> > - }
> > -
> > - if (!of_get_property(np, "linux,keymap", &proplen)) {
> > + if (!of_property_present(np, "linux,keymap")) {
> > dev_err(kbc->dev, "property linux,keymap not found\n");
> > return -ENOENT;
> > }
> >
> > - if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > - dev_err(kbc->dev,
> > - "keypad rows/columns not properly specified\n");
> > - return -EINVAL;
> > - }
> > -
> > /* Set all pins as non-configured */
> > for (i = 0; i < kbc->num_rows_and_columns; i++)
> > kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
> >
> > - ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
> > - rows_cfg, num_rows);
> > - if (ret < 0) {
> > + num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
> > + rows_cfg, 1, KBC_MAX_GPIO);
> > + if (num_rows < 0) {
> > dev_err(kbc->dev, "Rows configurations are not proper\n");
> > - return -EINVAL;
> > - }
> > -
> > - ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
> > - cols_cfg, num_cols);
> > - if (ret < 0) {
> > - dev_err(kbc->dev, "Cols configurations are not proper\n");
> > + return num_rows;
> > + } else if (num_rows > kbc->hw_support->max_rows) {
> > + dev_err(kbc->dev,
> > + "Number of rows is more than supported by hardware\n");
> > return -EINVAL;
> > }
> >
> > @@ -568,11 +533,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > kbc->pin_cfg[rows_cfg[i]].num = i;
> > }
> >
> > + num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
> > + cols_cfg, 1, KBC_MAX_GPIO);
> > + if (num_cols < 0) {
> > + dev_err(kbc->dev, "Cols configurations are not proper\n");
> > + return num_cols;
> > + } else if (num_cols > kbc->hw_support->max_columns) {
> > + dev_err(kbc->dev,
> > + "Number of cols is more than supported by hardware\n");
> > + return -EINVAL;
> > + }
> > +
> > for (i = 0; i < num_cols; i++) {
> > kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
> > kbc->pin_cfg[cols_cfg[i]].num = i;
> > }
> >
> > + if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > + dev_err(kbc->dev,
> > + "keypad rows/columns not properly specified\n");
> > + return -EINVAL;
> > + }
>
> Previously we wouldn't try to initialize the columns when the
> rows/columns were invalid, so this block could move before the last for
> loop above.
>
> But it doesn't really matter given that these are exceptions and really
> shouldn't happen, so:
>
> Acked-by: Thierry Reding <treding@nvidia.com>
I don't quite like of_property_read_variable_u32_array() because it is
OF-specific. device_property_count_u32() will return the number of
elements in an array. But I guess this driver will only be used on an OF
system...
Applied, thank you.
--
Dmitry
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-08-27 15:36 ` Dmitry Torokhov
@ 2024-08-27 17:53 ` Dmitry Torokhov
2024-09-06 5:33 ` Dmitry Torokhov
2024-08-28 15:05 ` Thierry Reding
1 sibling, 1 reply; 7+ messages in thread
From: Dmitry Torokhov @ 2024-08-27 17:53 UTC (permalink / raw)
To: Thierry Reding
Cc: Rob Herring (Arm), Laxman Dewangan, Jonathan Hunter, linux-input,
linux-tegra, linux-kernel
On Tue, Aug 27, 2024 at 08:36:29AM -0700, Dmitry Torokhov wrote:
> On Tue, Aug 27, 2024 at 04:23:48PM +0200, Thierry Reding wrote:
> > On Wed, Jul 31, 2024 at 02:14:01PM GMT, Rob Herring (Arm) wrote:
> > > There's no need to get the length of an DT array property before
> > > parsing the array. of_property_read_variable_u32_array() takes a
> > > minimum and maximum length and returns the actual length (or error
> > > code).
> > >
> > > This is part of a larger effort to remove callers of of_get_property()
> > > and similar functions. of_get_property() leaks the DT property data
> > > pointer which is a problem for dynamically allocated nodes which may
> > > be freed.
> > > ---
> > > drivers/input/keyboard/tegra-kbc.c | 72 +++++++++++-------------------
> > > 1 file changed, 27 insertions(+), 45 deletions(-)
> > >
> > > diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
> > > index a1765ed8c825..53f39fc155ea 100644
> > > --- a/drivers/input/keyboard/tegra-kbc.c
> > > +++ b/drivers/input/keyboard/tegra-kbc.c
> > > @@ -491,12 +491,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > struct device_node *np = kbc->dev->of_node;
> > > u32 prop;
> > > int i;
> > > - u32 num_rows = 0;
> > > - u32 num_cols = 0;
> > > + int num_rows;
> > > + int num_cols;
> > > u32 cols_cfg[KBC_MAX_GPIO];
> > > u32 rows_cfg[KBC_MAX_GPIO];
> > > - int proplen;
> > > - int ret;
> > >
> > > if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
> > > kbc->debounce_cnt = prop;
> > > @@ -510,56 +508,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
> > > kbc->wakeup = true;
> > >
> > > - if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
> > > - dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
> > > - return -ENOENT;
> > > - }
> > > - num_rows = proplen / sizeof(u32);
> > > -
> > > - if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
> > > - dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
> > > - return -ENOENT;
> > > - }
> > > - num_cols = proplen / sizeof(u32);
> > > -
> > > - if (num_rows > kbc->hw_support->max_rows) {
> > > - dev_err(kbc->dev,
> > > - "Number of rows is more than supported by hardware\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - if (num_cols > kbc->hw_support->max_columns) {
> > > - dev_err(kbc->dev,
> > > - "Number of cols is more than supported by hardware\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - if (!of_get_property(np, "linux,keymap", &proplen)) {
> > > + if (!of_property_present(np, "linux,keymap")) {
> > > dev_err(kbc->dev, "property linux,keymap not found\n");
> > > return -ENOENT;
> > > }
> > >
> > > - if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > > - dev_err(kbc->dev,
> > > - "keypad rows/columns not properly specified\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > /* Set all pins as non-configured */
> > > for (i = 0; i < kbc->num_rows_and_columns; i++)
> > > kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
> > >
> > > - ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
> > > - rows_cfg, num_rows);
> > > - if (ret < 0) {
> > > + num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
> > > + rows_cfg, 1, KBC_MAX_GPIO);
> > > + if (num_rows < 0) {
> > > dev_err(kbc->dev, "Rows configurations are not proper\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
> > > - cols_cfg, num_cols);
> > > - if (ret < 0) {
> > > - dev_err(kbc->dev, "Cols configurations are not proper\n");
> > > + return num_rows;
> > > + } else if (num_rows > kbc->hw_support->max_rows) {
> > > + dev_err(kbc->dev,
> > > + "Number of rows is more than supported by hardware\n");
> > > return -EINVAL;
> > > }
> > >
> > > @@ -568,11 +533,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > kbc->pin_cfg[rows_cfg[i]].num = i;
> > > }
> > >
> > > + num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
> > > + cols_cfg, 1, KBC_MAX_GPIO);
> > > + if (num_cols < 0) {
> > > + dev_err(kbc->dev, "Cols configurations are not proper\n");
> > > + return num_cols;
> > > + } else if (num_cols > kbc->hw_support->max_columns) {
> > > + dev_err(kbc->dev,
> > > + "Number of cols is more than supported by hardware\n");
> > > + return -EINVAL;
> > > + }
> > > +
> > > for (i = 0; i < num_cols; i++) {
> > > kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
> > > kbc->pin_cfg[cols_cfg[i]].num = i;
> > > }
> > >
> > > + if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > > + dev_err(kbc->dev,
> > > + "keypad rows/columns not properly specified\n");
> > > + return -EINVAL;
> > > + }
> >
> > Previously we wouldn't try to initialize the columns when the
> > rows/columns were invalid, so this block could move before the last for
> > loop above.
> >
> > But it doesn't really matter given that these are exceptions and really
> > shouldn't happen, so:
> >
> > Acked-by: Thierry Reding <treding@nvidia.com>
>
> I don't quite like of_property_read_variable_u32_array() because it is
> OF-specific. device_property_count_u32() will return the number of
> elements in an array. But I guess this driver will only be used on an OF
> system...
>
> Applied, thank you.
Oh, wait, can I get your SOB please?
--
Dmitry
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-08-27 15:36 ` Dmitry Torokhov
2024-08-27 17:53 ` Dmitry Torokhov
@ 2024-08-28 15:05 ` Thierry Reding
1 sibling, 0 replies; 7+ messages in thread
From: Thierry Reding @ 2024-08-28 15:05 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Rob Herring (Arm), Laxman Dewangan, Jonathan Hunter, linux-input,
linux-tegra, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 5741 bytes --]
On Tue, Aug 27, 2024 at 08:36:29AM GMT, Dmitry Torokhov wrote:
> On Tue, Aug 27, 2024 at 04:23:48PM +0200, Thierry Reding wrote:
> > On Wed, Jul 31, 2024 at 02:14:01PM GMT, Rob Herring (Arm) wrote:
> > > There's no need to get the length of an DT array property before
> > > parsing the array. of_property_read_variable_u32_array() takes a
> > > minimum and maximum length and returns the actual length (or error
> > > code).
> > >
> > > This is part of a larger effort to remove callers of of_get_property()
> > > and similar functions. of_get_property() leaks the DT property data
> > > pointer which is a problem for dynamically allocated nodes which may
> > > be freed.
> > > ---
> > > drivers/input/keyboard/tegra-kbc.c | 72 +++++++++++-------------------
> > > 1 file changed, 27 insertions(+), 45 deletions(-)
> > >
> > > diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
> > > index a1765ed8c825..53f39fc155ea 100644
> > > --- a/drivers/input/keyboard/tegra-kbc.c
> > > +++ b/drivers/input/keyboard/tegra-kbc.c
> > > @@ -491,12 +491,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > struct device_node *np = kbc->dev->of_node;
> > > u32 prop;
> > > int i;
> > > - u32 num_rows = 0;
> > > - u32 num_cols = 0;
> > > + int num_rows;
> > > + int num_cols;
> > > u32 cols_cfg[KBC_MAX_GPIO];
> > > u32 rows_cfg[KBC_MAX_GPIO];
> > > - int proplen;
> > > - int ret;
> > >
> > > if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
> > > kbc->debounce_cnt = prop;
> > > @@ -510,56 +508,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
> > > kbc->wakeup = true;
> > >
> > > - if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
> > > - dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
> > > - return -ENOENT;
> > > - }
> > > - num_rows = proplen / sizeof(u32);
> > > -
> > > - if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
> > > - dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
> > > - return -ENOENT;
> > > - }
> > > - num_cols = proplen / sizeof(u32);
> > > -
> > > - if (num_rows > kbc->hw_support->max_rows) {
> > > - dev_err(kbc->dev,
> > > - "Number of rows is more than supported by hardware\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - if (num_cols > kbc->hw_support->max_columns) {
> > > - dev_err(kbc->dev,
> > > - "Number of cols is more than supported by hardware\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - if (!of_get_property(np, "linux,keymap", &proplen)) {
> > > + if (!of_property_present(np, "linux,keymap")) {
> > > dev_err(kbc->dev, "property linux,keymap not found\n");
> > > return -ENOENT;
> > > }
> > >
> > > - if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > > - dev_err(kbc->dev,
> > > - "keypad rows/columns not properly specified\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > /* Set all pins as non-configured */
> > > for (i = 0; i < kbc->num_rows_and_columns; i++)
> > > kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
> > >
> > > - ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
> > > - rows_cfg, num_rows);
> > > - if (ret < 0) {
> > > + num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
> > > + rows_cfg, 1, KBC_MAX_GPIO);
> > > + if (num_rows < 0) {
> > > dev_err(kbc->dev, "Rows configurations are not proper\n");
> > > - return -EINVAL;
> > > - }
> > > -
> > > - ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
> > > - cols_cfg, num_cols);
> > > - if (ret < 0) {
> > > - dev_err(kbc->dev, "Cols configurations are not proper\n");
> > > + return num_rows;
> > > + } else if (num_rows > kbc->hw_support->max_rows) {
> > > + dev_err(kbc->dev,
> > > + "Number of rows is more than supported by hardware\n");
> > > return -EINVAL;
> > > }
> > >
> > > @@ -568,11 +533,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
> > > kbc->pin_cfg[rows_cfg[i]].num = i;
> > > }
> > >
> > > + num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
> > > + cols_cfg, 1, KBC_MAX_GPIO);
> > > + if (num_cols < 0) {
> > > + dev_err(kbc->dev, "Cols configurations are not proper\n");
> > > + return num_cols;
> > > + } else if (num_cols > kbc->hw_support->max_columns) {
> > > + dev_err(kbc->dev,
> > > + "Number of cols is more than supported by hardware\n");
> > > + return -EINVAL;
> > > + }
> > > +
> > > for (i = 0; i < num_cols; i++) {
> > > kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
> > > kbc->pin_cfg[cols_cfg[i]].num = i;
> > > }
> > >
> > > + if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
> > > + dev_err(kbc->dev,
> > > + "keypad rows/columns not properly specified\n");
> > > + return -EINVAL;
> > > + }
> >
> > Previously we wouldn't try to initialize the columns when the
> > rows/columns were invalid, so this block could move before the last for
> > loop above.
> >
> > But it doesn't really matter given that these are exceptions and really
> > shouldn't happen, so:
> >
> > Acked-by: Thierry Reding <treding@nvidia.com>
>
> I don't quite like of_property_read_variable_u32_array() because it is
> OF-specific. device_property_count_u32() will return the number of
> elements in an array. But I guess this driver will only be used on an OF
> system...
Yeah, this hardware only exists on fairly old Tegra devices and I know
of no plans to support anything other that DT on those.
Thierry
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-08-27 17:53 ` Dmitry Torokhov
@ 2024-09-06 5:33 ` Dmitry Torokhov
2024-09-13 20:06 ` Rob Herring
0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Torokhov @ 2024-09-06 5:33 UTC (permalink / raw)
To: Rob Herring (Arm)
Cc: Laxman Dewangan, Jonathan Hunter, linux-input, linux-tegra,
linux-kernel, Thierry Reding
On Tue, Aug 27, 2024 at 10:53:38AM -0700, Dmitry Torokhov wrote:
> >
> > Applied, thank you.
>
> Oh, wait, can I get your SOB please?
Rob, I am still waiting on your SOB please.
--
Dmitry
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present()
2024-09-06 5:33 ` Dmitry Torokhov
@ 2024-09-13 20:06 ` Rob Herring
0 siblings, 0 replies; 7+ messages in thread
From: Rob Herring @ 2024-09-13 20:06 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Laxman Dewangan, Jonathan Hunter, linux-input, linux-tegra,
linux-kernel, Thierry Reding
On Fri, Sep 6, 2024 at 12:33 AM Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:
>
> On Tue, Aug 27, 2024 at 10:53:38AM -0700, Dmitry Torokhov wrote:
> > >
> > > Applied, thank you.
> >
> > Oh, wait, can I get your SOB please?
>
> Rob, I am still waiting on your SOB please.
Oh, missed this. That's weird it is missing. New patch coming.
Rob
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-09-13 20:06 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-31 20:14 [PATCH] input: tegra: Use of_property_read_variable_u32_array() and of_property_present() Rob Herring (Arm)
2024-08-27 14:23 ` Thierry Reding
2024-08-27 15:36 ` Dmitry Torokhov
2024-08-27 17:53 ` Dmitry Torokhov
2024-09-06 5:33 ` Dmitry Torokhov
2024-09-13 20:06 ` Rob Herring
2024-08-28 15:05 ` Thierry Reding
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).