Blame view

samples/timers/hpet_example.c 5.46 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
e61863099   Randy Dunlap   Documentation/tim...
2
3
4
5
6
7
8
9
10
11
12
13
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <string.h>
  #include <memory.h>
  #include <malloc.h>
  #include <time.h>
  #include <ctype.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  #include <signal.h>
e61863099   Randy Dunlap   Documentation/tim...
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <errno.h>
  #include <sys/time.h>
  #include <linux/hpet.h>
  
  
  extern void hpet_open_close(int, const char **);
  extern void hpet_info(int, const char **);
  extern void hpet_poll(int, const char **);
  extern void hpet_fasync(int, const char **);
  extern void hpet_read(int, const char **);
  
  #include <sys/poll.h>
  #include <sys/ioctl.h>
e61863099   Randy Dunlap   Documentation/tim...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
  struct hpet_command {
  	char		*command;
  	void		(*func)(int argc, const char ** argv);
  } hpet_command[] = {
  	{
  		"open-close",
  		hpet_open_close
  	},
  	{
  		"info",
  		hpet_info
  	},
  	{
  		"poll",
  		hpet_poll
  	},
  	{
  		"fasync",
  		hpet_fasync
  	},
  };
  
  int
  main(int argc, const char ** argv)
  {
f0cd147e6   Mahesh Khanwalkar   Documentation: Fi...
53
  	unsigned int	i;
e61863099   Randy Dunlap   Documentation/tim...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
  	argc--;
  	argv++;
  
  	if (!argc) {
  		fprintf(stderr, "-hpet: requires command
  ");
  		return -1;
  	}
  
  
  	for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
  		if (!strcmp(argv[0], hpet_command[i].command)) {
  			argc--;
  			argv++;
  			fprintf(stderr, "-hpet: executing %s
  ",
  				hpet_command[i].command);
  			hpet_command[i].func(argc, argv);
  			return 0;
  		}
  
  	fprintf(stderr, "do_hpet: command %s not implemented
  ", argv[0]);
  
  	return -1;
  }
  
  void
  hpet_open_close(int argc, const char **argv)
  {
  	int	fd;
  
  	if (argc != 1) {
  		fprintf(stderr, "hpet_open_close: device-name
  ");
  		return;
  	}
  
  	fd = open(argv[0], O_RDONLY);
  	if (fd < 0)
  		fprintf(stderr, "hpet_open_close: open failed
  ");
  	else
  		close(fd);
  
  	return;
  }
  
  void
  hpet_info(int argc, const char **argv)
  {
aaaddfe0b   Jaswinder Singh Rajput   Documentation/tim...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  	struct hpet_info	info;
  	int			fd;
  
  	if (argc != 1) {
  		fprintf(stderr, "hpet_info: device-name
  ");
  		return;
  	}
  
  	fd = open(argv[0], O_RDONLY);
  	if (fd < 0) {
  		fprintf(stderr, "hpet_info: open of %s failed
  ", argv[0]);
  		return;
  	}
  
  	if (ioctl(fd, HPET_INFO, &info) < 0) {
  		fprintf(stderr, "hpet_info: failed to get info
  ");
  		goto out;
  	}
  
  	fprintf(stderr, "hpet_info: hi_irqfreq 0x%lx hi_flags 0x%lx ",
  		info.hi_ireqfreq, info.hi_flags);
  	fprintf(stderr, "hi_hpet %d hi_timer %d
  ",
  		info.hi_hpet, info.hi_timer);
  
  out:
  	close(fd);
  	return;
e61863099   Randy Dunlap   Documentation/tim...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  }
  
  void
  hpet_poll(int argc, const char **argv)
  {
  	unsigned long		freq;
  	int			iterations, i, fd;
  	struct pollfd		pfd;
  	struct hpet_info	info;
  	struct timeval		stv, etv;
  	struct timezone		tz;
  	long			usec;
  
  	if (argc != 3) {
  		fprintf(stderr, "hpet_poll: device-name freq iterations
  ");
  		return;
  	}
  
  	freq = atoi(argv[1]);
  	iterations = atoi(argv[2]);
  
  	fd = open(argv[0], O_RDONLY);
  
  	if (fd < 0) {
  		fprintf(stderr, "hpet_poll: open of %s failed
  ", argv[0]);
  		return;
  	}
  
  	if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  		fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed
  ");
  		goto out;
  	}
  
  	if (ioctl(fd, HPET_INFO, &info) < 0) {
  		fprintf(stderr, "hpet_poll: failed to get info
  ");
  		goto out;
  	}
  
  	fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx
  ", info.hi_flags);
  
  	if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  		fprintf(stderr, "hpet_poll: HPET_EPI failed
  ");
  		goto out;
  	}
  
  	if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  		fprintf(stderr, "hpet_poll, HPET_IE_ON failed
  ");
  		goto out;
  	}
  
  	pfd.fd = fd;
  	pfd.events = POLLIN;
  
  	for (i = 0; i < iterations; i++) {
  		pfd.revents = 0;
  		gettimeofday(&stv, &tz);
  		if (poll(&pfd, 1, -1) < 0)
  			fprintf(stderr, "hpet_poll: poll failed
  ");
  		else {
  			long 	data;
  
  			gettimeofday(&etv, &tz);
  			usec = stv.tv_sec * 1000000 + stv.tv_usec;
  			usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
  
  			fprintf(stderr,
  				"hpet_poll: expired time = 0x%lx
  ", usec);
  
  			fprintf(stderr, "hpet_poll: revents = 0x%x
  ",
  				pfd.revents);
  
  			if (read(fd, &data, sizeof(data)) != sizeof(data)) {
  				fprintf(stderr, "hpet_poll: read failed
  ");
  			}
  			else
  				fprintf(stderr, "hpet_poll: data 0x%lx
  ",
  					data);
  		}
  	}
  
  out:
  	close(fd);
  	return;
  }
  
  static int hpet_sigio_count;
  
  static void
  hpet_sigio(int val)
  {
  	fprintf(stderr, "hpet_sigio: called
  ");
  	hpet_sigio_count++;
  }
  
  void
  hpet_fasync(int argc, const char **argv)
  {
  	unsigned long		freq;
  	int			iterations, i, fd, value;
  	sig_t			oldsig;
  	struct hpet_info	info;
  
  	hpet_sigio_count = 0;
  	fd = -1;
  
  	if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
  		fprintf(stderr, "hpet_fasync: failed to set signal handler
  ");
  		return;
  	}
  
  	if (argc != 3) {
  		fprintf(stderr, "hpet_fasync: device-name freq iterations
  ");
  		goto out;
  	}
  
  	fd = open(argv[0], O_RDONLY);
  
  	if (fd < 0) {
  		fprintf(stderr, "hpet_fasync: failed to open %s
  ", argv[0]);
  		return;
  	}
  
  
  	if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
  		((value = fcntl(fd, F_GETFL)) == 1) ||
  		(fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
  		fprintf(stderr, "hpet_fasync: fcntl failed
  ");
  		goto out;
  	}
  
  	freq = atoi(argv[1]);
  	iterations = atoi(argv[2]);
  
  	if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  		fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed
  ");
  		goto out;
  	}
  
  	if (ioctl(fd, HPET_INFO, &info) < 0) {
  		fprintf(stderr, "hpet_fasync: failed to get info
  ");
  		goto out;
  	}
  
  	fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx
  ", info.hi_flags);
  
  	if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  		fprintf(stderr, "hpet_fasync: HPET_EPI failed
  ");
  		goto out;
  	}
  
  	if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  		fprintf(stderr, "hpet_fasync, HPET_IE_ON failed
  ");
  		goto out;
  	}
  
  	for (i = 0; i < iterations; i++) {
  		(void) pause();
  		fprintf(stderr, "hpet_fasync: count = %d
  ", hpet_sigio_count);
  	}
  
  out:
  	signal(SIGIO, oldsig);
  
  	if (fd >= 0)
  		close(fd);
  
  	return;
  }