From 336f7c5f684be96b4908cd3f02226964ed302e63 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 16 Jul 2016 16:45:07 +0100 Subject: hwclock: move command-line options to control structure The control structure is read-only everywhere else but in main(). Almost all changes are about how variables are referred, with one exception. Calls to read_adjtime() from manipulate_clock() and compare_clock() are moved to main(). This way it is possible to keep variable that tells if hwclock is using UTC-0 be part of control structure. Changes within #ifdef __alpha__ segments were tested by flipping the preprocessor directivive otherway around and getting good compilaton all the way to the point where linking on none-alpha system failed. Reviewed-by: J William Piggott Signed-off-by: Sami Kerola --- sys-utils/hwclock-cmos.c | 144 +++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 66 deletions(-) (limited to 'sys-utils/hwclock-cmos.c') diff --git a/sys-utils/hwclock-cmos.c b/sys-utils/hwclock-cmos.c index 9103b7343..625da75c8 100644 --- a/sys-utils/hwclock-cmos.c +++ b/sys-utils/hwclock-cmos.c @@ -175,20 +175,21 @@ static int is_in_cpuinfo(char *fmt, char *str) * Set cmos_epoch, either from user options, or by asking the kernel, or by * looking at /proc/cpu_info */ -void set_cmos_epoch(int ARCconsole, int SRM) +void set_cmos_epoch(const struct hwclock_control *ctl) { unsigned long epoch; + int set_epoc; /* Believe the user */ - if (epoch_option != -1) { - cmos_epoch = epoch_option; + if (ctl->epoch_option) { + cmos_epoch = ctl->epoch_option; return; } - if (ARCconsole) + if (ctl->ARCconsole) cmos_epoch = 1980; - if (ARCconsole || SRM) + if (ctl->ARCconsole || ctl->SRM) return; #ifdef __linux__ @@ -196,7 +197,7 @@ void set_cmos_epoch(int ARCconsole, int SRM) * If we can ask the kernel, we don't need guessing from * /proc/cpuinfo */ - if (get_epoch_rtc(&epoch, 1) == 0) { + if (get_epoch_rtc(ctl, &epoch, 1) == 0) { cmos_epoch = epoch; return; } @@ -218,8 +219,8 @@ void set_cmos_epoch(int ARCconsole, int SRM) * different "epoch" ideas. */ if (is_in_cpuinfo("system serial number", "MILO")) { - ARCconsole = 1; - if (debug) + set_epoc = 1; + if (ctl->debug) printf(_("booted from MILO\n")); } @@ -229,28 +230,28 @@ void set_cmos_epoch(int ARCconsole, int SRM) * and not an ARC-style epoch. BCD is detected dynamically, but we * must NOT adjust like ARC. */ - if (ARCconsole && is_in_cpuinfo("system type", "Ruffian")) { - ARCconsole = 0; - if (debug) + if (set_epoc && is_in_cpuinfo("system type", "Ruffian")) { + set_epoc = 0; + if (ctl->debug) printf(_("Ruffian BCD clock\n")); } - if (ARCconsole) + if (set_epoc) cmos_epoch = 1980; } -void set_cmos_access(int Jensen, int funky_toy) +void set_cmos_access(const struct hwclock_control *ctl) { /* * See whether we're dealing with a Jensen---it has a weird I/O * system. DEC was just learning how to build Alpha PCs. */ - if (Jensen || is_in_cpuinfo("system type", "Jensen")) { + if (ctl->Jensen || is_in_cpuinfo("system type", "Jensen")) { use_dev_port = 1; clock_ctl_addr = 0x170; clock_data_addr = 0x171; - if (debug) + if (ctl->debug) printf(_("clockport adjusted to 0x%x\n"), clock_ctl_addr); } @@ -260,13 +261,13 @@ void set_cmos_access(int Jensen, int funky_toy) * TOY that must be accessed differently to work correctly. */ /* Nautilus stuff reported by Neoklis Kyriazis */ - if (funky_toy || + if (ctl->funky_toy || is_in_cpuinfo("system variation", "PC164") || is_in_cpuinfo("system variation", "LX164") || is_in_cpuinfo("system variation", "SX164") || is_in_cpuinfo("system type", "Nautilus")) { funkyTOY = 1; - if (debug) + if (ctl->debug) printf(_("funky TOY!\n")); } } @@ -282,13 +283,16 @@ void set_cmos_access(int Jensen, int funky_toy) * atomically, eventually. */ static unsigned long -atomic(const char *name, unsigned long (*op) (unsigned long), unsigned long arg) +atomic(const char *name, + unsigned long (*op) (const struct hwclock_control *ctl, unsigned long), + const struct hwclock_control *ctl, + unsigned long arg) { unsigned long ts1, ts2, n, v; for (n = 0; n < 1000; ++n) { asm volatile ("rpcc %0":"r=" (ts1)); - v = (*op) (arg); + v = (*op) (ctl, arg); asm volatile ("rpcc %0":"r=" (ts2)); if ((ts1 ^ ts2) >> 32 == 0) { @@ -307,24 +311,26 @@ atomic(const char *name, unsigned long (*op) (unsigned long), unsigned long arg) */ static unsigned long atomic(const char *name __attribute__ ((__unused__)), - unsigned long (*op) (unsigned long), + unsigned long (*op) (const struct hwclock_control *ctl, unsigned long), + const struct hwclock_control *ctl, unsigned long arg) { - return (*op) (arg); + return (*op) (ctl, arg); } #endif -static inline unsigned long cmos_read(unsigned long reg) +static inline unsigned long cmos_read(const struct hwclock_control *ctl, + unsigned long reg) { if (use_dev_port) { unsigned char v = reg | 0x80; lseek(dev_port_fd, clock_ctl_addr, 0); - if (write(dev_port_fd, &v, 1) == -1 && debug) + if (write(dev_port_fd, &v, 1) == -1 && ctl->debug) warn(_("cmos_read(): write to control address %X failed"), clock_ctl_addr); lseek(dev_port_fd, clock_data_addr, 0); - if (read(dev_port_fd, &v, 1) == -1 && debug) + if (read(dev_port_fd, &v, 1) == -1 && ctl->debug) warn(_("cmos_read(): read from data address %X failed"), clock_data_addr); return v; @@ -356,17 +362,18 @@ static inline unsigned long cmos_read(unsigned long reg) } } -static inline unsigned long cmos_write(unsigned long reg, unsigned long val) +static inline unsigned long cmos_write(const struct hwclock_control *ctl, + unsigned long reg, unsigned long val) { if (use_dev_port) { unsigned char v = reg | 0x80; lseek(dev_port_fd, clock_ctl_addr, 0); - if (write(dev_port_fd, &v, 1) == -1 && debug) + if (write(dev_port_fd, &v, 1) == -1 && ctl->debug) warn(_("cmos_write(): write to control address %X failed"), clock_ctl_addr); v = (val & 0xff); lseek(dev_port_fd, clock_data_addr, 0); - if (write(dev_port_fd, &v, 1) == -1 && debug) + if (write(dev_port_fd, &v, 1) == -1 && ctl->debug) warn(_("cmos_write(): write to data address %X failed"), clock_data_addr); } else { @@ -376,7 +383,8 @@ static inline unsigned long cmos_write(unsigned long reg, unsigned long val) return 0; } -static unsigned long cmos_set_time(unsigned long arg) +static unsigned long cmos_set_time(const struct hwclock_control *ctl, + unsigned long arg) { unsigned char save_control, save_freq_select, pmbit = 0; struct tm tm = *(struct tm *)arg; @@ -399,10 +407,10 @@ static unsigned long cmos_set_time(unsigned long arg) * 1111 500 milliseconds (maximum, 2 Hz) * 0110 976.562 microseconds (default 1024 Hz) */ - save_control = cmos_read(11); /* tell the clock it's being set */ - cmos_write(11, (save_control | 0x80)); - save_freq_select = cmos_read(10); /* stop and reset prescaler */ - cmos_write(10, (save_freq_select | 0x70)); + save_control = cmos_read(ctl, 11); /* tell the clock it's being set */ + cmos_write(ctl, 11, (save_control | 0x80)); + save_freq_select = cmos_read(ctl, 10); /* stop and reset prescaler */ + cmos_write(ctl, 10, (save_freq_select | 0x70)); tm.tm_year += TM_EPOCH; century = tm.tm_year / 100; @@ -431,15 +439,15 @@ static unsigned long cmos_set_time(unsigned long arg) BIN_TO_BCD(century); } - cmos_write(0, tm.tm_sec); - cmos_write(2, tm.tm_min); - cmos_write(4, tm.tm_hour | pmbit); - cmos_write(6, tm.tm_wday); - cmos_write(7, tm.tm_mday); - cmos_write(8, tm.tm_mon); - cmos_write(9, tm.tm_year); + cmos_write(ctl, 0, tm.tm_sec); + cmos_write(ctl, 2, tm.tm_min); + cmos_write(ctl, 4, tm.tm_hour | pmbit); + cmos_write(ctl, 6, tm.tm_wday); + cmos_write(ctl, 7, tm.tm_mday); + cmos_write(ctl, 8, tm.tm_mon); + cmos_write(ctl, 9, tm.tm_year); if (century_byte) - cmos_write(century_byte, century); + cmos_write(ctl, century_byte, century); /* * The kernel sources, linux/arch/i386/kernel/time.c, have the @@ -452,33 +460,34 @@ static unsigned long cmos_set_time(unsigned long arg) * the Dallas Semiconductor data sheets, but who believes data * sheets anyway ... -- Markus Kuhn */ - cmos_write(11, save_control); - cmos_write(10, save_freq_select); + cmos_write(ctl, 11, save_control); + cmos_write(ctl, 10, save_freq_select); return 0; } -static int hclock_read(unsigned long reg) +static int hclock_read(const struct hwclock_control *ctl, unsigned long reg) { - return atomic("clock read", cmos_read, (reg)); + return atomic("clock read", cmos_read, ctl, reg); } -static void hclock_set_time(const struct tm *tm) +static void hclock_set_time(const struct hwclock_control *ctl, const struct tm *tm) { - atomic("set time", cmos_set_time, (unsigned long)(tm)); + atomic("set time", cmos_set_time, ctl, (unsigned long)(tm)); } -static inline int cmos_clock_busy(void) +static inline int cmos_clock_busy(const struct hwclock_control *ctl) { return #ifdef __alpha__ /* poll bit 4 (UF) of Control Register C */ - funkyTOY ? (hclock_read(12) & 0x10) : + funkyTOY ? (hclock_read(ctl, 12) & 0x10) : #endif /* poll bit 7 (UIP) of Control Register A */ - (hclock_read(10) & 0x80); + (hclock_read(ctl, 10) & 0x80); } -static int synchronize_to_clock_tick_cmos(void) +static int synchronize_to_clock_tick_cmos(const struct hwclock_control *ctl + __attribute__((__unused__))) { int i; @@ -487,12 +496,12 @@ static int synchronize_to_clock_tick_cmos(void) * weird happens, we have a limit on this loop to reduce the impact * of this failure. */ - for (i = 0; !cmos_clock_busy(); i++) + for (i = 0; !cmos_clock_busy(ctl); i++) if (i >= 10000000) return 1; /* Wait for fall. Should be within 2.228 ms. */ - for (i = 0; cmos_clock_busy(); i++) + for (i = 0; cmos_clock_busy(ctl); i++) if (i >= 1000000) return 1; return 0; @@ -514,7 +523,8 @@ static int synchronize_to_clock_tick_cmos(void) * In practice, the chance of this function returning the wrong time is * extremely remote. */ -static int read_hardware_clock_cmos(struct tm *tm) +static int read_hardware_clock_cmos(const struct hwclock_control *ctl + __attribute__((__unused__)), struct tm *tm) { bool got_time = FALSE; unsigned char status, pmbit; @@ -536,25 +546,25 @@ static int read_hardware_clock_cmos(struct tm *tm) * at first, the clock has changed while we were running. We * check for that too, and if it happens, we start over. */ - if (!cmos_clock_busy()) { + if (!cmos_clock_busy(ctl)) { /* No clock update in progress, go ahead and read */ - tm->tm_sec = hclock_read(0); - tm->tm_min = hclock_read(2); - tm->tm_hour = hclock_read(4); - tm->tm_wday = hclock_read(6); - tm->tm_mday = hclock_read(7); - tm->tm_mon = hclock_read(8); - tm->tm_year = hclock_read(9); - status = hclock_read(11); + tm->tm_sec = hclock_read(ctl, 0); + tm->tm_min = hclock_read(ctl, 2); + tm->tm_hour = hclock_read(ctl, 4); + tm->tm_wday = hclock_read(ctl, 6); + tm->tm_mday = hclock_read(ctl, 7); + tm->tm_mon = hclock_read(ctl, 8); + tm->tm_year = hclock_read(ctl, 9); + status = hclock_read(ctl, 11); #if 0 if (century_byte) - century = hclock_read(century_byte); + century = hclock_read(ctl, century_byte); #endif /* * Unless the clock changed while we were reading, * consider this a good clock read . */ - if (tm->tm_sec == hclock_read(0)) + if (tm->tm_sec == hclock_read(ctl, 0)) got_time = TRUE; } /* @@ -602,10 +612,12 @@ static int read_hardware_clock_cmos(struct tm *tm) return 0; } -static int set_hardware_clock_cmos(const struct tm *new_broken_time) +static int set_hardware_clock_cmos(const struct hwclock_control *ctl + __attribute__((__unused__)), + const struct tm *new_broken_time) { - hclock_set_time(new_broken_time); + hclock_set_time(ctl, new_broken_time); return 0; } -- cgit v1.2.3-55-g7522