1 /* $NetBSD: tadpolectl.c,v 1.7 2008/04/28 20:24:17 martin Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/envsys.h>
42 #include <machine/apmvar.h>
43 #include <machine/tctrl.h>
45 #define TCTRL_DEV "/dev/tctrl0"
47 int aflag
, nflag
, wflag
, dev
;
49 #define PROTO(x) int x __P((int, int, int));
50 void usage
__P((void));
51 static void parse
__P((char *));
52 char *dashdot
__P((const char *));
53 int main
__P((int, char *[]));
55 PROTO(hw_microcontroller_version
)
56 PROTO(hw_poweroncycles
)
57 PROTO(hw_poweronseconds
)
59 PROTO(hw_power_battery_int
)
60 PROTO(hw_power_battery_ext
)
61 PROTO(hw_power_battery_int_chargerate
)
62 PROTO(hw_power_battery_ext_chargerate
)
63 PROTO(hw_power_battery_int_chargelevel
)
64 PROTO(hw_power_battery_ext_chargelevel
)
65 PROTO(hw_video_external
)
67 PROTO(hw_video_syncinva
)
68 PROTO(hw_video_syncinvb
)
69 PROTO(hw_video_compsync
)
70 PROTO(hw_video_tft_brightness
)
71 PROTO(hw_speaker_freq
)
72 PROTO(hw_speaker_volume
)
73 PROTO(hw_kbd_repeat_delay
)
74 PROTO(hw_kbd_repeat_speed
)
75 PROTO(hw_mouse_recalibrate
)
76 PROTO(hw_power_battery_chargedisabled
)
77 PROTO(hw_mouse_disable
)
79 PROTO(hw_mouse_intclick
)
80 PROTO(hw_mouse_extclick
)
81 PROTO(hw_mouse_sensitivity
)
82 PROTO(hw_serial_power
)
85 #define TABLE(n) { __STRING(n), 0, n }
90 int (*funcptr
)(int, int, int);
92 TABLE(hw_microcontroller_version
),
94 TABLE(hw_poweroncycles
),
95 TABLE(hw_poweronseconds
),
96 TABLE(hw_power_mains
),
97 TABLE(hw_power_battery_int
),
98 TABLE(hw_power_battery_ext
),
99 TABLE(hw_power_battery_chargedisabled
),
100 TABLE(hw_power_battery_int_chargerate
),
101 TABLE(hw_power_battery_ext_chargerate
),
102 TABLE(hw_power_battery_int_chargelevel
),
103 TABLE(hw_power_battery_ext_chargelevel
),
104 TABLE(hw_video_external
),
106 TABLE(hw_video_syncinva
),
107 TABLE(hw_video_syncinvb
),
108 TABLE(hw_video_compsync
),
109 TABLE(hw_video_tft_brightness
),
110 TABLE(hw_speaker_freq
),
111 TABLE(hw_speaker_volume
),
112 TABLE(hw_kbd_repeat_delay
),
113 TABLE(hw_kbd_repeat_speed
),
115 TABLE(hw_mouse_recalibrate
),
116 TABLE(hw_mouse_disable
),
117 TABLE(hw_mouse_intclick
),
118 TABLE(hw_mouse_extclick
),
119 TABLE(hw_mouse_sensitivity
),
120 TABLE(hw_serial_power
),
125 x(readflg, new, num) \
126 int readflg, new, num;
128 #define READ_REQ(a, b, c) \
132 ioctl(dev, TCTRL_CMD_REQ, &req)
134 #define WRITE_REQ(a, b, c) \
138 ioctl(dev, TCTRL_CMD_REQ, &req)
144 /* hardware functions */
146 FUNC(hw_mouse_sensitivity
)
148 struct tctrl_req req
;
150 req
.cmdbuf
[1] = 0xff;
151 req
.cmdbuf
[2] = 0x00;
152 READ_REQ(0x2c, 3, 2);
153 table
[num
].value
= req
.rspbuf
[0];
157 req
.cmdbuf
[2] = 0x00;
159 req
.cmdbuf
[2] = 0xff;
162 req
.cmdbuf
[1] = 0x00;
163 WRITE_REQ(0x2c, 3, 2);
164 req
.cmdbuf
[1] = 0xff;
165 req
.cmdbuf
[2] = 0x00;
166 READ_REQ(0x2c, 3, 2);
167 table
[num
].value
= req
.rspbuf
[0];
171 FUNC(hw_power_battery_chargedisabled
)
173 struct tctrl_req req
;
175 req
.cmdbuf
[1] = 0xff;
176 req
.cmdbuf
[2] = 0x00;
177 READ_REQ(0x22, 3, 2);
178 table
[num
].value
= req
.rspbuf
[0]&0x01 ? 1 : 0;
182 req
.cmdbuf
[2] = 0x00;
184 req
.cmdbuf
[2] = 0x01;
185 req
.cmdbuf
[1] = ~0x01;
186 WRITE_REQ(0x22, 3, 2);
187 req
.cmdbuf
[1] = 0xff;
188 req
.cmdbuf
[2] = 0x00;
189 READ_REQ(0x22, 3, 2);
190 table
[num
].value
= req
.rspbuf
[0]&0x01 ? 1 : 0;
194 FUNC(hw_mouse_disable
)
196 struct tctrl_req req
;
198 req
.cmdbuf
[1] = 0xff;
199 req
.cmdbuf
[2] = 0x00;
200 READ_REQ(0x22, 3, 2);
201 table
[num
].value
= req
.rspbuf
[0]&0x02 ? 1 : 0;
205 req
.cmdbuf
[2] = 0x00;
207 req
.cmdbuf
[2] = 0x02;
208 req
.cmdbuf
[1] = ~0x02;
209 WRITE_REQ(0x22, 3, 2);
210 req
.cmdbuf
[1] = 0xff;
211 req
.cmdbuf
[2] = 0x00;
212 READ_REQ(0x22, 3, 2);
213 table
[num
].value
= req
.rspbuf
[0]&0x02 ? 1 : 0;
219 struct tctrl_req req
;
221 req
.cmdbuf
[1] = 0xff;
222 req
.cmdbuf
[2] = 0x00;
223 READ_REQ(0x22, 3, 2);
224 table
[num
].value
= req
.rspbuf
[0]&0x04 ? 1 : 0;
228 req
.cmdbuf
[2] = 0x00;
230 req
.cmdbuf
[2] = 0x04;
231 req
.cmdbuf
[1] = ~0x04;
232 WRITE_REQ(0x22, 3, 2);
233 req
.cmdbuf
[1] = 0xff;
234 req
.cmdbuf
[2] = 0x00;
235 READ_REQ(0x22, 3, 2);
236 table
[num
].value
= req
.rspbuf
[0]&0x04 ? 1 : 0;
240 FUNC(hw_mouse_intclick
)
242 struct tctrl_req req
;
244 req
.cmdbuf
[1] = 0xff;
245 req
.cmdbuf
[2] = 0x00;
246 READ_REQ(0x22, 3, 2);
247 table
[num
].value
= req
.rspbuf
[0]&0x08 ? 1 : 0;
251 req
.cmdbuf
[2] = 0x00;
253 req
.cmdbuf
[2] = 0x08;
254 req
.cmdbuf
[1] = ~0x08;
255 WRITE_REQ(0x22, 3, 2);
256 req
.cmdbuf
[1] = 0xff;
257 req
.cmdbuf
[2] = 0x00;
258 READ_REQ(0x22, 3, 2);
259 table
[num
].value
= req
.rspbuf
[0]&0x08 ? 1 : 0;
263 FUNC(hw_mouse_extclick
)
265 struct tctrl_req req
;
267 req
.cmdbuf
[1] = 0xff;
268 req
.cmdbuf
[2] = 0x00;
269 READ_REQ(0x22, 3, 2);
270 table
[num
].value
= req
.rspbuf
[0]&0x10 ? 1 : 0;
274 req
.cmdbuf
[2] = 0x00;
276 req
.cmdbuf
[2] = 0x10;
277 req
.cmdbuf
[1] = ~0x10;
278 WRITE_REQ(0x22, 3, 2);
279 req
.cmdbuf
[1] = 0xff;
280 req
.cmdbuf
[2] = 0x00;
281 READ_REQ(0x22, 3, 2);
282 table
[num
].value
= req
.rspbuf
[0]&0x10 ? 1 : 0;
287 FUNC(hw_mouse_recalibrate
)
289 struct tctrl_req req
;
291 table
[num
].value
= 0;
294 READ_REQ(0x36, 1, 1);
298 FUNC(hw_kbd_repeat_delay
)
300 struct tctrl_req req
;
302 req
.cmdbuf
[1] = 0xff;
303 req
.cmdbuf
[2] = 0x00;
304 READ_REQ(0x28, 3, 2);
305 table
[num
].value
= req
.rspbuf
[0];
309 req
.cmdbuf
[2] = 0x00;
311 req
.cmdbuf
[2] = 0xff;
314 req
.cmdbuf
[1] = 0x00;
315 WRITE_REQ(0x28, 3, 2);
316 req
.cmdbuf
[1] = 0xff;
317 req
.cmdbuf
[2] = 0x00;
318 READ_REQ(0x28, 3, 2);
319 table
[num
].value
= req
.rspbuf
[0];
323 FUNC(hw_kbd_repeat_speed
)
325 struct tctrl_req req
;
327 req
.cmdbuf
[1] = 0xff;
328 req
.cmdbuf
[2] = 0x00;
329 READ_REQ(0x29, 3, 2);
330 table
[num
].value
= req
.rspbuf
[0];
334 req
.cmdbuf
[2] = 0x00;
336 req
.cmdbuf
[2] = 0xff;
339 req
.cmdbuf
[1] = 0x00;
340 WRITE_REQ(0x29, 3, 2);
341 req
.cmdbuf
[1] = 0xff;
342 req
.cmdbuf
[2] = 0x00;
343 READ_REQ(0x29, 3, 2);
344 table
[num
].value
= req
.rspbuf
[0];
348 FUNC(hw_speaker_freq
)
350 struct tctrl_req req
;
352 table
[num
].value
= 0;
355 req
.cmdbuf
[1] = new * 256;
356 req
.cmdbuf
[2] = new % 256;
357 WRITE_REQ(0x37, 3, 1);
361 FUNC(hw_speaker_volume
)
363 struct tctrl_req req
;
365 req
.cmdbuf
[1] = 0xff;
366 req
.cmdbuf
[2] = 0x00;
367 READ_REQ(0x23, 3, 2);
368 table
[num
].value
= req
.rspbuf
[0];
372 req
.cmdbuf
[2] = 0x00;
374 req
.cmdbuf
[2] = 0xff;
377 req
.cmdbuf
[1] = 0x00;
378 WRITE_REQ(0x23, 3, 2);
379 req
.cmdbuf
[1] = 0xff;
380 req
.cmdbuf
[2] = 0x00;
381 READ_REQ(0x23, 3, 2);
382 table
[num
].value
= req
.rspbuf
[0];
386 FUNC(hw_video_tft_brightness
)
388 struct tctrl_req req
;
390 req
.cmdbuf
[1] = 0xff;
391 req
.cmdbuf
[2] = 0x00;
392 READ_REQ(0x24, 3, 2);
393 table
[num
].value
= req
.rspbuf
[0];
397 req
.cmdbuf
[2] = 0x00;
399 req
.cmdbuf
[2] = 0xff;
402 req
.cmdbuf
[1] = 0x00;
403 WRITE_REQ(0x24, 3, 2);
404 req
.cmdbuf
[1] = 0xff;
405 req
.cmdbuf
[2] = 0x00;
406 READ_REQ(0x24, 3, 2);
407 table
[num
].value
= req
.rspbuf
[0];
411 FUNC(hw_video_syncinva
)
413 struct tctrl_req req
;
415 req
.cmdbuf
[1] = 0xff;
416 req
.cmdbuf
[2] = 0x00;
417 READ_REQ(0x21, 3, 2);
418 table
[num
].value
= req
.rspbuf
[0]&0x02 ? 1 : 0;
422 req
.cmdbuf
[2] = 0x00;
424 req
.cmdbuf
[2] = 0x02;
425 req
.cmdbuf
[1] = ~0x02;
426 WRITE_REQ(0x21, 3, 2);
427 req
.cmdbuf
[1] = 0xff;
428 req
.cmdbuf
[2] = 0x00;
429 READ_REQ(0x21, 3, 2);
430 table
[num
].value
= req
.rspbuf
[0]&0x02 ? 1 : 0;
434 FUNC(hw_video_syncinvb
)
436 struct tctrl_req req
;
438 req
.cmdbuf
[1] = 0xff;
439 req
.cmdbuf
[2] = 0x00;
440 READ_REQ(0x21, 3, 2);
441 table
[num
].value
= req
.rspbuf
[0]&0x04 ? 1 : 0;
445 req
.cmdbuf
[2] = 0x00;
447 req
.cmdbuf
[2] = 0x04;
448 req
.cmdbuf
[1] = ~0x04;
449 WRITE_REQ(0x21, 3, 2);
450 req
.cmdbuf
[1] = 0xff;
451 req
.cmdbuf
[2] = 0x00;
452 READ_REQ(0x21, 3, 2);
453 table
[num
].value
= req
.rspbuf
[0]&0x04 ? 1 : 0;
457 FUNC(hw_video_compsync
)
459 struct tctrl_req req
;
461 req
.cmdbuf
[1] = 0xff;
462 req
.cmdbuf
[2] = 0x00;
463 READ_REQ(0x21, 3, 2);
464 table
[num
].value
= req
.rspbuf
[0]&0x10 ? 1 : 0;
468 req
.cmdbuf
[2] = 0x00;
470 req
.cmdbuf
[2] = 0x10;
471 req
.cmdbuf
[1] = ~0x10;
472 WRITE_REQ(0x21, 3, 2);
473 req
.cmdbuf
[1] = 0xff;
474 req
.cmdbuf
[2] = 0x00;
475 READ_REQ(0x21, 3, 2);
476 table
[num
].value
= req
.rspbuf
[0]&0x10 ? 1 : 0;
483 struct tctrl_req req
;
487 READ_REQ(0x11, 1, 3);
488 i
= (req
.rspbuf
[0]<<8) + req
.rspbuf
[1];
489 table
[num
].value
= i
&0x0040 ? 0 : 1;
494 FUNC(hw_video_external
)
496 struct tctrl_req req
;
500 READ_REQ(0x11, 1, 3);
501 i
= (req
.rspbuf
[0]<<8) + req
.rspbuf
[1];
502 table
[num
].value
= i
&0x0008 ? 1 : 0;
507 FUNC(hw_power_battery_int_chargelevel
)
509 struct tctrl_req req
;
512 READ_REQ(0x7a, 1, 3);
513 table
[num
].value
= req
.rspbuf
[0] == 0xfb ? 0 : req
.rspbuf
[0];
519 FUNC(hw_power_battery_ext_chargelevel
)
521 struct tctrl_req req
;
524 READ_REQ(0x7b, 1, 3);
525 table
[num
].value
= req
.rspbuf
[0] == 0xfb ? 0 : req
.rspbuf
[0];
529 FUNC(hw_power_battery_int_chargerate
)
531 struct tctrl_req req
;
533 READ_REQ(0x18, 1, 2);
534 table
[num
].value
= req
.rspbuf
[0];
537 req
.cmdbuf
[1] = new < 255 ? new : 255;
538 WRITE_REQ(0x39, 2, 1);
539 READ_REQ(0x18, 1, 2);
540 table
[num
].value
= req
.rspbuf
[0];
544 FUNC(hw_power_battery_ext_chargerate
)
546 struct tctrl_req req
;
548 READ_REQ(0x18, 1, 2);
549 table
[num
].value
= req
.rspbuf
[0];
552 req
.cmdbuf
[1] = new < 255 ? new : 255;
553 WRITE_REQ(0x39, 2, 1);
554 READ_REQ(0x18, 1, 2);
555 table
[num
].value
= req
.rspbuf
[0];
560 FUNC(hw_power_battery_ext
)
563 struct tctrl_req req
;
566 READ_REQ(0x11, 1, 3);
567 i
= (req
.rspbuf
[0]<<8) + req
.rspbuf
[1];
568 table
[num
].value
= i
&0x0004 ? 1 : 0;
573 FUNC(hw_power_battery_int
)
576 struct tctrl_req req
;
579 READ_REQ(0x11, 1, 3);
580 i
= (req
.rspbuf
[0]<<8) + req
.rspbuf
[1];
581 table
[num
].value
= i
&0x0002 ? 1 : 0;
589 struct tctrl_req req
;
592 READ_REQ(0x11, 1, 3);
593 i
= (req
.rspbuf
[0]<<8) + req
.rspbuf
[1];
594 table
[num
].value
= i
&0x0001 ? 1 : 0;
599 FUNC(hw_poweroncycles
)
601 struct tctrl_req req
;
604 READ_REQ(0x09, 1, 5);
605 table
[num
].value
= (req
.rspbuf
[0]<<24)+(req
.rspbuf
[1]<<16)+
606 (req
.rspbuf
[2]<<8)+req
.rspbuf
[3];
611 FUNC(hw_poweronseconds
)
613 struct tctrl_req req
;
616 READ_REQ(0x0a, 1, 5);
617 table
[num
].value
= (req
.rspbuf
[0]<<24)+(req
.rspbuf
[1]<<16)+
618 (req
.rspbuf
[2]<<8)+req
.rspbuf
[3];
623 FUNC(hw_microcontroller_version
)
626 struct tctrl_req req
;
629 READ_REQ(0x04, 1, 3);
630 snprintf(buf
, sizeof(buf
), "%d%d", req
.rspbuf
[0]*1000,
632 table
[num
].value
= atoi(strdup(buf
));
641 struct tctrl_req req
;
644 READ_REQ(0x03, 1, 3);
645 snprintf(buf
, sizeof(buf
), "%d%d", req
.rspbuf
[0]*1000,
647 table
[num
].value
= atoi(strdup(buf
));
651 FUNC(hw_serial_power
)
653 struct tctrl_pwr pwrreq
;
658 ioctl(dev
, TCTRL_SERIAL_PWR
, &pwrreq
);
661 ioctl(dev
, TCTRL_SERIAL_PWR
, &pwrreq
);
662 table
[num
].value
= pwrreq
.state
;
669 (void)fprintf(stderr
,
670 "usage: tadpolectl [-n] name ...\n"
671 " tadpolectl [-n] -w name=value\n"
672 " tadpolectl [-n] -a\n");
680 char *cp
, buf
[BUFSIZ
];
684 string
= dashdot(string
);
685 snprintf(buf
, (size_t)BUFSIZ
, "%s", string
);
686 if ((cp
= strchr(string
, '=')) != NULL
) {
688 errx(2, "Must specify -w to set variables");
689 *strchr(buf
, '=') = '\0';
691 while (isspace((unsigned char) *cp
))
695 for (j
=0,i
=-1; j
< NUM_MIBS
; j
++) {
696 if (strcmp(string
, table
[j
].mib
) == 0) {
702 errx(2, "Named value does not exist");
705 ret
= (*table
[i
].funcptr
)(0, newval
, i
);
707 errx(2, "Cannot modify this value");
709 ret
= (*table
[i
].funcptr
)(1, 0, i
);
711 printf("%d\n", table
[i
].value
);
713 printf("%s = %d\n", dashdot(table
[i
].mib
), table
[i
].value
);
726 for (; (*p
= *string
) != '\0'; ++p
, ++string
) {
742 while ((ch
= getopt(argc
, argv
, "anw")) != -1) {
761 if ((dev
= open(TCTRL_DEV
, O_RDONLY
, NULL
)) == -1)
762 err(1, "%s", TCTRL_DEV
);
765 for (j
=0; j
< NUM_MIBS
; j
++) {
766 (void)(*table
[j
].funcptr
)(1, 0, j
);
768 printf("%d\n", table
[j
].value
);
770 printf("%s = %d\n", dashdot(table
[j
].mib
),