public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH]: pbook apm_emu.c fix remaining time when charging
@ 2004-10-09 20:11 Soeren Sonnenburg
  2004-10-10  0:09 ` Benjamin Herrenschmidt
  0 siblings, 1 reply; 3+ messages in thread
From: Soeren Sonnenburg @ 2004-10-09 20:11 UTC (permalink / raw)
  To: Benjamin Herrenschmidt; +Cc: Linux Kernel

[-- Attachment #1: Type: text/plain, Size: 370 bytes --]

Hi,

this should make /proc/apm 

a) display the remaining time until the battery is fully charged and
b) when the system is on AC but the battery is not getting charged 0 is
displayed as remaining time.

Please comment/apply,
Soeren
-- 
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." Benjamin Franklin

[-- Attachment #2: apm_emu_fix.diff --]
[-- Type: text/x-patch, Size: 1881 bytes --]

--- t/linux-2.6.9-rc3/drivers/macintosh/apm_emu.c	2004-09-30 05:05:41.000000000 +0200
+++ linux-2.6.9-rc3-sonne/drivers/macintosh/apm_emu.c	2004-10-09 21:32:30.000000000 +0200
@@ -440,6 +440,7 @@
 	char *		p = buf;
 	char		charging       = 0;
 	long		charge	       = -1;
+	long		max_charge	   = -1;
 	long		amperage       = 0;
 	unsigned long	btype          = 0;
 
@@ -450,9 +451,12 @@
 				percentage = 0;
 			if (charge < 0)
 				charge = 0;
+			if (max_charge < 0)
+				max_charge = 0;
 			percentage += (pmu_batteries[i].charge * 100) /
 				pmu_batteries[i].max_charge;
 			charge += pmu_batteries[i].charge;
+			max_charge += pmu_batteries[i].max_charge;
 			amperage += pmu_batteries[i].amperage;
 			if (btype == 0)
 				btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
@@ -461,13 +465,27 @@
 				charging++;
 		}
 	}
+
 	if (real_count) {
 		if (amperage < 0) {
+			/* when less than 100mA are used the machine must be on AC and as it is 
+			   not charging the battery is only slightly self decharging and thus full be definition */
+			if (amperage < 100) {
+				if (btype == PMU_BATT_TYPE_SMART)
+					time_units = (charge * 59) / (amperage * -1);
+				else
+					time_units = (charge * 16440) / (amperage * -60);
+			}
+			else
+				time_units = 0;
+		}
+		else if (amperage >0 && max_charge>=charge) {
 			if (btype == PMU_BATT_TYPE_SMART)
-				time_units = (charge * 59) / (amperage * -1);
+				time_units = ( (max_charge - charge) * 59) / amperage;
 			else
-				time_units = (charge * 16440) / (amperage * -60);
+				time_units = ( (max_charge - charge) * 16440) / amperage;
 		}
+
 		percentage /= real_count;
 		if (charging > 0) {
 			battery_status = 0x03;
@@ -483,6 +501,7 @@
 			battery_flag = 0x01;
 		}
 	}
+
 	p += sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
 		     driver_version,
 		     (FAKE_APM_BIOS_VERSION >> 8) & 0xff,

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

* Re: [PATCH]: pbook apm_emu.c fix remaining time when charging
  2004-10-09 20:11 [PATCH]: pbook apm_emu.c fix remaining time when charging Soeren Sonnenburg
@ 2004-10-10  0:09 ` Benjamin Herrenschmidt
  2004-10-10  7:16   ` Soeren Sonnenburg
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Herrenschmidt @ 2004-10-10  0:09 UTC (permalink / raw)
  To: Soeren Sonnenburg; +Cc: Linux Kernel list

On Sun, 2004-10-10 at 06:11, Soeren Sonnenburg wrote:
> Hi,
> 
> this should make /proc/apm 
> 
> a) display the remaining time until the battery is fully charged and
> b) when the system is on AC but the battery is not getting charged 0 is
> displayed as remaining time.
> 
> Please comment/apply,
> Soeren

                if (amperage < 0) {
+                       /* when less than 100mA are used the machine must be on AC and as it is 
+                          not charging the battery is only slightly self decharging and thus full be definition */
+                       if (amperage < 100) {

There must be something wrong in the above...

+                                       time_units = (charge * 59) / (amperage * -1);
+                               else
+                                       time_units = (charge * 16440) / (amperage * -60);

Can you make sure also that amperage is never 0 ?

Ben.



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

* Re: [PATCH]: pbook apm_emu.c fix remaining time when charging
  2004-10-10  0:09 ` Benjamin Herrenschmidt
