Message ID | ae82abf9da86542f5657a8c37106bcdae5011927.1585929579.git.mirq-linux@rere.qmqm.pl (mailing list archive) |
---|---|
State | Not Applicable, archived |
Headers | show |
Series | power: supply: core: extensions and fixes | expand |
On 4/3/20 9:23 AM, Michał Mirosław wrote: > Rework power_supply_hwmon_read_string() to check it's parameters. > This allows to extend it later with labels for other types of > measurements. > > Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl> > --- > v2: split from fix temperature labels > --- > drivers/power/supply/power_supply_hwmon.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c > index 67b6ee60085e..48c73994732c 100644 > --- a/drivers/power/supply/power_supply_hwmon.c > +++ b/drivers/power/supply/power_supply_hwmon.c > @@ -43,6 +43,11 @@ static int power_supply_hwmon_curr_to_property(u32 attr) > } > } > > +static const char *const ps_temp_label[] = { > + "temp", > + "ambient temp", > +}; > + > static int power_supply_hwmon_temp_to_property(u32 attr, int channel) > { > if (channel) { > @@ -144,8 +149,20 @@ static int power_supply_hwmon_read_string(struct device *dev, > u32 attr, int channel, > const char **str) > { > - *str = channel ? "temp ambient" : "temp"; > - return 0; > + if (channel < 0) > + return -EINVAL; > + This is unnecessary. > + switch (type) { > + case hwmon_temp: > + if (channel >= ARRAY_SIZE(ps_temp_label)) > + return -EINVAL; As is this. We don't usually check boundaries like this for in-kernel APIs, and I personally would not want to have it introduced in the kernel more than necessary. This just increases kernel image size with zero benefit. Guenter > + *str = ps_temp_label[channel]; > + return 0; > + default: > + break; > + } > + > + return -EINVAL; > } > > static int >
On Fri, Apr 03, 2020 at 09:47:40AM -0700, Guenter Roeck wrote: > On 4/3/20 9:23 AM, Michał Mirosław wrote: > > Rework power_supply_hwmon_read_string() to check it's parameters. > > This allows to extend it later with labels for other types of > > measurements. > > > > Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl> > > --- > > v2: split from fix temperature labels > > --- > > drivers/power/supply/power_supply_hwmon.c | 21 +++++++++++++++++++-- > > 1 file changed, 19 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c > > index 67b6ee60085e..48c73994732c 100644 > > --- a/drivers/power/supply/power_supply_hwmon.c > > +++ b/drivers/power/supply/power_supply_hwmon.c > > @@ -43,6 +43,11 @@ static int power_supply_hwmon_curr_to_property(u32 attr) > > } > > } > > > > +static const char *const ps_temp_label[] = { > > + "temp", > > + "ambient temp", > > +}; > > + > > static int power_supply_hwmon_temp_to_property(u32 attr, int channel) > > { > > if (channel) { > > @@ -144,8 +149,20 @@ static int power_supply_hwmon_read_string(struct device *dev, > > u32 attr, int channel, > > const char **str) > > { > > - *str = channel ? "temp ambient" : "temp"; > > - return 0; > > + if (channel < 0) > > + return -EINVAL; > > + > > This is unnecessary. > > > + switch (type) { > > + case hwmon_temp: > > + if (channel >= ARRAY_SIZE(ps_temp_label)) > > + return -EINVAL; > > As is this. We don't usually check boundaries like this for > in-kernel APIs, and I personally would not want to have it > introduced in the kernel more than necessary. This just increases > kernel image size with zero benefit. I'll amend other patches in the same spirit, then. I like the code size argument. Best Regards, Michał Mirosław
diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c index 67b6ee60085e..48c73994732c 100644 --- a/drivers/power/supply/power_supply_hwmon.c +++ b/drivers/power/supply/power_supply_hwmon.c @@ -43,6 +43,11 @@ static int power_supply_hwmon_curr_to_property(u32 attr) } } +static const char *const ps_temp_label[] = { + "temp", + "ambient temp", +}; + static int power_supply_hwmon_temp_to_property(u32 attr, int channel) { if (channel) { @@ -144,8 +149,20 @@ static int power_supply_hwmon_read_string(struct device *dev, u32 attr, int channel, const char **str) { - *str = channel ? "temp ambient" : "temp"; - return 0; + if (channel < 0) + return -EINVAL; + + switch (type) { + case hwmon_temp: + if (channel >= ARRAY_SIZE(ps_temp_label)) + return -EINVAL; + *str = ps_temp_label[channel]; + return 0; + default: + break; + } + + return -EINVAL; } static int
Rework power_supply_hwmon_read_string() to check it's parameters. This allows to extend it later with labels for other types of measurements. Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl> --- v2: split from fix temperature labels --- drivers/power/supply/power_supply_hwmon.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)