Skip to content

Commit c7c878f

Browse files
robherringdtor
authored andcommitted
Input: tegra-kbc - use of_property_read_variable_u32_array() and of_property_present()
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. Acked-by: Thierry Reding <[email protected]> Signed-off-by: Rob Herring (Arm) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent dcd18a3 commit c7c878f

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed

drivers/input/keyboard/tegra-kbc.c

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
484484
struct device_node *np = kbc->dev->of_node;
485485
u32 prop;
486486
int i;
487-
u32 num_rows = 0;
488-
u32 num_cols = 0;
487+
int num_rows;
488+
int num_cols;
489489
u32 cols_cfg[KBC_MAX_GPIO];
490490
u32 rows_cfg[KBC_MAX_GPIO];
491-
int proplen;
492-
int ret;
493491

494492
if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
495493
kbc->debounce_cnt = prop;
@@ -503,56 +501,23 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
503501
of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
504502
kbc->wakeup = true;
505503

506-
if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
507-
dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
508-
return -ENOENT;
509-
}
510-
num_rows = proplen / sizeof(u32);
511-
512-
if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
513-
dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
514-
return -ENOENT;
515-
}
516-
num_cols = proplen / sizeof(u32);
517-
518-
if (num_rows > kbc->hw_support->max_rows) {
519-
dev_err(kbc->dev,
520-
"Number of rows is more than supported by hardware\n");
521-
return -EINVAL;
522-
}
523-
524-
if (num_cols > kbc->hw_support->max_columns) {
525-
dev_err(kbc->dev,
526-
"Number of cols is more than supported by hardware\n");
527-
return -EINVAL;
528-
}
529-
530-
if (!of_get_property(np, "linux,keymap", &proplen)) {
504+
if (!of_property_present(np, "linux,keymap")) {
531505
dev_err(kbc->dev, "property linux,keymap not found\n");
532506
return -ENOENT;
533507
}
534508

535-
if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
536-
dev_err(kbc->dev,
537-
"keypad rows/columns not properly specified\n");
538-
return -EINVAL;
539-
}
540-
541509
/* Set all pins as non-configured */
542510
for (i = 0; i < kbc->num_rows_and_columns; i++)
543511
kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
544512

545-
ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
546-
rows_cfg, num_rows);
547-
if (ret < 0) {
513+
num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
514+
rows_cfg, 1, KBC_MAX_GPIO);
515+
if (num_rows < 0) {
548516
dev_err(kbc->dev, "Rows configurations are not proper\n");
549-
return -EINVAL;
550-
}
551-
552-
ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
553-
cols_cfg, num_cols);
554-
if (ret < 0) {
555-
dev_err(kbc->dev, "Cols configurations are not proper\n");
517+
return num_rows;
518+
} else if (num_rows > kbc->hw_support->max_rows) {
519+
dev_err(kbc->dev,
520+
"Number of rows is more than supported by hardware\n");
556521
return -EINVAL;
557522
}
558523

@@ -561,11 +526,28 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
561526
kbc->pin_cfg[rows_cfg[i]].num = i;
562527
}
563528

529+
num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins",
530+
cols_cfg, 1, KBC_MAX_GPIO);
531+
if (num_cols < 0) {
532+
dev_err(kbc->dev, "Cols configurations are not proper\n");
533+
return num_cols;
534+
} else if (num_cols > kbc->hw_support->max_columns) {
535+
dev_err(kbc->dev,
536+
"Number of cols is more than supported by hardware\n");
537+
return -EINVAL;
538+
}
539+
564540
for (i = 0; i < num_cols; i++) {
565541
kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
566542
kbc->pin_cfg[cols_cfg[i]].num = i;
567543
}
568544

545+
if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
546+
dev_err(kbc->dev,
547+
"keypad rows/columns not properly specified\n");
548+
return -EINVAL;
549+
}
550+
569551
return 0;
570552
}
571553

0 commit comments

Comments
 (0)