ed4cc05788afa7a155ad511676f2e2c191208b8d
[dragonfly.git] / usr.sbin / apm / apm.c
1 /*
2  * apm / zzz    APM BIOS utility for FreeBSD
3  *
4  * Copyright (C) 1994-1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
5  *
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
11  * use.
12  *
13  * Sep., 1994   Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
14  *
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.7 2004/08/13 19:01:18 asmodai Exp $
17  */
18
19 #include <sys/file.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23
24 #include <machine/apm_bios.h>
25
26 #include <err.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/file.h>
31 #include <sys/ioctl.h>
32 #include <time.h>
33 #include <unistd.h>
34
35 #define APMDEV  "/dev/apm"
36
37 #define xh(a)   (((a) & 0xff00) >> 8)
38 #define xl(a)   ((a) & 0xff)
39 #define APMERR(a) xh(a)
40
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);
56
57 int cmos_wall = 0;      /* True when wall time is in cmos clock, else UTC */
58
59 static void
60 usage(void)
61 {
62         fprintf(stderr, "%s\n%s\n",
63                 "usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
64                 "[ -h enable ] [-r delta]",
65                 "       zzz");
66         exit(EXIT_FAILURE);
67 }
68
69 /*
70  * Return 1 for boolean true, and 0 for false, according to the
71  * interpretation of the string argument given.
72  */
73 static int
74 is_true(const char *boolean) {
75         char *endp;
76         long val;
77
78         val = strtoul(boolean, &endp, 0);
79         if (*endp == '\0')
80                 return (val != 0 ? 1 : 0);
81         if (strcasecmp(boolean, "true") == 0 ||
82             strcasecmp(boolean, "yes") == 0 ||
83             strcasecmp(boolean, "enable") == 0)
84                 return (1);
85         if (strcasecmp(boolean, "false") == 0 ||
86             strcasecmp(boolean, "no") == 0 ||
87             strcasecmp(boolean, "disable") == 0)
88                 return (0);
89         /* Well, I have no idea what the user wants, so... */
90         warnx("invalid boolean argument \"%s\"", boolean);
91         usage();
92         /* NOTREACHED */
93 }
94
95 static int
96 int2bcd(int i)
97 {
98         int retval = 0;
99         int base = 0;
100
101         if (i >= 10000)
102                 return -1;
103     
104         while (i) {
105                 retval |= (i % 10) << base;
106                 i /= 10;
107                 base += 4;
108         }
109         return retval;
110 }
111
112 static int
113 bcd2int(int bcd)
114 {
115         int retval = 0;
116         int place = 1;
117
118         if (bcd > 0x9999)
119                 return -1;
120
121         while (bcd) {
122                 retval += (bcd & 0xf) * place;
123                 bcd >>= 4;
124                 place *= 10;
125         }
126         return retval;
127 }
128
129 static void 
130 apm_suspend(int fd)
131 {
132         if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
133                 err(1, "ioctl(APMIO_SUSPEND)");
134 }
135
136 static void 
137 apm_standby(int fd)
138 {
139         if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
140                 err(1, "ioctl(APMIO_STANDBY)");
141 }
142
143 static void 
144 apm_getinfo(int fd, apm_info_t aip)
145 {
146         if (ioctl(fd, APMIO_GETINFO, aip) == -1)
147                 err(1, "ioctl(APMIO_GETINFO)");
148 }
149
150 static void 
151 apm_enable(int fd, int enable) {
152
153         if (enable) {
154                 if (ioctl(fd, APMIO_ENABLE) == -1)
155                         err(1, "ioctl(APMIO_ENABLE)");
156         } else {
157                 if (ioctl(fd, APMIO_DISABLE) == -1)
158                         err(1, "ioctl(APMIO_DISABLE)");
159         }
160 }
161
162 static void
163 print_batt_life(u_int batt_life)
164 {
165         printf("Remaining battery life: ");
166         if (batt_life >= 255)
167                 printf("unknown\n");
168         else if (batt_life <= 100)
169                 printf("%d%%\n", batt_life);
170         else
171                 printf("invalid value (0x%x)\n", batt_life);
172 }
173
174 static void
175 print_batt_stat(u_int batt_stat)
176 {
177         const char *batt_msg[] = { "high", "low", "critical", "charging" };
178
179         printf("Battery Status: ");
180         if (batt_stat >= 255)
181                 printf("unknown\n");
182         else if (batt_stat > 3)
183                 printf("invalid value (0x%x)\n", batt_stat);
184         else
185                 printf("%s\n", batt_msg[batt_stat]);
186 }
187
188 static void
189 print_batt_time(int batt_time)
190 {
191         printf("Remaining battery time: ");
192         if (batt_time == -1)
193                 printf("unknown\n");
194         else {
195                 int h, m, s;
196
197                 h = batt_time;
198                 s = h % 60;
199                 h /= 60;
200                 m = h % 60;
201                 h /= 60;
202                 printf("%2d:%02d:%02d\n", h, m, s);
203         }
204 }
205
206 static void 
207 print_all_info(int fd, apm_info_t aip, int bioscall_available)
208 {
209         struct apm_bios_arg args;
210         int apmerr;
211         char *line_msg[] = { "off-line", "on-line" };
212
213         printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
214         printf("APM Management: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
215         printf("AC Line status: ");
216         if (aip->ai_acline == 255)
217                 printf("unknown");
218         else if (aip->ai_acline > 1)
219                 printf("invalid value (0x%x)", aip->ai_acline);
220         else
221                 printf("%s", line_msg[aip->ai_acline]);
222
223         print_batt_stat(aip->ai_batt_stat);
224         print_batt_life(aip->ai_batt_life);
225         print_batt_life(aip->ai_batt_life);
226
227         if (aip->ai_infoversion >= 1) {
228                 printf("Number of batteries: ");
229                 if (aip->ai_batteries == (u_int) -1)
230                         printf("unknown\n");
231                 else {
232                         int i;
233                         struct apm_pwstatus aps;
234
235                         printf("%d\n", aip->ai_batteries);
236                         for (i = 0; i < aip->ai_batteries; ++i) {
237                                 bzero(&aps, sizeof(aps));
238                                 aps.ap_device = PMDV_BATT0 + i;
239                                 if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
240                                         continue;
241                                 printf("Battery %d:\n", i);
242                                 if (aps.ap_batt_flag != 255 &&
243                                     (aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
244                                         printf("not present\n");
245                                         continue;
246                                 }
247
248                                 printf("\t");
249                                 print_batt_stat(aps.ap_batt_stat);
250                                 printf("\t");
251                                 print_batt_life(aps.ap_batt_life);
252                                 printf("\t");
253                                 print_batt_time(aps.ap_batt_time);
254                         }
255                 }
256         }
257
258         if (bioscall_available) {
259                 /*
260                  * try to get the suspend timer
261                  */
262                 bzero(&args, sizeof(args));
263                 args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
264                 args.ebx = PMDV_APMBIOS;
265                 args.ecx = 0x0001;
266                 if (ioctl(fd, APMIO_BIOS, &args)) {
267                         printf("Resume timer: unknown\n");
268                 } else {
269                         apmerr = APMERR(args.eax);
270                         if (apmerr == 0x0d || apmerr == 0x86)
271                                 printf("Resume timer: disabled\n");
272                         else if (apmerr)
273                                 fprintf(stderr, 
274                                         "Failed to get the resume timer: APM error0x%x\n",
275                                         apmerr);
276                         else {
277                                 /*
278                                  * OK.  We have the time (all bcd).
279                                  * CH - seconds
280                                  * DH - hours
281                                  * DL - minutes
282                                  * xh(SI) - month (1-12)
283                                  * xl(SI) - day of month (1-31)
284                                  * DI - year
285                                  */
286                                 struct tm tm;
287                                 char buf[1024];
288                                 time_t t;
289
290                                 tm.tm_sec = bcd2int(xh(args.ecx));
291                                 tm.tm_min = bcd2int(xl(args.edx));
292                                 tm.tm_hour = bcd2int(xh(args.edx));
293                                 tm.tm_mday = bcd2int(xl(args.esi));
294                                 tm.tm_mon = bcd2int(xh(args.esi)) - 1;
295                                 tm.tm_year = bcd2int(args.edi) - 1900;
296                                 if (cmos_wall)
297                                         t = mktime(&tm);
298                                 else
299                                         t = timegm(&tm);
300                                 tm = *localtime(&t);
301                                 strftime(buf, sizeof(buf), "%s", &tm);
302                                 printf("Resume timer: %s\n", buf);
303                         }
304                 }
305
306                 /*
307                  * Get the ring indicator resume state
308                  */
309                 bzero(&args, sizeof(args));
310                 args.eax  = (APM_BIOS) << 8 | APM_RESUMEONRING;
311                 args.ebx = PMDV_APMBIOS;
312                 args.ecx = 0x0002;
313                 if (ioctl(fd, APMIO_BIOS, &args) == 0) {
314                         printf("Resume on ring indicator: %sabled\n",
315                                args.ecx ? "en" : "dis");
316                 }
317         }
318
319         if (aip->ai_infoversion >= 1) {
320                 printf("APM Capacities:\n", aip->ai_capabilities);
321                 if (aip->ai_capabilities == 0xff00)
322                         printf("\tunknown\n");
323                 if (aip->ai_capabilities & 0x01)
324                         printf("\tglobal standby state\n");
325                 if (aip->ai_capabilities & 0x02)
326                         printf("\tglobal suspend state\n");
327                 if (aip->ai_capabilities & 0x04)
328                         printf("\tresume timer from standby\n");
329                 if (aip->ai_capabilities & 0x08)
330                         printf("\tresume timer from suspend\n");
331                 if (aip->ai_capabilities & 0x10)
332                         printf("\tRI resume from standby\n");
333                 if (aip->ai_capabilities & 0x20)
334                         printf("\tRI resume from suspend\n");
335                 if (aip->ai_capabilities & 0x40)
336                         printf("\tPCMCIA RI resume from standby\n");
337                 if (aip->ai_capabilities & 0x80)
338                         printf("\tPCMCIA RI resume from suspend\n");
339         }
340
341 }
342
343 /*
344  * currently, it can turn off the display, but the display never comes
345  * back until the machine suspend/resumes :-).
346  */
347 static void 
348 apm_display(int fd, int newstate)
349 {
350         if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
351                 err(1, "ioctl(APMIO_DISPLAY)");
352 }
353
354 static void
355 apm_haltcpu(int fd, int enable) {
356
357         if (enable) {
358                 if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
359                         err(1, "ioctl(APMIO_HALTCPU)");
360         } else {
361                 if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
362                         err(1, "ioctl(APMIO_NOTHALTCPU)");
363         }
364 }
365
366 static void
367 apm_set_timer(int fd, int delta)
368 {
369         time_t tmr;
370         struct tm *tm;
371         struct apm_bios_arg args;
372
373         tmr = time(NULL) + delta;
374         if (cmos_wall)
375                 tm = localtime(&tmr);
376         else
377                 tm = gmtime(&tmr);
378         bzero(&args, sizeof(args));
379         args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
380         args.ebx = PMDV_APMBIOS;
381         if (delta > 0) {
382                 args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
383                 args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
384                 args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
385                 args.edi = int2bcd(tm->tm_year + 1900);
386         } else {
387                 args.ecx = 0x0000;
388         }
389         if (ioctl(fd, APMIO_BIOS, &args)) {
390                 err(1,"Set resume timer");
391         }
392 }
393
394 int 
395 main(int argc, char *argv[])
396 {
397         int     c, fd;
398         int     sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
399         int     display = -1, batt_life = 0, ac_status = 0, standby = 0;
400         int     batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
401         char    *cmdname;
402         int     bioscall_available = 0;
403         size_t  cmos_wall_len = sizeof(cmos_wall);
404
405         if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
406             NULL, 0) == -1)
407                 err(1, "sysctlbyname(machdep.wall_cmos_clock)");
408         if ((cmdname = strrchr(argv[0], '/')) != NULL)
409                 cmdname++;
410         else
411                 cmdname = argv[0];
412
413         if (strcmp(cmdname, "zzz") == 0) {
414                 sleep = 1;
415                 all_info = 0;
416                 goto finish_option;
417         }
418         while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) {
419                 switch (c) {
420                 case 'a':
421                         ac_status = 1;
422                         all_info = 0;
423                         break;
424                 case 'b':
425                         batt_status = 1;
426                         all_info = 0;
427                         break;
428                 case 'd':
429                         display = is_true(optarg);
430                         all_info = 0;
431                         break;
432                 case 'l':
433                         batt_life = 1;
434                         all_info = 0;
435                         break;
436                 case 'R':
437                         delta = -1;
438                         break;
439                 case 'r':
440                         delta = atoi(optarg);
441                         break;
442                 case 's':
443                         apm_status = 1;
444                         all_info = 0;
445                         break;
446                 case 'e':
447                         enable = is_true(optarg);
448                         all_info = 0;
449                         break;
450                 case 'h':
451                         haltcpu = is_true(optarg);
452                         all_info = 0;
453                         break;
454                 case 't':
455                         batt_time = 1;
456                         all_info = 0;
457                         break;
458                 case 'z':
459                         sleep = 1;
460                         all_info = 0;
461                         break;
462                 case 'Z':
463                         standby = 1;
464                         all_info = 0;
465                         break;
466                 default:
467                         usage();
468                 }
469                 argc -= optind;
470                 argv += optind;
471         }
472 finish_option:
473         if (haltcpu != -1 || enable != -1 || display != -1 || delta || sleep || standby) {
474                 fd = open(APMDEV, O_RDWR);
475                 bioscall_available = 1;
476         } else if ((fd = open(APMDEV, O_RDWR)) >= 0)
477                 bioscall_available = 1;
478         else
479                 fd = open(APMDEV, O_RDONLY);
480         if (fd == -1)
481                 err(1, "can't open %s", APMDEV);
482         if (enable != -1)
483                 apm_enable(fd, enable);
484         if (haltcpu != -1)
485                 apm_haltcpu(fd, haltcpu);
486         if (delta)
487                 apm_set_timer(fd, delta);
488         if (sleep)
489                 apm_suspend(fd);
490         else if (standby)
491                 apm_standby(fd);
492         else if (delta == 0) {
493                 struct apm_info info;
494
495                 apm_getinfo(fd, &info);
496                 if (all_info)
497                         print_all_info(fd, &info, bioscall_available);
498                 if (ac_status)
499                         printf("%d\n", info.ai_acline);
500                 if (batt_status)
501                         printf("%d\n", info.ai_batt_stat);
502                 if (batt_life)
503                         printf("%d\n", info.ai_batt_life);
504                 if (apm_status)
505                         printf("%d\n", info.ai_status);
506                 if (batt_time)
507                         printf("%d\n", info.ai_batt_time);
508                 if (display != -1)
509                         apm_display(fd, display);
510         }
511         close(fd);
512         exit(EXIT_SUCCESS);
513 }