Skip to contentSkip to conversation

APM emulation driver for class batteries

RFC PATCH In review Anton Vorontsov 4 participants 7 messages Apr 11, 2007 – Apr 20, 2007

lore
Share & tools
lore
#0 lore raw

RFC PATCH 7/7 APM emulation driver for class batteries

It finds battery with "main_battery" flag set (or with max_capacity if no batteries marked as main), and converts battery values to APM form.

---
 drivers/battery/Kconfig     |    7 +++
 drivers/battery/Makefile    |    1 +
 drivers/battery/apm_power.c |  121 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+), 0 deletions(-)
 create mode 100644 drivers/battery/apm_power.c
drivers/battery/Kconfig +7 -0
index 0c14ae0..bbf8283 100644
@@ -15,4 +15,11 @@ config BATTERY_DS2760
 	help
 	  Say Y here to enable support for batteries with ds2760 chip.
 
+config APM_POWER
+	tristate "APM emulation"
+	depends on BATTERY && APM
+	help
+	  Say Y here to enable support APM status emulation using
+	  battery class devices.
+
 endmenu
drivers/battery/Makefile +1 -0
index 9902513..cea5807 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_BATTERY)              += battery.o
 obj-$(CONFIG_BATTERY_DS2760)       += ds2760_battery.o
+obj-$(CONFIG_APM_POWER)            += apm_power.o
drivers/battery/apm_power.c new file +121 -0

121 lines not on this page · raw message

Signature · 1 line
1.5.0.5-dirty
#1 lore raw
Quoted 2 lines · Anton Vorontsov

It finds battery with "main_battery" flag set (or with max_capacity if no batteries marked as main), and converts battery values to APM form.

Changes:

- follows battery class changes

- minor cleanups

Subject: [PATCH] [take2] APM emulation driver for class batteries

---
 drivers/battery/Kconfig     |    7 +++
 drivers/battery/Makefile    |    1 +
 drivers/battery/apm_power.c |  122 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 0 deletions(-)
 create mode 100644 drivers/battery/apm_power.c
drivers/battery/Kconfig +7 -0
index 0c14ae0..bbf8283 100644
@@ -15,4 +15,11 @@ config BATTERY_DS2760
 	help
 	  Say Y here to enable support for batteries with ds2760 chip.
 
+config APM_POWER
+	tristate "APM emulation"
+	depends on BATTERY && APM
+	help
+	  Say Y here to enable support APM status emulation using
+	  battery class devices.
+
 endmenu
drivers/battery/Makefile +1 -0
index 9902513..cea5807 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_BATTERY)              += battery.o
 obj-$(CONFIG_BATTERY_DS2760)       += ds2760_battery.o
+obj-$(CONFIG_APM_POWER)            += apm_power.o
drivers/battery/apm_power.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
1.5.0.5-dirty
#2 lore raw
Quoted 6 lines · Anton Vorontsov
drivers/battery/apm_power.c new file +6 -0
+static void (*old_apm_get_power_status)(struct apm_power_info*);
+
+static int __init apm_battery_init(void)
+{
+	printk(KERN_INFO "APM Battery Driver\n");
+

drivers/battery/apm_power.c line 115

Quoted 10 lines
drivers/battery/apm_power.c new file +10 -0
+	old_apm_get_power_status = apm_get_power_status;
+	apm_get_power_status = apm_battery_apm_get_power_status;
+	return 0;
+}
+
+static void __exit apm_battery_exit(void)
+{
+	apm_get_power_status = old_apm_get_power_status;
+	return;
+}

Utterly unsafe. What happens if some other module gets loaded which does this, and then this module is unloaded followed by the other module. Result: Oops.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
#3 lore raw
Quoted 13 lines · Russell King
Quoted 12 lines · Anton Vorontsov
drivers/battery/apm_power.c new file +12 -0
+static void (*old_apm_get_power_status)(struct apm_power_info*);
+
+static int __init apm_battery_init(void)
+{
+	printk(KERN_INFO "APM Battery Driver\n");
+
+	old_apm_get_power_status = apm_get_power_status;
+	apm_get_power_status = apm_battery_apm_get_power_status;
+	return 0;
+}
+
+static void __exit apm_battery_exit(void)
Quoted 8 lines

