1/* Based on hw/xfree86/os-support/bsd/bsd_apm.c which bore no explicit
2 * copyright notice, so is covered by the following notice:
3 *
4 * Copyright (C) 1994-2003 The XFree86 Project, Inc.  All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Except as contained in this notice, the name of the XFree86 Project shall
25 * not be used in advertising or otherwise to promote the sale, use or other
26 * dealings in this Software without prior written authorization from the
27 * XFree86 Project.
28 */
29
30/* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice (including the next
40 * paragraph) shall be included in all copies or substantial portions of the
41 * Software.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
50 */
51
52#ifdef HAVE_XORG_CONFIG_H
53#include <xorg-config.h>
54#endif
55
56#include <X11/X.h>
57#include "os.h"
58#include "xf86.h"
59#include "xf86Priv.h"
60#define XF86_OS_PRIVS
61#include "xf86_OSproc.h"
62#include "xf86_OSlib.h"
63
64#ifndef PLEASE_FIX_THIS
65#define APM_STANDBY_REQ 0xa01
66#define APM_SUSPEND_REQ 0xa02
67#define APM_NORMAL_RESUME 0xa03
68#define APM_CRIT_RESUME 0xa04
69#define APM_BATTERY_LOW 0xa05
70#define APM_POWER_CHANGE 0xa06
71#define APM_UPDATE_TIME 0xa07
72#define APM_CRIT_SUSPEND_REQ 0xa08
73#define APM_USER_STANDBY_REQ 0xa09
74#define APM_USER_SUSPEND_REQ 0xa0a
75#define APM_SYS_STANDBY_RESUME 0xa0b
76#define APM_IOC_NEXTEVENT 0xa0c
77#define APM_IOC_RESUME 0xa0d
78#define APM_IOC_SUSPEND 0xa0e
79#define APM_IOC_STANDBY 0xa0f
80#endif
81
82typedef struct apm_event_info
83{
84	int	type;
85} apm_event_info;
86
87/*
88 This may be replaced with a better device name
89 very soon...
90*/
91#define APM_DEVICE "/dev/srn"
92#define APM_DEVICE1 "/dev/apm"
93
94static pointer APMihPtr = NULL;
95static void sunCloseAPM(void);
96
97static struct {
98    u_int apmBsd;
99    pmEvent xf86;
100} sunToXF86Array [] = {
101    { APM_STANDBY_REQ, XF86_APM_SYS_STANDBY },
102    { APM_SUSPEND_REQ, XF86_APM_SYS_SUSPEND },
103    { APM_NORMAL_RESUME, XF86_APM_NORMAL_RESUME },
104    { APM_CRIT_RESUME, XF86_APM_CRITICAL_RESUME },
105    { APM_BATTERY_LOW, XF86_APM_LOW_BATTERY },
106    { APM_POWER_CHANGE, XF86_APM_POWER_STATUS_CHANGE },
107    { APM_UPDATE_TIME, XF86_APM_UPDATE_TIME },
108    { APM_CRIT_SUSPEND_REQ, XF86_APM_CRITICAL_SUSPEND },
109    { APM_USER_STANDBY_REQ, XF86_APM_USER_STANDBY },
110    { APM_USER_SUSPEND_REQ, XF86_APM_USER_SUSPEND },
111    { APM_SYS_STANDBY_RESUME, XF86_APM_STANDBY_RESUME },
112#ifdef APM_CAPABILITY_CHANGE
113    { APM_CAPABILITY_CHANGE, XF86_APM_CAPABILITY_CHANGED },
114#endif
115};
116
117#define numApmEvents (sizeof(sunToXF86Array) / sizeof(sunToXF86Array[0]))
118
119static pmEvent
120sunToXF86(int type)
121{
122    int i;
123
124    for (i = 0; i < numApmEvents; i++) {
125	if (type == sunToXF86Array[i].apmBsd) {
126	    return sunToXF86Array[i].xf86;
127	}
128    }
129    return XF86_APM_UNKNOWN;
130}
131
132/*
133 * APM events can be requested direclty from /dev/apm
134 */
135static int
136sunPMGetEventFromOS(int fd, pmEvent *events, int num)
137{
138    struct apm_event_info sunEvent;
139    int i;
140
141    for (i = 0; i < num; i++) {
142
143	if (ioctl(fd, APM_IOC_NEXTEVENT, &sunEvent) < 0) {
144	    if (errno != EAGAIN) {
145		xf86Msg(X_WARNING, "sunPMGetEventFromOS: APM_IOC_NEXTEVENT"
146			" %s\n", strerror(errno));
147	    }
148	    break;
149	}
150	events[i] = sunToXF86(sunEvent.type);
151    }
152    xf86Msg(X_WARNING, "Got some events\n");
153    return i;
154}
155
156static pmWait
157sunPMConfirmEventToOs(int fd, pmEvent event)
158{
159    switch (event) {
160/* XXX: NOT CURRENTLY RETURNED FROM OS */
161      case XF86_APM_SYS_STANDBY:
162      case XF86_APM_USER_STANDBY:
163        if (ioctl( fd, APM_IOC_STANDBY, NULL ) == 0)
164            return PM_WAIT;  /* should we stop the Xserver in standby, too? */
165        else
166            return PM_NONE;
167      case XF86_APM_SYS_SUSPEND:
168      case XF86_APM_CRITICAL_SUSPEND:
169      case XF86_APM_USER_SUSPEND:
170	    xf86Msg(X_WARNING, "Got SUSPENDED\n");
171        if (ioctl( fd, APM_IOC_SUSPEND, NULL ) == 0)
172            return PM_CONTINUE;
173        else {
174	    xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_SUSPEND"
175		" %s\n", strerror(errno));
176            return PM_FAILED;
177	}
178      case XF86_APM_STANDBY_RESUME:
179      case XF86_APM_NORMAL_RESUME:
180      case XF86_APM_CRITICAL_RESUME:
181      case XF86_APM_STANDBY_FAILED:
182      case XF86_APM_SUSPEND_FAILED:
183	    xf86Msg(X_WARNING, "Got RESUME\n");
184        if (ioctl( fd, APM_IOC_RESUME, NULL ) == 0)
185            return PM_CONTINUE;
186        else {
187	    xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_RESUME"
188		" %s\n", strerror(errno));
189            return PM_FAILED;
190	}
191      default:
192	return PM_NONE;
193    }
194}
195
196PMClose
197xf86OSPMOpen(void)
198{
199    int fd;
200
201    if (APMihPtr || !xf86Info.pmFlag) {
202	return NULL;
203    }
204
205    if ((fd = open(APM_DEVICE, O_RDWR)) == -1) {
206    	if ((fd = open(APM_DEVICE1, O_RDWR)) == -1) {
207		return NULL;
208	}
209    }
210    xf86PMGetEventFromOs = sunPMGetEventFromOS;
211    xf86PMConfirmEventToOs = sunPMConfirmEventToOs;
212    APMihPtr = xf86AddGeneralHandler(fd, xf86HandlePMEvents, NULL);
213    return sunCloseAPM;
214}
215
216static void
217sunCloseAPM(void)
218{
219    int fd;
220
221    if (APMihPtr) {
222	fd = xf86RemoveGeneralHandler(APMihPtr);
223	close(fd);
224	APMihPtr = NULL;
225    }
226}
227