mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] intel_menlow: reduce code duplication
@ 2016-03-26 21:40 Rasmus Villemoes
  2016-03-26 23:53 ` Joe Perches
  2016-05-23 21:36 ` Darren Hart
  0 siblings, 2 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2016-03-26 21:40 UTC (permalink / raw)
  To: Sujith Thomas, Darren Hart
  Cc: Rasmus Villemoes, platform-driver-x86, linux-kernel

aux0_show and aux1_show consists of almost identical code. Pull that
into a common helper and make them thin wrappers. Similarly for
_store.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/platform/x86/intel_menlow.c | 48 ++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 27 deletions(-)

diff --git a/drivers/platform/x86/intel_menlow.c b/drivers/platform/x86/intel_menlow.c
index 0a919d81662c..ae3a2a831533 100644
--- a/drivers/platform/x86/intel_menlow.c
+++ b/drivers/platform/x86/intel_menlow.c
@@ -306,33 +306,32 @@ static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
 #define to_intel_menlow_attr(_attr)	\
 	container_of(_attr, struct intel_menlow_attribute, attr)
 
-static ssize_t aux0_show(struct device *dev,
-			 struct device_attribute *dev_attr, char *buf)
+static ssize_t aux_show(struct device *dev, struct device_attribute *dev_attr,
+			char *buf, int idx)
 {
 	struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
 	unsigned long long value;
 	int result;
 
-	result = sensor_get_auxtrip(attr->handle, 0, &value);
+	result = sensor_get_auxtrip(attr->handle, idx, &value);
 
 	return result ? result : sprintf(buf, "%lu", DECI_KELVIN_TO_CELSIUS(value));
 }
 
-static ssize_t aux1_show(struct device *dev,
+static ssize_t aux0_show(struct device *dev,
 			 struct device_attribute *dev_attr, char *buf)
 {
-	struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
-	unsigned long long value;
-	int result;
-
-	result = sensor_get_auxtrip(attr->handle, 1, &value);
+	return aux_show(dev, dev_attr, buf, 0);
+}
 
-	return result ? result : sprintf(buf, "%lu", DECI_KELVIN_TO_CELSIUS(value));
+static ssize_t aux1_show(struct device *dev,
+			 struct device_attribute *dev_attr, char *buf)
+{
+	return aux_show(dev, dev_attr, buf, 1);
 }
 
-static ssize_t aux0_store(struct device *dev,
-			  struct device_attribute *dev_attr,
-			  const char *buf, size_t count)
+static ssize_t aux_store(struct device *dev, struct device_attribute *dev_attr,
+			 const char *buf, size_t count, int idx)
 {
 	struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
 	int value;
@@ -345,27 +344,22 @@ static ssize_t aux0_store(struct device *dev,
 	if (value < 0)
 		return -EINVAL;
 
-	result = sensor_set_auxtrip(attr->handle, 0, CELSIUS_TO_DECI_KELVIN(value));
+	result = sensor_set_auxtrip(attr->handle, idx, CELSIUS_TO_DECI_KELVIN(value));
 	return result ? result : count;
 }
 
-static ssize_t aux1_store(struct device *dev,
+static ssize_t aux0_store(struct device *dev,
 			  struct device_attribute *dev_attr,
 			  const char *buf, size_t count)
 {
-	struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
-	int value;
-	int result;
-
-	/*Sanity check; should be a positive integer */
-	if (!sscanf(buf, "%d", &value))
-		return -EINVAL;
-
-	if (value < 0)
-		return -EINVAL;
+	return aux_store(dev, dev_attr, buf, count, 0);
+}
 
-	result = sensor_set_auxtrip(attr->handle, 1, CELSIUS_TO_DECI_KELVIN(value));
-	return result ? result : count;
+static ssize_t aux1_store(struct device *dev,
+			  struct device_attribute *dev_attr,
+			  const char *buf, size_t count)
+{
+	return aux_store(dev, dev_attr, buf, count, 1);
 }
 
 /* BIOS can enable/disable the thermal user application in dabney platform */
-- 
2.1.4

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] intel_menlow: reduce code duplication
  2016-03-26 21:40 [PATCH] intel_menlow: reduce code duplication Rasmus Villemoes
@ 2016-03-26 23:53 ` Joe Perches
  2016-05-23 21:36 ` Darren Hart
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Perches @ 2016-03-26 23:53 UTC (permalink / raw)
  To: Rasmus Villemoes, Sujith Thomas, Darren Hart
  Cc: platform-driver-x86, linux-kernel

On Sat, 2016-03-26 at 22:40 +0100, Rasmus Villemoes wrote:
> aux0_show and aux1_show consists of almost identical code. Pull that
> into a common helper and make them thin wrappers. Similarly for
> _store.

Seems sensible, thanks

> diff --git a/drivers/platform/x86/intel_menlow.c b/drivers/platform/x86/intel_menlow.c
[]
> @@ -306,33 +306,32 @@ static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
>  #define to_intel_menlow_attr(_attr)	\
>  	container_of(_attr, struct intel_menlow_attribute, attr)
>  
> -static ssize_t aux0_show(struct device *dev,
> -			 struct device_attribute *dev_attr, char *buf)
> +static ssize_t aux_show(struct device *dev, struct device_attribute *dev_attr,
> +			char *buf, int idx)
>  {
>  	struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
>  	unsigned long long value;
>  	int result;
>  
> -	result = sensor_get_auxtrip(attr->handle, 0, &value);
> +	result = sensor_get_auxtrip(attr->handle, idx, &value);
>  
>  	return result ? result : sprintf(buf, "%lu", DECI_KELVIN_TO_CELSIUS(value));

While not something you've done differently than the
existing code, perhaps this would be more common
without the ternary operator like:

	result = sensor_get_auxtrip(attr->handle, idx, &value);
	if (result < 0)
		return result;

	return sprintf(buf, "%lu", DECI_KELVIN_TO_CELSIUS(value));

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] intel_menlow: reduce code duplication
  2016-03-26 21:40 [PATCH] intel_menlow: reduce code duplication Rasmus Villemoes
  2016-03-26 23:53 ` Joe Perches
@ 2016-05-23 21:36 ` Darren Hart
  1 sibling, 0 replies; 3+ messages in thread
From: Darren Hart @ 2016-05-23 21:36 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Sujith Thomas, platform-driver-x86, linux-kernel

On Sat, Mar 26, 2016 at 10:40:26PM +0100, Rasmus Villemoes wrote:
> aux0_show and aux1_show consists of almost identical code. Pull that
> into a common helper and make them thin wrappers. Similarly for
> _store.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

This one fell off my queue, apologies. Now queued.
-- 
Darren Hart
Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-05-23 21:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-26 21:40 [PATCH] intel_menlow: reduce code duplication Rasmus Villemoes
2016-03-26 23:53 ` Joe Perches
2016-05-23 21:36 ` Darren Hart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®