Commit f7b6443f authored by Justin Stitt's avatar Justin Stitt Committed by Jonathan Cameron

iio: sx9324: avoid copying property strings

We're doing some needless string copies when trying to assign the proper
`prop` string. We can make `prop` a const char* and simply assign to
string literals.

For the case where a format string is used, let's extract the parsing
logic out into sx9324_parse_phase_prop(). We no longer need to create
copies or allocate new memory.

sx9324_parse_phase_prop() will simply return the default def value if it
fails.

This also cleans up some deprecated strncpy() uses [1].

Furthermore, let's clean up this code further by removing some unused
defines:
|  #define SX9324_PIN_DEF "semtech,ph0-pin"
|  #define SX9324_RESOLUTION_DEF "semtech,ph01-resolution"
|  #define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength"

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: default avatarJustin Stitt <justinstitt@google.com>
Reviewed-by: default avatarStephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20240201-strncpy-drivers-iio-proximity-sx9324-c-v5-1-78dde23553bc@google.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent c8f883a7
...@@ -873,6 +873,29 @@ static int sx9324_init_compensation(struct iio_dev *indio_dev) ...@@ -873,6 +873,29 @@ static int sx9324_init_compensation(struct iio_dev *indio_dev)
20000, 2000000); 20000, 2000000);
} }
static u8 sx9324_parse_phase_prop(struct device *dev,
struct sx_common_reg_default *reg_def,
const char *prop)
{
unsigned int pin_defs[SX9324_NUM_PINS];
int count, ret, pin;
u32 raw = 0;
count = device_property_count_u32(dev, prop);
if (count != ARRAY_SIZE(pin_defs))
return reg_def->def;
ret = device_property_read_u32_array(dev, prop, pin_defs,
ARRAY_SIZE(pin_defs));
if (ret)
return reg_def->def;
for (pin = 0; pin < SX9324_NUM_PINS; pin++)
raw |= (pin_defs[pin] << (2 * pin)) &
SX9324_REG_AFE_PH0_PIN_MASK(pin);
return raw;
}
static const struct sx_common_reg_default * static const struct sx_common_reg_default *
sx9324_get_default_reg(struct device *dev, int idx, sx9324_get_default_reg(struct device *dev, int idx,
struct sx_common_reg_default *reg_def) struct sx_common_reg_default *reg_def)
...@@ -881,37 +904,29 @@ sx9324_get_default_reg(struct device *dev, int idx, ...@@ -881,37 +904,29 @@ sx9324_get_default_reg(struct device *dev, int idx,
"highest" }; "highest" };
static const char * const sx9324_csidle[] = { "hi-z", "hi-z", "gnd", static const char * const sx9324_csidle[] = { "hi-z", "hi-z", "gnd",
"vdd" }; "vdd" };
#define SX9324_PIN_DEF "semtech,ph0-pin"
#define SX9324_RESOLUTION_DEF "semtech,ph01-resolution"
#define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength"
unsigned int pin_defs[SX9324_NUM_PINS];
char prop[] = SX9324_PROXRAW_DEF;
u32 start = 0, raw = 0, pos = 0; u32 start = 0, raw = 0, pos = 0;
int ret, count, ph, pin; const char *prop;
int ret;
memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def)); memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def));
sx_common_get_raw_register_config(dev, reg_def); sx_common_get_raw_register_config(dev, reg_def);
switch (reg_def->reg) { switch (reg_def->reg) {
case SX9324_REG_AFE_PH0: case SX9324_REG_AFE_PH0:
reg_def->def = sx9324_parse_phase_prop(dev, reg_def,
"semtech,ph0-pin");
break;
case SX9324_REG_AFE_PH1: case SX9324_REG_AFE_PH1:
reg_def->def = sx9324_parse_phase_prop(dev, reg_def,
"semtech,ph1-pin");
break;
case SX9324_REG_AFE_PH2: case SX9324_REG_AFE_PH2:
reg_def->def = sx9324_parse_phase_prop(dev, reg_def,
"semtech,ph2-pin");
break;
case SX9324_REG_AFE_PH3: case SX9324_REG_AFE_PH3:
ph = reg_def->reg - SX9324_REG_AFE_PH0; reg_def->def = sx9324_parse_phase_prop(dev, reg_def,
snprintf(prop, ARRAY_SIZE(prop), "semtech,ph%d-pin", ph); "semtech,ph3-pin");
count = device_property_count_u32(dev, prop);
if (count != ARRAY_SIZE(pin_defs))
break;
ret = device_property_read_u32_array(dev, prop, pin_defs,
ARRAY_SIZE(pin_defs));
if (ret)
break;
for (pin = 0; pin < SX9324_NUM_PINS; pin++)
raw |= (pin_defs[pin] << (2 * pin)) &
SX9324_REG_AFE_PH0_PIN_MASK(pin);
reg_def->def = raw;
break; break;
case SX9324_REG_AFE_CTRL0: case SX9324_REG_AFE_CTRL0:
ret = device_property_match_property_string(dev, "semtech,cs-idle-sleep", ret = device_property_match_property_string(dev, "semtech,cs-idle-sleep",
...@@ -933,11 +948,9 @@ sx9324_get_default_reg(struct device *dev, int idx, ...@@ -933,11 +948,9 @@ sx9324_get_default_reg(struct device *dev, int idx,
case SX9324_REG_AFE_CTRL4: case SX9324_REG_AFE_CTRL4:
case SX9324_REG_AFE_CTRL7: case SX9324_REG_AFE_CTRL7:
if (reg_def->reg == SX9324_REG_AFE_CTRL4) if (reg_def->reg == SX9324_REG_AFE_CTRL4)
strncpy(prop, "semtech,ph01-resolution", prop = "semtech,ph01-resolution";
ARRAY_SIZE(prop));
else else
strncpy(prop, "semtech,ph23-resolution", prop = "semtech,ph23-resolution";
ARRAY_SIZE(prop));
ret = device_property_read_u32(dev, prop, &raw); ret = device_property_read_u32(dev, prop, &raw);
if (ret) if (ret)
...@@ -1008,11 +1021,9 @@ sx9324_get_default_reg(struct device *dev, int idx, ...@@ -1008,11 +1021,9 @@ sx9324_get_default_reg(struct device *dev, int idx,
case SX9324_REG_PROX_CTRL0: case SX9324_REG_PROX_CTRL0:
case SX9324_REG_PROX_CTRL1: case SX9324_REG_PROX_CTRL1:
if (reg_def->reg == SX9324_REG_PROX_CTRL0) if (reg_def->reg == SX9324_REG_PROX_CTRL0)
strncpy(prop, "semtech,ph01-proxraw-strength", prop = "semtech,ph01-proxraw-strength";
ARRAY_SIZE(prop));
else else
strncpy(prop, "semtech,ph23-proxraw-strength", prop = "semtech,ph23-proxraw-strength";
ARRAY_SIZE(prop));
ret = device_property_read_u32(dev, prop, &raw); ret = device_property_read_u32(dev, prop, &raw);
if (ret) if (ret)
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment