rtctest.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Real Time Clock Driver Test/Example Program
  3. *
  4. * Compile with:
  5. * gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
  6. *
  7. * Copyright (C) 1996, Paul Gortmaker.
  8. *
  9. * Released under the GNU General Public License, version 2,
  10. * included herein by reference.
  11. *
  12. */
  13. #include <stdio.h>
  14. #include <linux/rtc.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #ifndef ARRAY_SIZE
  23. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  24. #endif
  25. /*
  26. * This expects the new RTC class driver framework, working with
  27. * clocks that will often not be clones of what the PC-AT had.
  28. * Use the command line to specify another RTC if you need one.
  29. */
  30. static const char default_rtc[] = "/dev/rtc0";
  31. static struct rtc_time cutoff_dates[] = {
  32. {
  33. .tm_year = 70, /* 1970 -1900 */
  34. .tm_mday = 1,
  35. },
  36. /* signed time_t 19/01/2038 3:14:08 */
  37. {
  38. .tm_year = 138,
  39. .tm_mday = 19,
  40. },
  41. {
  42. .tm_year = 138,
  43. .tm_mday = 20,
  44. },
  45. {
  46. .tm_year = 199, /* 2099 -1900 */
  47. .tm_mday = 1,
  48. },
  49. {
  50. .tm_year = 200, /* 2100 -1900 */
  51. .tm_mday = 1,
  52. },
  53. /* unsigned time_t 07/02/2106 7:28:15*/
  54. {
  55. .tm_year = 205,
  56. .tm_mon = 1,
  57. .tm_mday = 7,
  58. },
  59. {
  60. .tm_year = 206,
  61. .tm_mon = 1,
  62. .tm_mday = 8,
  63. },
  64. /* signed time on 64bit in nanoseconds 12/04/2262 01:47:16*/
  65. {
  66. .tm_year = 362,
  67. .tm_mon = 3,
  68. .tm_mday = 12,
  69. },
  70. {
  71. .tm_year = 362, /* 2262 -1900 */
  72. .tm_mon = 3,
  73. .tm_mday = 13,
  74. },
  75. };
  76. static int compare_dates(struct rtc_time *a, struct rtc_time *b)
  77. {
  78. if (a->tm_year != b->tm_year ||
  79. a->tm_mon != b->tm_mon ||
  80. a->tm_mday != b->tm_mday ||
  81. a->tm_hour != b->tm_hour ||
  82. a->tm_min != b->tm_min ||
  83. ((b->tm_sec - a->tm_sec) > 1))
  84. return 1;
  85. return 0;
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. int i, fd, retval, irqcount = 0, dangerous = 0;
  90. unsigned long tmp, data;
  91. struct rtc_time rtc_tm;
  92. const char *rtc = default_rtc;
  93. struct timeval start, end, diff;
  94. switch (argc) {
  95. case 3:
  96. if (*argv[2] == 'd')
  97. dangerous = 1;
  98. case 2:
  99. rtc = argv[1];
  100. /* FALLTHROUGH */
  101. case 1:
  102. break;
  103. default:
  104. fprintf(stderr, "usage: rtctest [rtcdev] [d]\n");
  105. return 1;
  106. }
  107. fd = open(rtc, O_RDONLY);
  108. if (fd == -1) {
  109. perror(rtc);
  110. exit(errno);
  111. }
  112. fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
  113. /* Turn on update interrupts (one per second) */
  114. retval = ioctl(fd, RTC_UIE_ON, 0);
  115. if (retval == -1) {
  116. if (errno == EINVAL) {
  117. fprintf(stderr,
  118. "\n...Update IRQs not supported.\n");
  119. goto test_READ;
  120. }
  121. perror("RTC_UIE_ON ioctl");
  122. exit(errno);
  123. }
  124. fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
  125. rtc);
  126. fflush(stderr);
  127. for (i=1; i<6; i++) {
  128. /* This read will block */
  129. retval = read(fd, &data, sizeof(unsigned long));
  130. if (retval == -1) {
  131. perror("read");
  132. exit(errno);
  133. }
  134. fprintf(stderr, " %d",i);
  135. fflush(stderr);
  136. irqcount++;
  137. }
  138. fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
  139. fflush(stderr);
  140. for (i=1; i<6; i++) {
  141. struct timeval tv = {5, 0}; /* 5 second timeout on select */
  142. fd_set readfds;
  143. FD_ZERO(&readfds);
  144. FD_SET(fd, &readfds);
  145. /* The select will wait until an RTC interrupt happens. */
  146. retval = select(fd+1, &readfds, NULL, NULL, &tv);
  147. if (retval == -1) {
  148. perror("select");
  149. exit(errno);
  150. }
  151. /* This read won't block unlike the select-less case above. */
  152. retval = read(fd, &data, sizeof(unsigned long));
  153. if (retval == -1) {
  154. perror("read");
  155. exit(errno);
  156. }
  157. fprintf(stderr, " %d",i);
  158. fflush(stderr);
  159. irqcount++;
  160. }
  161. /* Turn off update interrupts */
  162. retval = ioctl(fd, RTC_UIE_OFF, 0);
  163. if (retval == -1) {
  164. perror("RTC_UIE_OFF ioctl");
  165. exit(errno);
  166. }
  167. test_READ:
  168. /* Read the RTC time/date */
  169. retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  170. if (retval == -1) {
  171. perror("RTC_RD_TIME ioctl");
  172. exit(errno);
  173. }
  174. fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
  175. rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  176. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  177. /* Set the alarm to 5 sec in the future, and check for rollover */
  178. rtc_tm.tm_sec += 5;
  179. if (rtc_tm.tm_sec >= 60) {
  180. rtc_tm.tm_sec %= 60;
  181. rtc_tm.tm_min++;
  182. }
  183. if (rtc_tm.tm_min == 60) {
  184. rtc_tm.tm_min = 0;
  185. rtc_tm.tm_hour++;
  186. }
  187. if (rtc_tm.tm_hour == 24)
  188. rtc_tm.tm_hour = 0;
  189. retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
  190. if (retval == -1) {
  191. if (errno == EINVAL) {
  192. fprintf(stderr,
  193. "\n...Alarm IRQs not supported.\n");
  194. goto test_PIE;
  195. }
  196. perror("RTC_ALM_SET ioctl");
  197. exit(errno);
  198. }
  199. /* Read the current alarm settings */
  200. retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
  201. if (retval == -1) {
  202. if (errno == EINVAL) {
  203. fprintf(stderr,
  204. "\n...EINVAL reading current alarm setting.\n");
  205. goto test_PIE;
  206. }
  207. perror("RTC_ALM_READ ioctl");
  208. exit(errno);
  209. }
  210. fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
  211. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  212. /* Enable alarm interrupts */
  213. retval = ioctl(fd, RTC_AIE_ON, 0);
  214. if (retval == -1) {
  215. if (errno == EINVAL || errno == EIO) {
  216. fprintf(stderr,
  217. "\n...Alarm IRQs not supported.\n");
  218. goto test_PIE;
  219. }
  220. perror("RTC_AIE_ON ioctl");
  221. exit(errno);
  222. }
  223. fprintf(stderr, "Waiting 5 seconds for alarm...");
  224. fflush(stderr);
  225. /* This blocks until the alarm ring causes an interrupt */
  226. retval = read(fd, &data, sizeof(unsigned long));
  227. if (retval == -1) {
  228. perror("read");
  229. exit(errno);
  230. }
  231. irqcount++;
  232. fprintf(stderr, " okay. Alarm rang.\n");
  233. /* Disable alarm interrupts */
  234. retval = ioctl(fd, RTC_AIE_OFF, 0);
  235. if (retval == -1) {
  236. perror("RTC_AIE_OFF ioctl");
  237. exit(errno);
  238. }
  239. test_PIE:
  240. /* Read periodic IRQ rate */
  241. retval = ioctl(fd, RTC_IRQP_READ, &tmp);
  242. if (retval == -1) {
  243. /* not all RTCs support periodic IRQs */
  244. if (errno == EINVAL) {
  245. fprintf(stderr, "\nNo periodic IRQ support\n");
  246. goto test_DATE;
  247. }
  248. perror("RTC_IRQP_READ ioctl");
  249. exit(errno);
  250. }
  251. fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
  252. fprintf(stderr, "Counting 20 interrupts at:");
  253. fflush(stderr);
  254. /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
  255. for (tmp=2; tmp<=64; tmp*=2) {
  256. retval = ioctl(fd, RTC_IRQP_SET, tmp);
  257. if (retval == -1) {
  258. /* not all RTCs can change their periodic IRQ rate */
  259. if (errno == EINVAL) {
  260. fprintf(stderr,
  261. "\n...Periodic IRQ rate is fixed\n");
  262. goto test_DATE;
  263. }
  264. perror("RTC_IRQP_SET ioctl");
  265. exit(errno);
  266. }
  267. fprintf(stderr, "\n%ldHz:\t", tmp);
  268. fflush(stderr);
  269. /* Enable periodic interrupts */
  270. retval = ioctl(fd, RTC_PIE_ON, 0);
  271. if (retval == -1) {
  272. perror("RTC_PIE_ON ioctl");
  273. exit(errno);
  274. }
  275. for (i=1; i<21; i++) {
  276. gettimeofday(&start, NULL);
  277. /* This blocks */
  278. retval = read(fd, &data, sizeof(unsigned long));
  279. if (retval == -1) {
  280. perror("read");
  281. exit(errno);
  282. }
  283. gettimeofday(&end, NULL);
  284. timersub(&end, &start, &diff);
  285. if (diff.tv_sec > 0 ||
  286. diff.tv_usec > ((1000000L / tmp) * 1.10)) {
  287. fprintf(stderr, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
  288. diff.tv_sec, diff.tv_usec,
  289. (1000000L / tmp));
  290. fflush(stdout);
  291. exit(-1);
  292. }
  293. fprintf(stderr, " %d",i);
  294. fflush(stderr);
  295. irqcount++;
  296. }
  297. /* Disable periodic interrupts */
  298. retval = ioctl(fd, RTC_PIE_OFF, 0);
  299. if (retval == -1) {
  300. perror("RTC_PIE_OFF ioctl");
  301. exit(errno);
  302. }
  303. }
  304. test_DATE:
  305. if (!dangerous)
  306. goto done;
  307. fprintf(stderr, "\nTesting problematic dates\n");
  308. for (i = 0; i < ARRAY_SIZE(cutoff_dates); i++) {
  309. struct rtc_time current;
  310. /* Write the new date in RTC */
  311. retval = ioctl(fd, RTC_SET_TIME, &cutoff_dates[i]);
  312. if (retval == -1) {
  313. perror("RTC_SET_TIME ioctl");
  314. close(fd);
  315. exit(errno);
  316. }
  317. /* Read back */
  318. retval = ioctl(fd, RTC_RD_TIME, &current);
  319. if (retval == -1) {
  320. perror("RTC_RD_TIME ioctl");
  321. exit(errno);
  322. }
  323. if(compare_dates(&cutoff_dates[i], &current)) {
  324. fprintf(stderr,"Setting date %d failed\n",
  325. cutoff_dates[i].tm_year + 1900);
  326. goto done;
  327. }
  328. cutoff_dates[i].tm_sec += 5;
  329. /* Write the new alarm in RTC */
  330. retval = ioctl(fd, RTC_ALM_SET, &cutoff_dates[i]);
  331. if (retval == -1) {
  332. perror("RTC_ALM_SET ioctl");
  333. close(fd);
  334. exit(errno);
  335. }
  336. /* Read back */
  337. retval = ioctl(fd, RTC_ALM_READ, &current);
  338. if (retval == -1) {
  339. perror("RTC_ALM_READ ioctl");
  340. exit(errno);
  341. }
  342. if(compare_dates(&cutoff_dates[i], &current)) {
  343. fprintf(stderr,"Setting alarm %d failed\n",
  344. cutoff_dates[i].tm_year + 1900);
  345. goto done;
  346. }
  347. fprintf(stderr, "Setting year %d is OK \n",
  348. cutoff_dates[i].tm_year + 1900);
  349. }
  350. done:
  351. fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
  352. close(fd);
  353. return 0;
  354. }