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