• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Real Time Clock Driver Test Program
4  *
5  * Copyright (c) 2018 Alexandre Belloni <alexandre.belloni@bootlin.com>
6  */
7 
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <linux/rtc.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/ioctl.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <time.h>
17 #include <unistd.h>
18 
19 #include "../kselftest_harness.h"
20 
21 #define NUM_UIE 3
22 #define ALARM_DELTA 3
23 
24 static char *rtc_file = "/dev/rtc0";
25 
FIXTURE(rtc)26 FIXTURE(rtc) {
27 	int fd;
28 };
29 
FIXTURE_SETUP(rtc)30 FIXTURE_SETUP(rtc) {
31 	self->fd = open(rtc_file, O_RDONLY);
32 	ASSERT_NE(-1, self->fd);
33 }
34 
FIXTURE_TEARDOWN(rtc)35 FIXTURE_TEARDOWN(rtc) {
36 	close(self->fd);
37 }
38 
TEST_F(rtc,date_read)39 TEST_F(rtc, date_read) {
40 	int rc;
41 	struct rtc_time rtc_tm;
42 
43 	/* Read the RTC time/date */
44 	rc = ioctl(self->fd, RTC_RD_TIME, &rtc_tm);
45 	ASSERT_NE(-1, rc);
46 
47 	TH_LOG("Current RTC date/time is %02d/%02d/%02d %02d:%02d:%02d.",
48 	       rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
49 	       rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
50 }
51 
52 #ifndef __ANDROID__ // b/31578457
TEST_F(rtc,uie_read)53 TEST_F(rtc, uie_read) {
54 	int i, rc, irq = 0;
55 	unsigned long data;
56 
57 	/* Turn on update interrupts */
58 	rc = ioctl(self->fd, RTC_UIE_ON, 0);
59 	if (rc == -1) {
60 		ASSERT_EQ(EINVAL, errno);
61 		TH_LOG("skip update IRQs not supported.");
62 		return;
63 	}
64 
65 	for (i = 0; i < NUM_UIE; i++) {
66 		/* This read will block */
67 		rc = read(self->fd, &data, sizeof(data));
68 		ASSERT_NE(-1, rc);
69 		irq++;
70 	}
71 
72 	EXPECT_EQ(NUM_UIE, irq);
73 
74 	rc = ioctl(self->fd, RTC_UIE_OFF, 0);
75 	ASSERT_NE(-1, rc);
76 }
77 
TEST_F(rtc,uie_select)78 TEST_F(rtc, uie_select) {
79 	int i, rc, irq = 0;
80 	unsigned long data;
81 
82 	/* Turn on update interrupts */
83 	rc = ioctl(self->fd, RTC_UIE_ON, 0);
84 	if (rc == -1) {
85 		ASSERT_EQ(EINVAL, errno);
86 		TH_LOG("skip update IRQs not supported.");
87 		return;
88 	}
89 
90 	for (i = 0; i < NUM_UIE; i++) {
91 		struct timeval tv = { .tv_sec = 2 };
92 		fd_set readfds;
93 
94 		FD_ZERO(&readfds);
95 		FD_SET(self->fd, &readfds);
96 		/* The select will wait until an RTC interrupt happens. */
97 		rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
98 		ASSERT_NE(-1, rc);
99 		ASSERT_NE(0, rc);
100 
101 		/* This read won't block */
102 		rc = read(self->fd, &data, sizeof(unsigned long));
103 		ASSERT_NE(-1, rc);
104 		irq++;
105 	}
106 
107 	EXPECT_EQ(NUM_UIE, irq);
108 
109 	rc = ioctl(self->fd, RTC_UIE_OFF, 0);
110 	ASSERT_NE(-1, rc);
111 }
112 
TEST_F(rtc,alarm_alm_set)113 TEST_F(rtc, alarm_alm_set) {
114 	struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
115 	unsigned long data;
116 	struct rtc_time tm;
117 	fd_set readfds;
118 	time_t secs, new;
119 	int rc;
120 
121 	rc = ioctl(self->fd, RTC_RD_TIME, &tm);
122 	ASSERT_NE(-1, rc);
123 
124 	secs = timegm((struct tm *)&tm) + ALARM_DELTA;
125 	gmtime_r(&secs, (struct tm *)&tm);
126 
127 	rc = ioctl(self->fd, RTC_ALM_SET, &tm);
128 	if (rc == -1) {
129 		ASSERT_EQ(EINVAL, errno);
130 		TH_LOG("skip alarms are not supported.");
131 		return;
132 	}
133 
134 	rc = ioctl(self->fd, RTC_ALM_READ, &tm);
135 	ASSERT_NE(-1, rc);
136 
137 	TH_LOG("Alarm time now set to %02d:%02d:%02d.",
138 	       tm.tm_hour, tm.tm_min, tm.tm_sec);
139 
140 	/* Enable alarm interrupts */
141 	rc = ioctl(self->fd, RTC_AIE_ON, 0);
142 	ASSERT_NE(-1, rc);
143 
144 	FD_ZERO(&readfds);
145 	FD_SET(self->fd, &readfds);
146 
147 	rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
148 	ASSERT_NE(-1, rc);
149 	EXPECT_NE(0, rc);
150 
151 	/* Disable alarm interrupts */
152 	rc = ioctl(self->fd, RTC_AIE_OFF, 0);
153 	ASSERT_NE(-1, rc);
154 
155 	if (rc == 0)
156 		return;
157 
158 	rc = read(self->fd, &data, sizeof(unsigned long));
159 	ASSERT_NE(-1, rc);
160 	TH_LOG("data: %lx", data);
161 
162 	rc = ioctl(self->fd, RTC_RD_TIME, &tm);
163 	ASSERT_NE(-1, rc);
164 
165 	new = timegm((struct tm *)&tm);
166 	ASSERT_EQ(new, secs);
167 }
168 
TEST_F(rtc,alarm_wkalm_set)169 TEST_F(rtc, alarm_wkalm_set) {
170 	struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
171 	struct rtc_wkalrm alarm = { 0 };
172 	struct rtc_time tm;
173 	unsigned long data;
174 	fd_set readfds;
175 	time_t secs, new;
176 	int rc;
177 
178 	rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time);
179 	ASSERT_NE(-1, rc);
180 
181 	secs = timegm((struct tm *)&alarm.time) + ALARM_DELTA;
182 	gmtime_r(&secs, (struct tm *)&alarm.time);
183 
184 	alarm.enabled = 1;
185 
186 	rc = ioctl(self->fd, RTC_WKALM_SET, &alarm);
187 	if (rc == -1) {
188 		ASSERT_EQ(EINVAL, errno);
189 		TH_LOG("skip alarms are not supported.");
190 		return;
191 	}
192 
193 	rc = ioctl(self->fd, RTC_WKALM_RD, &alarm);
194 	ASSERT_NE(-1, rc);
195 
196 	TH_LOG("Alarm time now set to %02d/%02d/%02d %02d:%02d:%02d.",
197 	       alarm.time.tm_mday, alarm.time.tm_mon + 1,
198 	       alarm.time.tm_year + 1900, alarm.time.tm_hour,
199 	       alarm.time.tm_min, alarm.time.tm_sec);
200 
201 	FD_ZERO(&readfds);
202 	FD_SET(self->fd, &readfds);
203 
204 	rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
205 	ASSERT_NE(-1, rc);
206 	EXPECT_NE(0, rc);
207 
208 	rc = read(self->fd, &data, sizeof(unsigned long));
209 	ASSERT_NE(-1, rc);
210 
211 	rc = ioctl(self->fd, RTC_RD_TIME, &tm);
212 	ASSERT_NE(-1, rc);
213 
214 	new = timegm((struct tm *)&tm);
215 	ASSERT_EQ(new, secs);
216 }
217 #endif
218 
219 static void __attribute__((constructor))
__constructor_order_last(void)220 __constructor_order_last(void)
221 {
222 	if (!__constructor_order)
223 		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
224 }
225 
main(int argc,char ** argv)226 int main(int argc, char **argv)
227 {
228 	switch (argc) {
229 	case 2:
230 		rtc_file = argv[1];
231 		/* FALLTHROUGH */
232 	case 1:
233 		break;
234 	default:
235 		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
236 		return 1;
237 	}
238 
239 	return test_harness_run(argc, argv);
240 }
241