1/*
2 * This file is part of ioctl_rtc strace test.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 The strace developers.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "tests.h"
32#include <errno.h>
33#include <stdio.h>
34#include <string.h>
35#include <sys/ioctl.h>
36#include <linux/types.h>
37#include <linux/rtc.h>
38#include "xlat.h"
39
40static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
41
42static void
43print_rtc_time(const struct rtc_time *rt)
44{
45	printf("{tm_sec=%d, tm_min=%d, tm_hour=%d"
46	       ", tm_mday=%d, tm_mon=%d, tm_year=%d",
47	       rt->tm_sec, rt->tm_min, rt->tm_hour,
48	       rt->tm_mday, rt->tm_mon, rt->tm_year);
49#if VERBOSE
50	printf(", tm_wday=%d, tm_yday=%d, tm_isdst=%d}",
51	       rt->tm_wday, rt->tm_yday, rt->tm_isdst);
52#else
53	printf(", ...}");
54#endif
55}
56
57static struct xlat rtc_argless[] = {
58	XLAT(RTC_AIE_OFF),
59	XLAT(RTC_PIE_ON),
60	XLAT(RTC_PIE_OFF),
61	XLAT(RTC_UIE_ON),
62	XLAT(RTC_WIE_ON),
63	XLAT(RTC_WIE_OFF),
64#ifdef RTC_VL_CLR
65	XLAT(RTC_VL_CLR),
66#endif
67};
68
69int
70main(void)
71{
72	const unsigned int size = get_page_size();
73
74	void *const page = tail_alloc(size);
75	fill_memory(page, size);
76
77	TAIL_ALLOC_OBJECT_CONST_PTR(struct rtc_time, rt);
78	fill_memory(rt, sizeof(*rt));
79
80	TAIL_ALLOC_OBJECT_CONST_PTR(struct rtc_wkalrm, wk);
81	fill_memory(wk, sizeof(*wk));
82
83	TAIL_ALLOC_OBJECT_CONST_PTR(struct rtc_pll_info, pll);
84	fill_memory(pll, sizeof(*pll));
85
86	/* RTC_ALM_READ */
87	ioctl(-1, RTC_ALM_READ, 0);
88	printf("ioctl(-1, RTC_ALM_READ, NULL) = -1 EBADF (%m)\n");
89
90	ioctl(-1, RTC_ALM_READ, page);
91	printf("ioctl(-1, RTC_ALM_READ, %p) = -1 EBADF (%m)\n", page);
92
93	/* RTC_RD_TIME */
94	ioctl(-1, RTC_RD_TIME, 0);
95	printf("ioctl(-1, RTC_RD_TIME, NULL) = -1 EBADF (%m)\n");
96
97	ioctl(-1, RTC_RD_TIME, page);
98	printf("ioctl(-1, RTC_RD_TIME, %p) = -1 EBADF (%m)\n", page);
99
100	/* RTC_ALM_SET */
101	ioctl(-1, RTC_ALM_SET, 0);
102	printf("ioctl(-1, RTC_ALM_SET, NULL) = -1 EBADF (%m)\n");
103
104	ioctl(-1, RTC_ALM_SET, rt);
105	printf("ioctl(-1, RTC_ALM_SET, ");
106	print_rtc_time(rt);
107	errno = EBADF;
108	printf(") = -1 EBADF (%m)\n");
109
110	/* RTC_SET_TIME */
111	ioctl(-1, RTC_SET_TIME, 0);
112	printf("ioctl(-1, RTC_SET_TIME, NULL) = -1 EBADF (%m)\n");
113
114	ioctl(-1, RTC_SET_TIME, rt);
115	printf("ioctl(-1, RTC_SET_TIME, ");
116	print_rtc_time(rt);
117	errno = EBADF;
118	printf(") = -1 EBADF (%m)\n");
119
120	/* RTC_IRQP_SET */
121	ioctl(-1, RTC_IRQP_SET, lmagic);
122	printf("ioctl(-1, RTC_IRQP_SET, %lu) = -1 EBADF (%m)\n", lmagic);
123
124	/* RTC_EPOCH_SET */
125	ioctl(-1, RTC_EPOCH_SET, lmagic);
126	printf("ioctl(-1, RTC_EPOCH_SET, %lu) = -1 EBADF (%m)\n", lmagic);
127
128	/* RTC_IRQP_READ */
129	ioctl(-1, RTC_IRQP_READ, 0);
130	printf("ioctl(-1, RTC_IRQP_READ, NULL) = -1 EBADF (%m)\n");
131
132	ioctl(-1, RTC_IRQP_READ, page);
133	printf("ioctl(-1, RTC_IRQP_READ, %p) = -1 EBADF (%m)\n", page);
134
135	/* RTC_EPOCH_READ */
136	ioctl(-1, RTC_EPOCH_READ, 0);
137	printf("ioctl(-1, RTC_EPOCH_READ, NULL) = -1 EBADF (%m)\n");
138
139	ioctl(-1, RTC_EPOCH_READ, page);
140	printf("ioctl(-1, RTC_EPOCH_READ, %p) = -1 EBADF (%m)\n", page);
141
142	/* RTC_WKALM_RD */
143	ioctl(-1, RTC_WKALM_RD, 0);
144	printf("ioctl(-1, RTC_WKALM_RD, NULL) = -1 EBADF (%m)\n");
145
146	ioctl(-1, RTC_WKALM_RD, page);
147	printf("ioctl(-1, RTC_WKALM_RD, %p) = -1 EBADF (%m)\n", page);
148
149	/* RTC_WKALM_SET */
150	ioctl(-1, RTC_WKALM_SET, 0);
151	printf("ioctl(-1, RTC_WKALM_SET, NULL) = -1 EBADF (%m)\n");
152
153	ioctl(-1, RTC_WKALM_SET, wk);
154	printf("ioctl(-1, RTC_WKALM_SET, {enabled=%u, pending=%u, time=",
155	       (unsigned) wk->enabled, (unsigned) wk->pending);
156	print_rtc_time(&wk->time);
157	errno = EBADF;
158	printf("}) = -1 EBADF (%m)\n");
159
160	/* RTC_PLL_GET */
161	ioctl(-1, RTC_PLL_GET, 0);
162	printf("ioctl(-1, RTC_PLL_GET, NULL) = -1 EBADF (%m)\n");
163
164	ioctl(-1, RTC_PLL_GET, page);
165	printf("ioctl(-1, RTC_PLL_GET, %p) = -1 EBADF (%m)\n", page);
166
167	/* RTC_PLL_SET */
168	ioctl(-1, RTC_PLL_SET, 0);
169	printf("ioctl(-1, RTC_PLL_SET, NULL) = -1 EBADF (%m)\n");
170
171	ioctl(-1, RTC_PLL_SET, pll);
172	printf("ioctl(-1, RTC_PLL_SET, {pll_ctrl=%d, pll_value=%d"
173	       ", pll_max=%d, pll_min=%d, pll_posmult=%d, pll_negmult=%d"
174	       ", pll_clock=%ld}) = -1 EBADF (%m)\n",
175	       pll->pll_ctrl, pll->pll_value, pll->pll_max, pll->pll_min,
176	       pll->pll_posmult, pll->pll_negmult, pll->pll_clock);
177
178#ifdef RTC_VL_READ
179	/* RTC_VL_READ */
180	ioctl(-1, RTC_VL_READ, 0);
181	printf("ioctl(-1, RTC_VL_READ, NULL) = -1 EBADF (%m)\n");
182
183	ioctl(-1, RTC_VL_READ, page);
184	printf("ioctl(-1, RTC_VL_READ, %p) = -1 EBADF (%m)\n", page);
185#endif
186
187	unsigned int i;
188	for (i = 0; i < ARRAY_SIZE(rtc_argless); ++i) {
189		ioctl(-1, (unsigned long) rtc_argless[i].val, lmagic);
190		printf("ioctl(-1, %s) = -1 EBADF (%m)\n", rtc_argless[i].str);
191	}
192
193	ioctl(-1, RTC_UIE_OFF, lmagic);
194	printf("ioctl(-1, %s) = -1 EBADF (%m)\n", "PHN_NOT_OH or RTC_UIE_OFF");
195
196	ioctl(-1, RTC_AIE_ON, lmagic);
197#ifdef HPPA
198	printf("ioctl(-1, %s) = -1 EBADF (%m)\n", "PA_PERF_ON or RTC_AIE_ON");
199#else
200	printf("ioctl(-1, %s) = -1 EBADF (%m)\n", "RTC_AIE_ON");
201#endif
202
203	ioctl(-1, _IO(0x70, 0x40), lmagic);
204	printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n", "NVRAM_INIT", lmagic);
205
206	puts("+++ exited with 0 +++");
207	return 0;
208}
209