summaryrefslogtreecommitdiff
path: root/battstat/acpi-freebsd.c
blob: afa66bdbe87d8a1948cc25e3491a0223d6a3752c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* battstat        A MATE battery meter for laptops.
 * Copyright (C) 2000 by Jörgen Pehrson <jp@spektr.eu.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 $Id$
 */

/*
 * ACPI battery functions for FreeBSD >= 5.2.
 * September 2004 by Joe Marcus Clarke <marcus@FreeBSD.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)

#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <machine/apm_bios.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <glib.h>

#include "acpi-freebsd.h"

#ifdef HAVE_ACPIIO

#include <dev/acpica/acpiio.h>

static gboolean
update_ac_info(struct acpi_info * acpiinfo)
{
  int acline;
  size_t len = sizeof(acline);

  acpiinfo->ac_online = FALSE;

  if (sysctlbyname(ACPI_ACLINE, &acline, &len, NULL, 0) == -1) {
    return FALSE;
  }

  acpiinfo->ac_online = acline ? TRUE : FALSE;

  return TRUE;
}

static gboolean
update_battery_info(struct acpi_info * acpiinfo)
{
  union acpi_battery_ioctl_arg battio;
  int i;

  /* We really don't have to do this here.  All of the relevant battery
   * info can be obtained through sysctl.  However, one day, the rate
   * may be useful to get time left to full charge.
   */

  for(i = BATT_MIN; i < BATT_MAX; i++) {
    battio.unit = i;
    if (ioctl(acpiinfo->acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1) {
      continue;
    }

    acpiinfo->max_capacity += battio.bif.lfcap;
    acpiinfo->low_capacity += battio.bif.wcap;
    acpiinfo->critical_capacity += battio.bif.lcap;
  }

  return TRUE;
}

gboolean
acpi_freebsd_init(struct acpi_info * acpiinfo)
{
  int acpi_fd;

  g_assert(acpiinfo);

  acpi_fd = open(ACPIDEV, O_RDONLY);
  if (acpi_fd >= 0) {
    acpiinfo->acpifd = acpi_fd;
  }
  else {
    acpiinfo->acpifd = -1;
    return FALSE;
  }

  update_battery_info(acpiinfo);
  update_ac_info(acpiinfo);

  return TRUE;
}

void
acpi_freebsd_cleanup(struct acpi_info * acpiinfo)
{
  g_assert(acpiinfo);

  if (acpiinfo->acpifd >= 0) {
    close(acpiinfo->acpifd);
    acpiinfo->acpifd = -1;
  }
}

/* XXX This is a hack since user-land applications can't get ACPI events yet.
 * Devd provides this (or supposedly provides this), but you need to be
 * root to access devd.
 */
gboolean
acpi_process_event(struct acpi_info * acpiinfo)
{
  g_assert(acpiinfo);

  update_ac_info(acpiinfo);
  update_battery_info(acpiinfo);

  return TRUE;
}

gboolean
acpi_freebsd_read(struct apm_info *apminfo, struct acpi_info * acpiinfo)
{
  int time;
  int life;
  int acline;
  int state;
  size_t len;
  int rate;
  int remain;
  union acpi_battery_ioctl_arg battio;
  gboolean charging;
  int i;

  g_assert(acpiinfo);

  charging = FALSE;

  for(i = BATT_MIN; i < BATT_MAX; i++) {
    battio.unit = i;
    if (ioctl(acpiinfo->acpifd, ACPIIO_CMBAT_GET_BST, &battio) == -1) {
      continue;
    }

    remain += battio.bst.cap;
    rate += battio.bst.rate;
  }

  len = sizeof(time);
  if (sysctlbyname(ACPI_TIME, &time, &len, NULL, 0) == -1) {
    return FALSE;
  }

  len = sizeof(life);
  if (sysctlbyname(ACPI_LIFE, &life, &len, NULL, 0) == -1) {
    return FALSE;
  }

  len = sizeof(state);
  if (sysctlbyname(ACPI_STATE, &state, &len, NULL, 0) == -1) {
    return FALSE;
  }

  apminfo->ai_acline = acpiinfo->ac_online ? 1 : 0;
  if (state & ACPI_BATT_STAT_CHARGING) {
    apminfo->ai_batt_stat = 3;
    charging = TRUE;
  }
  else if (state & ACPI_BATT_STAT_CRITICAL) {
    /* Add a special check here since FreeBSD's ACPI interface will tell us
     * when the battery is critical.
     */
    apminfo->ai_batt_stat = 2;
  }
  else {
    apminfo->ai_batt_stat = remain < acpiinfo->low_capacity ? 1 : remain < acpiinfo->critical_capacity ? 2 : 0;
  }
  apminfo->ai_batt_life = life;
  if (!charging) {
    apminfo->ai_batt_time = time;
  }
  else if (charging && rate > 0) {
    apminfo->ai_batt_time = (int) ((acpiinfo->max_capacity-remain)/(float)rate);
  }
  else
    apminfo->ai_batt_time = -1;

  return TRUE;
}

#else /* !defined(HAVE_ACPIIO) */

gboolean
acpi_freebsd_init(struct acpi_info * acpiinfo)
{
  return FALSE;
}

gboolean
acpi_process_event(struct acpi_info * acpiinfo)
{
  return FALSE;
}

gboolean
acpi_freebsd_read(struct apm_info *apminfo, struct acpi_info * acpiinfo)
{
  return FALSE;
}

void
acpi_freebsd_cleanup(struct acpi_info * acpiinfo)
{
}

#endif /* defined(HAVE_ACPIIO) */

#endif /* defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */