1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/types.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 **);
26 #include <sys/ioctl.h>
30 void (*func)(int argc, const char ** argv);
51 main(int argc, const char ** argv)
59 fprintf(stderr, "-hpet: requires command\n");
64 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
65 if (!strcmp(argv[0], hpet_command[i].command)) {
68 fprintf(stderr, "-hpet: executing %s\n",
69 hpet_command[i].command);
70 hpet_command[i].func(argc, argv);
74 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
80 hpet_open_close(int argc, const char **argv)
85 fprintf(stderr, "hpet_open_close: device-name\n");
89 fd = open(argv[0], O_RDONLY);
91 fprintf(stderr, "hpet_open_close: open failed\n");
99 hpet_info(int argc, const char **argv)
101 struct hpet_info info;
105 fprintf(stderr, "hpet_info: device-name\n");
109 fd = open(argv[0], O_RDONLY);
111 fprintf(stderr, "hpet_info: open of %s failed\n", argv[0]);
115 if (ioctl(fd, HPET_INFO, &info) < 0) {
116 fprintf(stderr, "hpet_info: failed to get info\n");
120 fprintf(stderr, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ",
121 info.hi_ireqfreq, info.hi_flags);
122 fprintf(stderr, "hi_hpet %d hi_timer %d\n",
123 info.hi_hpet, info.hi_timer);
131 hpet_poll(int argc, const char **argv)
134 int iterations, i, fd;
136 struct hpet_info info;
137 struct timeval stv, etv;
142 fprintf(stderr, "hpet_poll: device-name freq iterations\n");
146 freq = atoi(argv[1]);
147 iterations = atoi(argv[2]);
149 fd = open(argv[0], O_RDONLY);
152 fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
156 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
157 fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
161 if (ioctl(fd, HPET_INFO, &info) < 0) {
162 fprintf(stderr, "hpet_poll: failed to get info\n");
166 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
168 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
169 fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
173 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
174 fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
181 for (i = 0; i < iterations; i++) {
183 gettimeofday(&stv, &tz);
184 if (poll(&pfd, 1, -1) < 0)
185 fprintf(stderr, "hpet_poll: poll failed\n");
189 gettimeofday(&etv, &tz);
190 usec = stv.tv_sec * 1000000 + stv.tv_usec;
191 usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
194 "hpet_poll: expired time = 0x%lx\n", usec);
196 fprintf(stderr, "hpet_poll: revents = 0x%x\n",
199 if (read(fd, &data, sizeof(data)) != sizeof(data)) {
200 fprintf(stderr, "hpet_poll: read failed\n");
203 fprintf(stderr, "hpet_poll: data 0x%lx\n",
213 static int hpet_sigio_count;
218 fprintf(stderr, "hpet_sigio: called\n");
223 hpet_fasync(int argc, const char **argv)
226 int iterations, i, fd, value;
228 struct hpet_info info;
230 hpet_sigio_count = 0;
233 if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
234 fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
239 fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
243 fd = open(argv[0], O_RDONLY);
246 fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
251 if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
252 ((value = fcntl(fd, F_GETFL)) == 1) ||
253 (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
254 fprintf(stderr, "hpet_fasync: fcntl failed\n");
258 freq = atoi(argv[1]);
259 iterations = atoi(argv[2]);
261 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
262 fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
266 if (ioctl(fd, HPET_INFO, &info) < 0) {
267 fprintf(stderr, "hpet_fasync: failed to get info\n");
271 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
273 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
274 fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
278 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
279 fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
283 for (i = 0; i < iterations; i++) {
285 fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
289 signal(SIGIO, oldsig);