Drop main() prototype. Syncs with NetBSD-8
[minix.git] / minix / drivers / clock / readclock / readclock.c
blobb3c0990ef1f74ffa10de2515f761c029748011ab
1 /* readclock - manipulate the hardware real time clock */
3 #include <sys/types.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <time.h>
8 #include <errno.h>
9 #include <lib.h>
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 = {
25 .name = "readclock",
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);
38 int
39 main(int argc, char **argv)
41 int r;
42 endpoint_t caller;
43 struct tm t;
44 message m;
45 int ipc_status, reply_status;
47 env_setargs(argc, argv);
48 sef_local_startup();
50 while (TRUE) {
52 /* Receive Message */
53 r = sef_receive_status(ANY, &m, &ipc_status);
54 if (r != OK) {
55 log_warn(&log, "sef_receive_status() failed\n");
56 continue;
59 if (is_ipc_notify(ipc_status)) {
61 /* Do not reply to notifications. */
62 continue;
65 caller = m.m_source;
67 log_debug(&log, "Got message 0x%x from 0x%x\n", m.m_type,
68 caller);
70 switch (m.m_type) {
71 case RTCDEV_GET_TIME:
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) {
75 break;
78 /* write results back to calling process */
79 reply_status =
80 store_t(caller, m.m_lc_readclock_rtcdev.tm, &t);
81 break;
83 case RTCDEV_SET_TIME:
84 /* Only super user is allowed to set the time */
85 if (getnuid(caller) == SUPER_USER) {
86 /* read time from calling process */
87 reply_status =
88 fetch_t(caller, m.m_lc_readclock_rtcdev.tm,
89 &t);
90 if (reply_status != OK) {
91 break;
94 reply_status =
95 rtc.set_time(&t, m.m_lc_readclock_rtcdev.flags);
96 } else {
97 reply_status = EPERM;
99 break;
101 case RTCDEV_PWR_OFF:
102 /* Only PM is allowed to set the power off time */
103 if (caller == PM_PROC_NR) {
104 reply_status = rtc.pwr_off();
105 } else {
106 reply_status = EPERM;
108 break;
110 default:
111 /* Unrecognized call */
112 reply_status = EINVAL;
113 break;
116 /* Send Reply */
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);
123 if (r != OK) {
124 log_warn(&log, "ipc_sendnb() failed\n");
125 continue;
129 rtc.exit();
130 return 0;
133 static int
134 sef_cb_init(int type, sef_init_info_t * UNUSED(info))
136 int r;
138 r = arch_setup(&rtc);
139 if (r != OK) {
140 log_warn(&log, "Clock setup failed\n");
141 return r;
144 r = rtc.init();
145 if (r != OK) {
146 log_warn(&log, "Clock initalization failed\n");
147 return r;
150 return OK;
153 static void
154 sef_local_startup()
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. */
164 sef_startup();
168 bcd_to_dec(int n)
170 return ((n >> 4) & 0x0F) * 10 + (n & 0x0F);
174 dec_to_bcd(int n)
176 return ((n / 10) << 4) | (n % 10);
179 static int
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,
183 sizeof(struct tm));
186 static int
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,
190 sizeof(struct tm));