目录
简介:
Linux中RTC设备驱动是一个标准的字符设备驱动,应用程序通过 open、 release、 read、 write 和 ioctl 等函数完成对 RTC 设备的操作。
rtc子系统分为三部分:
- rtc core:负责rtc设备注册注销;对用户空间提供rtc字符设备文件,以及rtc类sysfs接口;
- rtc driver:将rtc设备注册到rtc子系统,提供针对rtc设备的底层操作函数集;
- 用户空间sysfs节点:/dev/rtcX字符设备文件,以及其他调试接口;
图中,RTC Core已经在kernel中实现了,它初始化RTC子系统并向用户空间提供 file_operations 操作集(open、 read、 write 和 ioctl 等)。我们只需实现 RTC Driver(RTC 设备驱动) 和 设备树中对RTC的配置,RTC Driver中实现对RTC芯片的底层操作集。
一、代码分析
Linux 内核将 RTC 设备抽象为 rtc_device 结构体,因此 RTC 设备驱动就是申请并初始化 rtc_device,最后将 rtc_device 注册到 Linux 内核里面。
/* 路径:include/linux/rtc.h */
struct rtc_device
{
struct device dev;
struct module *owner;
int id; //当前rtc设备在rtc子系统的子序号
char name[RTC_DEVICE_NAME_SIZE];
const struct rtc_class_ops *ops;
struct mutex ops_lock;
struct cdev char_dev; //rtc设备对应的字符设备
unsigned long flags;
unsigned long irq_data;
spinlock_t irq_lock;
wait_queue_head_t irq_queue; //和用户空间同步的poll调用所使用的等待队列,由中断唤醒
struct fasync_struct *async_queue; //和用户空间同步基于文件的fasync调用,由中断触发
struct rtc_task *irq_task;
spinlock_t irq_task_lock;
int irq_freq;
int max_user_freq;
struct timerqueue_head timerqueue;
struct rtc_timer aie_timer;
struct rtc_timer uie_rtctimer;
struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
int pie_enabled;
struct work_struct irqwork;
/* Some hardware can't support UIE mode */
int uie_unsupported;
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
struct work_struct uie_task;
struct timer_list uie_timer;
/* Those fields are protected by rtc->irq_lock */
unsigned int oldsecs;
unsigned int uie_irq_active:1;
unsigned int stop_uie_polling:1;
unsigned int uie_task_active:1;
unsigned int uie_timer_active:1;
#endif
};
重点关注 struct rtc_class_ops *ops 成员变量,rtc_class_ops 为 RTC 设备的最底层操作函数集合,包括从 RTC 设备中读取时间、向 RTC 设备写入新的时间等。因此,rtc_class_ops 操作集需要用户根据所使用的 RTC 设备自己实现。
/* 路径:include/linux/rtc.h */
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_mmss64)(struct device *, time64_t secs);
int (*set_mmss)(struct device *, unsigned long secs);
int (*read_callback)(struct device *, int data);
int (*alarm_irq_enable)(struct device *, unsigned int enabled);
};
注意:rtc_class_ops 中的这些函数只是最底层对 RTC 设备的操作函数,并不是提供给应用层的file_operations 函数操作集。Linux 内核提供了一个 RTC 通用字符设备驱动文件 d