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)
17 static const char rcsid[] =
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
26 #include <machine/apm_bios.h>
33 #include <sys/ioctl.h>
37 #define APMDEV "/dev/apm"
39 #define xh(a) (((a) & 0xff00) >> 8)
40 #define xl(a) ((a) & 0xff)
41 #define APMERR(a) xh(a)
43 int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
48 fprintf(stderr, "%s\n%s\n",
49 "usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
50 "[ -h enable ] [-r delta]",
56 * Return 1 for boolean true, and 0 for false, according to the
57 * interpretation of the string argument given.
60 is_true(const char *boolean) {
64 val = strtoul(boolean, &endp, 0);
66 return (val != 0 ? 1 : 0);
67 if (strcasecmp(boolean, "true") == 0 ||
68 strcasecmp(boolean, "yes") == 0 ||
69 strcasecmp(boolean, "enable") == 0)
71 if (strcasecmp(boolean, "false") == 0 ||
72 strcasecmp(boolean, "no") == 0 ||
73 strcasecmp(boolean, "disable") == 0)
75 /* Well, I have no idea what the user wants, so... */
76 warnx("invalid boolean argument \"%s\"", boolean);
91 retval |= (i % 10) << base;
108 retval += (bcd & 0xf) * place;
118 if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
119 err(1, "ioctl(APMIO_SUSPEND)");
125 if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
126 err(1, "ioctl(APMIO_STANDBY)");
130 apm_getinfo(int fd, apm_info_t aip)
132 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
133 err(1, "ioctl(APMIO_GETINFO)");
137 apm_enable(int fd, int enable) {
140 if (ioctl(fd, APMIO_ENABLE) == -1)
141 err(1, "ioctl(APMIO_ENABLE)");
143 if (ioctl(fd, APMIO_DISABLE) == -1)
144 err(1, "ioctl(APMIO_DISABLE)");
149 print_all_info(int fd, apm_info_t aip, int bioscall_available)
151 struct apm_bios_arg args;
154 printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
155 printf("APM Managment: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
156 printf("AC Line status: ");
157 if (aip->ai_acline == 255)
159 else if (aip->ai_acline > 1)
160 printf("invalid value (0x%x)", aip->ai_acline);
162 char *messages[] = { "off-line", "on-line" };
163 printf("%s", messages[aip->ai_acline]);
166 printf("Battery status: ");
167 if (aip->ai_batt_stat == 255)
169 else if (aip->ai_batt_stat > 3)
170 printf("invalid value (0x%x)", aip->ai_batt_stat);
172 char *messages[] = { "high", "low", "critical", "charging" };
173 printf("%s", messages[aip->ai_batt_stat]);
176 printf("Remaining battery life: ");
177 if (aip->ai_batt_life == 255)
179 else if (aip->ai_batt_life <= 100)
180 printf("%d%%\n", aip->ai_batt_life);
182 printf("invalid value (0x%x)\n", aip->ai_batt_life);
183 printf("Remaining battery time: ");
184 if (aip->ai_batt_time == -1)
189 t = aip->ai_batt_time;
195 printf("%2d:%02d:%02d\n", h, m, s);
197 if (aip->ai_infoversion >= 1) {
198 printf("Number of batteries: ");
199 if (aip->ai_batteries == (u_int) -1)
203 struct apm_pwstatus aps;
205 printf("%d\n", aip->ai_batteries);
206 for (i = 0; i < aip->ai_batteries; ++i) {
207 bzero(&aps, sizeof(aps));
208 aps.ap_device = PMDV_BATT0 + i;
209 if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
211 printf("Battery %d:\n", i);
212 printf("\tBattery status: ");
213 if (aps.ap_batt_flag != 255 &&
214 (aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
215 printf("not present\n");
218 if (aps.ap_batt_stat == 255)
220 else if (aps.ap_batt_stat > 3)
221 printf("invalid value (0x%x)\n",
224 char *messages[] = { "high",
229 messages[aps.ap_batt_stat]);
231 printf("\tRemaining battery life: ");
232 if (aps.ap_batt_life == 255)
234 else if (aps.ap_batt_life <= 100)
235 printf("%d%%\n", aps.ap_batt_life);
237 printf("invalid value (0x%x)\n",
239 printf("\tRemaining battery time: ");
240 if (aps.ap_batt_time == -1)
245 t = aps.ap_batt_time;
251 printf("%2d:%02d:%02d\n", h, m, s);
257 if (bioscall_available) {
259 * try to get the suspend timer
261 bzero(&args, sizeof(args));
262 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
263 args.ebx = PMDV_APMBIOS;
265 if (ioctl(fd, APMIO_BIOS, &args)) {
266 printf("Resume timer: unknown\n");
268 apmerr = APMERR(args.eax);
269 if (apmerr == 0x0d || apmerr == 0x86)
270 printf("Resume timer: disabled\n");
273 "Failed to get the resume timer: APM error0x%x\n",
277 * OK. We have the time (all bcd).
281 * xh(SI) - month (1-12)
282 * xl(SI) - day of month (1-31)
289 tm.tm_sec = bcd2int(xh(args.ecx));
290 tm.tm_min = bcd2int(xl(args.edx));
291 tm.tm_hour = bcd2int(xh(args.edx));
292 tm.tm_mday = bcd2int(xl(args.esi));
293 tm.tm_mon = bcd2int(xh(args.esi)) - 1;
294 tm.tm_year = bcd2int(args.edi) - 1900;
300 strftime(buf, sizeof(buf), "%c", &tm);
301 printf("Resume timer: %s\n", buf);
306 * Get the ring indicator resume state
308 bzero(&args, sizeof(args));
309 args.eax = (APM_BIOS) << 8 | APM_RESUMEONRING;
310 args.ebx = PMDV_APMBIOS;
312 if (ioctl(fd, APMIO_BIOS, &args) == 0) {
313 printf("Resume on ring indicator: %sabled\n",
314 args.ecx ? "en" : "dis");
318 if (aip->ai_infoversion >= 1) {
319 printf("APM Capacities:\n", aip->ai_capabilities);
320 if (aip->ai_capabilities == 0xff00)
321 printf("\tunknown\n");
322 if (aip->ai_capabilities & 0x01)
323 printf("\tglobal standby state\n");
324 if (aip->ai_capabilities & 0x02)
325 printf("\tglobal suspend state\n");
326 if (aip->ai_capabilities & 0x04)
327 printf("\tresume timer from standby\n");
328 if (aip->ai_capabilities & 0x08)
329 printf("\tresume timer from suspend\n");
330 if (aip->ai_capabilities & 0x10)
331 printf("\tRI resume from standby\n");
332 if (aip->ai_capabilities & 0x20)
333 printf("\tRI resume from suspend\n");
334 if (aip->ai_capabilities & 0x40)
335 printf("\tPCMCIA RI resume from standby\n");
336 if (aip->ai_capabilities & 0x80)
337 printf("\tPCMCIA RI resume from suspend\n");
343 * currently, it can turn off the display, but the display never comes
344 * back until the machine suspend/resumes :-).
347 apm_display(int fd, int newstate)
349 if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
350 err(1, "ioctl(APMIO_DISPLAY)");
354 apm_haltcpu(int fd, int enable) {
357 if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
358 err(1, "ioctl(APMIO_HALTCPU)");
360 if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
361 err(1, "ioctl(APMIO_NOTHALTCPU)");
366 apm_set_timer(int fd, int delta)
370 struct apm_bios_arg args;
372 tmr = time(NULL) + delta;
374 tm = localtime(&tmr);
377 bzero(&args, sizeof(args));
378 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
379 args.ebx = PMDV_APMBIOS;
381 args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
382 args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
383 args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
384 args.edi = int2bcd(tm->tm_year + 1900);
388 if (ioctl(fd, APMIO_BIOS, &args)) {
389 err(1,"Set resume timer");
394 main(int argc, char *argv[])
397 int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
398 int display = -1, batt_life = 0, ac_status = 0, standby = 0;
399 int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
401 int bioscall_available = 0;
402 size_t cmos_wall_len = sizeof(cmos_wall);
404 if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
406 err(1, "sysctlbyname(machdep.wall_cmos_clock)");
407 if ((cmdname = strrchr(argv[0], '/')) != NULL)
412 if (strcmp(cmdname, "zzz") == 0) {
417 while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) {
428 display = is_true(optarg);
439 delta = atoi(optarg);
446 enable = is_true(optarg);
449 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);