diff mbox

power: supply: sysfs: Use enum to specify property

Message ID 20180321155453.GB7528@lenoch (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Ladislav Michl March 21, 2018, 3:54 p.m. UTC
Power supply property is in fact enum, so reflect it in code.
Also use switch statement in show property function as is done
for storing property.

Signed-off-by: Ladislav Michl <ladis@linux-mips.org>
---
 drivers/power/supply/power_supply_sysfs.c | 77 ++++++++++++++++++-------------
 1 file changed, 44 insertions(+), 33 deletions(-)

Comments

Sebastian Reichel April 25, 2018, 10:01 p.m. UTC | #1
Hi,

On Wed, Mar 21, 2018 at 04:54:53PM +0100, Ladislav Michl wrote:
> Power supply property is in fact enum, so reflect it in code.
> Also use switch statement in show property function as is done
> for storing property.
> 
> Signed-off-by: Ladislav Michl <ladis@linux-mips.org>
> ---

Thanks, queued.

-- Sebastian

>  drivers/power/supply/power_supply_sysfs.c | 77 ++++++++++++++++++-------------
>  1 file changed, 44 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
> index 5204f115970f..ca2f869165ed 100644
> --- a/drivers/power/supply/power_supply_sysfs.c
> +++ b/drivers/power/supply/power_supply_sysfs.c
> @@ -76,15 +76,15 @@ static const char * const power_supply_scope_text[] = {
>  static ssize_t power_supply_show_property(struct device *dev,
>  					  struct device_attribute *attr,
>  					  char *buf) {
> -	ssize_t ret = 0;
> +	ssize_t ret;
>  	struct power_supply *psy = dev_get_drvdata(dev);
> -	const ptrdiff_t off = attr - power_supply_attrs;
> +	enum power_supply_property psp = attr - power_supply_attrs;
>  	union power_supply_propval value;
>  
> -	if (off == POWER_SUPPLY_PROP_TYPE) {
> +	if (psp == POWER_SUPPLY_PROP_TYPE) {
>  		value.intval = psy->desc->type;
>  	} else {
> -		ret = power_supply_get_property(psy, off, &value);
> +		ret = power_supply_get_property(psy, psp, &value);
>  
>  		if (ret < 0) {
>  			if (ret == -ENODATA)
> @@ -97,31 +97,43 @@ static ssize_t power_supply_show_property(struct device *dev,
>  		}
>  	}
>  
> -	if (off == POWER_SUPPLY_PROP_STATUS)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_status_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_charge_type_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_HEALTH)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_health_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_technology_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_capacity_level_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_TYPE)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_type_text[value.intval]);
> -	else if (off == POWER_SUPPLY_PROP_SCOPE)
> -		return sprintf(buf, "%s\n",
> -			       power_supply_scope_text[value.intval]);
> -	else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
> -		return sprintf(buf, "%s\n", value.strval);
> -
> -	return sprintf(buf, "%d\n", value.intval);
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_STATUS:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_status_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_CHARGE_TYPE:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_charge_type_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_HEALTH:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_health_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_TECHNOLOGY:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_technology_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_capacity_level_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_TYPE:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_type_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_SCOPE:
> +		ret = sprintf(buf, "%s\n",
> +			      power_supply_scope_text[value.intval]);
> +		break;
> +	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
> +		ret = sprintf(buf, "%s\n", value.strval);
> +		break;
> +	default:
> +		ret = sprintf(buf, "%d\n", value.intval);
> +	}
> +
> +	return ret;
>  }
>  
>  static ssize_t power_supply_store_property(struct device *dev,
> @@ -129,11 +141,10 @@ static ssize_t power_supply_store_property(struct device *dev,
>  					   const char *buf, size_t count) {
>  	ssize_t ret;
>  	struct power_supply *psy = dev_get_drvdata(dev);
> -	const ptrdiff_t off = attr - power_supply_attrs;
> +	enum power_supply_property psp = attr - power_supply_attrs;
>  	union power_supply_propval value;
>  
> -	/* maybe it is a enum property? */
> -	switch (off) {
> +	switch (psp) {
>  	case POWER_SUPPLY_PROP_STATUS:
>  		ret = sysfs_match_string(power_supply_status_text, buf);
>  		break;
> @@ -172,7 +183,7 @@ static ssize_t power_supply_store_property(struct device *dev,
>  
>  	value.intval = ret;
>  
> -	ret = power_supply_set_property(psy, off, &value);
> +	ret = power_supply_set_property(psy, psp, &value);
>  	if (ret < 0)
>  		return ret;
>  
> -- 
> 2.16.2
>
diff mbox

Patch

diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
index 5204f115970f..ca2f869165ed 100644
--- a/drivers/power/supply/power_supply_sysfs.c
+++ b/drivers/power/supply/power_supply_sysfs.c
@@ -76,15 +76,15 @@  static const char * const power_supply_scope_text[] = {
 static ssize_t power_supply_show_property(struct device *dev,
 					  struct device_attribute *attr,
 					  char *buf) {
-	ssize_t ret = 0;
+	ssize_t ret;
 	struct power_supply *psy = dev_get_drvdata(dev);
-	const ptrdiff_t off = attr - power_supply_attrs;
+	enum power_supply_property psp = attr - power_supply_attrs;
 	union power_supply_propval value;
 
-	if (off == POWER_SUPPLY_PROP_TYPE) {
+	if (psp == POWER_SUPPLY_PROP_TYPE) {
 		value.intval = psy->desc->type;
 	} else {
-		ret = power_supply_get_property(psy, off, &value);
+		ret = power_supply_get_property(psy, psp, &value);
 
 		if (ret < 0) {
 			if (ret == -ENODATA)
@@ -97,31 +97,43 @@  static ssize_t power_supply_show_property(struct device *dev,
 		}
 	}
 
-	if (off == POWER_SUPPLY_PROP_STATUS)
-		return sprintf(buf, "%s\n",
-			       power_supply_status_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
-		return sprintf(buf, "%s\n",
-			       power_supply_charge_type_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_HEALTH)
-		return sprintf(buf, "%s\n",
-			       power_supply_health_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
-		return sprintf(buf, "%s\n",
-			       power_supply_technology_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
-		return sprintf(buf, "%s\n",
-			       power_supply_capacity_level_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_TYPE)
-		return sprintf(buf, "%s\n",
-			       power_supply_type_text[value.intval]);
-	else if (off == POWER_SUPPLY_PROP_SCOPE)
-		return sprintf(buf, "%s\n",
-			       power_supply_scope_text[value.intval]);
-	else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
-		return sprintf(buf, "%s\n", value.strval);
-
-	return sprintf(buf, "%d\n", value.intval);
+	switch (psp) {
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_status_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_charge_type_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_HEALTH:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_health_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_TECHNOLOGY:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_technology_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_capacity_level_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_TYPE:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_type_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		ret = sprintf(buf, "%s\n",
+			      power_supply_scope_text[value.intval]);
+		break;
+	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
+		ret = sprintf(buf, "%s\n", value.strval);
+		break;
+	default:
+		ret = sprintf(buf, "%d\n", value.intval);
+	}
+
+	return ret;
 }
 
 static ssize_t power_supply_store_property(struct device *dev,
@@ -129,11 +141,10 @@  static ssize_t power_supply_store_property(struct device *dev,
 					   const char *buf, size_t count) {
 	ssize_t ret;
 	struct power_supply *psy = dev_get_drvdata(dev);
-	const ptrdiff_t off = attr - power_supply_attrs;
+	enum power_supply_property psp = attr - power_supply_attrs;
 	union power_supply_propval value;
 
-	/* maybe it is a enum property? */
-	switch (off) {
+	switch (psp) {
 	case POWER_SUPPLY_PROP_STATUS:
 		ret = sysfs_match_string(power_supply_status_text, buf);
 		break;
@@ -172,7 +183,7 @@  static ssize_t power_supply_store_property(struct device *dev,
 
 	value.intval = ret;
 
-	ret = power_supply_set_property(psy, off, &value);
+	ret = power_supply_set_property(psy, psp, &value);
 	if (ret < 0)
 		return ret;