drivers/battery/apm_power.c line 115

Quoted 4 lines
drivers/battery/apm_power.c new file +4 -0
+{
+	apm_get_power_status = old_apm_get_power_status;
+	return;
+}

Utterly unsafe. What happens if some other module gets loaded which does this, and then this module is unloaded followed by the other module. Result: Oops.

Right. And loading two modules which changing apm_get_power_status is a race already. Thus, APM interface needs a mutex.

Or pda_power should be marked "bool" in Kconfig, as it is done in arch/arm/common/sharpsl_pm.c. Sharpsl_pm is safe only because it can't be a module.

Personally I'd keep things as is for now (i.e. I'd want tristate for PDA_POWER, not bool). Later APM API can be fixed.

Quoted 11 lines
Signature · 8 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Thanks,

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2
#4 lore raw
Quoted 8 lines · Anton Vorontsov
Quoted 3 lines · Russell King

Utterly unsafe. What happens if some other module gets loaded which does this, and then this module is unloaded followed by the other module. Result: Oops.

Right. And loading two modules which changing apm_get_power_status is a race already. Thus, APM interface needs a mutex.

Quoted 6 lines

Or pda_power should be marked "bool" in Kconfig, as it is done in arch/arm/common/sharpsl_pm.c. Sharpsl_pm is safe only because it can't be a module.

Personally I'd keep things as is for now (i.e. I'd want tristate for PDA_POWER, not bool). Later APM API can be fixed.

Experience shows "Later" more often than not means "never", inspite of what is said at the time the word is used...

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
#5 lore raw
In reply to Anton Vorontsov

Hi!

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/battery/apm_power.c new file +12 -0
+static void (*old_apm_get_power_status)(struct apm_power_info*);
+
+static int __init apm_battery_init(void)
+{
+	printk(KERN_INFO "APM Battery Driver\n");
+
+	old_apm_get_power_status = apm_get_power_status;
+	apm_get_power_status = apm_battery_apm_get_power_status;
+	return 0;
+}
+
+static void __exit apm_battery_exit(void)
Quoted 8 lines

drivers/battery/apm_power.c line 115

Quoted 4 lines
drivers/battery/apm_power.c new file +4 -0
+{
+	apm_get_power_status = old_apm_get_power_status;
+	return;
+}

Utterly unsafe. What happens if some other module gets loaded which does this, and then this module is unloaded followed by the other module. Result: Oops.

Quoted 6 lines

Right. And loading two modules which changing apm_get_power_status is a race already. Thus, APM interface needs a mutex.

Or pda_power should be marked "bool" in Kconfig, as it is done in arch/arm/common/sharpsl_pm.c. Sharpsl_pm is safe only because it can't be a module.

Just mark it bool, when you fix it with mutex, you can go back to tristate.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#6 lore raw
In reply to Russell King

Hello Russell,

Quoted 13 lines · you
Quoted 12 lines · Anton Vorontsov
drivers/battery/apm_power.c new file +12 -0
+static void (*old_apm_get_power_status)(struct apm_power_info*);
+
+static int __init apm_battery_init(void)
+{
+     printk(KERN_INFO "APM Battery Driver\n");
+
+     old_apm_get_power_status = apm_get_power_status;
+     apm_get_power_status = apm_battery_apm_get_power_status;
+     return 0;
+}
+
+static void __exit apm_battery_exit(void)
Quoted 8 lines

drivers/battery/apm_power.c line 115

Quoted 4 lines
drivers/battery/apm_power.c new file +4 -0
+{
+     apm_get_power_status = old_apm_get_power_status;
+     return;
+}

Utterly unsafe. What happens if some other module gets loaded which does this, and then this module is unloaded followed by the other module. Result: Oops.

That's apparently why "APM emulation" goes on its way towards deprecation, right? And why people so detailed about new battery API, as it's everyone's hope that it should replace APM.

We exactly provide APM emulation on top of battery API as separate driver because of such issues with APM API. Anyway, any suggestions on solving this "pointer API" issue? Would at least assigning NULL on exit be more safe? (Because yes, there just shouldn't be two APM drivers, and for the weird case there're, it would be nice to at least not segfault.)

Signature · 2 lines
Best regards,
 Paul                            mailto:[email protected]