Annotation of Gnu-Mach/i386/i386at/rtc.c, revision 1.1.1.1

1.1       root        1: /* 
                      2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
                      4:  * All Rights Reserved.
                      5:  * 
                      6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
                     11:  * 
                     12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
                     15:  * 
                     16:  * Carnegie Mellon requests users of this software to return to
                     17:  * 
                     18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
                     22:  * 
                     23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: 
                     27: /*
                     28:   Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
                     29: 
                     30:                All Rights Reserved
                     31: 
                     32: Permission to use, copy, modify, and distribute this software and
                     33: its documentation for any purpose and without fee is hereby
                     34: granted, provided that the above copyright notice appears in all
                     35: copies and that both the copyright notice and this permission notice
                     36: appear in supporting documentation, and that the name of Intel
                     37: not be used in advertising or publicity pertaining to distribution
                     38: of the software without specific, written prior permission.
                     39: 
                     40: INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
                     41: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
                     42: IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
                     43: CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
                     44: LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
                     45: NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
                     46: WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     47: */
                     48: 
                     49: #include <sys/types.h>
                     50: #include <sys/time.h>
                     51: #include <kern/time_out.h>
                     52: #include <i386/machspl.h>
                     53: #include <i386at/rtc.h>
                     54: 
                     55: static unsigned char rtc[RTC_NREG];
                     56: static int first_rtcopen_ever = 1;
                     57: 
                     58: rtcinit()
                     59: {
                     60:        outb(RTC_ADDR, RTC_A);
                     61:        outb(RTC_DATA, RTC_DIV2 | RTC_RATE6);
                     62:        outb(RTC_ADDR, RTC_B);
                     63:        outb(RTC_DATA, RTC_HM);
                     64: }
                     65: 
                     66: 
                     67: int
                     68: rtcget(regs)
                     69: unsigned char  *regs;
                     70: {
                     71:        if (first_rtcopen_ever) {
                     72:                rtcinit();
                     73:                first_rtcopen_ever = 0;
                     74:        }
                     75:        outb(RTC_ADDR, RTC_D); 
                     76:        if (inb(RTC_DATA) & RTC_VRT == 0) return(-1);
                     77:        outb(RTC_ADDR, RTC_A);  
                     78:        while (inb(RTC_DATA) & RTC_UIP)         /* busy wait */
                     79:                outb(RTC_ADDR, RTC_A);  
                     80:        load_rtc(regs);
                     81:        return(0);
                     82: }      
                     83: 
                     84: rtcput(regs)
                     85: unsigned char  *regs;
                     86: {
                     87:        register unsigned char  x;
                     88: 
                     89:        if (first_rtcopen_ever) {
                     90:                rtcinit();
                     91:                first_rtcopen_ever = 0;
                     92:        }
                     93:        outb(RTC_ADDR, RTC_B);
                     94:        x = inb(RTC_DATA);
                     95:        outb(RTC_ADDR, RTC_B);
                     96:        outb(RTC_DATA, x | RTC_SET);    
                     97:        save_rtc(regs);
                     98:        outb(RTC_ADDR, RTC_B);
                     99:        outb(RTC_DATA, x & ~RTC_SET); 
                    100: }
                    101: 
                    102: 
                    103: extern struct timeval time;
                    104: extern struct timezone tz;
                    105: 
                    106: static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                    107: 
                    108: yeartoday(year)
                    109: int year;
                    110: {
                    111:        return((year%4) ? 365 : 366);
                    112: }
                    113: 
                    114: hexdectodec(n)
                    115: char n;
                    116: {
                    117:        return(((n>>4)&0x0F)*10 + (n&0x0F));
                    118: }
                    119: 
                    120: char
                    121: dectohexdec(n)
                    122: int n;
                    123: {
                    124:        return((char)(((n/10)<<4)&0xF0) | ((n%10)&0x0F));
                    125: }
                    126: 
                    127: 
                    128: readtodc(tp)
                    129:        u_int   *tp;
                    130: {
                    131:        struct rtc_st rtclk;
                    132:        time_t n;
                    133:        int sec, min, hr, dom, mon, yr;
                    134:        int i, days = 0;
                    135:        spl_t   ospl;
                    136: 
                    137: #ifdef MACH_KERNEL
                    138:        ospl = splclock();
                    139: #else  MACH_KERNEL
                    140:        ospl = spl5();
                    141: #endif MACH_KERNEL
                    142:        if (rtcget(&rtclk)) {
                    143:                splx(ospl);
                    144:                return(-1);
                    145:        }
                    146:        splx (ospl);
                    147: 
                    148:        sec = hexdectodec(rtclk.rtc_sec);
                    149:        min = hexdectodec(rtclk.rtc_min);
                    150:        hr = hexdectodec(rtclk.rtc_hr);
                    151:        dom = hexdectodec(rtclk.rtc_dom);
                    152:        mon = hexdectodec(rtclk.rtc_mon);
                    153:        yr = hexdectodec(rtclk.rtc_yr);
                    154:        yr = (yr < 70) ? yr+100 : yr;
                    155: 
                    156:        n = sec + 60 * min + 3600 * hr;
                    157:        n += (dom - 1) * 3600 * 24;
                    158: 
                    159:        if (yeartoday(yr) == 366)
                    160:                month[1] = 29;
                    161:        for (i = mon - 2; i >= 0; i--)
                    162:                days += month[i];
                    163:        month[1] = 28;
                    164:        for (i = 70; i < yr; i++)
                    165:                days += yeartoday(i);
                    166:        n += days * 3600 * 24;
                    167: 
                    168: #ifdef MACH_KERNEL
                    169: #else  MACH_KERNEL
                    170:        n += tz.tz_minuteswest * 60;
                    171:        if (tz.tz_dsttime)
                    172:                n -= 3600;
                    173: #endif MACH_KERNEL
                    174: 
                    175:        *tp = n;
                    176: 
                    177:        return(0);
                    178: }
                    179: 
                    180: writetodc()
                    181: {
                    182:        struct rtc_st rtclk;
                    183:        time_t n;
                    184:        int diff, i, j;
                    185:        spl_t   ospl;
                    186: 
                    187: #ifdef MACH_KERNEL
                    188:        ospl = splclock();
                    189: #else  MACH_KERNEL
                    190:        ospl = spl5();
                    191: #endif MACH_KERNEL
                    192:        if (rtcget(&rtclk)) {
                    193:                splx(ospl);
                    194:                return(-1);
                    195:        }
                    196:        splx(ospl);
                    197: 
                    198: #ifdef MACH_KERNEL
                    199:        diff = 0;
                    200: #else  MACH_KERNEL
                    201:        diff = tz.tz_minuteswest * 60;
                    202:        if (tz.tz_dsttime)
                    203:                diff -= 3600;
                    204: #endif MACH_KERNEL
                    205:        n = (time.tv_sec - diff) % (3600 * 24);   /* hrs+mins+secs */
                    206:        rtclk.rtc_sec = dectohexdec(n%60);
                    207:        n /= 60;
                    208:        rtclk.rtc_min = dectohexdec(n%60);
                    209:        rtclk.rtc_hr = dectohexdec(n/60);
                    210: 
                    211:        n = (time.tv_sec - diff) / (3600 * 24); /* days */
                    212:        rtclk.rtc_dow = (n + 4) % 7;  /* 1/1/70 is Thursday */
                    213: 
                    214:        for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
                    215:                n -= i;
                    216: 
                    217:        rtclk.rtc_yr = dectohexdec(j - 1900);
                    218: 
                    219:        if (i == 366)
                    220:                month[1] = 29;
                    221:        for (i = 0; n >= month[i]; i++)
                    222:                n -= month[i];
                    223:        month[1] = 28;
                    224:        rtclk.rtc_mon = dectohexdec(++i);
                    225: 
                    226:        rtclk.rtc_dom = dectohexdec(++n);
                    227: 
                    228: #ifdef MACH_KERNEL
                    229:        ospl = splclock();
                    230: #else  MACH_KERNEL
                    231:        ospl = spl5();
                    232: #endif MACH_KERNEL
                    233:        rtcput(&rtclk);
                    234:        splx(ospl);
                    235: 
                    236:        return(0);
                    237: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.