hpet_example.c 4.9 KB

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