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>
20 #include "readclock.h"
22 static struct rtc rtc
;
24 static struct log log
= {
26 .log_level
= LEVEL_INFO
,
27 .log_func
= default_log
30 /* functions for transfering struct tm to/from this driver and calling proc. */
31 static int fetch_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
);
32 static int store_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
);
34 /* SEF functions and variables. */
35 static void sef_local_startup(void);
36 static int sef_cb_init(int type
, sef_init_info_t
* info
);
39 main(int argc
, char **argv
)
45 int ipc_status
, reply_status
;
47 env_setargs(argc
, argv
);
53 r
= sef_receive_status(ANY
, &m
, &ipc_status
);
55 log_warn(&log
, "sef_receive_status() failed\n");
59 if (is_ipc_notify(ipc_status
)) {
61 /* Do not reply to notifications. */
67 log_debug(&log
, "Got message 0x%x from 0x%x\n", m
.m_type
,
72 /* Any user can read the time */
73 reply_status
= rtc
.get_time(&t
, m
.m_lc_readclock_rtcdev
.flags
);
74 if (reply_status
!= OK
) {
78 /* write results back to calling process */
80 store_t(caller
, m
.m_lc_readclock_rtcdev
.tm
, &t
);
84 /* Only super user is allowed to set the time */
85 if (getnuid(caller
) == SUPER_USER
) {
86 /* read time from calling process */
88 fetch_t(caller
, m
.m_lc_readclock_rtcdev
.tm
,
90 if (reply_status
!= OK
) {
95 rtc
.set_time(&t
, m
.m_lc_readclock_rtcdev
.flags
);
102 /* Only PM is allowed to set the power off time */
103 if (caller
== PM_PROC_NR
) {
104 reply_status
= rtc
.pwr_off();
106 reply_status
= EPERM
;
111 /* Unrecognized call */
112 reply_status
= EINVAL
;
117 m
.m_type
= RTCDEV_REPLY
;
118 m
.m_readclock_lc_rtcdev
.status
= reply_status
;
120 log_debug(&log
, "Sending Reply");
122 r
= ipc_sendnb(caller
, &m
);
124 log_warn(&log
, "ipc_sendnb() failed\n");
134 sef_cb_init(int type
, sef_init_info_t
* UNUSED(info
))
138 r
= arch_setup(&rtc
);
140 log_warn(&log
, "Clock setup failed\n");
146 log_warn(&log
, "Clock initalization failed\n");
157 * Register init callbacks. Use the same function for all event types
159 sef_setcb_init_fresh(sef_cb_init
);
160 sef_setcb_init_lu(sef_cb_init
);
161 sef_setcb_init_restart(sef_cb_init
);
163 /* Let SEF perform startup. */
170 return ((n
>> 4) & 0x0F) * 10 + (n
& 0x0F);
176 return ((n
/ 10) << 4) | (n
% 10);
180 fetch_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
)
182 return sys_datacopy(who_e
, rtcdev_tm
, SELF
, (vir_bytes
) t
,
187 store_t(endpoint_t who_e
, vir_bytes rtcdev_tm
, struct tm
*t
)
189 return sys_datacopy(SELF
, (vir_bytes
) t
, who_e
, rtcdev_tm
,