2 * 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)
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
24 #include <machine/apm_bios.h>
33 #define APMDEV "/dev/apm"
35 #define APM_UNKNOWN 255
37 #define xh(a) (((a) & 0xff00) >> 8)
38 #define xl(a) ((a) & 0xff)
39 #define APMERR(a) xh(a)
41 static int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
47 "usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
48 "[ -h enable ] [-r delta]\n");
53 * Return 1 for boolean true, and 0 for false, according to the
54 * interpretation of the string argument given.
57 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);
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_batt_time(int batt_time)
151 printf("Remaining battery time: ");
162 printf("%2d:%02d:%02d\n", h, m, s);
167 print_batt_life(u_int batt_life)
169 printf("Remaining battery life: ");
170 if (batt_life == APM_UNKNOWN)
172 else if (batt_life <= 100)
173 printf("%d%%\n", batt_life);
175 printf("invalid value (0x%x)\n", batt_life);
179 print_batt_stat(u_int batt_stat)
181 const char *batt_msg[] = { "high", "low", "critical", "charging" };
183 printf("Battery Status: ");
184 if (batt_stat == APM_UNKNOWN)
186 else if (batt_stat > 3)
187 printf("invalid value (0x%x)\n", batt_stat);
189 printf("%s\n", batt_msg[batt_stat]);
193 print_all_info(int fd, apm_info_t aip, int bioscall_available)
195 struct apm_bios_arg args;
197 const char *line_msg[] = { "off-line", "on-line" , "backup power"};
199 printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
200 printf("APM Management: %s\n", aip->ai_status ? "Enabled" : "Disabled");
201 printf("AC Line status: ");
202 if (aip->ai_acline == APM_UNKNOWN)
204 else if (aip->ai_acline > 2)
205 printf("invalid value (0x%x)\n", aip->ai_acline);
207 printf("%s\n", line_msg[aip->ai_acline]);
209 print_batt_stat(aip->ai_batt_stat);
210 print_batt_life(aip->ai_batt_life);
211 print_batt_time(aip->ai_batt_time);
213 if (aip->ai_infoversion >= 1) {
214 printf("Number of batteries: ");
215 if (aip->ai_batteries == ~0U)
219 struct apm_pwstatus aps;
221 printf("%d\n", aip->ai_batteries);
222 for (i = 0; i < aip->ai_batteries; ++i) {
223 bzero(&aps, sizeof(aps));
224 aps.ap_device = PMDV_BATT0 + i;
225 if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
227 printf("Battery %d:\n", i);
228 if (aps.ap_batt_flag & APM_BATT_NOT_PRESENT) {
229 printf("not present\n");
233 print_batt_stat(aps.ap_batt_stat);
235 print_batt_life(aps.ap_batt_life);
237 print_batt_time(aps.ap_batt_time);
242 if (bioscall_available) {
244 * try to get the suspend timer
246 bzero(&args, sizeof(args));
247 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
248 args.ebx = PMDV_APMBIOS;
250 if (ioctl(fd, APMIO_BIOS, &args)) {
251 printf("Resume timer: unknown\n");
253 apmerr = APMERR(args.eax);
254 if (apmerr == 0x0d || apmerr == 0x86)
255 printf("Resume timer: disabled\n");
258 "failed to get the resume timer: APM error0x%x", apmerr);
261 * OK. We have the time (all bcd).
265 * xh(SI) - month (1-12)
266 * xl(SI) - day of month (1-31)
273 tm.tm_sec = bcd2int(xh(args.ecx));
274 tm.tm_min = bcd2int(xl(args.edx));
275 tm.tm_hour = bcd2int(xh(args.edx));
276 tm.tm_mday = bcd2int(xl(args.esi));
277 tm.tm_mon = bcd2int(xh(args.esi)) - 1;
278 tm.tm_year = bcd2int(args.edi) - 1900;
285 strftime(buf, sizeof(buf), "%c", &tm);
286 printf("Resume timer: %s\n", buf);
288 printf("Resume timer: unknown\n");
293 * Get the ring indicator resume state
295 bzero(&args, sizeof(args));
296 args.eax = (APM_BIOS) << 8 | APM_RESUMEONRING;
297 args.ebx = PMDV_APMBIOS;
299 if (ioctl(fd, APMIO_BIOS, &args) == 0) {
300 printf("Resume on ring indicator: %sabled\n",
301 args.ecx ? "en" : "dis");
305 if (aip->ai_infoversion >= 1) {
306 if (aip->ai_capabilities == 0xff00)
308 printf("APM Capabilities:\n");
309 if (aip->ai_capabilities & 0x01)
310 printf("\tglobal standby state\n");
311 if (aip->ai_capabilities & 0x02)
312 printf("\tglobal suspend state\n");
313 if (aip->ai_capabilities & 0x04)
314 printf("\tresume timer from standby\n");
315 if (aip->ai_capabilities & 0x08)
316 printf("\tresume timer from suspend\n");
317 if (aip->ai_capabilities & 0x10)
318 printf("\tRI resume from standby\n");
319 if (aip->ai_capabilities & 0x20)
320 printf("\tRI resume from suspend\n");
321 if (aip->ai_capabilities & 0x40)
322 printf("\tPCMCIA RI resume from standby\n");
323 if (aip->ai_capabilities & 0x80)
324 printf("\tPCMCIA RI resume from suspend\n");
330 * currently, it can turn off the display, but the display never comes
331 * back until the machine suspend/resumes :-).
334 apm_display(int fd, int newstate)
336 if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
337 err(1, "ioctl(APMIO_DISPLAY)");
341 apm_haltcpu(int fd, int enable)
344 if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
345 err(1, "ioctl(APMIO_HALTCPU)");
347 if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
348 err(1, "ioctl(APMIO_NOTHALTCPU)");
353 apm_set_timer(int fd, int delta)
357 struct apm_bios_arg args;
359 tmr = time(NULL) + delta;
361 tm = localtime(&tmr);
364 bzero(&args, sizeof(args));
365 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
366 args.ebx = PMDV_APMBIOS;
368 args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
369 args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
370 args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
371 args.edi = int2bcd(tm->tm_year + 1900);
375 if (ioctl(fd, APMIO_BIOS, &args)) {
376 err(1,"set resume timer");
381 main(int argc, char *argv[])
384 int dosleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
385 int display = -1, batt_life = 0, ac_status = 0, standby = 0;
386 int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
387 int bioscall_available = 0;
388 size_t cmos_wall_len = sizeof(cmos_wall);
390 if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
392 err(1, "sysctlbyname(machdep.wall_cmos_clock)");
394 while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) {
405 display = is_true(optarg);
416 delta = atoi(optarg);
423 enable = is_true(optarg);
427 haltcpu = is_true(optarg);
449 if (haltcpu != -1 || enable != -1 || display != -1 || delta || dosleep
451 fd = open(APMDEV, O_RDWR);
452 bioscall_available = 1;
453 } else if ((fd = open(APMDEV, O_RDWR)) >= 0)
454 bioscall_available = 1;
456 fd = open(APMDEV, O_RDONLY);
458 err(1, "can't open %s", APMDEV);
460 apm_enable(fd, enable);
462 apm_haltcpu(fd, haltcpu);
464 apm_set_timer(fd, delta);
469 else if (delta == 0) {
470 struct apm_info info;
472 apm_getinfo(fd, &info);
474 print_all_info(fd, &info, bioscall_available);
476 printf("%d\n", info.ai_acline);
478 printf("%d\n", info.ai_batt_stat);
480 printf("%d\n", info.ai_batt_life);
482 printf("%d\n", info.ai_status);
484 printf("%d\n", info.ai_batt_time);
486 apm_display(fd, display);