@ 2004-10-10  7:16   ` Soeren Sonnenburg
  0 siblings, 0 replies; 3+ messages in thread
From: Soeren Sonnenburg @ 2004-10-10  7:16 UTC (permalink / raw)
  To: Benjamin Herrenschmidt; +Cc: Linux Kernel

[-- Attachment #1: Type: text/plain, Size: 1939 bytes --]

On Sun, 2004-10-10 at 10:09 +1000, Benjamin Herrenschmidt wrote: 
> On Sun, 2004-10-10 at 06:11, Soeren Sonnenburg wrote:
[...] 
>                 if (amperage < 0) {
> +                       /* when less than 100mA are used the machine must be on AC and as it is 
> +                          not charging the battery is only slightly self decharging and thus full be definition */
> +                       if (amperage < 100) {
> 
> There must be something wrong in the above...

Yes you are right, I check for amperage < 0 first and then for 
amperage < 100. It should of course be amperage < -100 and is meant for
the case where one is on AC, battery was fully charged and which is now
slightly decharging by less than 100mA/min leading to a remaining time
being displayed of more than 700 hours left.

/proc/apm output with that patch (but wrong check amperage < 100):
0.5 1.1 0x00 0x01 0x00 0x01 99% 92276 min

/proc/apm output with new patch:
0.5 1.1 0x00 0x01 0x00 0x01 99% 0 min

This is the /proc/pmu/battery_0 equivalent.

flags      : 00000011
charge     : 3130
max_charge : 3148
current    : -2
voltage    : 16629
time rem.  : 5634000

Well it is a special case to be dealt with... one could aswell change
the flags from 'high battery' status to 'charging' or return -1 as the
remaining time... However the battery is fully charged but as the system
is on AC it is simply idling messing up the remaining time.

> +                                       time_units = (charge * 59) / (amperage * -1);
> +                               else
> +                                       time_units = (charge * 16440) / (amperage * -60);
> 
> Can you make sure also that amperage is never 0 ?

actually that is dealt with already as there is a check for both
amperage < 0 and for amperage > 0.

Soeren.
-- 
Sometimes, there's a moment as you're waking, when you become aware of
the real world around you, but you're still dreaming.

[-- Attachment #2: apm_emu_fix.diff --]
[-- Type: text/x-patch, Size: 1885 bytes --]

--- t/linux-2.6.9-rc3/drivers/macintosh/apm_emu.c	2004-09-30 05:05:41.000000000 +0200
+++ linux-2.6.9-rc3-sonne/drivers/macintosh/apm_emu.c	2004-10-10 08:46:33.000000000 +0200
@@ -440,6 +440,7 @@
 	char *		p = buf;
 	char		charging       = 0;
 	long		charge	       = -1;
+	long		max_charge	   = -1;
 	long		amperage       = 0;
 	unsigned long	btype          = 0;
 
@@ -450,9 +451,12 @@
 				percentage = 0;
 			if (charge < 0)
 				charge = 0;
+			if (max_charge < 0)
+				max_charge = 0;
 			percentage += (pmu_batteries[i].charge * 100) /
 				pmu_batteries[i].max_charge;
 			charge += pmu_batteries[i].charge;
+			max_charge += pmu_batteries[i].max_charge;
 			amperage += pmu_batteries[i].amperage;
 			if (btype == 0)
 				btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
@@ -461,13 +465,27 @@
 				charging++;
 		}
 	}
+
 	if (real_count) {
 		if (amperage < 0) {
+			/* when less than 100mA are used the machine must be on AC and as it is 
+			   not charging the battery is only slightly self decharging and thus full be definition */
+			if (amperage < -100) {
+				if (btype == PMU_BATT_TYPE_SMART)
+					time_units = (charge * 59) / (amperage * -1);
+				else
+					time_units = (charge * 16440) / (amperage * -60);
+			}
+			else
+				time_units = 0;
+		}
+		else if (amperage > 0 && max_charge >= charge) {
 			if (btype == PMU_BATT_TYPE_SMART)
-				time_units = (charge * 59) / (amperage * -1);
+				time_units = ( (max_charge - charge) * 59) / amperage;
 			else
-				time_units = (charge * 16440) / (amperage * -60);
+				time_units = ( (max_charge - charge) * 16440) / amperage;
 		}
+
 		percentage /= real_count;
 		if (charging > 0) {
 			battery_status = 0x03;
@@ -483,6 +501,7 @@
 			battery_flag = 0x01;
 		}
 	}
+
 	p += sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
 		     driver_version,
 		     (FAKE_APM_BIOS_VERSION >> 8) & 0xff,

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

end of thread, other threads:[~2004-10-10 14:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-09 20:11 [PATCH]: pbook apm_emu.c fix remaining time when charging Soeren Sonnenburg
2004-10-10  0:09 ` Benjamin Herrenschmidt
2004-10-10  7:16   ` Soeren Sonnenburg

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