2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * rtc.c - Use /dev/rtc for clock access
9 #include <linux/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
19 #include "monotonic.h"
38 # define RTC_PARAM_GET _IOW('p', 0x13, struct rtc_param)
39 # define RTC_PARAM_SET _IOW('p', 0x14, struct rtc_param)
41 # define RTC_PARAM_FEATURES 0
42 # define RTC_PARAM_CORRECTION 1
43 # define RTC_PARAM_BACKUP_SWITCH_MODE 2
44 #endif /* RTC_PARAM_GET */
46 static const struct hwclock_param hwclock_params
[] =
48 { RTC_PARAM_FEATURES
, "features", N_("supported features") },
49 { RTC_PARAM_CORRECTION
, "correction", N_("time correction") },
50 { RTC_PARAM_BACKUP_SWITCH_MODE
, "bsm", N_("backup switch mode") },
54 const struct hwclock_param
*get_hwclock_params(void)
56 return hwclock_params
;
60 * /dev/rtc is conventionally chardev 10/135
61 * ia64 uses /dev/efirtc, chardev 10/136
62 * devfs (obsolete) used /dev/misc/... for miscdev
63 * new RTC framework + udev uses dynamic major and /dev/rtc0.../dev/rtcN
64 * ... so we need an overridable default
67 /* default or user defined dev (by hwclock --rtc=<path>) */
68 static const char *rtc_dev_name
;
69 static int rtc_dev_fd
= -1;
71 static void close_rtc(void)
78 static int open_rtc(const struct hwclock_control
*ctl
)
80 static const char * const fls
[] = {
94 /* --rtc option has been given */
95 if (ctl
->rtc_dev_name
) {
96 rtc_dev_name
= ctl
->rtc_dev_name
;
97 rtc_dev_fd
= open(rtc_dev_name
, O_RDONLY
);
99 for (i
= 0; i
< ARRAY_SIZE(fls
); i
++) {
101 printf(_("Trying to open: %s\n"), fls
[i
]);
102 rtc_dev_fd
= open(fls
[i
], O_RDONLY
);
104 if (rtc_dev_fd
< 0) {
105 if (errno
== ENOENT
|| errno
== ENODEV
)
108 warn(_("cannot open %s"), fls
[i
]);
110 rtc_dev_name
= fls
[i
];
114 rtc_dev_name
= *fls
; /* default for error messages */
116 if (rtc_dev_fd
!= -1)
121 static int open_rtc_or_exit(const struct hwclock_control
*ctl
)
123 int rtc_fd
= open_rtc(ctl
);
126 warn(_("cannot open rtc device"));
127 hwclock_exit(ctl
, EXIT_FAILURE
);
132 static int do_rtc_read_ioctl(int rtc_fd
, struct tm
*tm
)
135 struct rtc_time rtc_tm
= { 0 };
137 rc
= ioctl(rtc_fd
, RTC_RD_TIME
, &rtc_tm
);
140 warn(_("ioctl(RTC_RD_NAME) to %s to read the time failed"),
145 /* kernel uses private struct tm definition to be self contained */
146 tm
->tm_sec
= rtc_tm
.tm_sec
;
147 tm
->tm_min
= rtc_tm
.tm_min
;
148 tm
->tm_hour
= rtc_tm
.tm_hour
;
149 tm
->tm_mday
= rtc_tm
.tm_mday
;
150 tm
->tm_mon
= rtc_tm
.tm_mon
;
151 tm
->tm_year
= rtc_tm
.tm_year
;
152 tm
->tm_wday
= rtc_tm
.tm_wday
;
153 tm
->tm_yday
= rtc_tm
.tm_yday
;
154 tm
->tm_isdst
= -1; /* don't know whether it's dst */
159 * Wait for the top of a clock tick by reading /dev/rtc in a busy loop
160 * until we see it. This function is used for rtc drivers without ioctl
161 * interrupts. This is typical on an Alpha, where the Hardware Clock
162 * interrupts are used by the kernel for the system clock, so aren't at
163 * the user's disposal.
165 static int busywait_for_rtc_clock_tick(const struct hwclock_control
*ctl
,
168 struct tm start_time
= { 0 };
169 /* The time when we were called (and started waiting) */
170 struct tm nowtime
= { 0 };
172 struct timeval begin
= { 0 }, now
= { 0 };
175 printf("ioctl(%d, RTC_UIE_ON, 0): %s\n",
176 rtc_fd
, strerror(errno
));
177 printf(_("Waiting in loop for time from %s to change\n"),
181 if (do_rtc_read_ioctl(rtc_fd
, &start_time
))
185 * Wait for change. Should be within a second, but in case
186 * something weird happens, we have a time limit (1.5s) on this loop
187 * to reduce the impact of this failure.
189 gettime_monotonic(&begin
);
191 rc
= do_rtc_read_ioctl(rtc_fd
, &nowtime
);
192 if (rc
|| start_time
.tm_sec
!= nowtime
.tm_sec
)
194 gettime_monotonic(&now
);
195 if (time_diff(now
, begin
) > 1.5) {
196 warnx(_("Timed out waiting for time change."));
207 * Same as synchronize_to_clock_tick(), but just for /dev/rtc.
209 static int synchronize_to_clock_tick_rtc(const struct hwclock_control
*ctl
)
211 int rtc_fd
; /* File descriptor of /dev/rtc */
214 rtc_fd
= open_rtc(ctl
);
216 warn(_("cannot open rtc device"));
220 /* Turn on update interrupts (one per second) */
221 int rc
= ioctl(rtc_fd
, RTC_UIE_ON
, 0);
225 * Just reading rtc_fd fails on broken hardware: no
226 * update interrupt comes and a bootscript with a
233 * Wait up to ten seconds for the next update
237 FD_SET(rtc_fd
, &rfds
);
240 rc
= select(rtc_fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
244 warnx(_("select() to %s to wait for clock tick timed out"),
247 warn(_("select() to %s to wait for clock tick failed"),
249 /* Turn off update interrupts */
250 rc
= ioctl(rtc_fd
, RTC_UIE_OFF
, 0);
252 warn(_("ioctl() to %s to turn off update interrupts failed"),
254 } else if (errno
== ENOTTY
|| errno
== EINVAL
) {
255 /* rtc ioctl interrupts are unimplemented */
256 ret
= busywait_for_rtc_clock_tick(ctl
, rtc_fd
);
258 warn(_("ioctl(%d, RTC_UIE_ON, 0) to %s failed"),
259 rtc_fd
, rtc_dev_name
);
263 static int read_hardware_clock_rtc(const struct hwclock_control
*ctl
,
268 rtc_fd
= open_rtc_or_exit(ctl
);
270 /* Read the RTC time/date, return answer via tm */
271 rc
= do_rtc_read_ioctl(rtc_fd
, tm
);
277 * Set the Hardware Clock to the broken down time <new_broken_time>. Use
278 * ioctls to "rtc" device /dev/rtc.
280 static int set_hardware_clock_rtc(const struct hwclock_control
*ctl
,
281 const struct tm
*new_broken_time
)
285 struct rtc_time rtc_tm
= { 0 };
287 rtc_fd
= open_rtc_or_exit(ctl
);
289 /* kernel uses private struct tm definition to be self contained */
290 rtc_tm
.tm_sec
= new_broken_time
->tm_sec
;
291 rtc_tm
.tm_min
= new_broken_time
->tm_min
;
292 rtc_tm
.tm_hour
= new_broken_time
->tm_hour
;
293 rtc_tm
.tm_mday
= new_broken_time
->tm_mday
;
294 rtc_tm
.tm_mon
= new_broken_time
->tm_mon
;
295 rtc_tm
.tm_year
= new_broken_time
->tm_year
;
296 rtc_tm
.tm_wday
= new_broken_time
->tm_wday
;
297 rtc_tm
.tm_yday
= new_broken_time
->tm_yday
;
298 rtc_tm
.tm_isdst
= new_broken_time
->tm_isdst
;
300 rc
= ioctl(rtc_fd
, RTC_SET_TIME
, &rtc_tm
);
303 warn(_("ioctl(RTC_SET_TIME) to %s to set the time failed"),
305 hwclock_exit(ctl
, EXIT_FAILURE
);
309 printf(_("ioctl(RTC_SET_TIME) was successful.\n"));
314 static int get_permissions_rtc(void)
319 static const char *get_device_path(void)
324 static const struct clock_ops rtc_interface
= {
325 N_("Using the rtc interface to the clock."),
327 read_hardware_clock_rtc
,
328 set_hardware_clock_rtc
,
329 synchronize_to_clock_tick_rtc
,
333 /* return &rtc if /dev/rtc can be opened, NULL otherwise */
334 const struct clock_ops
*probe_for_rtc_clock(const struct hwclock_control
*ctl
)
336 const int rtc_fd
= open_rtc(ctl
);
340 return &rtc_interface
;
345 * Get the Hardware Clock epoch setting from the kernel.
347 int get_epoch_rtc(const struct hwclock_control
*ctl
, unsigned long *epoch_p
)
351 rtc_fd
= open_rtc(ctl
);
353 warn(_("cannot open %s"), rtc_dev_name
);
357 if (ioctl(rtc_fd
, RTC_EPOCH_READ
, epoch_p
) == -1) {
358 warn(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s failed"),
359 rtc_fd
, rtc_dev_name
);
364 printf(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s succeeded.\n"),
365 rtc_fd
, rtc_dev_name
);
371 * Set the Hardware Clock epoch in the kernel.
373 int set_epoch_rtc(const struct hwclock_control
*ctl
)
379 epoch
= strtoul(ctl
->epoch_option
, NULL
, 10);
381 /* There were no RTC clocks before 1900. */
382 if (errno
|| epoch
< 1900 || epoch
== ULONG_MAX
) {
383 warnx(_("invalid epoch '%s'."), ctl
->epoch_option
);
387 rtc_fd
= open_rtc(ctl
);
389 warn(_("cannot open %s"), rtc_dev_name
);
393 if (ioctl(rtc_fd
, RTC_EPOCH_SET
, epoch
) == -1) {
394 warn(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s failed"),
395 rtc_fd
, epoch
, rtc_dev_name
);
400 printf(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s succeeded.\n"),
401 rtc_fd
, epoch
, rtc_dev_name
);
405 #endif /* __alpha__ */
409 static int resolve_rtc_param_alias(const char *alias
, __u64
*value
)
411 const struct hwclock_param
*param
= &hwclock_params
[0];
413 while (param
->name
) {
414 if (!strcmp(alias
, param
->name
)) {
424 /* kernel uapi __u64 can be defined differently than uint64_t */
425 static int strtoku64(const char *str
, __u64
*num
, int base
)
427 return ul_strtou64(str
, (uint64_t *) &num
, base
);
431 * Get the Hardware Clock parameter setting from the kernel.
433 int get_param_rtc(const struct hwclock_control
*ctl
,
434 const char *name
, uint64_t *id
, uint64_t *value
)
437 struct rtc_param param
= { .param
= 0 };
440 if (resolve_rtc_param_alias(name
, ¶m
.param
) != 0
441 && strtoku64(name
, ¶m
.param
, 0) != 0) {
442 warnx(_("could not convert parameter name to number"));
447 rtc_fd
= open_rtc(ctl
);
449 warn(_("cannot open %s"), rtc_dev_name
);
453 if (ioctl(rtc_fd
, RTC_PARAM_GET
, ¶m
) == -1) {
454 warn(_("ioctl(%d, RTC_PARAM_GET, param) to %s failed"),
455 rtc_fd
, rtc_dev_name
);
462 *value
= param
.uvalue
;
465 printf(_("ioctl(%d, RTC_PARAM_GET, param) to %s succeeded.\n"),
466 rtc_fd
, rtc_dev_name
);
472 * Set the Hardware Clock parameter in the kernel.
474 int set_param_rtc(const struct hwclock_control
*ctl
, const char *opt0
)
477 struct rtc_param param
= { .param
= 0 };
478 char *tok
, *opt
= xstrdup(opt0
);
481 tok
= strtok(opt
, "=");
482 if (resolve_rtc_param_alias(tok
, ¶m
.param
) != 0
483 && strtoku64(tok
, ¶m
.param
, 0) != 0) {
484 warnx(_("could not convert parameter name to number"));
489 tok
= strtok(NULL
, "=");
491 warnx(_("expected <param>=<value>"));
494 if (strtoku64(tok
, ¶m
.uvalue
, 0) != 0) {
495 warnx(_("could not convert parameter value to number"));
500 rtc_fd
= open_rtc(ctl
);
502 warnx(_("cannot open %s"), rtc_dev_name
);
506 if (ioctl(rtc_fd
, RTC_PARAM_SET
, ¶m
) == -1) {
507 warn(_("ioctl(%d, RTC_PARAM_SET, param) to %s failed"),
508 rtc_fd
, rtc_dev_name
);
513 printf(_("ioctl(%d, RTC_PARAM_SET, param) to %s succeeded.\n"),
514 rtc_fd
, rtc_dev_name
);
522 #ifndef RTC_VL_DATA_INVALID
523 #define RTC_VL_DATA_INVALID 0x1
525 #ifndef RTC_VL_BACKUP_LOW
526 #define RTC_VL_BACKUP_LOW 0x2
528 #ifndef RTC_VL_BACKUP_EMPTY
529 #define RTC_VL_BACKUP_EMPTY 0x4
531 #ifndef RTC_VL_ACCURACY_LOW
532 #define RTC_VL_ACCURACY_LOW 0x8
534 #ifndef RTC_VL_BACKUP_SWITCH
535 #define RTC_VL_BACKUP_SWITCH 0x10
538 int rtc_vl_read(const struct hwclock_control
*ctl
)
543 static const struct vl_bit
{
547 { RTC_VL_DATA_INVALID
, N_("Voltage too low, RTC data is invalid") },
548 { RTC_VL_BACKUP_LOW
, N_("Backup voltage is low") },
549 { RTC_VL_BACKUP_EMPTY
, N_("Backup empty or not present") },
550 { RTC_VL_ACCURACY_LOW
, N_("Voltage is low, RTC accuracy is reduced") },
551 { RTC_VL_BACKUP_SWITCH
, N_("Backup switchover happened") },
554 rtc_fd
= open_rtc(ctl
);
556 warnx(_("cannot open %s"), rtc_dev_name
);
560 if (ioctl(rtc_fd
, RTC_VL_READ
, &vl
) == -1) {
561 warn(_("ioctl(%d, RTC_VL_READ) on %s failed"),
562 rtc_fd
, rtc_dev_name
);
567 printf(_("ioctl(%d, RTC_VL_READ) on %s returned 0x%x\n"),
568 rtc_fd
, rtc_dev_name
, vl
);
571 for (i
= 0; i
< ARRAY_SIZE(vl_bits
); ++i
) {
572 const struct vl_bit
*vlb
= &vl_bits
[i
];
575 printf("0x%02x - %s\n", vlb
->bit
, vlb
->desc
);
580 printf("0x%02x - unknown bit(s)\n", vl
);
585 int rtc_vl_clear(const struct hwclock_control
*ctl
)
589 rtc_fd
= open_rtc(ctl
);
591 warnx(_("cannot open %s"), rtc_dev_name
);
595 if (ioctl(rtc_fd
, RTC_VL_CLR
) == -1) {
596 warn(_("ioctl(%d, RTC_VL_CLEAR) on %s failed"),
597 rtc_fd
, rtc_dev_name
);
602 printf(_("ioctl(%d, RTC_VL_CLEAR) on %s succeeded.\n"),
603 rtc_fd
, rtc_dev_name
);