Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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