1*433d6423SLionel Sambuc /* readclock - manipulate the hardware real time clock */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <sys/types.h>
4*433d6423SLionel Sambuc #include <stdlib.h>
5*433d6423SLionel Sambuc #include <unistd.h>
6*433d6423SLionel Sambuc #include <stdio.h>
7*433d6423SLionel Sambuc #include <time.h>
8*433d6423SLionel Sambuc #include <errno.h>
9*433d6423SLionel Sambuc #include <lib.h>
10*433d6423SLionel Sambuc #include <minix/type.h>
11*433d6423SLionel Sambuc #include <minix/const.h>
12*433d6423SLionel Sambuc #include <minix/callnr.h>
13*433d6423SLionel Sambuc #include <minix/log.h>
14*433d6423SLionel Sambuc #include <minix/syslib.h>
15*433d6423SLionel Sambuc #include <minix/sysutil.h>
16*433d6423SLionel Sambuc #include <minix/com.h>
17*433d6423SLionel Sambuc #include <minix/type.h>
18*433d6423SLionel Sambuc #include <minix/safecopies.h>
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc #include "readclock.h"
21*433d6423SLionel Sambuc
22*433d6423SLionel Sambuc static struct rtc rtc;
23*433d6423SLionel Sambuc
24*433d6423SLionel Sambuc static struct log log = {
25*433d6423SLionel Sambuc .name = "readclock",
26*433d6423SLionel Sambuc .log_level = LEVEL_INFO,
27*433d6423SLionel Sambuc .log_func = default_log
28*433d6423SLionel Sambuc };
29*433d6423SLionel Sambuc
30*433d6423SLionel Sambuc /* functions for transfering struct tm to/from this driver and calling proc. */
31*433d6423SLionel Sambuc static int fetch_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t);
32*433d6423SLionel Sambuc static int store_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t);
33*433d6423SLionel Sambuc
34*433d6423SLionel Sambuc /* SEF functions and variables. */
35*433d6423SLionel Sambuc static void sef_local_startup(void);
36*433d6423SLionel Sambuc static int sef_cb_init(int type, sef_init_info_t * info);
37*433d6423SLionel Sambuc
38*433d6423SLionel Sambuc int
main(int argc,char ** argv)39*433d6423SLionel Sambuc main(int argc, char **argv)
40*433d6423SLionel Sambuc {
41*433d6423SLionel Sambuc int r;
42*433d6423SLionel Sambuc endpoint_t caller;
43*433d6423SLionel Sambuc struct tm t;
44*433d6423SLionel Sambuc message m;
45*433d6423SLionel Sambuc int ipc_status, reply_status;
46*433d6423SLionel Sambuc
47*433d6423SLionel Sambuc env_setargs(argc, argv);
48*433d6423SLionel Sambuc sef_local_startup();
49*433d6423SLionel Sambuc
50*433d6423SLionel Sambuc while (TRUE) {
51*433d6423SLionel Sambuc
52*433d6423SLionel Sambuc /* Receive Message */
53*433d6423SLionel Sambuc r = sef_receive_status(ANY, &m, &ipc_status);
54*433d6423SLionel Sambuc if (r != OK) {
55*433d6423SLionel Sambuc log_warn(&log, "sef_receive_status() failed\n");
56*433d6423SLionel Sambuc continue;
57*433d6423SLionel Sambuc }
58*433d6423SLionel Sambuc
59*433d6423SLionel Sambuc if (is_ipc_notify(ipc_status)) {
60*433d6423SLionel Sambuc
61*433d6423SLionel Sambuc /* Do not reply to notifications. */
62*433d6423SLionel Sambuc continue;
63*433d6423SLionel Sambuc }
64*433d6423SLionel Sambuc
65*433d6423SLionel Sambuc caller = m.m_source;
66*433d6423SLionel Sambuc
67*433d6423SLionel Sambuc log_debug(&log, "Got message 0x%x from 0x%x\n", m.m_type,
68*433d6423SLionel Sambuc caller);
69*433d6423SLionel Sambuc
70*433d6423SLionel Sambuc switch (m.m_type) {
71*433d6423SLionel Sambuc case RTCDEV_GET_TIME:
72*433d6423SLionel Sambuc /* Any user can read the time */
73*433d6423SLionel Sambuc reply_status = rtc.get_time(&t, m.m_lc_readclock_rtcdev.flags);
74*433d6423SLionel Sambuc if (reply_status != OK) {
75*433d6423SLionel Sambuc break;
76*433d6423SLionel Sambuc }
77*433d6423SLionel Sambuc
78*433d6423SLionel Sambuc /* write results back to calling process */
79*433d6423SLionel Sambuc reply_status =
80*433d6423SLionel Sambuc store_t(caller, m.m_lc_readclock_rtcdev.tm, &t);
81*433d6423SLionel Sambuc break;
82*433d6423SLionel Sambuc
83*433d6423SLionel Sambuc case RTCDEV_SET_TIME:
84*433d6423SLionel Sambuc /* Only super user is allowed to set the time */
85*433d6423SLionel Sambuc if (getnuid(caller) == SUPER_USER) {
86*433d6423SLionel Sambuc /* read time from calling process */
87*433d6423SLionel Sambuc reply_status =
88*433d6423SLionel Sambuc fetch_t(caller, m.m_lc_readclock_rtcdev.tm,
89*433d6423SLionel Sambuc &t);
90*433d6423SLionel Sambuc if (reply_status != OK) {
91*433d6423SLionel Sambuc break;
92*433d6423SLionel Sambuc }
93*433d6423SLionel Sambuc
94*433d6423SLionel Sambuc reply_status =
95*433d6423SLionel Sambuc rtc.set_time(&t, m.m_lc_readclock_rtcdev.flags);
96*433d6423SLionel Sambuc } else {
97*433d6423SLionel Sambuc reply_status = EPERM;
98*433d6423SLionel Sambuc }
99*433d6423SLionel Sambuc break;
100*433d6423SLionel Sambuc
101*433d6423SLionel Sambuc case RTCDEV_PWR_OFF:
102*433d6423SLionel Sambuc /* Only PM is allowed to set the power off time */
103*433d6423SLionel Sambuc if (caller == PM_PROC_NR) {
104*433d6423SLionel Sambuc reply_status = rtc.pwr_off();
105*433d6423SLionel Sambuc } else {
106*433d6423SLionel Sambuc reply_status = EPERM;
107*433d6423SLionel Sambuc }
108*433d6423SLionel Sambuc break;
109*433d6423SLionel Sambuc
110*433d6423SLionel Sambuc default:
111*433d6423SLionel Sambuc /* Unrecognized call */
112*433d6423SLionel Sambuc reply_status = EINVAL;
113*433d6423SLionel Sambuc break;
114*433d6423SLionel Sambuc }
115*433d6423SLionel Sambuc
116*433d6423SLionel Sambuc /* Send Reply */
117*433d6423SLionel Sambuc m.m_type = RTCDEV_REPLY;
118*433d6423SLionel Sambuc m.m_readclock_lc_rtcdev.status = reply_status;
119*433d6423SLionel Sambuc
120*433d6423SLionel Sambuc log_debug(&log, "Sending Reply");
121*433d6423SLionel Sambuc
122*433d6423SLionel Sambuc r = ipc_sendnb(caller, &m);
123*433d6423SLionel Sambuc if (r != OK) {
124*433d6423SLionel Sambuc log_warn(&log, "ipc_sendnb() failed\n");
125*433d6423SLionel Sambuc continue;
126*433d6423SLionel Sambuc }
127*433d6423SLionel Sambuc }
128*433d6423SLionel Sambuc
129*433d6423SLionel Sambuc rtc.exit();
130*433d6423SLionel Sambuc return 0;
131*433d6423SLionel Sambuc }
132*433d6423SLionel Sambuc
133*433d6423SLionel Sambuc static int
sef_cb_init(int type,sef_init_info_t * UNUSED (info))134*433d6423SLionel Sambuc sef_cb_init(int type, sef_init_info_t * UNUSED(info))
135*433d6423SLionel Sambuc {
136*433d6423SLionel Sambuc int r;
137*433d6423SLionel Sambuc
138*433d6423SLionel Sambuc r = arch_setup(&rtc);
139*433d6423SLionel Sambuc if (r != OK) {
140*433d6423SLionel Sambuc log_warn(&log, "Clock setup failed\n");
141*433d6423SLionel Sambuc return r;
142*433d6423SLionel Sambuc }
143*433d6423SLionel Sambuc
144*433d6423SLionel Sambuc r = rtc.init();
145*433d6423SLionel Sambuc if (r != OK) {
146*433d6423SLionel Sambuc log_warn(&log, "Clock initalization failed\n");
147*433d6423SLionel Sambuc return r;
148*433d6423SLionel Sambuc }
149*433d6423SLionel Sambuc
150*433d6423SLionel Sambuc return OK;
151*433d6423SLionel Sambuc }
152*433d6423SLionel Sambuc
153*433d6423SLionel Sambuc static void
sef_local_startup()154*433d6423SLionel Sambuc sef_local_startup()
155*433d6423SLionel Sambuc {
156*433d6423SLionel Sambuc /*
157*433d6423SLionel Sambuc * Register init callbacks. Use the same function for all event types
158*433d6423SLionel Sambuc */
159*433d6423SLionel Sambuc sef_setcb_init_fresh(sef_cb_init);
160*433d6423SLionel Sambuc sef_setcb_init_lu(sef_cb_init);
161*433d6423SLionel Sambuc sef_setcb_init_restart(sef_cb_init);
162*433d6423SLionel Sambuc
163*433d6423SLionel Sambuc /* Let SEF perform startup. */
164*433d6423SLionel Sambuc sef_startup();
165*433d6423SLionel Sambuc }
166*433d6423SLionel Sambuc
167*433d6423SLionel Sambuc int
bcd_to_dec(int n)168*433d6423SLionel Sambuc bcd_to_dec(int n)
169*433d6423SLionel Sambuc {
170*433d6423SLionel Sambuc return ((n >> 4) & 0x0F) * 10 + (n & 0x0F);
171*433d6423SLionel Sambuc }
172*433d6423SLionel Sambuc
173*433d6423SLionel Sambuc int
dec_to_bcd(int n)174*433d6423SLionel Sambuc dec_to_bcd(int n)
175*433d6423SLionel Sambuc {
176*433d6423SLionel Sambuc return ((n / 10) << 4) | (n % 10);
177*433d6423SLionel Sambuc }
178*433d6423SLionel Sambuc
179*433d6423SLionel Sambuc static int
fetch_t(endpoint_t who_e,vir_bytes rtcdev_tm,struct tm * t)180*433d6423SLionel Sambuc fetch_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t)
181*433d6423SLionel Sambuc {
182*433d6423SLionel Sambuc return sys_datacopy(who_e, rtcdev_tm, SELF, (vir_bytes) t,
183*433d6423SLionel Sambuc sizeof(struct tm));
184*433d6423SLionel Sambuc }
185*433d6423SLionel Sambuc
186*433d6423SLionel Sambuc static int
store_t(endpoint_t who_e,vir_bytes rtcdev_tm,struct tm * t)187*433d6423SLionel Sambuc store_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t)
188*433d6423SLionel Sambuc {
189*433d6423SLionel Sambuc return sys_datacopy(SELF, (vir_bytes) t, who_e, rtcdev_tm,
190*433d6423SLionel Sambuc sizeof(struct tm));
191*433d6423SLionel Sambuc }
192