• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 | TERRNO, "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 		if (errno == EINVAL) {
112 			tst_resm(TCONF | TERRNO, "RTC_ALM_READ not suported");
113 		} else {
114 			tst_resm(TFAIL | TERRNO, "RTC_ALM_READ ioctl failed");
115 			return;
116 		}
117 	} else {
118 		tst_resm(TINFO, "Alarm time set to %02d:%02d:%02d.",
119 			 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
120 	}
121 
122 	/* Enable alarm interrupts */
123 	ret = ioctl(rtc_fd, RTC_AIE_ON, 0);
124 	if (ret == -1) {
125 		tst_resm(TINFO | TERRNO, "RTC_AIE_ON ioctl failed");
126 		return;
127 	}
128 
129 	tst_resm(TINFO, "Waiting 5 seconds for the alarm...");
130 
131 	tv.tv_sec = 6;		/*set 6 seconds as the time out */
132 	tv.tv_usec = 0;
133 
134 	FD_ZERO(&rfds);
135 	FD_SET(rtc_fd, &rfds);
136 
137 	ret = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);	/*wait for alarm */
138 
139 	if (ret == -1) {
140 		tst_resm(TFAIL | TERRNO, "select failed");
141 		return;
142 	} else if (ret) {
143 		ret = read(rtc_fd, &data, sizeof(unsigned long));
144 		if (ret == -1) {
145 			tst_resm(TFAIL | TERRNO, "read failed");
146 			return;
147 		}
148 		tst_resm(TINFO, "Alarm rang.");
149 	} else {
150 		tst_resm(TFAIL, "Timed out waiting for the alarm");
151 		return;
152 	}
153 
154 	/* Disable alarm interrupts */
155 	ret = ioctl(rtc_fd, RTC_AIE_OFF, 0);
156 	if (ret == -1) {
157 		tst_resm(TFAIL | TERRNO, "RTC_AIE_OFF ioctl failed");
158 		return;
159 	}
160 	tst_resm(TPASS, "RTC ALARM TEST Passed");
161 }
162 
163 /* Update_interrupts_test :Once the Update interrupts is enabled,
164  * the RTC gives interrupts (1/sec) on the interrupts line(if the rtc
165  * has one). This is tested by enabling the update interrupts
166  * and then waiting for 5 interrupts.*/
167 
update_interrupts_test(void)168 void update_interrupts_test(void)
169 {
170 	int ret, i;
171 	unsigned long data;
172 	fd_set rfds;
173 	struct timeval tv;
174 
175 	tst_resm(TINFO, "RTC UPDATE INTERRUPTS TEST :");
176 	/*Turn on update interrupts */
177 	ret = ioctl(rtc_fd, RTC_UIE_ON, 0);
178 	if (ret == -1) {
179 		if (errno == EINVAL)
180 			tst_resm(TCONF | TERRNO, "RTC_UIE_ON not supported");
181 		else
182 			tst_resm(TFAIL | TERRNO, "RTC_UIE_ON ioctl failed");
183 		return;
184 	}
185 
186 	tst_resm(TINFO, "Waiting for  5 update interrupts...");
187 	for (i = 1; i < 6; i++) {
188 
189 		tv.tv_sec = 2;	/*2 sec time out for each interrupt */
190 		tv.tv_usec = 0;
191 
192 		FD_ZERO(&rfds);
193 		FD_SET(rtc_fd, &rfds);
194 
195 		ret = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);
196 		if (ret == -1) {
197 			tst_resm(TFAIL | TERRNO, "select failed");
198 			return;
199 		} else if (ret) {
200 			ret = read(rtc_fd, &data, sizeof(unsigned long));
201 			if (ret == -1) {
202 				tst_resm(TFAIL | TERRNO, "read failed");
203 				return;
204 			}
205 			tst_resm(TINFO, "Update interrupt %d", i);
206 		} else {
207 			tst_resm(TFAIL,
208 				 "Timed out waiting for the update interrupt");
209 			return;
210 		}
211 	}
212 
213 	/* Turn off update interrupts */
214 	ret = ioctl(rtc_fd, RTC_UIE_OFF, 0);
215 	if (ret == -1) {
216 		tst_resm(TFAIL | TERRNO, "RTC_UIE_OFF ioctl failed");
217 		return;
218 	}
219 	tst_resm(TPASS, "RTC UPDATE INTERRUPTS TEST Passed");
220 }
221 
main(int argc,char * argv[])222 int main(int argc, char *argv[])
223 {
224 	tst_parse_opts(argc, argv, options, help);
225 
226 	tst_require_root();
227 
228 	if (access(rtc_dev, F_OK) == -1)
229 		tst_brkm(TCONF, NULL, "couldn't find rtc device '%s'", rtc_dev);
230 
231 	rtc_fd = open(rtc_dev, O_RDONLY);
232 
233 	if (rtc_fd < 0)
234 		tst_brkm(TBROK | TERRNO, NULL, "couldn't open %s", rtc_dev);
235 
236 	/*Read and alarm tests */
237 	read_alarm_test();
238 
239 	/*Update interrupts test */
240 	update_interrupts_test();
241 
242 	close(rtc_fd);
243 
244 	tst_resm(TINFO, "RTC Tests Done!");
245 	tst_exit();
246 }
247