This document discusses device drivers for timers, real-time clocks (RTCs), and watchdog timers in Linux. It provides code examples for initializing system timers, implementing RTC driver IOCTL commands, and registering new RTC drivers that use the RTC class structure. It also describes what a watchdog timer is and things to note when using one, such as potential file system crashes from direct CPU resets.
9. Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [-l|--
localtime] [-u|--utc] [-f FILE]
Query and set hardware clock (RTC)
Options:
-r Show hardware clock time
-s Set system time from hardware clock
-w Set hardware clock to system time
-u Hardware clock is in UTC
-l Hardware clock is in local time
-f FILE Use specified device (e.g. /dev/rtc2)
9
hwclock usage
10. RTC Device Driver Example
可架構在misc之下
主要是支援兩個ioctl command
1) RTC_RD_TIME 讀取時間
2) RTC_SET_TIME 設定時間
struct rtc_time {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
10
12. Ioctl Example
static int
rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
struct rtc_time rtc_tm;
switch (cmd) {
case RTC_RD_TIME: /* Read the time/date from RTC */
get_rtc_time(&rtc_tm);
return copy_to_user((void *) arg, &rtc_tm, sizeof(rtc_tm)) ? -EFAULT : 0;
case RTC_SET_TIME: /* Set the RTC */
if (copy_from_user(&rtc_tm, (struct rtc_time *) arg, sizeof(struct rtc_time)))
return -EFAULT;
// set to hardware RTC with the time
return 0;
default:
return -EINVAL;
}
}
13. 新RTC Driver
Device node is put on /dev/misc directory.
Up to 16 RTC node, from rtc0
The major number is 254.
The minor number is from 0.
These are the same IOCTL command with the
old type RTC driver.
More features.
14. RTC Class Structure
struct rtc_class_ops {
int (*open)(struct device *);
void (*release)(struct device *);
int (*ioctl)(struct device *, unsigned int, unsigned long);
int (*read_time)(struct device *, struct rtc_time *);
int (*set_time)(struct device *, struct rtc_time *);
int (*read_alarm)(struct device *, struct rtc_wkalrm *);
int (*set_alarm)(struct device *, struct rtc_wkalrm *);
int (*proc)(struct device *, struct seq_file *);
int (*set_mmss)(struct device *, unsigned long secs);
int (*irq_set_state)(struct device *, int enabled);
int (*irq_set_freq)(struct device *, int freq);
int (*read_callback)(struct device *, int data);
int (*alarm_irq_enable)(struct device *, unsigned int enabled);
int (*update_irq_enable)(struct device *, unsigned int enabled);
};
15. RTC Time structure
struct rtc_time {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};