Merge commit 'v2.6.29-rc8' into cpus4096
[safe/jmp/linux-2.6] / arch / x86 / kernel / apm_32.c
index 7496c2e..10033fe 100644 (file)
  *         Work around byte swap bug in one of the Vaio's BIOS's
  *         (Marc Boucher <marc@mbsi.ca>).
  *         Exposed the disable flag to dmi so that we can handle known
- *         broken APM (Alan Cox <alan@redhat.com>).
+ *         broken APM (Alan Cox <alan@lxorguk.ukuu.org.uk>).
  *   1.14ac: If the BIOS says "I slowed the CPU down" then don't spin
- *         calling it - instead idle. (Alan Cox <alan@redhat.com>)
+ *         calling it - instead idle. (Alan Cox <alan@lxorguk.ukuu.org.uk>)
  *         If an APM idle fails log it and idle sensibly
  *   1.15: Don't queue events to clients who open the device O_WRONLY.
  *         Don't expect replies from clients who open the device O_RDONLY.
 #include <linux/module.h>
 
 #include <linux/poll.h>
+#include <linux/smp_lock.h>
 #include <linux/types.h>
 #include <linux/stddef.h>
 #include <linux/timer.h>
 #include <linux/time.h>
 #include <linux/sched.h>
 #include <linux/pm.h>
-#include <linux/pm_legacy.h>
 #include <linux/capability.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
 #include <linux/dmi.h>
 #include <linux/suspend.h>
 #include <linux/kthread.h>
+#include <linux/jiffies.h>
 
 #include <asm/system.h>
 #include <asm/uaccess.h>
 #include <asm/desc.h>
 #include <asm/i8253.h>
+#include <asm/olpc.h>
 #include <asm/paravirt.h>
 #include <asm/reboot.h>
 
@@ -299,7 +301,7 @@ extern int (*console_blank_hook)(int);
  */
 #define APM_ZERO_SEGS
 
-#include "apm.h"
+#include <asm/apm.h>
 
 /*
  * Define to re-initialize the interrupt 0 timer to 100 Hz after a suspend.
@@ -322,7 +324,7 @@ extern int (*console_blank_hook)(int);
 /*
  * Ignore suspend events for this amount of time after a resume
  */
