Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <treding@nvidia.com>
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
Link: https://lore.kernel.org/r/20240913200827.546649-1-robh@kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Rob Herring (Arm) and committed by
Dmitry Torokhov
c7c878ff dcd18a3f

+27 -45
+27 -45
drivers/input/keyboard/tegra-kbc.c
··· 484 484 struct device_node *np = kbc->dev->of_node; 485 485 u32 prop; 486 486 int i; 487 - u32 num_rows = 0; 488 - u32 num_cols = 0; 487 + int num_rows; 488 + int num_cols; 489 489 u32 cols_cfg[KBC_MAX_GPIO]; 490 490 u32 rows_cfg[KBC_MAX_GPIO]; 491 - int proplen; 492 - int ret; 493 491 494 492 if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop)) 495 493 kbc->debounce_cnt = prop; ··· 501 503 of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */ 502 504 kbc->wakeup = true; 503 505 504 - if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) { 505 - dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n"); 506 - return -ENOENT; 507 - } 508 - num_rows = proplen / sizeof(u32); 509 - 510 - if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) { 511 - dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n"); 512 - return -ENOENT; 513 - } 514 - num_cols = proplen / sizeof(u32); 515 - 516 - if (num_rows > kbc->hw_support->max_rows) { 517 - dev_err(kbc->dev, 518 - "Number of rows is more than supported by hardware\n"); 519 - return -EINVAL; 520 - } 521 - 522 - if (num_cols > kbc->hw_support->max_columns) { 523 - dev_err(kbc->dev, 524 - "Number of cols is more than supported by hardware\n"); 525 - return -EINVAL; 526 - } 527 - 528 - if (!of_get_property(np, "linux,keymap", &proplen)) { 506 + if (!of_property_present(np, "linux,keymap")) { 529 507 dev_err(kbc->dev, "property linux,keymap not found\n"); 530 508 return -ENOENT; 531 - } 532 - 533 - if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) { 534 - dev_err(kbc->dev, 535 - "keypad rows/columns not properly specified\n"); 536 - return -EINVAL; 537 509 } 538 510 539 511 /* Set all pins as non-configured */ 540 512 for (i = 0; i < kbc->num_rows_and_columns; i++) 541 513 kbc->pin_cfg[i].type = PIN_CFG_IGNORE; 542 514 543 - ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins", 544 - rows_cfg, num_rows); 545 - if (ret < 0) { 515 + num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins", 516 + rows_cfg, 1, KBC_MAX_GPIO); 517 + if (num_rows < 0) { 546 518 dev_err(kbc->dev, "Rows configurations are not proper\n"); 547 - return -EINVAL; 548 - } 549 - 550 - ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins", 551 - cols_cfg, num_cols); 552 - if (ret < 0) { 553 - dev_err(kbc->dev, "Cols configurations are not proper\n"); 519 + return num_rows; 520 + } else if (num_rows > kbc->hw_support->max_rows) { 521 + dev_err(kbc->dev, 522 + "Number of rows is more than supported by hardware\n"); 554 523 return -EINVAL; 555 524 } 556 525 ··· 526 561 kbc->pin_cfg[rows_cfg[i]].num = i; 527 562 } 528 563 564 + num_cols = of_property_read_variable_u32_array(np, "nvidia,kbc-col-pins", 565 + cols_cfg, 1, KBC_MAX_GPIO); 566 + if (num_cols < 0) { 567 + dev_err(kbc->dev, "Cols configurations are not proper\n"); 568 + return num_cols; 569 + } else if (num_cols > kbc->hw_support->max_columns) { 570 + dev_err(kbc->dev, 571 + "Number of cols is more than supported by hardware\n"); 572 + return -EINVAL; 573 + } 574 + 529 575 for (i = 0; i < num_cols; i++) { 530 576 kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL; 531 577 kbc->pin_cfg[cols_cfg[i]].num = i; 578 + } 579 + 580 + if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) { 581 + dev_err(kbc->dev, 582 + "keypad rows/columns not properly specified\n"); 583 + return -EINVAL; 532 584 } 533 585 534 586 return 0;