ARM: 6126/1: ARM mpcore_wdt: fix build failure and other fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / timers / hpet_example.c
blobf9ce2d9fdfd5becebf0ed9a7a3db3cd60ed7ae55
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <memory.h>
7 #include <malloc.h>
8 #include <time.h>
9 #include <ctype.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <signal.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/time.h>
16 #include <linux/hpet.h>
19 extern void hpet_open_close(int, const char **);
20 extern void hpet_info(int, const char **);
21 extern void hpet_poll(int, const char **);
22 extern void hpet_fasync(int, const char **);
23 extern void hpet_read(int, const char **);
25 #include <sys/poll.h>
26 #include <sys/ioctl.h>
27 #include <signal.h>
29 struct hpet_command {
30 char *command;
31 void (*func)(int argc, const char ** argv);
32 } hpet_command[] = {
34 "open-close",
35 hpet_open_close
38 "info",
39 hpet_info
42 "poll",
43 hpet_poll
46 "fasync",
47 hpet_fasync
51 int
52 main(int argc, const char ** argv)
54 int i;
56 argc--;
57 argv++;
59 if (!argc) {
60 fprintf(stderr, "-hpet: requires command\n");
61 return -1;
65 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
66 if (!strcmp(argv[0], hpet_command[i].command)) {
67 argc--;
68 argv++;
69 fprintf(stderr, "-hpet: executing %s\n",
70 hpet_command[i].command);
71 hpet_command[i].func(argc, argv);
72 return 0;
75 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
77 return -1;
80 void
81 hpet_open_close(int argc, const char **argv)
83 int fd;
85 if (argc != 1) {
86 fprintf(stderr, "hpet_open_close: device-name\n");
87 return;
90 fd = open(argv[0], O_RDONLY);
91 if (fd < 0)
92 fprintf(stderr, "hpet_open_close: open failed\n");
93 else
94 close(fd);
96 return;
99 void
100 hpet_info(int argc, const char **argv)
104 void
105 hpet_poll(int argc, const char **argv)
107 unsigned long freq;
108 int iterations, i, fd;
109 struct pollfd pfd;
110 struct hpet_info info;
111 struct timeval stv, etv;
112 struct timezone tz;
113 long usec;
115 if (argc != 3) {
116 fprintf(stderr, "hpet_poll: device-name freq iterations\n");
117 return;
120 freq = atoi(argv[1]);
121 iterations = atoi(argv[2]);
123 fd = open(argv[0], O_RDONLY);
125 if (fd < 0) {
126 fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
127 return;
130 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
131 fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
132 goto out;
135 if (ioctl(fd, HPET_INFO, &info) < 0) {
136 fprintf(stderr, "hpet_poll: failed to get info\n");
137 goto out;
140 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
142 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
143 fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
144 goto out;
147 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
148 fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
149 goto out;
152 pfd.fd = fd;
153 pfd.events = POLLIN;
155 for (i = 0; i < iterations; i++) {
156 pfd.revents = 0;
157 gettimeofday(&stv, &tz);
158 if (poll(&pfd, 1, -1) < 0)
159 fprintf(stderr, "hpet_poll: poll failed\n");
160 else {
161 long data;
163 gettimeofday(&etv, &tz);
164 usec = stv.tv_sec * 1000000 + stv.tv_usec;
165 usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
167 fprintf(stderr,
168 "hpet_poll: expired time = 0x%lx\n", usec);
170 fprintf(stderr, "hpet_poll: revents = 0x%x\n",
171 pfd.revents);
173 if (read(fd, &data, sizeof(data)) != sizeof(data)) {
174 fprintf(stderr, "hpet_poll: read failed\n");
176 else
177 fprintf(stderr, "hpet_poll: data 0x%lx\n",
178 data);
182 out:
183 close(fd);
184 return;
187 static int hpet_sigio_count;
189 static void
190 hpet_sigio(int val)
192 fprintf(stderr, "hpet_sigio: called\n");
193 hpet_sigio_count++;
196 void
197 hpet_fasync(int argc, const char **argv)
199 unsigned long freq;
200 int iterations, i, fd, value;
201 sig_t oldsig;
202 struct hpet_info info;
204 hpet_sigio_count = 0;
205 fd = -1;
207 if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
208 fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
209 return;
212 if (argc != 3) {
213 fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
214 goto out;
217 fd = open(argv[0], O_RDONLY);
219 if (fd < 0) {
220 fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
221 return;
225 if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
226 ((value = fcntl(fd, F_GETFL)) == 1) ||
227 (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
228 fprintf(stderr, "hpet_fasync: fcntl failed\n");
229 goto out;
232 freq = atoi(argv[1]);
233 iterations = atoi(argv[2]);
235 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
236 fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
237 goto out;
240 if (ioctl(fd, HPET_INFO, &info) < 0) {
241 fprintf(stderr, "hpet_fasync: failed to get info\n");
242 goto out;
245 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
247 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
248 fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
249 goto out;
252 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
253 fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
254 goto out;
257 for (i = 0; i < iterations; i++) {
258 (void) pause();
259 fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
262 out:
263 signal(SIGIO, oldsig);
265 if (fd >= 0)
266 close(fd);
268 return;