-#define DEFAULT_BOUNCE_INTERVAL                (3 * HZ)
+#define DEFAULT_BOUNCE_INTERVAL        (3 * HZ)
 
 /*
  * Maximum number of events stored
@@ -334,7 +336,7 @@ extern int (*console_blank_hook)(int);
  */
 struct apm_user {
        int             magic;
-       struct apm_user *       next;
+       struct apm_user *next;
        unsigned int    suser: 1;
        unsigned int    writer: 1;
        unsigned int    reader: 1;
@@ -370,44 +372,40 @@ struct apm_user {
 static struct {
        unsigned long   offset;
        unsigned short  segment;
-}                              apm_bios_entry;
-static int                     clock_slowed;
-static int                     idle_threshold __read_mostly = DEFAULT_IDLE_THRESHOLD;
-static int                     idle_period __read_mostly = DEFAULT_IDLE_PERIOD;
-static int                     set_pm_idle;
-static int                     suspends_pending;
-static int                     standbys_pending;
-static int                     ignore_sys_suspend;
-static int                     ignore_normal_resume;
-static int                     bounce_interval __read_mostly = DEFAULT_BOUNCE_INTERVAL;
-
-static int                     debug __read_mostly;
-static int                     smp __read_mostly;
-static int                     apm_disabled = -1;
+} apm_bios_entry;
+static int clock_slowed;
+static int idle_threshold __read_mostly = DEFAULT_IDLE_THRESHOLD;
+static int idle_period __read_mostly = DEFAULT_IDLE_PERIOD;
+static int set_pm_idle;
+static int suspends_pending;
+static int standbys_pending;
+static int ignore_sys_suspend;
+static int ignore_normal_resume;
+static int bounce_interval __read_mostly = DEFAULT_BOUNCE_INTERVAL;
+
+static int debug __read_mostly;
+static int smp __read_mostly;
+static int apm_disabled = -1;
 #ifdef CONFIG_SMP
-static int                     power_off;
+static int power_off;
 #else
-static int                     power_off = 1;
-#endif
-#ifdef CONFIG_APM_REAL_MODE_POWER_OFF
-static int                     realmode_power_off = 1;
-#else
-static int                     realmode_power_off;
+static int power_off = 1;
 #endif
+static int realmode_power_off;
 #ifdef CONFIG_APM_ALLOW_INTS
-static int                     allow_ints = 1;
+static int allow_ints = 1;
 #else
-static int                     allow_ints;
+static int allow_ints;
 #endif
-static int                     broken_psr;
+static int broken_psr;
 
 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
-static struct apm_user *       user_list;
+static struct apm_user *user_list;
 static DEFINE_SPINLOCK(user_list_lock);
-static const struct desc_struct        bad_bios_desc = { 0, 0x00409200 };
+static const struct desc_struct        bad_bios_desc = { { { 0, 0x00409200 } } };
 
-static const char              driver_version[] = "1.16ac";    /* no spaces */
+static const char driver_version[] = "1.16ac"; /* no spaces */
 
 static struct task_struct *kapmd_task;
 
@@ -415,7 +413,7 @@ static struct task_struct *kapmd_task;
  *     APM event names taken from the APM 1.2 specification. These are
  *     the message codes that the BIOS uses to tell us about events
  */
-static const char *    const apm_event_name[] = {
+static const char * const apm_event_name[] = {
        "system standby",
        "system suspend",
        "normal resume",
@@ -433,14 +431,14 @@ static const char *       const apm_event_name[] = {
 
 typedef struct lookup_t {
        int     key;
-       char *  msg;
+       char    *msg;
 } lookup_t;
 
 /*
  *     The BIOS returns a set of standard error codes in AX when the
  *     carry flag is set.
  */
+
 static const lookup_t error_table[] = {
 /* N/A { APM_SUCCESS,          "Operation succeeded" }, */
        { APM_DISABLED,         "Power management disabled" },
@@ -470,24 +468,25 @@ static const lookup_t error_table[] = {
  *     Write a meaningful log entry to the kernel log in the event of
  *     an APM error.
  */
+
 static void apm_error(char *str, int err)
 {
-       int     i;
+       int i;
 
        for (i = 0; i < ERROR_COUNT; i++)
-               if (error_table[i].key == err) break;
+               if (error_table[i].key == err)
+                       break;
        if (i < ERROR_COUNT)
                printk(KERN_NOTICE "apm: %s: %s\n", str, error_table[i].msg);
        else
                printk(KERN_NOTICE "apm: %s: unknown error code %#2.2x\n",
-                       str, err);
+                      str, err);
 }
 
 /*
  * Lock APM functionality to physical CPU 0
  */
+
 #ifdef CONFIG_SMP
 
 static cpumask_t apm_save_cpus(void)
@@ -509,7 +508,7 @@ static inline void apm_restore_cpus(cpumask_t mask)
 /*
  *     No CPU lockdown needed on a uniprocessor
  */
+
 #define apm_save_cpus()                (current->cpus_allowed)
 #define apm_restore_cpus(x)    (void)(x)
 
@@ -588,7 +587,7 @@ static inline void apm_irq_restore(unsigned long flags)
  *     code is returned in AH (bits 8-15 of eax) and this function
  *     returns non-zero.
  */
+
 static u8 apm_bios_call(u32 func, u32 ebx_in, u32 ecx_in,
        u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, u32 *esi)
 {
@@ -600,7 +599,7 @@ static u8 apm_bios_call(u32 func, u32 ebx_in, u32 ecx_in,
        struct desc_struct      *gdt;
 
        cpus = apm_save_cpus();
-       
+
        cpu = get_cpu();
        gdt = get_cpu_gdt_table(cpu);
        save_desc_40 = gdt[0x40 / 8];
@@ -614,7 +613,7 @@ static u8 apm_bios_call(u32 func, u32 ebx_in, u32 ecx_in,
        gdt[0x40 / 8] = save_desc_40;
        put_cpu();
        apm_restore_cpus(cpus);
-       
+
        return *eax & 0xff;
 }
 
@@ -643,7 +642,7 @@ static u8 apm_bios_call_simple(u32 func, u32 ebx_in, u32 ecx_in, u32 *eax)
        struct desc_struct      *gdt;
 
        cpus = apm_save_cpus();
-       
+
        cpu = get_cpu();
        gdt = get_cpu_gdt_table(cpu);
        save_desc_40 = gdt[0x40 / 8];
@@ -678,7 +677,7 @@ static u8 apm_bios_call_simple(u32 func, u32 ebx_in, u32 ecx_in, u32 *eax)
 
 static int apm_driver_version(u_short *val)
 {
-       u32     eax;
+       u32 eax;
 
        if (apm_bios_call_simple(APM_FUNC_VERSION, 0, *val, &eax))
                return (eax >> 8) & 0xff;
@@ -702,16 +701,16 @@ static int apm_driver_version(u_short *val)
  *     that APM 1.2 is in use. If no messges are pending the value 0x80
  *     is returned (No power management events pending).
  */
+
 static int apm_get_event(apm_event_t *event, apm_eventinfo_t *info)
 {
-       u32     eax;
-       u32     ebx;
-       u32     ecx;
-       u32     dummy;
+       u32 eax;
+       u32 ebx;
+       u32 ecx;
+       u32 dummy;
 
        if (apm_bios_call(APM_FUNC_GET_EVENT, 0, 0, &eax, &ebx, &ecx,
-                       &dummy, &dummy))
+                         &dummy, &dummy))
                return (eax >> 8) & 0xff;
        *event = ebx;
        if (apm_info.connection_version < 0x0102)
@@ -734,10 +733,10 @@ static int apm_get_event(apm_event_t *event, apm_eventinfo_t *info)
  *     The state holds the state to transition to, which may in fact
  *     be an acceptance of a BIOS requested state change.
  */
+
 static int set_power_state(u_short what, u_short state)
 {
-       u32     eax;
+       u32 eax;
 
        if (apm_bios_call_simple(APM_FUNC_SET_STATE, what, state, &eax))
                return (eax >> 8) & 0xff;
@@ -750,7 +749,7 @@ static int set_power_state(u_short what, u_short state)
  *
  *     Transition the entire system into a new APM power state.
  */
+
 static int set_system_power_state(u_short state)
 {
        return set_power_state(APM_DEVICE_ALL, state);
@@ -764,13 +763,13 @@ static int set_system_power_state(u_short state)
  *     to handle the idle request. On a success the function returns 1
  *     if the BIOS did clock slowing or 0 otherwise.
  */
+
 static int apm_do_idle(void)
 {
-       u32     eax;
-       u8      ret = 0;
-       int     idled = 0;
-       int     polling;
+       u32 eax;
+       u8 ret = 0;
+       int idled = 0;
+       int polling;
 
        polling = !!(current_thread_info()->status & TS_POLLING);
        if (polling) {
@@ -797,10 +796,9 @@ static int apm_do_idle(void)
                /* This always fails on some SMP boards running UP kernels.
                 * Only report the failure the first 5 times.
                 */
-               if (++t < 5)
-               {
+               if (++t < 5) {
                        printk(KERN_DEBUG "apm_do_idle failed (%d)\n",
-                                       (eax >> 8) & 0xff);
+                              (eax >> 8) & 0xff);
                        t = jiffies;
                }
                return -1;
@@ -812,15 +810,15 @@ static int apm_do_idle(void)
 /**
  *     apm_do_busy     -       inform the BIOS the CPU is busy
  *
- *     Request that the BIOS brings the CPU back to full performance. 
+ *     Request that the BIOS brings the CPU back to full performance.
  */
+
 static void apm_do_busy(void)
 {
-       u32     dummy;
+       u32 dummy;
 
        if (clock_slowed || ALWAYS_CALL_BUSY) {
-               (void) apm_bios_call_simple(APM_FUNC_BUSY, 0, 0, &dummy);
+               (void)apm_bios_call_simple(APM_FUNC_BUSY, 0, 0, &dummy);
                clock_slowed = 0;
        }
 }
@@ -831,15 +829,15 @@ static void apm_do_busy(void)
  * power management - we probably want
  * to conserve power.
  */
-#define IDLE_CALC_LIMIT   (HZ * 100)
-#define IDLE_LEAKY_MAX    16
+#define IDLE_CALC_LIMIT        (HZ * 100)
+#define IDLE_LEAKY_MAX 16
 
 static void (*original_pm_idle)(void) __read_mostly;
 
 /**
  * apm_cpu_idle                -       cpu idling for APM capable Linux
  *
- * This is the idling function the kernel executes when APM is available. It 
+ * This is the idling function the kernel executes when APM is available. It
  * tries to do BIOS powermanagement based on the average system idle time.
  * Furthermore it calls the system default idle routine.
  */
@@ -880,7 +878,8 @@ recalc:
 
                        t = jiffies;
                        switch (apm_do_idle()) {
-                       case 0: apm_idle_done = 1;
+                       case 0:
+                               apm_idle_done = 1;
                                if (t != jiffies) {
                                        if (bucket) {
                                                bucket = IDLE_LEAKY_MAX;
@@ -891,7 +890,8 @@ recalc:
                                        continue;
                                }
                                break;
-                       case 1: apm_idle_done = 1;
+                       case 1:
+                               apm_idle_done = 1;
                                break;
                        default: /* BIOS refused */
                                break;
@@ -901,6 +901,7 @@ recalc:
                        original_pm_idle();
                else
                        default_idle();
+               local_irq_disable();
                jiffies_since_last_check = jiffies - last_jiffies;
                if (jiffies_since_last_check > idle_period)
                        goto recalc;
@@ -908,6 +909,8 @@ recalc:
 
        if (apm_idle_done)
                apm_do_busy();
+
+       local_irq_enable();
 }
 
 /**
@@ -919,10 +922,10 @@ recalc:
  *     the SMP call on CPU0 as some systems will only honour this call
  *     on their first cpu.
  */
+
 static void apm_power_off(void)
 {
-       unsigned char   po_bios_call[] = {
+       unsigned char po_bios_call[] = {
                0xb8, 0x00, 0x10,       /* movw  $0x1000,ax  */
                0x8e, 0xd0,             /* movw  ax,ss       */
                0xbc, 0x00, 0xf0,       /* movw  $0xf000,sp  */
@@ -933,13 +936,12 @@ static void apm_power_off(void)
        };
 
        /* Some bioses don't like being called from CPU != 0 */
-       if (apm_info.realmode_power_off)
-       {
+       if (apm_info.realmode_power_off) {
                (void)apm_save_cpus();
                machine_real_restart(po_bios_call, sizeof(po_bios_call));
+       } else {
+               (void)set_system_power_state(APM_STATE_OFF);
        }
-       else
-               (void) set_system_power_state(APM_STATE_OFF);
 }
 
 #ifdef CONFIG_APM_DO_ENABLE
@@ -948,17 +950,17 @@ static void apm_power_off(void)
  *     apm_enable_power_management - enable BIOS APM power management
  *     @enable: enable yes/no
  *
- *     Enable or disable the APM BIOS power services. 
+ *     Enable or disable the APM BIOS power services.
  */
+
 static int apm_enable_power_management(int enable)
 {
-       u32     eax;
+       u32 eax;
 
        if ((enable == 0) && (apm_info.bios.flags & APM_BIOS_DISENGAGED))
                return APM_NOT_ENGAGED;
        if (apm_bios_call_simple(APM_FUNC_ENABLE_PM, APM_DEVICE_BALL,
-                       enable, &eax))
+                                enable, &eax))
                return (eax >> 8) & 0xff;
        if (enable)
                apm_info.bios.flags &= ~APM_BIOS_DISABLED;
@@ -981,19 +983,19 @@ static int apm_enable_power_management(int enable)
  *     if reported is a lifetime in secodnds/minutes at current powwer
  *     consumption.
  */
+
 static int apm_get_power_status(u_short *status, u_short *bat, u_short *life)
 {
-       u32     eax;
-       u32     ebx;
-       u32     ecx;
-       u32     edx;
-       u32     dummy;
+       u32 eax;
+       u32 ebx;
+       u32 ecx;
+       u32 edx;
+       u32 dummy;
 
        if (apm_info.get_power_status_broken)
                return APM_32_UNSUPPORTED;
        if (apm_bios_call(APM_FUNC_GET_STATUS, APM_DEVICE_ALL, 0,
-                       &eax, &ebx, &ecx, &edx, &dummy))
+                         &eax, &ebx, &ecx, &edx, &dummy))
                return (eax >> 8) & 0xff;
        *status = ebx;
        *bat = ecx;
@@ -1009,11 +1011,11 @@ static int apm_get_power_status(u_short *status, u_short *bat, u_short *life)
 static int apm_get_battery_status(u_short which, u_short *status,
                                  u_short *bat, u_short *life, u_short *nbat)
 {
-       u32     eax;
-       u32     ebx;
-       u32     ecx;
-       u32     edx;
-       u32     esi;
+       u32 eax;
+       u32 ebx;
+       u32 ecx;
+       u32 edx;
+       u32 esi;
 
        if (apm_info.connection_version < 0x0102) {
                /* pretend we only have one battery. */
@@ -1024,7 +1026,7 @@ static int apm_get_battery_status(u_short which, u_short *status,
        }
 
        if (apm_bios_call(APM_FUNC_GET_STATUS, (0x8000 | (which)), 0, &eax,
-                       &ebx, &ecx, &edx, &esi))
+                         &ebx, &ecx, &edx, &esi))
                return (eax >> 8) & 0xff;
        *status = ebx;
        *bat = ecx;
@@ -1042,10 +1044,10 @@ static int apm_get_battery_status(u_short which, u_short *status,
  *     Activate or deactive power management on either a specific device
  *     or the entire system (%APM_DEVICE_ALL).
  */
+
 static int apm_engage_power_management(u_short device, int enable)
 {
-       u32     eax;
+       u32 eax;
 
        if ((enable == 0) && (device == APM_DEVICE_ALL)
            && (apm_info.bios.flags & APM_BIOS_DISABLED))
@@ -1072,7 +1074,7 @@ static int apm_engage_power_management(u_short device, int enable)
  *     all video devices. Typically the BIOS will do laptop backlight and
  *     monitor powerdown for us.
  */
+
 static int apm_console_blank(int blank)
 {
        int error = APM_NOT_ENGAGED; /* silence gcc */
@@ -1124,7 +1126,7 @@ static apm_event_t get_queued_event(struct apm_user *as)
 
 static void queue_event(apm_event_t event, struct apm_user *sender)
 {
-       struct apm_user *       as;
+       struct apm_user *as;
 
        spin_lock(&user_list_lock);
        if (user_list == NULL)
@@ -1144,7 +1146,7 @@ static void queue_event(apm_event_t event, struct apm_user *sender)
                                as->event_tail = 0;
                }
                as->events[as->event_head] = event;
-               if ((!as->suser) || (!as->writer))
+               if (!as->suser || !as->writer)
                        continue;
                switch (event) {
                case APM_SYS_SUSPEND:
@@ -1172,11 +1174,11 @@ static void reinit_timer(void)
 
        spin_lock_irqsave(&i8253_lock, flags);
        /* set the clock to HZ */
-       outb_p(0x34, PIT_MODE);         /* binary, mode 2, LSB/MSB, ch 0 */
+       outb_pit(0x34, PIT_MODE);               /* binary, mode 2, LSB/MSB, ch 0 */
        udelay(10);
-       outb_p(LATCH & 0xff, PIT_CH0);  /* LSB */
+       outb_pit(LATCH & 0xff, PIT_CH0);        /* LSB */
        udelay(10);
-       outb(LATCH >> 8, PIT_CH0);      /* MSB */
+       outb_pit(LATCH >> 8, PIT_CH0);  /* MSB */
        udelay(10);
        spin_unlock_irqrestore(&i8253_lock, flags);
 #endif
@@ -1184,25 +1186,13 @@ static void reinit_timer(void)
 
 static int suspend(int vetoable)
 {
-       int             err;
+       int err;
        struct apm_user *as;
 
-       if (pm_send_all(PM_SUSPEND, (void *)3)) {
-               /* Vetoed */
-               if (vetoable) {
-                       if (apm_info.connection_version > 0x100)
-                               set_system_power_state(APM_STATE_REJECT);
-                       err = -EBUSY;
-                       ignore_sys_suspend = 0;
-                       printk(KERN_WARNING "apm: suspend was vetoed.\n");
-                       goto out;
-               }
-               printk(KERN_CRIT "apm: suspend was vetoed, but suspending anyway.\n");
-       }
-
        device_suspend(PMSG_SUSPEND);
        local_irq_disable();
        device_power_down(PMSG_SUSPEND);
+       sysdev_suspend(PMSG_SUSPEND);
 
        local_irq_enable();
 
@@ -1219,12 +1209,11 @@ static int suspend(int vetoable)
        if (err != APM_SUCCESS)
                apm_error("suspend", err);
        err = (err == APM_SUCCESS) ? 0 : -EIO;
-       device_power_up();
+       sysdev_resume();
+       device_power_up(PMSG_RESUME);
        local_irq_enable();
-       device_resume();
-       pm_send_all(PM_RESUME, (void *)0);
+       device_resume(PMSG_RESUME);
        queue_event(APM_NORMAL_RESUME, NULL);
- out:
        spin_lock(&user_list_lock);
        for (as = user_list; as != NULL; as = as->next) {
                as->suspend_wait = 0;
@@ -1237,10 +1226,11 @@ static int suspend(int vetoable)
 
 static void standby(void)
 {
-       int     err;
+       int err;
 
        local_irq_disable();
        device_power_down(PMSG_SUSPEND);
+       sysdev_suspend(PMSG_SUSPEND);
        local_irq_enable();
 
        err = set_system_power_state(APM_STATE_STANDBY);
@@ -1248,14 +1238,15 @@ static void standby(void)
                apm_error("standby", err);
 
        local_irq_disable();
-       device_power_up();
+       sysdev_resume();
+       device_power_up(PMSG_RESUME);
        local_irq_enable();
 }
 
 static apm_event_t get_event(void)
 {
-       int             error;
-       apm_event_t     event = APM_NO_EVENTS; /* silence gcc */
+       int error;
+       apm_event_t event = APM_NO_EVENTS; /* silence gcc */
        apm_eventinfo_t info;
 
        static int notified;
@@ -1273,9 +1264,9 @@ static apm_event_t get_event(void)
 
 static void check_events(void)
 {
-       apm_event_t             event;
-       static unsigned long    last_resume;
-       static int              ignore_bounce;
+       apm_event_t event;
+       static unsigned long last_resume;
+       static int ignore_bounce;
 
        while ((event = get_event()) != 0) {
                if (debug) {
@@ -1287,7 +1278,7 @@ static void check_events(void)
                                       "event 0x%02x\n", event);
                }
                if (ignore_bounce
-                   && ((jiffies - last_resume) > bounce_interval))
+                   && (time_after(jiffies, last_resume + bounce_interval)))
                        ignore_bounce = 0;
 
                switch (event) {
@@ -1334,8 +1325,7 @@ static void check_events(void)
                        ignore_bounce = 1;
                        if ((event != APM_NORMAL_RESUME)
                            || (ignore_normal_resume == 0)) {
-                               device_resume();
-                               pm_send_all(PM_RESUME, (void *)0);
+                               device_resume(PMSG_RESUME);
                                queue_event(event, NULL);
                        }
                        ignore_normal_resume = 0;
@@ -1355,7 +1345,7 @@ static void check_events(void)
                        /*
                         * We are not allowed to reject a critical suspend.
                         */
-                       (void) suspend(0);
+                       (void)suspend(0);
                        break;
                }
        }
@@ -1363,12 +1353,12 @@ static void check_events(void)
 
 static void apm_event_handler(void)
 {
-       static int      pending_count = 4;
-       int             err;
+       static int pending_count = 4;
+       int err;
 
        if ((standbys_pending > 0) || (suspends_pending > 0)) {
                if ((apm_info.connection_version > 0x100) &&
-                               (pending_count-- <= 0)) {
+                   (pending_count-- <= 0)) {
                        pending_count = 4;
                        if (debug)
                                printk(KERN_DEBUG "apm: setting state busy\n");
@@ -1407,7 +1397,7 @@ static void apm_mainloop(void)
 
 static int check_apm_user(struct apm_user *as, const char *func)
 {
-       if ((as == NULL) || (as->magic != APM_BIOS_MAGIC)) {
+       if (as == NULL || as->magic != APM_BIOS_MAGIC) {
                printk(KERN_ERR "apm: %s passed bad filp\n", func);
                return 1;
        }
@@ -1416,9 +1406,9 @@ static int check_apm_user(struct apm_user *as, const char *func)
 
 static ssize_t do_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
 {
-       struct apm_user *       as;
-       int                     i;
-       apm_event_t             event;
+       struct apm_user *as;
+       int i;
+       apm_event_t event;
 
        as = fp->private_data;
        if (check_apm_user(as, "read"))
@@ -1457,9 +1447,9 @@ static ssize_t do_read(struct file *fp, char __user *buf, size_t count, loff_t *
        return 0;
 }
 
-static unsigned int do_poll(struct file *fp, poll_table * wait)
+static unsigned int do_poll(struct file *fp, poll_table *wait)
 {
-       struct apm_user * as;
+       struct apm_user *as;
 
        as = fp->private_data;
        if (check_apm_user(as, "poll"))
@@ -1470,18 +1460,19 @@ static unsigned int do_poll(struct file *fp, poll_table * wait)
        return 0;
 }
 
-static int do_ioctl(struct inode * inode, struct file *filp,
-                   u_int cmd, u_long arg)
+static long do_ioctl(struct file *filp, u_int cmd, u_long arg)
 {
-       struct apm_user *       as;
+       struct apm_user *as;
+       int ret;
 
        as = filp->private_data;
        if (check_apm_user(as, "ioctl"))
                return -EIO;
-       if ((!as->suser) || (!as->writer))
+       if (!as->suser || !as->writer)
                return -EPERM;
        switch (cmd) {
        case APM_IOC_STANDBY:
+               lock_kernel();
                if (as->standbys_read > 0) {
                        as->standbys_read--;
                        as->standbys_pending--;
@@ -1490,8 +1481,10 @@ static int do_ioctl(struct inode * inode, struct file *filp,
                        queue_event(APM_USER_STANDBY, as);
                if (standbys_pending <= 0)
                        standby();
+               unlock_kernel();
                break;
        case APM_IOC_SUSPEND:
+               lock_kernel();
                if (as->suspends_read > 0) {
                        as->suspends_read--;
                        as->suspends_pending--;
@@ -1499,23 +1492,24 @@ static int do_ioctl(struct inode * inode, struct file *filp,
                } else
                        queue_event(APM_USER_SUSPEND, as);
                if (suspends_pending <= 0) {
-                       return suspend(1);
+                       ret = suspend(1);
                } else {
                        as->suspend_wait = 1;
                        wait_event_interruptible(apm_suspend_waitqueue,
                                        as->suspend_wait == 0);
-                       return as->suspend_result;
+                       ret = as->suspend_result;
                }
-               break;
+               unlock_kernel();
+               return ret;
        default:
-               return -EINVAL;
+               return -ENOTTY;
        }
        return 0;
 }
 
-static int do_release(struct inode * inode, struct file * filp)
+static int do_release(struct inode *inode, struct file *filp)
 {
-       struct apm_user *       as;
+       struct apm_user *as;
 
        as = filp->private_data;
        if (check_apm_user(as, "release"))
@@ -1531,11 +1525,11 @@ static int do_release(struct inode * inode, struct file * filp)
                if (suspends_pending <= 0)
                        (void) suspend(1);
        }
-       spin_lock(&user_list_lock);
+       spin_lock(&user_list_lock);
        if (user_list == as)
                user_list = as->next;
        else {
-               struct apm_user *       as1;
+               struct apm_user *as1;
 
                for (as1 = user_list;
                     (as1 != NULL) && (as1->next != as);
@@ -1551,14 +1545,16 @@ static int do_release(struct inode * inode, struct file * filp)
        return 0;
 }
 
-static int do_open(struct inode * inode, struct file * filp)
+static int do_open(struct inode *inode, struct file *filp)
 {
-       struct apm_user *       as;
+       struct apm_user *as;
 
+       lock_kernel();
        as = kmalloc(sizeof(*as), GFP_KERNEL);
        if (as == NULL) {
                printk(KERN_ERR "apm: cannot allocate struct of size %d bytes\n",
                       sizeof(*as));
+                      unlock_kernel();
                return -ENOMEM;
        }
        as->magic = APM_BIOS_MAGIC;
@@ -1567,7 +1563,7 @@ static int do_open(struct inode * inode, struct file * filp)
        as->suspends_read = as->standbys_read = 0;
        /*
         * XXX - this is a tiny bit broken, when we consider BSD
-         * process accounting. If the device is opened by root, we
+        * process accounting. If the device is opened by root, we
         * instantly flag that we used superuser privs. Who knows,
         * we might close the device immediately without doing a
         * privileged operation -- cevans
@@ -1580,6 +1576,7 @@ static int do_open(struct inode * inode, struct file * filp)
        user_list = as;
        spin_unlock(&user_list_lock);
        filp->private_data = as;
+       unlock_kernel();
        return 0;
 }
 
@@ -1650,16 +1647,16 @@ static int proc_apm_show(struct seq_file *m, void *v)
           8) min = minutes; sec = seconds */
 
        seq_printf(m, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
-                    driver_version,
-                    (apm_info.bios.version >> 8) & 0xff,
-                    apm_info.bios.version & 0xff,
-                    apm_info.bios.flags,
-                    ac_line_status,
-                    battery_status,
-                    battery_flag,
-                    percentage,
-                    time_units,
-                    units);
+                  driver_version,
+                  (apm_info.bios.version >> 8) & 0xff,
+                  apm_info.bios.version & 0xff,
+                  apm_info.bios.flags,
+                  ac_line_status,
+                  battery_status,
+                  battery_flag,
+                  percentage,
+                  time_units,
+                  units);
        return 0;
 }
 
@@ -1682,8 +1679,8 @@ static int apm(void *unused)
        unsigned short  cx;
        unsigned short  dx;
        int             error;
-       char *          power_stat;
-       char *          bat_stat;
+       char            *power_stat;
+       char            *bat_stat;
 
 #ifdef CONFIG_SMP
        /* 2002/08/01 - WT
@@ -1742,23 +1739,41 @@ static int apm(void *unused)
                }
        }
 
-       if (debug && (num_online_cpus() == 1 || smp )) {
+       if (debug && (num_online_cpus() == 1 || smp)) {
                error = apm_get_power_status(&bx, &cx, &dx);
                if (error)
                        printk(KERN_INFO "apm: power status not available\n");
                else {
                        switch ((bx >> 8) & 0xff) {
-                       case 0: power_stat = "off line"; break;
-                       case 1: power_stat = "on line"; break;
-                       case 2: power_stat = "on backup power"; break;
-                       default: power_stat = "unknown"; break;
+                       case 0:
+                               power_stat = "off line";
+                               break;
+                       case 1:
+                               power_stat = "on line";
+                               break;
+                       case 2:
+                               power_stat = "on backup power";
+                               break;
+                       default:
+                               power_stat = "unknown";
+                               break;
                        }
                        switch (bx & 0xff) {
-                       case 0: bat_stat = "high"; break;
-                       case 1: bat_stat = "low"; break;
-                       case 2: bat_stat = "critical"; break;
-                       case 3: bat_stat = "charging"; break;
-                       default: bat_stat = "unknown"; break;
+                       case 0:
+                               bat_stat = "high";
+                               break;
+                       case 1:
+                               bat_stat = "low";
+                               break;
+                       case 2:
+                               bat_stat = "critical";
+                               break;
+                       case 3:
+                               bat_stat = "charging";
+                               break;
+                       default:
+                               bat_stat = "unknown";
+                               break;
                        }
                        printk(KERN_INFO
                               "apm: AC %s, battery status %s, battery life ",
@@ -1775,8 +1790,8 @@ static int apm(void *unused)
                                        printk("unknown\n");
                                else
                                        printk("%d %s\n", dx & 0x7fff,
-                                               (dx & 0x8000) ?
-                                               "minutes" : "seconds");
+                                              (dx & 0x8000) ?
+                                              "minutes" : "seconds");
                        }
                }
        }
@@ -1801,7 +1816,7 @@ static int apm(void *unused)
 #ifndef MODULE
 static int __init apm_setup(char *str)
 {
-       int     invert;
+       int invert;
 
        while ((str != NULL) && (*str != '\0')) {
                if (strncmp(str, "off", 3) == 0)
@@ -1826,14 +1841,13 @@ static int __init apm_setup(char *str)
                if ((strncmp(str, "power-off", 9) == 0) ||
                    (strncmp(str, "power_off", 9) == 0))
                        power_off = !invert;
-               if (strncmp(str, "smp", 3) == 0)
-               {
+               if (strncmp(str, "smp", 3) == 0) {
                        smp = !invert;
                        idle_threshold = 100;
                }
                if ((strncmp(str, "allow-ints", 10) == 0) ||
                    (strncmp(str, "allow_ints", 10) == 0))
-                       apm_info.allow_ints = !invert;
+                       apm_info.allow_ints = !invert;
                if ((strncmp(str, "broken-psr", 10) == 0) ||
                    (strncmp(str, "broken_psr", 10) == 0))
                        apm_info.get_power_status_broken = !invert;
@@ -1854,7 +1868,7 @@ static const struct file_operations apm_bios_fops = {
        .owner          = THIS_MODULE,
        .read           = do_read,
        .poll           = do_poll,
-       .ioctl          = do_ioctl,
+       .unlocked_ioctl = do_ioctl,
        .open           = do_open,
        .release        = do_release,
 };
@@ -1879,7 +1893,8 @@ static int __init print_if_true(const struct dmi_system_id *d)
  */
 static int __init broken_ps2_resume(const struct dmi_system_id *d)
 {
-       printk(KERN_INFO "%s machine detected. Mousepad Resume Bug workaround hopefully not needed.\n", d->ident);
+       printk(KERN_INFO "%s machine detected. Mousepad Resume Bug "
+              "workaround hopefully not needed.\n", d->ident);
        return 0;
 }
 
@@ -1888,7 +1903,8 @@ static int __init set_realmode_power_off(const struct dmi_system_id *d)
 {
        if (apm_info.realmode_power_off == 0) {
                apm_info.realmode_power_off = 1;
-               printk(KERN_INFO "%s bios detected. Using realmode poweroff only.\n", d->ident);
+               printk(KERN_INFO "%s bios detected. "
+                      "Using realmode poweroff only.\n", d->ident);
        }
        return 0;
 }
@@ -1898,7 +1914,8 @@ static int __init set_apm_ints(const struct dmi_system_id *d)
 {
        if (apm_info.allow_ints == 0) {
                apm_info.allow_ints = 1;
-               printk(KERN_INFO "%s machine detected. Enabling interrupts during APM calls.\n", d->ident);
+               printk(KERN_INFO "%s machine detected. "
+                      "Enabling interrupts during APM calls.\n", d->ident);
        }
        return 0;
 }
@@ -1908,7 +1925,8 @@ static int __init apm_is_horked(const struct dmi_system_id *d)
 {
        if (apm_info.disabled == 0) {
                apm_info.disabled = 1;
-               printk(KERN_INFO "%s machine detected. Disabling APM.\n", d->ident);
+               printk(KERN_INFO "%s machine detected. "
+                      "Disabling APM.\n", d->ident);
        }
        return 0;
 }
@@ -1917,7 +1935,8 @@ static int __init apm_is_horked_d850md(const struct dmi_system_id *d)
 {
        if (apm_info.disabled == 0) {
                apm_info.disabled = 1;
-               printk(KERN_INFO "%s machine detected. Disabling APM.\n", d->ident);
+               printk(KERN_INFO "%s machine detected. "
+                      "Disabling APM.\n", d->ident);
                printk(KERN_INFO "This bug is fixed in bios P15 which is available for \n");
                printk(KERN_INFO "download from support.intel.com \n");
        }
@@ -1929,7 +1948,8 @@ static int __init apm_likes_to_melt(const struct dmi_system_id *d)
 {
        if (apm_info.forbid_idle == 0) {
                apm_info.forbid_idle = 1;
-               printk(KERN_INFO "%s machine detected. Disabling APM idle calls.\n", d->ident);
+               printk(KERN_INFO "%s machine detected. "
+                      "Disabling APM idle calls.\n", d->ident);
        }
        return 0;
 }
@@ -1952,7 +1972,8 @@ static int __init apm_likes_to_melt(const struct dmi_system_id *d)
 static int __init broken_apm_power(const struct dmi_system_id *d)
 {
        apm_info.get_power_status_broken = 1;
-       printk(KERN_WARNING "BIOS strings suggest APM bugs, disabling power status reporting.\n");
+       printk(KERN_WARNING "BIOS strings suggest APM bugs, "
+              "disabling power status reporting.\n");
        return 0;
 }
 
@@ -1963,7 +1984,8 @@ static int __init broken_apm_power(const struct dmi_system_id *d)
 static int __init swab_apm_power_in_minutes(const struct dmi_system_id *d)
 {
        apm_info.get_power_status_swabinminutes = 1;
-       printk(KERN_WARNING "BIOS strings suggest APM reports battery life in minutes and wrong byte order.\n");
+       printk(KERN_WARNING "BIOS strings suggest APM reports battery life "
+              "in minutes and wrong byte order.\n");
        return 0;
 }
 
@@ -1988,8 +2010,8 @@ static struct dmi_system_id __initdata apm_dmi_table[] = {
                apm_is_horked, "Dell Inspiron 2500",
                {       DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
                        DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 2500"),
-                       DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
-                       DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
+                       DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
+                       DMI_MATCH(DMI_BIOS_VERSION, "A11"), },
        },
        {       /* Allow interrupts during suspend on Dell Inspiron laptops*/
                set_apm_ints, "Dell Inspiron", {
@@ -2012,15 +2034,15 @@ static struct dmi_system_id __initdata apm_dmi_table[] = {
                apm_is_horked, "Dell Dimension 4100",
                {       DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
                        DMI_MATCH(DMI_PRODUCT_NAME, "XPS-Z"),
-                       DMI_MATCH(DMI_BIOS_VENDOR,"Intel Corp."),
-                       DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
+                       DMI_MATCH(DMI_BIOS_VENDOR, "Intel Corp."),
+                       DMI_MATCH(DMI_BIOS_VERSION, "A11"), },
        },
        {       /* Allow interrupts during suspend on Compaq Laptops*/
                set_apm_ints, "Compaq 12XL125",
                {       DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
                        DMI_MATCH(DMI_PRODUCT_NAME, "Compaq PC"),
                        DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
-                       DMI_MATCH(DMI_BIOS_VERSION,"4.06"), },
+                       DMI_MATCH(DMI_BIOS_VERSION, "4.06"), },
        },
        {       /* Allow interrupts during APM or the clock goes slow */
                set_apm_ints, "ASUSTeK",
@@ -2062,15 +2084,15 @@ static struct dmi_system_id __initdata apm_dmi_table[] = {
                apm_is_horked, "Sharp PC-PJ/AX",
                {       DMI_MATCH(DMI_SYS_VENDOR, "SHARP"),
                        DMI_MATCH(DMI_PRODUCT_NAME, "PC-PJ/AX"),
-                       DMI_MATCH(DMI_BIOS_VENDOR,"SystemSoft"),
-                       DMI_MATCH(DMI_BIOS_VERSION,"Version R2.08"), },
+                       DMI_MATCH(DMI_BIOS_VENDOR, "SystemSoft"),
+                       DMI_MATCH(DMI_BIOS_VERSION, "Version R2.08"), },
        },
        {       /* APM crashes */
                apm_is_horked, "Dell Inspiron 2500",
                {       DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
                        DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 2500"),
-                       DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
-                       DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
+                       DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
+                       DMI_MATCH(DMI_BIOS_VERSION, "A11"), },
        },
        {       /* APM idle hangs */
                apm_likes_to_melt, "Jabil AMD",
@@ -2190,22 +2212,21 @@ static struct dmi_system_id __initdata apm_dmi_table[] = {
  */
 static int __init apm_init(void)
 {
-       struct proc_dir_entry *apm_proc;
        struct desc_struct *gdt;
        int err;
 
        dmi_check_system(apm_dmi_table);
 
-       if (apm_info.bios.version == 0 || paravirt_enabled()) {
+       if (apm_info.bios.version == 0 || paravirt_enabled() || machine_is_olpc()) {
                printk(KERN_INFO "apm: BIOS not found.\n");
                return -ENODEV;
        }
        printk(KERN_INFO
-               "apm: BIOS version %d.%d Flags 0x%02x (Driver version %s)\n",
-               ((apm_info.bios.version >> 8) & 0xff),
-               (apm_info.bios.version & 0xff),
-               apm_info.bios.flags,
-               driver_version);
+              "apm: BIOS version %d.%d Flags 0x%02x (Driver version %s)\n",
+              ((apm_info.bios.version >> 8) & 0xff),
+              (apm_info.bios.version & 0xff),
+              apm_info.bios.flags,
+              driver_version);
        if ((apm_info.bios.flags & APM_32_BIT_SUPPORT) == 0) {
                printk(KERN_INFO "apm: no 32 bit BIOS support\n");
                return -ENODEV;
@@ -2295,9 +2316,7 @@ static int __init apm_init(void)
        set_base(gdt[APM_DS >> 3],
                 __va((unsigned long)apm_info.bios.dseg << 4));
 
-       apm_proc = create_proc_entry("apm", 0, NULL);
-       if (apm_proc)
-               apm_proc->proc_fops = &apm_file_ops;
+       proc_create("apm", 0, NULL, &apm_file_ops);
 
        kapmd_task = kthread_create(apm, NULL, "kapmd");
        if (IS_ERR(kapmd_task)) {
@@ -2310,9 +2329,9 @@ static int __init apm_init(void)
        }
        wake_up_process(kapmd_task);
 
-       if (num_online_cpus() > 1 && !smp ) {
+       if (num_online_cpus() > 1 && !smp) {
                printk(KERN_NOTICE
-                  "apm: disabled - APM is not SMP safe (power off active).\n");
+                      "apm: disabled - APM is not SMP safe (power off active).\n");
                return 0;
        }
 
@@ -2337,7 +2356,7 @@ static int __init apm_init(void)
 
 static void __exit apm_exit(void)
 {
-       int     error;
+       int error;
 
        if (set_pm_idle) {
                pm_idle = original_pm_idle;