1 /* rtc-test.c
2 *
3 * Tests for the Real Time Clock driver.
4 *
5 * Copyright (c) Larsen & Toubro Infotech Ltd., 2010
6 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
7 *
8 * Author : Silesh C V <Silesh.Vellattu@lntinfotech.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 #include <sys/ioctl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <linux/rtc.h>
32 #include <errno.h>
33 #include <time.h>
34
35 #include "test.h"
36
37 int rtc_fd = -1;
38 char *TCID = "rtc01";
39 int TST_TOTAL = 3;
40
41 static char *rtc_dev = "/dev/rtc";
42 static int dflag;
43 static const option_t options[] = {
44 {"d:", &dflag, &rtc_dev},
45 {NULL, NULL, NULL}
46 };
47
help(void)48 static void help(void)
49 {
50 printf(" -d x rtc device node, default is %s\n",
51 rtc_dev);
52 }
53
54 /* Read and Alarm Tests : Read test reads the Date/time from RTC
55 * while Alarm test, sets the alarm to 5 seconds in future and
56 * waits for it to ring.The ioctls tested in these tests are
57 * RTC_RD_TIME, RTC_ALM_SET, RTC_ALM_READ, RTC_AIE_OFF */
58
read_alarm_test(void)59 void read_alarm_test(void)
60 {
61 struct rtc_time rtc_tm;
62 int ret;
63 unsigned long data;
64 fd_set rfds;
65 struct timeval tv;
66
67 tst_resm(TINFO, "RTC READ TEST:");
68
69 /*Read RTC Time */
70 ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm);
71 if (ret == -1) {
72 tst_resm(TFAIL, "RTC_RD_TIME ioctl failed");
73 return;
74 }
75
76 tst_resm(TPASS, "RTC READ TEST Passed");
77
78 tst_resm(TINFO, "Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.",
79 rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
80 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
81
82 tst_resm(TINFO, "RTC ALARM TEST :");
83
84 /*set Alarm to 5 Seconds */
85 rtc_tm.tm_sec += 5;
86 if (rtc_tm.tm_sec >= 60) {
87 rtc_tm.tm_sec %= 60;
88 rtc_tm.tm_min++;
89 }
90
91 if (rtc_tm.tm_min == 60) {
92 rtc_tm.tm_min = 0;
93 rtc_tm.tm_hour++;
94 }
95
96 if (rtc_tm.tm_hour == 24)
97 rtc_tm.tm_hour = 0;
98
99 ret = ioctl(rtc_fd, RTC_ALM_SET, &rtc_tm);
100 if (ret == -1) {
101 if (errno == EINVAL)
102 tst_resm(TCONF | TERRNO, "RTC_ALM_SET not supported");
103 else
104 tst_resm(TFAIL | TERRNO , "RTC_ALM_SET ioctl failed");
105 return;
106 }
107
108 /*Read current alarm time */
109 ret = ioctl(rtc_fd, RTC_ALM_READ, &rtc_tm);
110 if (ret == -1) {
111 tst_resm(TFAIL, "RTC_ALM_READ ioctl failed");
112 return;
113 }
114
115 tst_resm(TINFO, "Alarm time set to %02d:%02d:%02d.",
116 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
117 /* Enable alarm interrupts */
118 ret = ioctl(rtc_fd, RTC_AIE_ON, 0);
119 if (ret == -1) {
120 tst_resm(TINFO, "RTC_AIE_ON ioctl failed");
121 return;
122 }
123
124 tst_resm(TINFO, "Waiting 5 seconds for the alarm...");
125
126 tv.tv_sec = 6; /*set 6 seconds as the time out */
127 tv.tv_usec = 0;
128
129 FD_ZERO(&rfds);
130 FD_SET(rtc_fd, &rfds);
131
132 ret = select(rtc_fd + 1, &rfds, NULL, NULL, &tv); /*wait for alarm */
133
134 if (ret == -1) {
135 tst_resm(TFAIL, "select failed");
136 return;
137 } else if (ret) {
138 ret = read(rtc_fd, &data, sizeof(unsigned long));
139 if (ret == -1) {
140 tst_resm(TFAIL, "read failed");
141 return;
142 }
143 tst_resm(TINFO, "Alarm rang.");
144 } else {
145 tst_resm(TFAIL, "Timed out waiting for the alarm");
146 return;
147 }
148
149 /* Disable alarm interrupts */
150 ret = ioctl(rtc_fd, RTC_AIE_OFF, 0);
151 if (ret == -1) {
152 tst_resm(TFAIL, "RTC_AIE_OFF ioctl failed");
153 return;
154 }
155 tst_resm(TPASS, "RTC ALARM TEST Passed");
156 }
157
158 /* Update_interrupts_test :Once the Update interrupts is enabled,
159 * the RTC gives interrupts (1/sec) on the interrupts line(if the rtc
160 * has one). This is tested by enabling the update interrupts
161 * and then waiting for 5 interrupts.*/
162
update_interrupts_test(void)163 void update_interrupts_test(void)
164 {
165 int ret, i;
166 unsigned long data;
167 fd_set rfds;
168 struct timeval tv;
169
170 tst_resm(TINFO, "RTC UPDATE INTERRUPTS TEST :");
171 /*Turn on update interrupts */
172 ret = ioctl(rtc_fd, RTC_UIE_ON, 0);
173 if (ret == -1) {
174 if (errno == EINVAL)
175 tst_resm(TCONF | TERRNO, "RTC_UIE_ON not supported");
176 else
177 tst_resm(TFAIL | TERRNO, "RTC_UIE_ON ioctl failed");
178 return;
179 }
180
181 tst_resm(TINFO, "Waiting for 5 update interrupts...");
182 for (i = 1; i < 6; i++) {
183
184 tv.tv_sec = 2; /*2 sec time out for each interrupt */
185 tv.tv_usec = 0;
186
187 FD_ZERO(&rfds);
188 FD_SET(rtc_fd, &rfds);
189
190 ret = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);
191 if (ret == -1) {
192 tst_resm(TFAIL, "select failed");
193 return;
194 } else if (ret) {
195 ret = read(rtc_fd, &data, sizeof(unsigned long));
196 if (ret == -1) {
197 tst_resm(TFAIL, "read failed");
198 return;
199 }
200 tst_resm(TINFO, "Update interrupt %d", i);
201 } else {
202 tst_resm(TFAIL,
203 "Timed out waiting for the update interrupt");
204 return;
205 }
206 }
207
208 /* Turn off update interrupts */
209 ret = ioctl(rtc_fd, RTC_UIE_OFF, 0);
210 if (ret == -1) {
211 tst_resm(TFAIL, "RTC_UIE_OFF ioctl failed");
212 return;
213 }
214 tst_resm(TPASS, "RTC UPDATE INTERRUPTS TEST Passed");
215 }
216
main(int argc,char * argv[])217 int main(int argc, char *argv[])
218 {
219 tst_parse_opts(argc, argv, options, help);
220
221 tst_require_root();
222
223 if (access(rtc_dev, F_OK) == -1)
224 tst_brkm(TCONF, NULL, "couldn't find rtc device '%s'", rtc_dev);
225
226 rtc_fd = open(rtc_dev, O_RDONLY);
227
228 if (rtc_fd < 0)
229 tst_brkm(TBROK | TERRNO, NULL, "couldn't open %s", rtc_dev);
230
231 /*Read and alarm tests */
232 read_alarm_test();
233
234 /*Update interrupts test */
235 update_interrupts_test();
236
237 close(rtc_fd);
238
239 tst_resm(TINFO, "RTC Tests Done!");
240 tst_exit();
241 }
242