hpet_example.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  85. void
  86. hpet_poll(int argc, const char **argv)
  87. {
  88. unsigned long freq;
  89. int iterations, i, fd;
  90. struct pollfd pfd;
  91. struct hpet_info info;
  92. struct timeval stv, etv;
  93. struct timezone tz;
  94. long usec;
  95. if (argc != 3) {
  96. fprintf(stderr, "hpet_poll: device-name freq iterations\n");
  97. return;
  98. }
  99. freq = atoi(argv[1]);
  100. iterations = atoi(argv[2]);
  101. fd = open(argv[0], O_RDONLY);
  102. if (fd < 0) {
  103. fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
  104. return;
  105. }
  106. if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  107. fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
  108. goto out;
  109. }
  110. if (ioctl(fd, HPET_INFO, &info) < 0) {
  111. fprintf(stderr, "hpet_poll: failed to get info\n");
  112. goto out;
  113. }
  114. fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
  115. if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  116. fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
  117. goto out;
  118. }
  119. if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  120. fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
  121. goto out;
  122. }
  123. pfd.fd = fd;
  124. pfd.events = POLLIN;
  125. for (i = 0; i < iterations; i++) {
  126. pfd.revents = 0;
  127. gettimeofday(&stv, &tz);
  128. if (poll(&pfd, 1, -1) < 0)
  129. fprintf(stderr, "hpet_poll: poll failed\n");
  130. else {
  131. long data;
  132. gettimeofday(&etv, &tz);
  133. usec = stv.tv_sec * 1000000 + stv.tv_usec;
  134. usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
  135. fprintf(stderr,
  136. "hpet_poll: expired time = 0x%lx\n", usec);
  137. fprintf(stderr, "hpet_poll: revents = 0x%x\n",
  138. pfd.revents);
  139. if (read(fd, &data, sizeof(data)) != sizeof(data)) {
  140. fprintf(stderr, "hpet_poll: read failed\n");
  141. }
  142. else
  143. fprintf(stderr, "hpet_poll: data 0x%lx\n",
  144. data);
  145. }
  146. }
  147. out:
  148. close(fd);
  149. return;
  150. }
  151. static int hpet_sigio_count;
  152. static void
  153. hpet_sigio(int val)
  154. {
  155. fprintf(stderr, "hpet_sigio: called\n");
  156. hpet_sigio_count++;
  157. }
  158. void
  159. hpet_fasync(int argc, const char **argv)
  160. {
  161. unsigned long freq;
  162. int iterations, i, fd, value;
  163. sig_t oldsig;
  164. struct hpet_info info;
  165. hpet_sigio_count = 0;
  166. fd = -1;
  167. if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
  168. fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
  169. return;
  170. }
  171. if (argc != 3) {
  172. fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
  173. goto out;
  174. }
  175. fd = open(argv[0], O_RDONLY);
  176. if (fd < 0) {
  177. fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
  178. return;
  179. }
  180. if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
  181. ((value = fcntl(fd, F_GETFL)) == 1) ||
  182. (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
  183. fprintf(stderr, "hpet_fasync: fcntl failed\n");
  184. goto out;
  185. }
  186. freq = atoi(argv[1]);
  187. iterations = atoi(argv[2]);
  188. if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  189. fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
  190. goto out;
  191. }
  192. if (ioctl(fd, HPET_INFO, &info) < 0) {
  193. fprintf(stderr, "hpet_fasync: failed to get info\n");
  194. goto out;
  195. }
  196. fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
  197. if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  198. fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
  199. goto out;
  200. }
  201. if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  202. fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
  203. goto out;
  204. }
  205. for (i = 0; i < iterations; i++) {
  206. (void) pause();
  207. fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
  208. }
  209. out:
  210. signal(SIGIO, oldsig);
  211. if (fd >= 0)
  212. close(fd);
  213. return;
  214. }