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.8 2004/08/13 19:12:52 asmodai 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 static void apm_display(int, int);
42 static void apm_enable(int, int);
43 static void apm_getinfo(int, apm_info_t);
44 static void apm_haltcpu(int, int);
45 static void apm_set_timer(int, int);
46 static void apm_standby(int);
47 static void apm_suspend(int);
48 static int bcd2int(int);
49 static int int2bcd(int);
50 static int is_true(const char *);
51 static void print_all_info(int, apm_info_t, int);
52 static void print_batt_life(u_int);
53 static void print_batt_stat(u_int);
54 static void print_batt_time(int);
55 static void usage(void);
57 int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
62 fprintf(stderr, "%s\n%s\n",
63 "usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
64 "[ -h enable ] [-r delta]",
70 * Return 1 for boolean true, and 0 for false, according to the
71 * interpretation of the string argument given.
74 is_true(const char *boolean)
79 val = strtoul(boolean, &endp, 0);
81 return (val != 0 ? 1 : 0);
82 if (strcasecmp(boolean, "true") == 0 ||
83 strcasecmp(boolean, "yes") == 0 ||
84 strcasecmp(boolean, "enable") == 0)
86 if (strcasecmp(boolean, "false") == 0 ||
87 strcasecmp(boolean, "no") == 0 ||
88 strcasecmp(boolean, "disable") == 0)
90 /* Well, I have no idea what the user wants, so... */
91 warnx("invalid boolean argument \"%s\"", boolean);
106 retval |= (i % 10) << base;
123 retval += (bcd & 0xf) * place;
133 if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
134 err(1, "ioctl(APMIO_SUSPEND)");
140 if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
141 err(1, "ioctl(APMIO_STANDBY)");
145 apm_getinfo(int fd, apm_info_t aip)
147 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
148 err(1, "ioctl(APMIO_GETINFO)");
152 apm_enable(int fd, int enable)
156 if (ioctl(fd, APMIO_ENABLE) == -1)
157 err(1, "ioctl(APMIO_ENABLE)");
159 if (ioctl(fd, APMIO_DISABLE) == -1)
160 err(1, "ioctl(APMIO_DISABLE)");
165 print_batt_life(u_int batt_life)
167 printf("Remaining battery life: ");
168 if (batt_life >= 255)
170 else if (batt_life <= 100)
171 printf("%d%%\n", batt_life);
173 printf("invalid value (0x%x)\n", batt_life);
177 print_batt_stat(u_int batt_stat)
179 const char *batt_msg[] = { "high", "low", "critical", "charging" };
181 printf("Battery Status: ");
182 if (batt_stat >= 255)
184 else if (batt_stat > 3)
185 printf("invalid value (0x%x)\n", batt_stat);
187 printf("%s\n", batt_msg[batt_stat]);
191 print_batt_time(int batt_time)
193 printf("Remaining battery time: ");
204 printf("%2d:%02d:%02d\n", h, m, s);
209 print_all_info(int fd, apm_info_t aip, int bioscall_available)
211 struct apm_bios_arg args;
213 char *line_msg[] = { "off-line", "on-line" };
215 printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
216 printf("APM Management: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
217 printf("AC Line status: ");
218 if (aip->ai_acline == 255)
220 else if (aip->ai_acline > 1)
221 printf("invalid value (0x%x)", aip->ai_acline);
223 printf("%s", line_msg[aip->ai_acline]);
225 print_batt_stat(aip->ai_batt_stat);
226 print_batt_life(aip->ai_batt_life);
227 print_batt_life(aip->ai_batt_life);
229 if (aip->ai_infoversion >= 1) {
230 printf("Number of batteries: ");
231 if (aip->ai_batteries == (u_int) -1)
235 struct apm_pwstatus aps;
237 printf("%d\n", aip->ai_batteries);
238 for (i = 0; i < aip->ai_batteries; ++i) {
239 bzero(&aps, sizeof(aps));
240 aps.ap_device = PMDV_BATT0 + i;
241 if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
243 printf("Battery %d:\n", i);
244 if (aps.ap_batt_flag != 255 &&
245 (aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
246 printf("not present\n");
251 print_batt_stat(aps.ap_batt_stat);
253 print_batt_life(aps.ap_batt_life);
255 print_batt_time(aps.ap_batt_time);
260 if (bioscall_available) {
262 * try to get the suspend timer
264 bzero(&args, sizeof(args));
265 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
266 args.ebx = PMDV_APMBIOS;
268 if (ioctl(fd, APMIO_BIOS, &args)) {
269 printf("Resume timer: unknown\n");
271 apmerr = APMERR(args.eax);
272 if (apmerr == 0x0d || apmerr == 0x86)
273 printf("Resume timer: disabled\n");
276 "Failed to get the resume timer: APM error0x%x\n",
280 * OK. We have the time (all bcd).
284 * xh(SI) - month (1-12)
285 * xl(SI) - day of month (1-31)
292 tm.tm_sec = bcd2int(xh(args.ecx));
293 tm.tm_min = bcd2int(xl(args.edx));
294 tm.tm_hour = bcd2int(xh(args.edx));
295 tm.tm_mday = bcd2int(xl(args.esi));
296 tm.tm_mon = bcd2int(xh(args.esi)) - 1;
297 tm.tm_year = bcd2int(args.edi) - 1900;
303 strftime(buf, sizeof(buf), "%s", &tm);
304 printf("Resume timer: %s\n", buf);
309 * Get the ring indicator resume state
311 bzero(&args, sizeof(args));
312 args.eax = (APM_BIOS) << 8 | APM_RESUMEONRING;
313 args.ebx = PMDV_APMBIOS;
315 if (ioctl(fd, APMIO_BIOS, &args) == 0) {
316 printf("Resume on ring indicator: %sabled\n",
317 args.ecx ? "en" : "dis");
321 if (aip->ai_infoversion >= 1) {
322 printf("APM Capacities:\n", aip->ai_capabilities);
323 if (aip->ai_capabilities == 0xff00)
324 printf("\tunknown\n");
325 if (aip->ai_capabilities & 0x01)
326 printf("\tglobal standby state\n");
327 if (aip->ai_capabilities & 0x02)
328 printf("\tglobal suspend state\n");
329 if (aip->ai_capabilities & 0x04)
330 printf("\tresume timer from standby\n");
331 if (aip->ai_capabilities & 0x08)
332 printf("\tresume timer from suspend\n");
333 if (aip->ai_capabilities & 0x10)
334 printf("\tRI resume from standby\n");
335 if (aip->ai_capabilities & 0x20)
336 printf("\tRI resume from suspend\n");
337 if (aip->ai_capabilities & 0x40)
338 printf("\tPCMCIA RI resume from standby\n");
339 if (aip->ai_capabilities & 0x80)
340 printf("\tPCMCIA RI resume from suspend\n");
346 * currently, it can turn off the display, but the display never comes
347 * back until the machine suspend/resumes :-).
350 apm_display(int fd, int newstate)
352 if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
353 err(1, "ioctl(APMIO_DISPLAY)");
357 apm_haltcpu(int fd, int enable)
361 if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
362 err(1, "ioctl(APMIO_HALTCPU)");
364 if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
365 err(1, "ioctl(APMIO_NOTHALTCPU)");
370 apm_set_timer(int fd, int delta)
374 struct apm_bios_arg args;
376 tmr = time(NULL) + delta;
378 tm = localtime(&tmr);
381 bzero(&args, sizeof(args));
382 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
383 args.ebx = PMDV_APMBIOS;
385 args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
386 args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
387 args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
388 args.edi = int2bcd(tm->tm_year + 1900);
392 if (ioctl(fd, APMIO_BIOS, &args)) {
393 err(1,"Set resume timer");
398 main(int argc, char *argv[])
401 int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
402 int display = -1, batt_life = 0, ac_status = 0, standby = 0;
403 int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
405 int bioscall_available = 0;
406 size_t cmos_wall_len = sizeof(cmos_wall);
408 if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
410 err(1, "sysctlbyname(machdep.wall_cmos_clock)");
411 if ((cmdname = strrchr(argv[0], '/')) != NULL)
416 if (strcmp(cmdname, "zzz") == 0) {
421 while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) {
432 display = is_true(optarg);
443 delta = atoi(optarg);
450 enable = is_true(optarg);
454 haltcpu = is_true(optarg);
476 if (haltcpu != -1 || enable != -1 || display != -1 || delta || sleep || standby) {
477 fd = open(APMDEV, O_RDWR);
478 bioscall_available = 1;
479 } else if ((fd = open(APMDEV, O_RDWR)) >= 0)
480 bioscall_available = 1;
482 fd = open(APMDEV, O_RDONLY);
484 err(1, "can't open %s", APMDEV);
486 apm_enable(fd, enable);
488 apm_haltcpu(fd, haltcpu);
490 apm_set_timer(fd, delta);
495 else if (delta == 0) {
496 struct apm_info info;
498 apm_getinfo(fd, &info);
500 print_all_info(fd, &info, bioscall_available);
502 printf("%d\n", info.ai_acline);
504 printf("%d\n", info.ai_batt_stat);
506 printf("%d\n", info.ai_batt_life);
508 printf("%d\n", info.ai_status);
510 printf("%d\n", info.ai_batt_time);
512 apm_display(fd, display);