* [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
@ 2023-06-13 10:14 Rasmus Villemoes
2023-06-20 9:26 ` Karel Zak
2023-06-21 20:18 ` [PATCH] hwclock: add --vl-read, --vl-clear documentation and bash-completion Rasmus Villemoes
0 siblings, 2 replies; 7+ messages in thread
From: Rasmus Villemoes @ 2023-06-13 10:14 UTC (permalink / raw
To: util-linux; +Cc: Rasmus Villemoes
Implement a way for userspace to query the status of the backup
battery, if supported by the hardware and driver.
The RTC_VL_* bits are a somewhat recent addition (3431ca4837bf, but
really only from b0efe0281234) to the uapi header,
so provide our own definition if the build host's header doesn't.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
---
sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
sys-utils/hwclock.c | 35 +++++++++++++++++
sys-utils/hwclock.h | 5 +++
3 files changed, 126 insertions(+)
diff --git a/sys-utils/hwclock-rtc.c b/sys-utils/hwclock-rtc.c
index 7094cd063..f918272a1 100644
--- a/sys-utils/hwclock-rtc.c
+++ b/sys-utils/hwclock-rtc.c
@@ -518,3 +518,89 @@ done:
free(opt);
return rc;
}
+
+#ifndef RTC_VL_DATA_INVALID
+#define RTC_VL_DATA_INVALID 0x1
+#endif
+#ifndef RTC_VL_BACKUP_LOW
+#define RTC_VL_BACKUP_LOW 0x2
+#endif
+#ifndef RTC_VL_BACKUP_EMPTY
+#define RTC_VL_BACKUP_EMPTY 0x4
+#endif
+#ifndef RTC_VL_ACCURACY_LOW
+#define RTC_VL_ACCURACY_LOW 0x8
+#endif
+#ifndef RTC_VL_BACKUP_SWITCH
+#define RTC_VL_BACKUP_SWITCH 0x10
+#endif
+
+int rtc_vl_read(const struct hwclock_control *ctl)
+{
+ unsigned int vl;
+ int rtc_fd;
+ size_t i;
+ static const struct vl_bit {
+ unsigned int bit;
+ const char *desc;
+ } vl_bits[] = {
+ { RTC_VL_DATA_INVALID, N_("Voltage too low, RTC data is invalid") },
+ { RTC_VL_BACKUP_LOW, N_("Backup voltage is low") },
+ { RTC_VL_BACKUP_EMPTY, N_("Backup empty or not present") },
+ { RTC_VL_ACCURACY_LOW, N_("Voltage is low, RTC accuracy is reduced") },
+ { RTC_VL_BACKUP_SWITCH, N_("Backup switchover happened") },
+ };
+
+ rtc_fd = open_rtc(ctl);
+ if (rtc_fd < 0) {
+ warnx(_("cannot open %s"), rtc_dev_name);
+ return 1;
+ }
+
+ if (ioctl(rtc_fd, RTC_VL_READ, &vl) == -1) {
+ warn(_("ioctl(%d, RTC_VL_READ) on %s failed"),
+ rtc_fd, rtc_dev_name);
+ return 1;
+ }
+
+ if (ctl->verbose) {
+ printf(_("ioctl(%d, RTC_VL_READ) on %s returned 0x%x\n"),
+ rtc_fd, rtc_dev_name, vl);
+ }
+
+ for (i = 0; i < ARRAY_SIZE(vl_bits); ++i) {
+ const struct vl_bit *vlb = &vl_bits[i];
+
+ if (vl & vlb->bit) {
+ printf("0x%02x - %s\n", vlb->bit, vlb->desc);
+ vl &= ~vlb->bit;
+ }
+ }
+ if (vl)
+ printf("0x%02x - unknown bit(s)\n", vl);
+
+ return 0;
+}
+
+int rtc_vl_clear(const struct hwclock_control *ctl)
+{
+ int rtc_fd;
+
+ rtc_fd = open_rtc(ctl);
+ if (rtc_fd < 0) {
+ warnx(_("cannot open %s"), rtc_dev_name);
+ return 1;
+ }
+
+ if (ioctl(rtc_fd, RTC_VL_CLR) == -1) {
+ warn(_("ioctl(%d, RTC_VL_CLEAR) on %s failed"),
+ rtc_fd, rtc_dev_name);
+ return 1;
+ }
+
+ if (ctl->verbose)
+ printf(_("ioctl(%d, RTC_VL_CLEAR) on %s succeeded.\n"),
+ rtc_fd, rtc_dev_name);
+
+ return 0;
+}
diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c
index 2a1844309..b043646b3 100644
--- a/sys-utils/hwclock.c
+++ b/sys-utils/hwclock.c
@@ -1180,6 +1180,20 @@ manipulate_rtc_param(const struct hwclock_control *ctl)
return 1;
}
+
+static int
+manipulate_rtc_voltage_low(const struct hwclock_control *ctl)
+{
+ if (ctl->vl_read) {
+ if (rtc_vl_read(ctl))
+ return 1;
+ }
+ if (ctl->vl_clear) {
+ if (rtc_vl_clear(ctl))
+ return 1;
+ }
+ return 0;
+}
#endif
static void out_version(void)
@@ -1215,6 +1229,8 @@ usage(void)
#ifdef __linux__
puts(_(" --param-get <param> display the RTC parameter"));
puts(_(" --param-set <param>=<value> set the RTC parameter"));
+ puts(_(" --vl-read read voltage low information"));
+ puts(_(" --vl-clear clear voltage low information"));
#endif
puts(_(" --predict predict the drifted RTC time according to --date"));
fputs(USAGE_OPTIONS, stdout);
@@ -1286,6 +1302,8 @@ int main(int argc, char **argv)
OPT_NOADJFILE,
OPT_PARAM_GET,
OPT_PARAM_SET,
+ OPT_VL_READ,
+ OPT_VL_CLEAR,
OPT_PREDICT,
OPT_SET,
OPT_SETEPOCH,
@@ -1315,6 +1333,8 @@ int main(int argc, char **argv)
#ifdef __linux__
{ "param-get", required_argument, NULL, OPT_PARAM_GET },
{ "param-set", required_argument, NULL, OPT_PARAM_SET },
+ { "vl-read", no_argument, NULL, OPT_VL_READ },
+ { "vl-clear", no_argument, NULL, OPT_VL_CLEAR },
#endif
{ "noadjfile", no_argument, NULL, OPT_NOADJFILE },
{ "directisa", no_argument, NULL, OPT_DIRECTISA },
@@ -1439,6 +1459,14 @@ int main(int argc, char **argv)
ctl.show = 0;
ctl.hwaudit_on = 1;
break;
+ case OPT_VL_READ:
+ ctl.vl_read = 1;
+ ctl.show = 0;
+ break;
+ case OPT_VL_CLEAR:
+ ctl.vl_clear = 1;
+ ctl.show = 0;
+ break;
#endif
case OPT_NOADJFILE:
ctl.noadjfile = 1;
@@ -1540,6 +1568,13 @@ int main(int argc, char **argv)
hwclock_exit(&ctl, EXIT_SUCCESS);
}
+
+ if (ctl.vl_read || ctl.vl_clear) {
+ if (manipulate_rtc_voltage_low(&ctl))
+ hwclock_exit(&ctl, EXIT_FAILURE);
+
+ hwclock_exit(&ctl, EXIT_SUCCESS);
+ }
#endif
#if defined(__linux__) && defined(__alpha__)
diff --git a/sys-utils/hwclock.h b/sys-utils/hwclock.h
index b5b72d45d..a690e717b 100644
--- a/sys-utils/hwclock.h
+++ b/sys-utils/hwclock.h
@@ -53,6 +53,8 @@ struct hwclock_control {
set:1,
update:1,
universal:1, /* will store hw_clock_is_utc() return value */
+ vl_read:1,
+ vl_clear:1,
verbose:1;
};
@@ -88,6 +90,9 @@ extern int get_param_rtc(const struct hwclock_control *ctl,
const char *name, uint64_t *id, uint64_t *value);
extern int set_param_rtc(const struct hwclock_control *ctl, const char *name);
+extern int rtc_vl_read(const struct hwclock_control *ctl);
+extern int rtc_vl_clear(const struct hwclock_control *ctl);
+
extern void __attribute__((__noreturn__))
hwclock_exit(const struct hwclock_control *ctl, int status);
--
2.37.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
2023-06-13 10:14 [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls Rasmus Villemoes
@ 2023-06-20 9:26 ` Karel Zak
2023-06-20 9:30 ` Rasmus Villemoes
2023-06-21 20:18 ` [PATCH] hwclock: add --vl-read, --vl-clear documentation and bash-completion Rasmus Villemoes
1 sibling, 1 reply; 7+ messages in thread
From: Karel Zak @ 2023-06-20 9:26 UTC (permalink / raw
To: Rasmus Villemoes; +Cc: util-linux
On Tue, Jun 13, 2023 at 12:14:28PM +0200, Rasmus Villemoes wrote:
> sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
> sys-utils/hwclock.c | 35 +++++++++++++++++
> sys-utils/hwclock.h | 5 +++
> 3 files changed, 126 insertions(+)
The patch looks good. Can we also get something for sys-utils/hwclock.8.adoc
(man page) and bash-completion/hwclock? ;-)
Thanks!
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
2023-06-20 9:26 ` Karel Zak
@ 2023-06-20 9:30 ` Rasmus Villemoes
2023-06-20 10:22 ` Karel Zak
0 siblings, 1 reply; 7+ messages in thread
From: Rasmus Villemoes @ 2023-06-20 9:30 UTC (permalink / raw
To: Karel Zak; +Cc: util-linux
On 20/06/2023 11.26, Karel Zak wrote:
> On Tue, Jun 13, 2023 at 12:14:28PM +0200, Rasmus Villemoes wrote:
>> sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
>> sys-utils/hwclock.c | 35 +++++++++++++++++
>> sys-utils/hwclock.h | 5 +++
>> 3 files changed, 126 insertions(+)
>
> The patch looks good. Can we also get something for sys-utils/hwclock.8.adoc
> (man page) and bash-completion/hwclock? ;-)
Absolutely. I just didn't want to spend too much time on docs if the
feature was deemed out-of-scope or if significant changes to e.g. the
option names or other API was required.
Can I send the docs as a separate follow-up patch, or should I send a v2
with all changes in one?
Thanks,
Rasmus
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
2023-06-20 9:30 ` Rasmus Villemoes
@ 2023-06-20 10:22 ` Karel Zak
2023-06-20 11:22 ` Rasmus Villemoes
0 siblings, 1 reply; 7+ messages in thread
From: Karel Zak @ 2023-06-20 10:22 UTC (permalink / raw
To: Rasmus Villemoes; +Cc: util-linux
On Tue, Jun 20, 2023 at 11:30:07AM +0200, Rasmus Villemoes wrote:
> On 20/06/2023 11.26, Karel Zak wrote:
> > On Tue, Jun 13, 2023 at 12:14:28PM +0200, Rasmus Villemoes wrote:
> >> sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
> >> sys-utils/hwclock.c | 35 +++++++++++++++++
> >> sys-utils/hwclock.h | 5 +++
> >> 3 files changed, 126 insertions(+)
> >
> > The patch looks good. Can we also get something for sys-utils/hwclock.8.adoc
> > (man page) and bash-completion/hwclock? ;-)
>
> Absolutely. I just didn't want to spend too much time on docs if the
> feature was deemed out-of-scope or if significant changes to e.g. the
> option names or other API was required.
Good point. Option names are always the funny part ;-).
At first glance, I thought that --voltage-low-clear was too long and
that it would be better to follow the ioctl names. However, upon
further consideration, I realize that we already have longer options
in util-linux. So perhaps, choosing --voltage-low-clear and
--voltage-low-read wouldn't be such a bad choice after all.
What do you think?
> Can I send the docs as a separate follow-up patch, or should I send a v2
> with all changes in one?
Select what is better for you ;-)
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
2023-06-20 10:22 ` Karel Zak
@ 2023-06-20 11:22 ` Rasmus Villemoes
0 siblings, 0 replies; 7+ messages in thread
From: Rasmus Villemoes @ 2023-06-20 11:22 UTC (permalink / raw
To: Karel Zak; +Cc: util-linux
On 20/06/2023 12.22, Karel Zak wrote:
> On Tue, Jun 20, 2023 at 11:30:07AM +0200, Rasmus Villemoes wrote:
>> On 20/06/2023 11.26, Karel Zak wrote:
>>> On Tue, Jun 13, 2023 at 12:14:28PM +0200, Rasmus Villemoes wrote:
>>>> sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
>>>> sys-utils/hwclock.c | 35 +++++++++++++++++
>>>> sys-utils/hwclock.h | 5 +++
>>>> 3 files changed, 126 insertions(+)
>>>
>>> The patch looks good. Can we also get something for sys-utils/hwclock.8.adoc
>>> (man page) and bash-completion/hwclock? ;-)
>>
>> Absolutely. I just didn't want to spend too much time on docs if the
>> feature was deemed out-of-scope or if significant changes to e.g. the
>> option names or other API was required.
>
> Good point. Option names are always the funny part ;-).
>
> At first glance, I thought that --voltage-low-clear was too long and
> that it would be better to follow the ioctl names. However, upon
> further consideration, I realize that we already have longer options
> in util-linux. So perhaps, choosing --voltage-low-clear and
> --voltage-low-read wouldn't be such a bad choice after all.
>
> What do you think?
I prefer the current, shorter names. There's also precedent in the form
of --param-get, which isn't --parameter-get.
>> Can I send the docs as a separate follow-up patch, or should I send a v2
>> with all changes in one?
>
> Select what is better for you ;-)
OK, thanks. I won't get to it anyway until probably tomorrow, and unless
I hear otherwise, I'll just keep the current names etc. and send a
separate doc update patch.
Rasmus
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] hwclock: add --vl-read, --vl-clear documentation and bash-completion
@ 2023-06-21 20:18 ` Rasmus Villemoes
2023-06-22 8:29 ` [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls Karel Zak
0 siblings, 1 reply; 7+ messages in thread
From: Rasmus Villemoes @ 2023-06-21 20:18 UTC (permalink / raw
To: util-linux; +Cc: Karel Zak, Rasmus Villemoes
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
---
bash-completion/hwclock | 2 ++
sys-utils/hwclock.8.adoc | 5 +++++
2 files changed, 7 insertions(+)
diff --git a/bash-completion/hwclock b/bash-completion/hwclock
index 6b4c01724..6240d7d4b 100644
--- a/bash-completion/hwclock
+++ b/bash-completion/hwclock
@@ -54,6 +54,8 @@ _hwclock_module()
--epoch
--param-get
--param-set
+ --vl-read
+ --vl-clear
--update-drift
--noadjfile
--adjfile
diff --git a/sys-utils/hwclock.8.adoc b/sys-utils/hwclock.8.adoc
index 3e181edd2..0b23a37e8 100644
--- a/sys-utils/hwclock.8.adoc
+++ b/sys-utils/hwclock.8.adoc
@@ -101,6 +101,11 @@ The first two are only available on the first call of *settimeofday*(2) after bo
*-w*, *--systohc*::
Set the Hardware Clock from the System Clock, and update the timestamps in _{ADJTIME_PATH}_. With the *--update-drift* option also (re)calculate the drift factor. Try it without the option if *--systohc* fails. See *--update-drift* below.
+*--vl-read*, *--vl-clear*::
+Some RTC devices are able to monitor the voltage of the backup battery and thus provide a way for the user to know that the battery should be replaced. The *--vl-read* function retrieves the Voltage Low information and decodes the result into human-readable form. The *--vl-clear* function resets the Voltage Low information, which is necessary for some RTC devices after a battery replacement.
++
+See the Kernel's _include/uapi/linux/rtc.h_ for details on which pieces of information may be returned. Note that not all RTC devices have this monitoring capability, nor do all drivers necessarily support reading the information.
+
include::man-common/help-version.adoc[]
== OPTIONS
--
2.37.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls
2023-06-21 20:18 ` [PATCH] hwclock: add --vl-read, --vl-clear documentation and bash-completion Rasmus Villemoes
@ 2023-06-22 8:29 ` Karel Zak
0 siblings, 0 replies; 7+ messages in thread
From: Karel Zak @ 2023-06-22 8:29 UTC (permalink / raw
To: Rasmus Villemoes; +Cc: util-linux
On Tue, Jun 13, 2023 at 12:14:28PM +0200, Rasmus Villemoes wrote:
> sys-utils/hwclock-rtc.c | 86 +++++++++++++++++++++++++++++++++++++++++
> sys-utils/hwclock.c | 35 +++++++++++++++++
> sys-utils/hwclock.h | 5 +++
> 3 files changed, 126 insertions(+)
> bash-completion/hwclock | 2 ++
> sys-utils/hwclock.8.adoc | 5 +++++
> 2 files changed, 7 insertions(+)
Applied, thanks!
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-06-22 8:31 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-13 10:14 [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls Rasmus Villemoes
2023-06-20 9:26 ` Karel Zak
2023-06-20 9:30 ` Rasmus Villemoes
2023-06-20 10:22 ` Karel Zak
2023-06-20 11:22 ` Rasmus Villemoes
2023-06-21 20:18 ` [PATCH] hwclock: add --vl-read, --vl-clear documentation and bash-completion Rasmus Villemoes
2023-06-22 8:29 ` [PATCH] hwclock: add support for RTC_VL_READ/RTC_VL_CLR ioctls Karel Zak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).