writeback: split writeback_inodes_wb
[linux-2.6/next.git] / Documentation / timers / hpet_example.c
blob4bfafb7bc4c5852b2796fa15572a753bcb717a6e
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 <errno.h>
14 #include <sys/time.h>
15 #include <linux/hpet.h>
18 extern void hpet_open_close(int, const char **);
19 extern void hpet_info(int, const char **);
20 extern void hpet_poll(int, const char **);
21 extern void hpet_fasync(int, const char **);
22 extern void hpet_read(int, const char **);
24 #include <sys/poll.h>
25 #include <sys/ioctl.h>
27 struct hpet_command {
28 char *command;
29 void (*func)(int argc, const char ** argv);
30 } hpet_command[] = {
32 "open-close",
33 hpet_open_close
36 "info",
37 hpet_info
40 "poll",
41 hpet_poll
44 "fasync",
45 hpet_fasync
49 int
50 main(int argc, const char ** argv)
52 int i;
54 argc--;
55 argv++;
57 if (!argc) {
58 fprintf(stderr, "-hpet: requires command\n");
59 return -1;
63 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
64 if (!strcmp(argv[0], hpet_command[i].command)) {
65 argc--;
66 argv++;
67 fprintf(stderr, "-hpet: executing %s\n",
68 hpet_command[i].command);
69 hpet_command[i].func(argc, argv);
70 return 0;
73 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
75 return -1;
78 void
79 hpet_open_close(int argc, const char **argv)
81 int fd;
83 if (argc != 1) {
84 fprintf(stderr, "hpet_open_close: device-name\n");
85 return;
88 fd = open(argv[0], O_RDONLY);
89 if (fd < 0)
90 fprintf(stderr, "hpet_open_close: open failed\n");
91 else
92 close(fd);
94 return;
97 void
98 hpet_info(int argc, const char **argv)
102 void
103 hpet_poll(int argc, const char **argv)
105 unsigned long freq;
106 int iterations, i, fd;
107 struct pollfd pfd;
108 struct hpet_info info;
109 struct timeval stv, etv;
110 struct timezone tz;
111 long usec;
113 if (argc != 3) {
114 fprintf(stderr, "hpet_poll: device-name freq iterations\n");
115 return;
118 freq = atoi(argv[1]);
119 iterations = atoi(argv[2]);
121 fd = open(argv[0], O_RDONLY);
123 if (fd < 0) {
124 fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
125 return;
128 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
129 fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
130 goto out;
133 if (ioctl(fd, HPET_INFO, &info) < 0) {
134 fprintf(stderr, "hpet_poll: failed to get info\n");
135 goto out;
138 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
140 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
141 fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
142 goto out;
145 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
146 fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
147 goto out;
150 pfd.fd = fd;
151 pfd.events = POLLIN;
153 for (i = 0; i < iterations; i++) {
154 pfd.revents = 0;
155 gettimeofday(&stv, &tz);
156 if (poll(&pfd, 1, -1) < 0)
157 fprintf(stderr, "hpet_poll: poll failed\n");
158 else {
159 long data;
161 gettimeofday(&etv, &tz);
162 usec = stv.tv_sec * 1000000 + stv.tv_usec;
163 usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
165 fprintf(stderr,
166 "hpet_poll: expired time = 0x%lx\n", usec);
168 fprintf(stderr, "hpet_poll: revents = 0x%x\n",
169 pfd.revents);
171 if (read(fd, &data, sizeof(data)) != sizeof(data)) {
172 fprintf(stderr, "hpet_poll: read failed\n");
174 else
175 fprintf(stderr, "hpet_poll: data 0x%lx\n",
176 data);
180 out:
181 close(fd);
182 return;
185 static int hpet_sigio_count;
187 static void
188 hpet_sigio(int val)
190 fprintf(stderr, "hpet_sigio: called\n");
191 hpet_sigio_count++;
194 void
195 hpet_fasync(int argc, const char **argv)
197 unsigned long freq;
198 int iterations, i, fd, value;
199 sig_t oldsig;
200 struct hpet_info info;
202 hpet_sigio_count = 0;
203 fd = -1;
205 if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
206 fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
207 return;
210 if (argc != 3) {
211 fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
212 goto out;
215 fd = open(argv[0], O_RDONLY);
217 if (fd < 0) {
218 fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
219 return;
223 if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
224 ((value = fcntl(fd, F_GETFL)) == 1) ||
225 (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
226 fprintf(stderr, "hpet_fasync: fcntl failed\n");
227 goto out;
230 freq = atoi(argv[1]);
231 iterations = atoi(argv[2]);
233 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
234 fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
235 goto out;
238 if (ioctl(fd, HPET_INFO, &info) < 0) {
239 fprintf(stderr, "hpet_fasync: failed to get info\n");
240 goto out;
243 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
245 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
246 fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
247 goto out;
250 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
251 fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
252 goto out;
255 for (i = 0; i < iterations; i++) {
256 (void) pause();
257 fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
260 out:
261 signal(SIGIO, oldsig);
263 if (fd >= 0)
264 close(fd);
266 return;