summaryrefslogtreecommitdiff
path: root/src/sna/sna_acpi.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2013-09-06 15:07:43 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2013-09-06 16:06:06 +0100
commitf2ed1ac7b9807106405bbc2b18159a39c2d407d0 (patch)
treecd29111ecf445b6a7407ab033e2cd1b9b5ce29bb /src/sna/sna_acpi.c
parent56f532db2c9563c6d70da388f987f26530dcd38d (diff)
sna: Listen to ACPI events for power state notifications
When on-battery, we would prefer to use more power efficient operations. For example, the BCS is far more economical to more data around with, but it doesn't have quite the same throughput as the hungry RCS. (Not that there is any reason why, the BCS is supposed to run at full memory speed, unfortunately that is main memory speed and not the caches...) Note: that X already listens to acpid for video switch notifications, it would be useful if we could extend that interface to emit power notifications as well. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'src/sna/sna_acpi.c')
-rw-r--r--src/sna/sna_acpi.c187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/sna/sna_acpi.c b/src/sna/sna_acpi.c
new file mode 100644
index 00000000..ff7364c2
--- /dev/null
+++ b/src/sna/sna_acpi.c
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ * Chris Wilson <chris@chris-wilson.co.uk>
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "sna.h"
+
+#define ACPI_SOCKET "/var/run/acpid.socket"
+
+int sna_acpi_open(void)
+{
+ struct sockaddr_un addr;
+ int fd, ret;
+
+ DBG(("%s\n", __FUNCTION__));
+
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0)
+ return -1;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, ACPI_SOCKET);
+
+ ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+ if (ret < 0) {
+ close(fd);
+ return -1;
+ }
+
+ DBG(("%s: opened socket to APCI daemon, fd=%d\n", __FUNCTION__, fd));
+
+ return fd;
+}
+
+void _sna_acpi_wakeup(struct sna *sna)
+{
+ char *eol;
+ int n;
+
+ n = read(sna->acpi.fd,
+ sna->acpi.event + sna->acpi.offset,
+ sna->acpi.remain);
+ DBG(("%s: read %d bytes from acpid\n", __FUNCTION__, n));
+ if (n <= 0) {
+ /* We will get '0' if we run out of space whilst reading
+ * one event - that should never happen, so treat it as
+ * an error and give up.
+ */
+ if (n < 0)
+ n = errno;
+ switch (n) {
+ case EAGAIN:
+ case EINTR:
+ return;
+ }
+
+ DBG(("%s: error [%d], detaching from acpid\n", __FUNCTION__, n));
+
+ /* XXX reattach later? */
+ RemoveGeneralSocket(sna->acpi.fd);
+ sna_acpi_fini(sna);
+ return;
+ }
+
+ sna->acpi.event[sna->acpi.offset + n] = '\0';
+ sna->acpi.offset += n;
+ sna->acpi.remain -= n;
+
+ DBG(("%s: event string [%d]: '%s'\n", __FUNCTION__, sna->acpi.offset, sna->acpi.event));
+
+ do {
+ eol = strchr(sna->acpi.event, '\n');
+ if (eol == NULL)
+ return;
+
+ if (strncmp(sna->acpi.event, "ac_adapter", 10) == 0) {
+ char *space = sna->acpi.event;
+ int state = -1;
+
+ /* ac_adapter ACAD 00000080 00000001 */
+
+ space = strchr(space, ' ');
+ if (space)
+ space = strchr(space + 1, ' ');
+ if (space)
+ space = strchr(space + 1, ' ');
+ if (space)
+ state = atoi(space + 1);
+
+ DBG(("%s: ac_adapter event new state=%d\n", __FUNCTION__, state));
+ if (state)
+ sna->flags &= ~SNA_POWERSAVE;
+ else
+ sna->flags |= SNA_POWERSAVE;
+ }
+
+ n = (sna->acpi.event + sna->acpi.offset) - ++eol;
+ memmove(sna->acpi.event, eol, n+1);
+ sna->acpi.offset = n;
+ sna->acpi.remain = sizeof(sna->acpi.event) - 1 - n;
+ } while (n);
+}
+
+static int read_state(const char *path)
+{
+ char buf[80];
+ int i, fd;
+
+ fd = open(path, 0);
+ if (fd < 0)
+ return -1;
+
+ i = read(fd, buf, sizeof(buf)-1);
+ if (i > 0) {
+ buf[i] = '\0';
+ i = atoi(buf);
+ }
+
+ close(fd);
+ return i;
+}
+
+void sna_acpi_init(struct sna *sna)
+{
+ if (sna->acpi.fd < 0)
+ return;
+
+ if (sna->flags & SNA_PERFORMANCE)
+ return;
+
+ DBG(("%s: attaching to acpid\n", __FUNCTION__));
+
+ AddGeneralSocket(sna->acpi.fd);
+ sna->acpi.remain = sizeof(sna->acpi.event) - 1;
+ sna->acpi.offset = 0;
+
+ /* Read initial states */
+ if (read_state("/sys/class/power_supply/ACAD/online") == 0) {
+ DBG(("%s: AC adapter is currently offline\n", __FUNCTION__));
+ sna->flags |= SNA_POWERSAVE;
+ }
+}
+
+void sna_acpi_fini(struct sna *sna)
+{
+ if (sna->acpi.fd < 0)
+ return;
+
+ close(sna->acpi.fd);
+ sna->acpi.fd = -1;
+
+ sna->flags &= ~SNA_POWERSAVE;
+}