lnx_apm.c 4.88 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1

2
3
4
5
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

6
#include <X11/X.h>
7
#include "os.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
8
9
10
11
#include "xf86.h"
#include "xf86Priv.h"
#define XF86_OS_PRIVS
#include "xf86_OSproc.h"
12
13
14
15
16
17
18

#ifdef HAVE_ACPI
extern PMClose lnxACPIOpen(void);
#endif

#ifdef HAVE_APM

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
19
20
21
22
23
24
25
#include <linux/apm_bios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
26

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
27
28
29
30
#define APM_PROC   "/proc/apm"
#define APM_DEVICE "/dev/apm_bios"

#ifndef APM_STANDBY_FAILED
31
#define APM_STANDBY_FAILED 0xf000
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
32
33
#endif
#ifndef APM_SUSPEND_FAILED
34
#define APM_SUSPEND_FAILED 0xf001
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
35
36
#endif

37
static PMClose lnxAPMOpen(void);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
38
static void lnxCloseAPM(void);
39
static void *APMihPtr = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
40
41
42
43
44

static struct {
    apm_event_t apmLinux;
    pmEvent xf86;
} LinuxToXF86[] = {
45
46
47
48
49
50
51
52
53
54
55
    {APM_SYS_STANDBY, XF86_APM_SYS_STANDBY},
    {APM_SYS_SUSPEND, XF86_APM_SYS_SUSPEND},
    {APM_NORMAL_RESUME, XF86_APM_NORMAL_RESUME},
    {APM_CRITICAL_RESUME, XF86_APM_CRITICAL_RESUME},
    {APM_LOW_BATTERY, XF86_APM_LOW_BATTERY},
    {APM_POWER_STATUS_CHANGE, XF86_APM_POWER_STATUS_CHANGE},
    {APM_UPDATE_TIME, XF86_APM_UPDATE_TIME},
    {APM_CRITICAL_SUSPEND, XF86_APM_CRITICAL_SUSPEND},
    {APM_USER_STANDBY, XF86_APM_USER_STANDBY},
    {APM_USER_SUSPEND, XF86_APM_USER_SUSPEND},
    {APM_STANDBY_RESUME, XF86_APM_STANDBY_RESUME},
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
56
#if defined(APM_CAPABILITY_CHANGED)
57
    {APM_CAPABILITY_CHANGED, XF86_CAPABILITY_CHANGED},
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
58
59
#endif
#if 0
60
61
    {APM_STANDBY_FAILED, XF86_APM_STANDBY_FAILED},
    {APM_SUSPEND_FAILED, XF86_APM_SUSPEND_FAILED}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
62
63
64
65
66
67
68
69
70
71
72
73
#endif
};

/*
 * APM is still under construction.
 * I'm not sure if the places where I initialize/deinitialize
 * apm is correct. Also I don't know what to do in SETUP state.
 * This depends if wakeup gets called in this situation, too.
 * Also we need to check if the action that is taken on an
 * event is reasonable.
 */
static int
74
lnxPMGetEventFromOs(int fd, pmEvent * events, int num)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
75
{
76
    int i, j, n;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
77
78
    apm_event_t linuxEvents[8];

79
80
    if ((n = read(fd, linuxEvents, num * sizeof(apm_event_t))) == -1)
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
81
82
    n /= sizeof(apm_event_t);
    if (n > num)
83
        n = num;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
84
    for (i = 0; i < n; i++) {
85
        for (j = 0; j < ARRAY_SIZE(LinuxToXF86); j++)
86
87
88
89
            if (LinuxToXF86[j].apmLinux == linuxEvents[i]) {
                events[i] = LinuxToXF86[j].xf86;
                break;
            }
90
        if (j == ARRAY_SIZE(LinuxToXF86))
91
            events[i] = XF86_APM_UNKNOWN;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
92
93
94
95
96
97
98
99
100
101
    }
    return n;
}

static pmWait
lnxPMConfirmEventToOs(int fd, pmEvent event)
{
    switch (event) {
    case XF86_APM_SYS_STANDBY:
    case XF86_APM_USER_STANDBY:
102
103
104
        if (ioctl(fd, APM_IOC_STANDBY, NULL))
            return PM_FAILED;
        return PM_CONTINUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
105
106
107
    case XF86_APM_SYS_SUSPEND:
    case XF86_APM_CRITICAL_SUSPEND:
    case XF86_APM_USER_SUSPEND:
108
109
110
111
112
113
114
115
116
117
118
119
        if (ioctl(fd, APM_IOC_SUSPEND, NULL)) {
            /* I believe this is wrong (EE)
               EBUSY is sent when a device refuses to be suspended.
               In this case we still need to undo everything we have
               done to suspend ourselves or we will stay in suspended
               state forever. */
            if (errno == EBUSY)
                return PM_CONTINUE;
            else
                return PM_FAILED;
        }
        return PM_CONTINUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
120
121
122
123
124
    case XF86_APM_STANDBY_RESUME:
    case XF86_APM_NORMAL_RESUME:
    case XF86_APM_CRITICAL_RESUME:
    case XF86_APM_STANDBY_FAILED:
    case XF86_APM_SUSPEND_FAILED:
125
        return PM_CONTINUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
126
    default:
127
        return PM_NONE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
128
129
130
    }
}

131
#endif                          // HAVE_APM
132

133
PMClose
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
134
xf86OSPMOpen(void)
135
{
136
    PMClose ret = NULL;
137

138
#ifdef HAVE_ACPI
139
    /* Favour ACPI over APM, but only when enabled */
140

141
    if (!xf86acpiDisableFlag) {
142
        ret = lnxACPIOpen();
143
144
145
        if (ret)
            return ret;
    }
146
147
#endif
#ifdef HAVE_APM
148
    ret = lnxAPMOpen();
149
#endif
150

151
    return ret;
152
153
}

154
155
#ifdef HAVE_APM

156
157
static PMClose
lnxAPMOpen(void)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
158
{
159
    int fd, pfd;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
160

161
    DebugF("APM: OSPMOpen called\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
162
    if (APMihPtr || !xf86Info.pmFlag)
163
164
        return NULL;

165
    DebugF("APM: Opening device\n");
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    if ((fd = open(APM_DEVICE, O_RDWR)) > -1) {
        if (access(APM_PROC, R_OK) || ((pfd = open(APM_PROC, O_RDONLY)) == -1)) {
            xf86MsgVerb(X_WARNING, 3, "Cannot open APM (%s) (%s)\n",
                        APM_PROC, strerror(errno));
            close(fd);
            return NULL;
        }
        else
            close(pfd);
        xf86PMGetEventFromOs = lnxPMGetEventFromOs;
        xf86PMConfirmEventToOs = lnxPMConfirmEventToOs;
        APMihPtr = xf86AddGeneralHandler(fd, xf86HandlePMEvents, NULL);
        xf86MsgVerb(X_INFO, 3, "Open APM successful\n");
        return lnxCloseAPM;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
180
181
182
183
184
185
186
187
    }
    return NULL;
}

static void
lnxCloseAPM(void)
{
    int fd;
188
189

    DebugF("APM: Closing device\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
190
    if (APMihPtr) {
191
192
193
        fd = xf86RemoveGeneralHandler(APMihPtr);
        close(fd);
        APMihPtr = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
194
195
196
    }
}

197
#endif                          // HAVE_APM