staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / rtc / rtctest.c
blob66af608fb4c6a9c91c8c42867fe878a56a14b67b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Real Time Clock Driver Test Program
5 * Copyright (c) 2018 Alexandre Belloni <alexandre.belloni@bootlin.com>
6 */
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>
19 #include "../kselftest_harness.h"
21 #define NUM_UIE 3
22 #define ALARM_DELTA 3
24 static char *rtc_file = "/dev/rtc0";
26 FIXTURE(rtc) {
27 int fd;
30 FIXTURE_SETUP(rtc) {
31 self->fd = open(rtc_file, O_RDONLY);
32 ASSERT_NE(-1, self->fd);
35 FIXTURE_TEARDOWN(rtc) {
36 close(self->fd);
39 TEST_F(rtc, date_read) {
40 int rc;
41 struct rtc_time rtc_tm;
43 /* Read the RTC time/date */
44 rc = ioctl(self->fd, RTC_RD_TIME, &rtc_tm);
45 ASSERT_NE(-1, rc);
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);
52 TEST_F_TIMEOUT(rtc, uie_read, NUM_UIE + 2) {
53 int i, rc, irq = 0;
54 unsigned long data;
56 /* Turn on update interrupts */
57 rc = ioctl(self->fd, RTC_UIE_ON, 0);
58 if (rc == -1) {
59 ASSERT_EQ(EINVAL, errno);
60 TH_LOG("skip update IRQs not supported.");
61 return;
64 for (i = 0; i < NUM_UIE; i++) {
65 /* This read will block */
66 rc = read(self->fd, &data, sizeof(data));
67 ASSERT_NE(-1, rc);
68 irq++;
71 EXPECT_EQ(NUM_UIE, irq);
73 rc = ioctl(self->fd, RTC_UIE_OFF, 0);
74 ASSERT_NE(-1, rc);
77 TEST_F(rtc, uie_select) {
78 int i, rc, irq = 0;
79 unsigned long data;
81 /* Turn on update interrupts */
82 rc = ioctl(self->fd, RTC_UIE_ON, 0);
83 if (rc == -1) {
84 ASSERT_EQ(EINVAL, errno);
85 TH_LOG("skip update IRQs not supported.");
86 return;
89 for (i = 0; i < NUM_UIE; i++) {
90 struct timeval tv = { .tv_sec = 2 };
91 fd_set readfds;
93 FD_ZERO(&readfds);
94 FD_SET(self->fd, &readfds);
95 /* The select will wait until an RTC interrupt happens. */
96 rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
97 ASSERT_NE(-1, rc);
98 ASSERT_NE(0, rc);
100 /* This read won't block */
101 rc = read(self->fd, &data, sizeof(unsigned long));
102 ASSERT_NE(-1, rc);
103 irq++;
106 EXPECT_EQ(NUM_UIE, irq);
108 rc = ioctl(self->fd, RTC_UIE_OFF, 0);
109 ASSERT_NE(-1, rc);
112 TEST_F(rtc, alarm_alm_set) {
113 struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
114 unsigned long data;
115 struct rtc_time tm;
116 fd_set readfds;
117 time_t secs, new;
118 int rc;
120 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
121 ASSERT_NE(-1, rc);
123 secs = timegm((struct tm *)&tm) + ALARM_DELTA;
124 gmtime_r(&secs, (struct tm *)&tm);
126 rc = ioctl(self->fd, RTC_ALM_SET, &tm);
127 if (rc == -1) {
128 ASSERT_EQ(EINVAL, errno);
129 TH_LOG("skip alarms are not supported.");
130 return;
133 rc = ioctl(self->fd, RTC_ALM_READ, &tm);
134 ASSERT_NE(-1, rc);
136 TH_LOG("Alarm time now set to %02d:%02d:%02d.",
137 tm.tm_hour, tm.tm_min, tm.tm_sec);
139 /* Enable alarm interrupts */
140 rc = ioctl(self->fd, RTC_AIE_ON, 0);
141 ASSERT_NE(-1, rc);
143 FD_ZERO(&readfds);
144 FD_SET(self->fd, &readfds);
146 rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
147 ASSERT_NE(-1, rc);
148 ASSERT_NE(0, rc);
150 /* Disable alarm interrupts */
151 rc = ioctl(self->fd, RTC_AIE_OFF, 0);
152 ASSERT_NE(-1, rc);
154 rc = read(self->fd, &data, sizeof(unsigned long));
155 ASSERT_NE(-1, rc);
156 TH_LOG("data: %lx", data);
158 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
159 ASSERT_NE(-1, rc);
161 new = timegm((struct tm *)&tm);
162 ASSERT_EQ(new, secs);
165 TEST_F(rtc, alarm_wkalm_set) {
166 struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
167 struct rtc_wkalrm alarm = { 0 };
168 struct rtc_time tm;
169 unsigned long data;
170 fd_set readfds;
171 time_t secs, new;
172 int rc;
174 rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time);
175 ASSERT_NE(-1, rc);
177 secs = timegm((struct tm *)&alarm.time) + ALARM_DELTA;
178 gmtime_r(&secs, (struct tm *)&alarm.time);
180 alarm.enabled = 1;
182 rc = ioctl(self->fd, RTC_WKALM_SET, &alarm);
183 if (rc == -1) {
184 ASSERT_EQ(EINVAL, errno);
185 TH_LOG("skip alarms are not supported.");
186 return;
189 rc = ioctl(self->fd, RTC_WKALM_RD, &alarm);
190 ASSERT_NE(-1, rc);
192 TH_LOG("Alarm time now set to %02d/%02d/%02d %02d:%02d:%02d.",
193 alarm.time.tm_mday, alarm.time.tm_mon + 1,
194 alarm.time.tm_year + 1900, alarm.time.tm_hour,
195 alarm.time.tm_min, alarm.time.tm_sec);
197 FD_ZERO(&readfds);
198 FD_SET(self->fd, &readfds);
200 rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
201 ASSERT_NE(-1, rc);
202 ASSERT_NE(0, rc);
204 rc = read(self->fd, &data, sizeof(unsigned long));
205 ASSERT_NE(-1, rc);
207 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
208 ASSERT_NE(-1, rc);
210 new = timegm((struct tm *)&tm);
211 ASSERT_EQ(new, secs);
214 TEST_F_TIMEOUT(rtc, alarm_alm_set_minute, 65) {
215 struct timeval tv = { .tv_sec = 62 };
216 unsigned long data;
217 struct rtc_time tm;
218 fd_set readfds;
219 time_t secs, new;
220 int rc;
222 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
223 ASSERT_NE(-1, rc);
225 secs = timegm((struct tm *)&tm) + 60 - tm.tm_sec;
226 gmtime_r(&secs, (struct tm *)&tm);
228 rc = ioctl(self->fd, RTC_ALM_SET, &tm);
229 if (rc == -1) {
230 ASSERT_EQ(EINVAL, errno);
231 TH_LOG("skip alarms are not supported.");
232 return;
235 rc = ioctl(self->fd, RTC_ALM_READ, &tm);
236 ASSERT_NE(-1, rc);
238 TH_LOG("Alarm time now set to %02d:%02d:%02d.",
239 tm.tm_hour, tm.tm_min, tm.tm_sec);
241 /* Enable alarm interrupts */
242 rc = ioctl(self->fd, RTC_AIE_ON, 0);
243 ASSERT_NE(-1, rc);
245 FD_ZERO(&readfds);
246 FD_SET(self->fd, &readfds);
248 rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
249 ASSERT_NE(-1, rc);
250 ASSERT_NE(0, rc);
252 /* Disable alarm interrupts */
253 rc = ioctl(self->fd, RTC_AIE_OFF, 0);
254 ASSERT_NE(-1, rc);
256 rc = read(self->fd, &data, sizeof(unsigned long));
257 ASSERT_NE(-1, rc);
258 TH_LOG("data: %lx", data);
260 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
261 ASSERT_NE(-1, rc);
263 new = timegm((struct tm *)&tm);
264 ASSERT_EQ(new, secs);
267 TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
268 struct timeval tv = { .tv_sec = 62 };
269 struct rtc_wkalrm alarm = { 0 };
270 struct rtc_time tm;
271 unsigned long data;
272 fd_set readfds;
273 time_t secs, new;
274 int rc;
276 rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time);
277 ASSERT_NE(-1, rc);
279 secs = timegm((struct tm *)&alarm.time) + 60 - alarm.time.tm_sec;
280 gmtime_r(&secs, (struct tm *)&alarm.time);
282 alarm.enabled = 1;
284 rc = ioctl(self->fd, RTC_WKALM_SET, &alarm);
285 if (rc == -1) {
286 ASSERT_EQ(EINVAL, errno);
287 TH_LOG("skip alarms are not supported.");
288 return;
291 rc = ioctl(self->fd, RTC_WKALM_RD, &alarm);
292 ASSERT_NE(-1, rc);
294 TH_LOG("Alarm time now set to %02d/%02d/%02d %02d:%02d:%02d.",
295 alarm.time.tm_mday, alarm.time.tm_mon + 1,
296 alarm.time.tm_year + 1900, alarm.time.tm_hour,
297 alarm.time.tm_min, alarm.time.tm_sec);
299 FD_ZERO(&readfds);
300 FD_SET(self->fd, &readfds);
302 rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
303 ASSERT_NE(-1, rc);
304 ASSERT_NE(0, rc);
306 rc = read(self->fd, &data, sizeof(unsigned long));
307 ASSERT_NE(-1, rc);
309 rc = ioctl(self->fd, RTC_RD_TIME, &tm);
310 ASSERT_NE(-1, rc);
312 new = timegm((struct tm *)&tm);
313 ASSERT_EQ(new, secs);
316 static void __attribute__((constructor))
317 __constructor_order_last(void)
319 if (!__constructor_order)
320 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
323 int main(int argc, char **argv)
325 switch (argc) {
326 case 2:
327 rtc_file = argv[1];
328 /* FALLTHROUGH */
329 case 1:
330 break;
331 default:
332 fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
333 return 1;
336 return test_harness_run(argc, argv);