hpet_example.c 5.4 KB

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