2 * apm / zzz APM BIOS utility for FreeBSD
4 * Copyright (C) 1994-1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
6 * This software may be used, modified, copied, distributed, and sold,
7 * in both source and binary form provided that the above copyright and
8 * these terms are retained. Under no circumstances is the author
9 * responsible for the proper functioning of this software, nor does
10 * the author assume any responsibility for damages incurred with its
13 * Sep., 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
15 * $FreeBSD: src/usr.sbin/apm/apm.c,v 1.22.2.6 2003/04/29 08:53:04 maxim Exp $
16 * $DragonFly: src/usr.sbin/apm/apm.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
24 #include <machine/apm_bios.h>
31 #include <sys/ioctl.h>
35 #define APMDEV "/dev/apm"
37 #define xh(a) (((a) & 0xff00) >> 8)
38 #define xl(a) ((a) & 0xff)
39 #define APMERR(a) xh(a)
41 int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
46 fprintf(stderr, "%s\n%s\n",
47 "usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
48 "[ -h enable ] [-r delta]",
54 * Return 1 for boolean true, and 0 for false, according to the
55 * interpretation of the string argument given.
58 is_true(const char *boolean) {
62 val = strtoul(boolean, &endp, 0);
64 return (val != 0 ? 1 : 0);
65 if (strcasecmp(boolean, "true") == 0 ||
66 strcasecmp(boolean, "yes") == 0 ||
67 strcasecmp(boolean, "enable") == 0)
69 if (strcasecmp(boolean, "false") == 0 ||
70 strcasecmp(boolean, "no") == 0 ||
71 strcasecmp(boolean, "disable") == 0)
73 /* Well, I have no idea what the user wants, so... */
74 warnx("invalid boolean argument \"%s\"", boolean);
89 retval |= (i % 10) << base;
106 retval += (bcd & 0xf) * place;
116 if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
117 err(1, "ioctl(APMIO_SUSPEND)");
123 if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
124 err(1, "ioctl(APMIO_STANDBY)");
128 apm_getinfo(int fd, apm_info_t aip)
130 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
131 err(1, "ioctl(APMIO_GETINFO)");
135 apm_enable(int fd, int enable) {
138 if (ioctl(fd, APMIO_ENABLE) == -1)
139 err(1, "ioctl(APMIO_ENABLE)");
141 if (ioctl(fd, APMIO_DISABLE) == -1)
142 err(1, "ioctl(APMIO_DISABLE)");
147 print_all_info(int fd, apm_info_t aip, int bioscall_available)
149 struct apm_bios_arg args;
152 printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
153 printf("APM Management: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
154 printf("AC Line status: ");
155 if (aip->ai_acline == 255)
157 else if (aip->ai_acline > 1)
158 printf("invalid value (0x%x)", aip->ai_acline);
160 char *messages[] = { "off-line", "on-line" };
161 printf("%s", messages[aip->ai_acline]);
164 printf("Battery status: ");
165 if (aip->ai_batt_stat == 255)
167 else if (aip->ai_batt_stat > 3)
168 printf("invalid value (0x%x)", aip->ai_batt_stat);
170 char *messages[] = { "high", "low", "critical", "charging" };
171 printf("%s", messages[aip->ai_batt_stat]);
174 printf("Remaining battery life: ");
175 if (aip->ai_batt_life == 255)
177 else if (aip->ai_batt_life <= 100)
178 printf("%d%%\n", aip->ai_batt_life);
180 printf("invalid value (0x%x)\n", aip->ai_batt_life);
181 printf("Remaining battery time: ");
182 if (aip->ai_batt_time == -1)
187 t = aip->ai_batt_time;
193 printf("%2d:%02d:%02d\n", h, m, s);
195 if (aip->ai_infoversion >= 1) {
196 printf("Number of batteries: ");
197 if (aip->ai_batteries == (u_int) -1)
201 struct apm_pwstatus aps;
203 printf("%d\n", aip->ai_batteries);
204 for (i = 0; i < aip->ai_batteries; ++i) {
205 bzero(&aps, sizeof(aps));
206 aps.ap_device = PMDV_BATT0 + i;
207 if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
209 printf("Battery %d:\n", i);
210 printf("\tBattery status: ");
211 if (aps.ap_batt_flag != 255 &&
212 (aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
213 printf("not present\n");
216 if (aps.ap_batt_stat == 255)
218 else if (aps.ap_batt_stat > 3)
219 printf("invalid value (0x%x)\n",
222 char *messages[] = { "high",
227 messages[aps.ap_batt_stat]);
229 printf("\tRemaining battery life: ");
230 if (aps.ap_batt_life == 255)
232 else if (aps.ap_batt_life <= 100)
233 printf("%d%%\n", aps.ap_batt_life);
235 printf("invalid value (0x%x)\n",
237 printf("\tRemaining battery time: ");
238 if (aps.ap_batt_time == -1)
243 t = aps.ap_batt_time;
249 printf("%2d:%02d:%02d\n", h, m, s);
255 if (bioscall_available) {
257 * try to get the suspend timer
259 bzero(&args, sizeof(args));
260 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
261 args.ebx = PMDV_APMBIOS;
263 if (ioctl(fd, APMIO_BIOS, &args)) {
264 printf("Resume timer: unknown\n");
266 apmerr = APMERR(args.eax);
267 if (apmerr == 0x0d || apmerr == 0x86)
268 printf("Resume timer: disabled\n");
271 "Failed to get the resume timer: APM error0x%x\n",
275 * OK. We have the time (all bcd).
279 * xh(SI) - month (1-12)
280 * xl(SI) - day of month (1-31)
287 tm.tm_sec = bcd2int(xh(args.ecx));
288 tm.tm_min = bcd2int(xl(args.edx));
289 tm.tm_hour = bcd2int(xh(args.edx));
290 tm.tm_mday = bcd2int(xl(args.esi));
291 tm.tm_mon = bcd2int(xh(args.esi)) - 1;
292 tm.tm_year = bcd2int(args.edi) - 1900;
298 strftime(buf, sizeof(buf), "%c", &tm);
299 printf("Resume timer: %s\n", buf);
304 * Get the ring indicator resume state
306 bzero(&args, sizeof(args));
307 args.eax = (APM_BIOS) << 8 | APM_RESUMEONRING;
308 args.ebx = PMDV_APMBIOS;
310 if (ioctl(fd, APMIO_BIOS, &args) == 0) {
311 printf("Resume on ring indicator: %sabled\n",
312 args.ecx ? "en" : "dis");
316 if (aip->ai_infoversion >= 1) {
317 printf("APM Capacities:\n", aip->ai_capabilities);
318 if (aip->ai_capabilities == 0xff00)
319 printf("\tunknown\n");
320 if (aip->ai_capabilities & 0x01)
321 printf("\tglobal standby state\n");
322 if (aip->ai_capabilities & 0x02)
323 printf("\tglobal suspend state\n");
324 if (aip->ai_capabilities & 0x04)
325 printf("\tresume timer from standby\n");
326 if (aip->ai_capabilities & 0x08)
327 printf("\tresume timer from suspend\n");
328 if (aip->ai_capabilities & 0x10)
329 printf("\tRI resume from standby\n");
330 if (aip->ai_capabilities & 0x20)
331 printf("\tRI resume from suspend\n");
332 if (aip->ai_capabilities & 0x40)
333 printf("\tPCMCIA RI resume from standby\n");
334 if (aip->ai_capabilities & 0x80)
335 printf("\tPCMCIA RI resume from suspend\n");
341 * currently, it can turn off the display, but the display never comes
342 * back until the machine suspend/resumes :-).
345 apm_display(int fd, int newstate)
347 if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
348 err(1, "ioctl(APMIO_DISPLAY)");
352 apm_haltcpu(int fd, int enable) {
355 if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
356 err(1, "ioctl(APMIO_HALTCPU)");
358 if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
359 err(1, "ioctl(APMIO_NOTHALTCPU)");
364 apm_set_timer(int fd, int delta)
368 struct apm_bios_arg args;
370 tmr = time(NULL) + delta;
372 tm = localtime(&tmr);
375 bzero(&args, sizeof(args));
376 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
377 args.ebx = PMDV_APMBIOS;
379 args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
380 args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
381 args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
382 args.edi = int2bcd(tm->tm_year + 1900);
386 if (ioctl(fd, APMIO_BIOS, &args)) {
387 err(1,"Set resume timer");
392 main(int argc, char *argv[])
395 int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
396 int display = -1, batt_life = 0, ac_status = 0, standby = 0;
397 int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
399 int bioscall_available = 0;
400 size_t cmos_wall_len = sizeof(cmos_wall);
402 if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
404 err(1, "sysctlbyname(machdep.wall_cmos_clock)");
405 if ((cmdname = strrchr(argv[0], '/')) != NULL)
410 if (strcmp(cmdname, "zzz") == 0) {
415 while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) {
426 display = is_true(optarg);
437 delta = atoi(optarg);
444 enable = is_true(optarg);
448 haltcpu = is_true(optarg);
471 if (haltcpu != -1 || enable != -1 || display != -1 || delta || sleep || standby) {
472 fd = open(APMDEV, O_RDWR);
473 bioscall_available = 1;
474 } else if ((fd = open(APMDEV, O_RDWR)) >= 0)
475 bioscall_available = 1;
477 fd = open(APMDEV, O_RDONLY);
479 err(1, "can't open %s", APMDEV);
481 apm_enable(fd, enable);
483 apm_haltcpu(fd, haltcpu);
485 apm_set_timer(fd, delta);
490 else if (delta == 0) {
491 struct apm_info info;
493 apm_getinfo(fd, &info);
495 print_all_info(fd, &info, bioscall_available);
497 printf("%d\n", info.ai_acline);
499 printf("%d\n", info.ai_batt_stat);
501 printf("%d\n", info.ai_batt_life);
503 printf("%d\n", info.ai_status);
505 printf("%d\n", info.ai_batt_time);
507 apm_display(fd, display);