1 /* readclock - manipulate the hardware real time clock */
10 #include <minix/type.h>
11 #include <minix/const.h>
12 #include <minix/callnr.h>
13 #include <minix/log.h>
14 #include <minix/syslib.h>
15 #include <minix/sysutil.h>
16 #include <minix/com.h>
17 #include <minix/type.h>
18 #include <minix/safecopies.h>
19 #include <sys/svrctl.h>
21 #include "readclock.h"
23 static struct rtc rtc
;
25 static struct log log
= {
27 .log_level
= LEVEL_INFO
,
28 .log_func
= default_log
31 /* functions for transfering struct tm to/from this driver and calling proc. */
32 static int fetch_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
);
33 static int store_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
);
35 /* SEF functions and variables. */
36 static void sef_local_startup(void);
37 static int sef_cb_init(int type
, sef_init_info_t
* info
);
40 main(int argc
, char **argv
)
46 int ipc_status
, reply_status
;
48 env_setargs(argc
, argv
);
54 r
= sef_receive_status(ANY
, &m
, &ipc_status
);
56 log_warn(&log
, "sef_receive_status() failed\n");
60 if (is_ipc_notify(ipc_status
)) {
62 /* Do not reply to notifications. */
68 log_debug(&log
, "Got message 0x%x from 0x%x\n", m
.m_type
,
73 /* Any user can read the time */
74 reply_status
= rtc
.get_time(&t
, m
.m_lc_readclock_rtcdev
.flags
);
75 if (reply_status
!= OK
) {
79 /* write results back to calling process */
81 store_t(caller
, m
.m_lc_readclock_rtcdev
.tm
, &t
);
85 /* Only super user is allowed to set the time */
86 if (getnuid(caller
) == SUPER_USER
) {
87 /* read time from calling process */
89 fetch_t(caller
, m
.m_lc_readclock_rtcdev
.tm
,
91 if (reply_status
!= OK
) {
96 rtc
.set_time(&t
, m
.m_lc_readclock_rtcdev
.flags
);
103 /* Only PM is allowed to set the power off time */
104 if (caller
== PM_PROC_NR
) {
105 reply_status
= rtc
.pwr_off();
107 reply_status
= EPERM
;
112 /* Unrecognized call */
113 reply_status
= EINVAL
;
118 m
.m_type
= RTCDEV_REPLY
;
119 m
.m_readclock_lc_rtcdev
.status
= reply_status
;
121 log_debug(&log
, "Sending Reply");
123 r
= ipc_sendnb(caller
, &m
);
125 log_warn(&log
, "ipc_sendnb() failed\n");
135 sef_cb_init(int type
, sef_init_info_t
* UNUSED(info
))
139 r
= arch_setup(&rtc
);
141 log_warn(&log
, "Clock setup failed\n");
147 log_warn(&log
, "Clock initalization failed\n");
158 * Register init callbacks. Use the same function for all event types
160 sef_setcb_init_fresh(sef_cb_init
);
161 sef_setcb_init_lu(sef_cb_init
);
162 sef_setcb_init_restart(sef_cb_init
);
165 * Register live update callbacks.
167 /* Agree to update immediately when LU is requested in a valid state. */
168 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
169 /* Support live update starting from any standard state. */
170 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard
);
172 /* Let SEF perform startup. */
179 return ((n
>> 4) & 0x0F) * 10 + (n
& 0x0F);
185 return ((n
/ 10) << 4) | (n
% 10);
189 fetch_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
)
191 return sys_datacopy(who_e
, rtcdev_tm
, SELF
, (vir_bytes
) t
,
196 store_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
)
198 return sys_datacopy(SELF
, (vir_bytes
) t
, who_e
, rtcdev_tm
,