Repository navigation
Expand file tree
/
Copy pathcmos.c
More file actions
262 lines (225 loc) · 7.62 KB
/
Copy pathcmos.c
File metadata and controls
262 lines (225 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* This file contains a device driver that can access the CMOS chip to
* get or set the system time. It drives the special file:
*
* /dev/cmos - CMOS chip
*
* Changes:
* Aug 04, 2005 Created. Read CMOS time. (Jorrit N. Herder)
*
* Manufacturers usually use the ID value of the IBM model they emulate.
* However some manufacturers, notably HP and COMPAQ, have had different
* ideas in the past.
*
* Machine ID byte information source:
* _The Programmer's PC Sourcebook_ by Thom Hogan,
* published by Microsoft Press
*/
#include "../drivers.h"
#include <sys/ioc_cmos.h>
#include <time.h>
#include <ibm/cmos.h>
#include <ibm/bios.h>
extern int errno; /* error number for PM calls */
FORWARD _PROTOTYPE( int gettime, (int who, int y2kflag, vir_bytes dst_time));
FORWARD _PROTOTYPE( void reply, (int reply, int replyee, int proc, int s));
FORWARD _PROTOTYPE( int read_register, (int register_address));
FORWARD _PROTOTYPE( int get_cmostime, (struct tm *tmp, int y2kflag));
FORWARD _PROTOTYPE( int dec_to_bcd, (int dec));
FORWARD _PROTOTYPE( int bcd_to_dec, (int bcd));
/*===========================================================================*
* main *
*===========================================================================*/
PUBLIC void main(void)
{
message m;
int y2kflag;
int result;
int suspended = NONE;
int s;
while(TRUE) {
/* Get work. */
if (OK != (s=receive(ANY, &m)))
panic("CMOS", "attempt to receive work failed", s);
/* Handle request. */
switch(m.m_type) {
case DEV_OPEN:
case DEV_CLOSE:
case CANCEL:
reply(TASK_REPLY, m.m_source, m.PROC_NR, OK);
break;
case DEV_IOCTL:
/* Probably best to SUSPEND the caller, CMOS I/O has nasty timeouts.
* This way we don't block the rest of the system. First check if
* another process is already suspended. We cannot handle multiple
* requests at a time.
*/
if (suspended != NONE) {
reply(TASK_REPLY, m.m_source, m.PROC_NR, EBUSY);
break;
}
suspended = m.PROC_NR;
reply(TASK_REPLY, m.m_source, m.PROC_NR, SUSPEND);
switch(m.REQUEST) {
case CIOCGETTIME: /* get CMOS time */
case CIOCGETTIMEY2K:
y2kflag = (m.REQUEST = CIOCGETTIME) ? 0 : 1;
result = gettime(m.PROC_NR, y2kflag, (vir_bytes) m.ADDRESS);
break;
case CIOCSETTIME:
case CIOCSETTIMEY2K:
default: /* unsupported ioctl */
result = ENOSYS;
}
/* Request completed. Tell the caller to check our status. */
notify(m.m_source);
break;
case DEV_STATUS:
/* The FS calls back to get our status. Revive the suspended
* processes and return the status of reading the CMOS.
*/
if (suspended == NONE)
reply(DEV_NO_STATUS, m.m_source, NONE, OK);
else
reply(DEV_REVIVE, m.m_source, suspended, result);
suspended = NONE;
break;
case SYN_ALARM: /* shouldn't happen */
case SYS_SIG: /* ignore system events */
continue;
default:
reply(TASK_REPLY, m.m_source, m.PROC_NR, EINVAL);
}
}
}
/*===========================================================================*
* reply *
*===========================================================================*/
PRIVATE void reply(int code, int replyee, int process, int status)
{
message m;
int s;
m.m_type = code; /* TASK_REPLY or REVIVE */
m.REP_STATUS = status; /* result of device operation */
m.REP_PROC_NR = process; /* which user made the request */
if (OK != (s=send(replyee, &m)))
panic("CMOS", "sending reply failed", s);
}
/*===========================================================================*
* gettime *
*===========================================================================*/
PRIVATE int gettime(int who, int y2kflag, vir_bytes dst_time)
{
unsigned char mach_id, cmos_state;
struct tm time1;
int i, s;
/* First obtain the machine ID to see if we can read the CMOS clock. Only
* for PS_386 and PC_AT this is possible. Otherwise, return an error.
*/
sys_vircopy(SELF, BIOS_SEG, (vir_bytes) MACHINE_ID_ADDR,
SELF, D, (vir_bytes) &mach_id, MACHINE_ID_SIZE);
if (mach_id != PS_386_MACHINE && mach_id != PC_AT_MACHINE) {
printf("IS: Machine ID unknown. ID byte = %02x.\n", mach_id);
return(EFAULT);
}
/* Now check the CMOS' state to see if we can read a proper time from it.
* If the state is crappy, return an error.
*/
cmos_state = read_register(CMOS_STATUS);
if (cmos_state & (CS_LOST_POWER | CS_BAD_CHKSUM | CS_BAD_TIME)) {
printf( "IS: CMOS RAM error(s) found. State = 0x%02x\n", cmos_state );
if (cmos_state & CS_LOST_POWER)
printf("IS: RTC lost power. Reset CMOS RAM with SETUP." );
if (cmos_state & CS_BAD_CHKSUM)
printf("IS: CMOS RAM checksum is bad. Run SETUP." );
if (cmos_state & CS_BAD_TIME)
printf("IS: Time invalid in CMOS RAM. Reset clock." );
return(EFAULT);
}
/* Everything seems to be OK. Read the CMOS real time clock and copy the
* result back to the caller.
*/
if (get_cmostime(&time1, y2kflag) != 0)
return(EFAULT);
sys_datacopy(SELF, (vir_bytes) &time1,
who, dst_time, sizeof(struct tm));
return(OK);
}
PRIVATE int get_cmostime(struct tm *t, int y2kflag)
{
/* Update the structure pointed to by time with the current time as read
* from CMOS RAM of the RTC. If necessary, the time is converted into a
* binary format before being stored in the structure.
*/
int osec, n;
unsigned long i;
clock_t t0,t1;
/* Start a timer to keep us from getting stuck on a dead clock. */
getuptime(&t0);
do {
osec = -1;
n = 0;
do {
getuptime(&t1);
if (t1-t0 > 5*HZ) {
printf("readclock: CMOS clock appears dead\n");
return(1);
}
/* Clock update in progress? */
if (read_register(RTC_REG_A) & RTC_A_UIP) continue;
t->tm_sec = read_register(RTC_SEC);
if (t->tm_sec != osec) {
/* Seconds changed. First from -1, then because the
* clock ticked, which is what we're waiting for to
* get a precise reading.
*/
osec = t->tm_sec;
n++;
}
} while (n < 2);
/* Read the other registers. */
t->tm_min = read_register(RTC_MIN);
t->tm_hour = read_register(RTC_HOUR);
t->tm_mday = read_register(RTC_MDAY);
t->tm_mon = read_register(RTC_MONTH);
t->tm_year = read_register(RTC_YEAR);
/* Time stable? */
} while (read_register(RTC_SEC) != t->tm_sec
|| read_register(RTC_MIN) != t->tm_min
|| read_register(RTC_HOUR) != t->tm_hour
|| read_register(RTC_MDAY) != t->tm_mday
|| read_register(RTC_MONTH) != t->tm_mon
|| read_register(RTC_YEAR) != t->tm_year);
if ((read_register(RTC_REG_B) & RTC_B_DM_BCD) == 0) {
/* Convert BCD to binary (default RTC mode). */
t->tm_year = bcd_to_dec(t->tm_year);
t->tm_mon = bcd_to_dec(t->tm_mon);
t->tm_mday = bcd_to_dec(t->tm_mday);
t->tm_hour = bcd_to_dec(t->tm_hour);
t->tm_min = bcd_to_dec(t->tm_min);
t->tm_sec = bcd_to_dec(t->tm_sec);
}
t->tm_mon--; /* Counts from 0. */
/* Correct the year, good until 2080. */
if (t->tm_year < 80) t->tm_year += 100;
if (y2kflag) {
/* Clock with Y2K bug, interpret 1980 as 2000, good until 2020. */
if (t->tm_year < 100) t->tm_year += 20;
}
return 0;
}
PRIVATE int read_register(int reg_addr)
{
/* Read a single CMOS register value. */
int r = 0;
sys_outb(RTC_INDEX, reg_addr);
sys_inb(RTC_IO, &r);
return r;
}
PRIVATE int bcd_to_dec(int n)
{
return ((n >> 4) & 0x0F) * 10 + (n & 0x0F);
}
PRIVATE int dec_to_bcd(int n)
{
return ((n / 10) << 4) | (n % 10);
}