1 /* kd.c - KDGHWCLK stuff, possibly m68k only - deprecated */
14 #include <unistd.h> /* for close() */
15 #include <fcntl.h> /* for O_RDONLY */
17 #include <sys/ioctl.h>
21 static int con_fd = -1; /* opened by probe_for_kd_clock() */
23 static char *con_fd_filename; /* usually "/dev/tty1" */
25 /* Get defines for KDGHWCLK and KDSHWCLK (m68k) */
28 #define KDGHWCLK 0x4B50 /* get hardware clock */
29 #define KDSHWCLK 0x4B51 /* set hardware clock */
31 unsigned sec; /* 0..59 */
32 unsigned min; /* 0..59 */
33 unsigned hour; /* 0..23 */
34 unsigned day; /* 1..31 */
35 unsigned mon; /* 0..11 */
36 unsigned year; /* 70... */
37 int wday; /* 0..6, 0 is Sunday, -1 means unknown/don't set */
42 synchronize_to_clock_tick_kd(void) {
43 /*----------------------------------------------------------------------------
44 Wait for the top of a clock tick by calling KDGHWCLK in a busy loop until
46 -----------------------------------------------------------------------------*/
49 /* The time when we were called (and started waiting) */
50 struct hwclk_time start_time, nowtime;
53 printf(_("Waiting in loop for time from KDGHWCLK to change\n"));
55 if (ioctl(con_fd, KDGHWCLK, &start_time) == -1) {
56 outsyserr(_("KDGHWCLK ioctl to read time failed"));
62 /* Added by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
63 /* "The culprit is the fast loop with KDGHWCLK ioctls. It seems
64 the kernel gets confused by those on Amigas with A2000 RTCs
65 and simply hangs after some time. Inserting a nanosleep helps." */
66 /* Christian T. Steigies: 1 instead of 1000000 is still sufficient
67 to keep the machine from freezing. */
70 struct timespec xsleep = { 0, 1 };
71 nanosleep( &xsleep, NULL );
77 fprintf(stderr, _("Timed out waiting for time change.\n"));
80 if (ioctl(con_fd, KDGHWCLK, &nowtime) == -1) {
81 outsyserr(_("KDGHWCLK ioctl to read time failed in loop"));
84 } while (start_time.sec == nowtime.sec);
91 read_hardware_clock_kd(struct tm *tm) {
92 /*----------------------------------------------------------------------------
93 Read the hardware clock and return the current time via <tm>
94 argument. Use ioctls to /dev/tty1 on what we assume is an m68k
97 Note that we don't use /dev/console here. That might be a serial
99 -----------------------------------------------------------------------------*/
102 if (ioctl(con_fd, KDGHWCLK, &t) == -1) {
103 outsyserr(_("ioctl() failed to read time from %s"), con_fd_filename);
104 hwclock_exit(EX_IOERR);
109 tm->tm_hour = t.hour;
112 tm->tm_year = t.year;
113 tm->tm_wday = t.wday;
114 tm->tm_isdst = -1; /* Don't know if it's Daylight Savings Time */
121 set_hardware_clock_kd(const struct tm *new_broken_time) {
122 /*----------------------------------------------------------------------------
123 Set the Hardware Clock to the time <new_broken_time>. Use ioctls to
124 /dev/tty1 on what we assume is an m68k machine.
126 Note that we don't use /dev/console here. That might be a serial console.
127 ----------------------------------------------------------------------------*/
130 t.sec = new_broken_time->tm_sec;
131 t.min = new_broken_time->tm_min;
132 t.hour = new_broken_time->tm_hour;
133 t.day = new_broken_time->tm_mday;
134 t.mon = new_broken_time->tm_mon;
135 t.year = new_broken_time->tm_year;
136 t.wday = new_broken_time->tm_wday;
138 if (ioctl(con_fd, KDSHWCLK, &t ) == -1) {
139 outsyserr(_("ioctl KDSHWCLK failed"));
146 get_permissions_kd(void) {
150 static struct clock_ops kd = {
151 "KDGHWCLK interface to m68k clock",
153 read_hardware_clock_kd,
154 set_hardware_clock_kd,
155 synchronize_to_clock_tick_kd,
158 /* return &kd if KDGHWCLK works, NULL otherwise */
160 probe_for_kd_clock() {
161 struct clock_ops *ret = NULL;
164 if (con_fd < 0) { /* first time here */
165 con_fd_filename = "/dev/tty1";
166 con_fd = open(con_fd_filename, O_RDONLY);
169 /* perhaps they are using devfs? */
170 con_fd_filename = "/dev/vc/1";
171 con_fd = open(con_fd_filename, O_RDONLY);
174 /* probably KDGHWCLK exists on m68k only */
175 outsyserr(_("Can't open /dev/tty1 or /dev/vc/1"));
177 if (ioctl(con_fd, KDGHWCLK, &t) == -1) {
179 outsyserr(_("KDGHWCLK ioctl failed"));
185 #endif /* __m68k__ */