]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/hwclock-rtc.c
scriptreplay: cleanup usage()
[thirdparty/util-linux.git] / sys-utils / hwclock-rtc.c
1 /*
2 * rtc.c - Use /dev/rtc for clock access
3 */
4 #include <asm/ioctl.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/ioctl.h>
10 #include <sys/select.h>
11 #include <sys/time.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 #include "monotonic.h"
16 #include "nls.h"
17
18 #include "hwclock.h"
19
20 /*
21 * Get defines for rtc stuff.
22 *
23 * Getting the rtc defines is nontrivial. The obvious way is by including
24 * <linux/mc146818rtc.h> but that again includes <asm/io.h> which again
25 * includes ... and on sparc and alpha this gives compilation errors for
26 * many kernel versions. So, we give the defines ourselves here. Moreover,
27 * some Sparc person decided to be incompatible, and used a struct rtc_time
28 * different from that used in mc146818rtc.h.
29 */
30
31 /*
32 * On Sparcs, there is a <asm/rtc.h> that defines different ioctls (that are
33 * required on my machine). However, this include file does not exist on
34 * other architectures.
35 */
36 /* One might do:
37 #ifdef __sparc__
38 # include <asm/rtc.h>
39 #endif
40 */
41 #ifdef __sparc__
42 /* The following is roughly equivalent */
43 struct sparc_rtc_time
44 {
45 int sec; /* Seconds 0-59 */
46 int min; /* Minutes 0-59 */
47 int hour; /* Hour 0-23 */
48 int dow; /* Day of the week 1-7 */
49 int dom; /* Day of the month 1-31 */
50 int month; /* Month of year 1-12 */
51 int year; /* Year 0-99 */
52 };
53 #define RTCGET _IOR('p', 20, struct sparc_rtc_time)
54 #define RTCSET _IOW('p', 21, struct sparc_rtc_time)
55 #endif
56
57 /*
58 * struct rtc_time is present since 1.3.99.
59 * Earlier (since 1.3.89), a struct tm was used.
60 */
61 struct linux_rtc_time {
62 int tm_sec;
63 int tm_min;
64 int tm_hour;
65 int tm_mday;
66 int tm_mon;
67 int tm_year;
68 int tm_wday;
69 int tm_yday;
70 int tm_isdst;
71 };
72
73 /* RTC_RD_TIME etc have this definition since 1.99.9 (pre2.0-9) */
74 #ifndef RTC_RD_TIME
75 # define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time)
76 # define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time)
77 # define RTC_UIE_ON _IO('p', 0x03) /* Update int. enable on */
78 # define RTC_UIE_OFF _IO('p', 0x04) /* Update int. enable off */
79 #endif
80
81 /* RTC_EPOCH_READ and RTC_EPOCH_SET are present since 2.0.34 and 2.1.89 */
82 #ifndef RTC_EPOCH_READ
83 # define RTC_EPOCH_READ _IOR('p', 0x0d, unsigned long) /* Read epoch */
84 # define RTC_EPOCH_SET _IOW('p', 0x0e, unsigned long) /* Set epoch */
85 #endif
86
87 /*
88 * /dev/rtc is conventionally chardev 10/135
89 * ia64 uses /dev/efirtc, chardev 10/136
90 * devfs (obsolete) used /dev/misc/... for miscdev
91 * new RTC framework + udev uses dynamic major and /dev/rtc0.../dev/rtcN
92 * ... so we need an overridable default
93 */
94
95 /* default or user defined dev (by hwclock --rtc=<path>) */
96 static const char *rtc_dev_name;
97 static int rtc_dev_fd = -1;
98
99 static void close_rtc(void)
100 {
101 if (rtc_dev_fd != -1)
102 close(rtc_dev_fd);
103 rtc_dev_fd = -1;
104 }
105
106 static int open_rtc(const struct hwclock_control *ctl)
107 {
108 static const char *fls[] = {
109 #ifdef __ia64__
110 "/dev/efirtc",
111 "/dev/misc/efirtc",
112 #endif
113 "/dev/rtc0",
114 "/dev/rtc",
115 "/dev/misc/rtc"
116 };
117 size_t i;
118
119 if (rtc_dev_fd != -1)
120 return rtc_dev_fd;
121
122 /* --rtc option has been given */
123 if (ctl->rtc_dev_name) {
124 rtc_dev_name = ctl->rtc_dev_name;
125 rtc_dev_fd = open(rtc_dev_name, O_RDONLY);
126 } else {
127 for (i = 0; i < ARRAY_SIZE(fls); i++) {
128 if (ctl->verbose)
129 printf(_("Trying to open: %s\n"), fls[i]);
130 rtc_dev_fd = open(fls[i], O_RDONLY);
131
132 if (rtc_dev_fd < 0) {
133 if (errno == ENOENT || errno == ENODEV)
134 continue;
135 if (ctl->verbose)
136 warn(_("cannot open %s"), fls[i]);
137 }
138 rtc_dev_name = fls[i];
139 break;
140 }
141 if (rtc_dev_fd < 0)
142 rtc_dev_name = *fls; /* default for error messages */
143 }
144 if (rtc_dev_fd != -1)
145 atexit(close_rtc);
146 return rtc_dev_fd;
147 }
148
149 static int open_rtc_or_exit(const struct hwclock_control *ctl)
150 {
151 int rtc_fd = open_rtc(ctl);
152
153 if (rtc_fd < 0) {
154 warn(_("cannot open rtc device"));
155 hwclock_exit(ctl, EXIT_FAILURE);
156 }
157 return rtc_fd;
158 }
159
160 static int do_rtc_read_ioctl(int rtc_fd, struct tm *tm)
161 {
162 int rc = -1;
163 char *ioctlname;
164 #ifdef __sparc__
165 /* some but not all sparcs use a different ioctl and struct */
166 struct sparc_rtc_time stm;
167 #endif
168
169 ioctlname = "RTC_RD_TIME";
170 rc = ioctl(rtc_fd, RTC_RD_TIME, tm);
171
172 #ifdef __sparc__
173 if (rc == -1) { /* sparc sbus */
174 ioctlname = "RTCGET";
175 rc = ioctl(rtc_fd, RTCGET, &stm);
176 if (rc == 0) {
177 tm->tm_sec = stm.sec;
178 tm->tm_min = stm.min;
179 tm->tm_hour = stm.hour;
180 tm->tm_mday = stm.dom;
181 tm->tm_mon = stm.month - 1;
182 tm->tm_year = stm.year - 1900;
183 tm->tm_wday = stm.dow - 1;
184 tm->tm_yday = -1; /* day in the year */
185 }
186 }
187 #endif
188
189 if (rc == -1) {
190 warn(_("ioctl(%s) to %s to read the time failed"),
191 ioctlname, rtc_dev_name);
192 return -1;
193 }
194
195 tm->tm_isdst = -1; /* don't know whether it's dst */
196 return 0;
197 }
198
199 /*
200 * Wait for the top of a clock tick by reading /dev/rtc in a busy loop
201 * until we see it. This function is used for rtc drivers without ioctl
202 * interrupts. This is typical on an Alpha, where the Hardware Clock
203 * interrupts are used by the kernel for the system clock, so aren't at
204 * the user's disposal.
205 */
206 static int busywait_for_rtc_clock_tick(const struct hwclock_control *ctl,
207 const int rtc_fd)
208 {
209 struct tm start_time;
210 /* The time when we were called (and started waiting) */
211 struct tm nowtime;
212 int rc;
213 struct timeval begin, now;
214
215 if (ctl->verbose) {
216 printf("ioctl(%d, RTC_UIE_ON, 0): %s\n",
217 rtc_fd, strerror(errno));
218 printf(_("Waiting in loop for time from %s to change\n"),
219 rtc_dev_name);
220 }
221
222 if (do_rtc_read_ioctl(rtc_fd, &start_time))
223 return 1;
224
225 /*
226 * Wait for change. Should be within a second, but in case
227 * something weird happens, we have a time limit (1.5s) on this loop
228 * to reduce the impact of this failure.
229 */
230 gettime_monotonic(&begin);
231 do {
232 rc = do_rtc_read_ioctl(rtc_fd, &nowtime);
233 if (rc || start_time.tm_sec != nowtime.tm_sec)
234 break;
235 gettime_monotonic(&now);
236 if (time_diff(now, begin) > 1.5) {
237 warnx(_("Timed out waiting for time change."));
238 return 1;
239 }
240 } while (1);
241
242 if (rc)
243 return 1;
244 return 0;
245 }
246
247 /*
248 * Same as synchronize_to_clock_tick(), but just for /dev/rtc.
249 */
250 static int synchronize_to_clock_tick_rtc(const struct hwclock_control *ctl)
251 {
252 int rtc_fd; /* File descriptor of /dev/rtc */
253 int ret = 1;
254
255 rtc_fd = open_rtc(ctl);
256 if (rtc_fd == -1) {
257 warn(_("cannot open rtc device"));
258 return ret;
259 } else {
260 /* Turn on update interrupts (one per second) */
261 int rc = ioctl(rtc_fd, RTC_UIE_ON, 0);
262
263 if (rc != -1) {
264 /*
265 * Just reading rtc_fd fails on broken hardware: no
266 * update interrupt comes and a bootscript with a
267 * hwclock call hangs
268 */
269 fd_set rfds;
270 struct timeval tv;
271
272 /*
273 * Wait up to ten seconds for the next update
274 * interrupt
275 */
276 FD_ZERO(&rfds);
277 FD_SET(rtc_fd, &rfds);
278 tv.tv_sec = 10;
279 tv.tv_usec = 0;
280 rc = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);
281 if (0 < rc)
282 ret = 0;
283 else if (rc == 0) {
284 warnx(_("select() to %s to wait for clock tick timed out"),
285 rtc_dev_name);
286 } else
287 warn(_("select() to %s to wait for clock tick failed"),
288 rtc_dev_name);
289 /* Turn off update interrupts */
290 rc = ioctl(rtc_fd, RTC_UIE_OFF, 0);
291 if (rc == -1)
292 warn(_("ioctl() to %s to turn off update interrupts failed"),
293 rtc_dev_name);
294 } else if (errno == ENOTTY || errno == EINVAL) {
295 /* rtc ioctl interrupts are unimplemented */
296 ret = busywait_for_rtc_clock_tick(ctl, rtc_fd);
297 } else
298 warn(_("ioctl(%d, RTC_UIE_ON, 0) to %s failed"),
299 rtc_fd, rtc_dev_name);
300 }
301 return ret;
302 }
303
304 static int read_hardware_clock_rtc(const struct hwclock_control *ctl,
305 struct tm *tm)
306 {
307 int rtc_fd, rc;
308
309 rtc_fd = open_rtc_or_exit(ctl);
310
311 /* Read the RTC time/date, return answer via tm */
312 rc = do_rtc_read_ioctl(rtc_fd, tm);
313
314 return rc;
315 }
316
317 /*
318 * Set the Hardware Clock to the broken down time <new_broken_time>. Use
319 * ioctls to "rtc" device /dev/rtc.
320 */
321 static int set_hardware_clock_rtc(const struct hwclock_control *ctl,
322 const struct tm *new_broken_time)
323 {
324 int rc = -1;
325 int rtc_fd;
326 char *ioctlname;
327
328 rtc_fd = open_rtc_or_exit(ctl);
329
330 ioctlname = "RTC_SET_TIME";
331 rc = ioctl(rtc_fd, RTC_SET_TIME, new_broken_time);
332
333 #ifdef __sparc__
334 if (rc == -1) { /* sparc sbus */
335 struct sparc_rtc_time stm;
336
337 stm.sec = new_broken_time->tm_sec;
338 stm.min = new_broken_time->tm_min;
339 stm.hour = new_broken_time->tm_hour;
340 stm.dom = new_broken_time->tm_mday;
341 stm.month = new_broken_time->tm_mon + 1;
342 stm.year = new_broken_time->tm_year + 1900;
343 stm.dow = new_broken_time->tm_wday + 1;
344
345 ioctlname = "RTCSET";
346 rc = ioctl(rtc_fd, RTCSET, &stm);
347 }
348 #endif
349
350 if (rc == -1) {
351 warn(_("ioctl(%s) to %s to set the time failed"),
352 ioctlname, rtc_dev_name);
353 hwclock_exit(ctl, EXIT_FAILURE);
354 }
355
356 if (ctl->verbose)
357 printf(_("ioctl(%s) was successful.\n"), ioctlname);
358
359 return 0;
360 }
361
362 static int get_permissions_rtc(void)
363 {
364 return 0;
365 }
366
367 static const char *get_device_path(void)
368 {
369 return rtc_dev_name;
370 }
371
372 static struct clock_ops rtc_interface = {
373 N_("Using the rtc interface to the clock."),
374 get_permissions_rtc,
375 read_hardware_clock_rtc,
376 set_hardware_clock_rtc,
377 synchronize_to_clock_tick_rtc,
378 get_device_path,
379 };
380
381 /* return &rtc if /dev/rtc can be opened, NULL otherwise */
382 struct clock_ops *probe_for_rtc_clock(const struct hwclock_control *ctl)
383 {
384 const int rtc_fd = open_rtc(ctl);
385
386 if (rtc_fd < 0)
387 return NULL;
388 return &rtc_interface;
389 }
390
391 #ifdef __alpha__
392 /*
393 * Get the Hardware Clock epoch setting from the kernel.
394 */
395 int get_epoch_rtc(const struct hwclock_control *ctl, unsigned long *epoch_p)
396 {
397 int rtc_fd;
398
399 rtc_fd = open_rtc(ctl);
400 if (rtc_fd < 0) {
401 warn(_("cannot open %s"), rtc_dev_name);
402 return 1;
403 }
404
405 if (ioctl(rtc_fd, RTC_EPOCH_READ, epoch_p) == -1) {
406 warn(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s failed"),
407 rtc_fd, rtc_dev_name);
408 return 1;
409 }
410
411 if (ctl->verbose)
412 printf(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s succeeded.\n"),
413 rtc_fd, rtc_dev_name);
414
415 return 0;
416 }
417
418 /*
419 * Set the Hardware Clock epoch in the kernel.
420 */
421 int set_epoch_rtc(const struct hwclock_control *ctl)
422 {
423 int rtc_fd;
424 unsigned long epoch;
425
426 epoch = strtoul(ctl->epoch_option, NULL, 10);
427
428 /* There were no RTC clocks before 1900. */
429 if (epoch < 1900 || epoch == ULONG_MAX) {
430 warnx(_("invalid epoch '%s'."), ctl->epoch_option);
431 return 1;
432 }
433
434 rtc_fd = open_rtc(ctl);
435 if (rtc_fd < 0) {
436 warn(_("cannot open %s"), rtc_dev_name);
437 return 1;
438 }
439
440 if (ioctl(rtc_fd, RTC_EPOCH_SET, epoch) == -1) {
441 warn(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s failed"),
442 rtc_fd, epoch, rtc_dev_name);
443 return 1;
444 }
445
446 if (ctl->verbose)
447 printf(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s succeeded.\n"),
448 rtc_fd, epoch, rtc_dev_name);
449
450 return 0;
451 }
452 #endif /* __alpha__ */