hpet_example.c 5.5 KB

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