前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux RTC驱动模型分析之rtc-proc.c

Linux RTC驱动模型分析之rtc-proc.c

作者头像
DragonKingZhu
发布2020-03-24 11:08:30
2.3K0
发布2020-03-24 11:08:30
举报

认识rtc节点

proc下的rtc节点的位置是: /proc/driver/rtc。 该节点可以清晰的显示出当前的时间,当前的日期,alarm的时间,日期,alarm是否使能等详细信息。在分析代码之前先认识一下。

代码语言:javascript
复制
root@test:test # cat /proc/driver/rtc 
rtc_time	: 06:25:56                                //当前的时间
rtc_date	: 2012-01-12                              //当前的日期
alrm_time	: 05:04:36                                //alarm的时间
alrm_date	: 2033-04-23                              //alarm的日期
alarm_IRQ	: no                                      //alarm IRQ是否使能
alrm_pending	: no                                      //alarm pending是否使能
update IRQ enabled	: no                              //update IRQ是否使能
periodic IRQ enabled	: no                              //periodic(周期性)IRQ是否使能
periodic IRQ frequency	: 1                               //periodic IRQ频率
max user IRQ frequency	: 64                              //periodic IRQ最大的频率
24hr		: yes                                     //24小时制

在认识了rtc节点之后,接下来分析代码。

rtc-proc.c

代码语言:javascript
复制
void rtc_proc_add_device(struct rtc_device *rtc)
{
	if (is_rtc_hctosys(rtc))                                                  //判断是否用rtc设备设置系统时间
		proc_create_data("driver/rtc", 0, NULL, &rtc_proc_fops, rtc);     //如果是,则创建proc节点
}

如下是is_rtc_hctosys的实现:

代码语言:javascript
复制
#if defined(CONFIG_RTC_HCTOSYS_DEVICE)
static bool is_rtc_hctosys(struct rtc_device *rtc)
{
	int size;
	char name[NAME_SIZE];

	size = scnprintf(name, NAME_SIZE, "rtc%d", rtc->id);
	if (size > NAME_SIZE)
		return false;

	return !strncmp(name, CONFIG_RTC_HCTOSYS_DEVICE, NAME_SIZE);            //也就是比较rtc设备与内核config中配置的是否一样。
}
#else
static bool is_rtc_hctosys(struct rtc_device *rtc)
{
	return (rtc->id == 0);
}
#endif

而此函数是通过内核配置: CONFIG_RTC_HCTOSYS_DEVICE决定的。 可以通过kmconfig查看该配置的详细信息,如下:

代码语言:javascript
复制
CONFIG_RTC_HCTOSYS_DEVICE:                                                                                                                            
  │ The RTC device that will be used to (re)initialize the system                                                                                         
  │ clock, usually rtc0. Initialization is done when the system                                                                                           
  │ starts up, and when it resumes from a low power state. This                                                                                           
  │ device should record time in UTC, since the kernel won't do                                                                                           
  │ timezone correction.                                                                                                                                 
  │                                                                                                                                                       
  │ The driver for this RTC device must be loaded before late_initcall                                                                                    
  │ functions run, so it must usually be statically linked.                                                                                               
  │                                                                                                                                                       
  │ This clock should be battery-backed, so that it reads the correct                                                                                     
  │ time when the system boots from a power-off state. Otherwise, your                                                                                    
  │ system will need an external clock source (like an NTP server).                                                                                       
  │                                                                                                                                                       
  │ If the clock you specify here is not battery backed, it may still                                                                                     
  │ be useful to reinitialize system time when resuming from system                                                                                      
  │ sleep states. Do not specify an RTC here unless it stays powered                                                                                      
  │ during all this system's supported sleep states.            

大概意识是当系统启动时,rtc设备通常被用来设置系统时间。

如下是rtc_proc_fops的结构:

代码语言:javascript
复制
static const struct file_operations rtc_proc_fops = {
	.open		= rtc_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= rtc_proc_release,
};

当系统打开这个文件的时候就调用到open函数,这都是proc文件系统的执行过程,不是这部分的重点,跳过。

代码语言:javascript
复制
static int rtc_proc_open(struct inode *inode, struct file *file)
{
	int ret;
	struct rtc_device *rtc = PDE_DATA(inode);                  //获得rtc设备结构

	if (!try_module_get(THIS_MODULE))
		return -ENODEV;

	ret = single_open(file, rtc_proc_show, rtc);              //调用顺序文件结构初始化
	if (ret)
		module_put(THIS_MODULE);
	return ret;
}

当cat /proc/driver/rtc的时候就调用到read函数,然后最终调用到rtc_proc_show函数。

代码语言:javascript
复制
static int rtc_proc_show(struct seq_file *seq, void *offset)
{
	int err;
	struct rtc_device *rtc = seq->private;
	const struct rtc_class_ops *ops = rtc->ops;
	struct rtc_wkalrm alrm;
	struct rtc_time tm;

	err = rtc_read_time(rtc, &tm);                                    //读取当前的时间
	if (err == 0) {
		seq_printf(seq,
			"rtc_time\t: %02d:%02d:%02d\n"
			"rtc_date\t: %04d-%02d-%02d\n",
			tm.tm_hour, tm.tm_min, tm.tm_sec,
			tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
	}

	err = rtc_read_alarm(rtc, &alrm);                                 //读取alarm时间
	if (err == 0) {
		seq_printf(seq, "alrm_time\t: ");                         //alarm时间
		if ((unsigned int)alrm.time.tm_hour <= 24)
			seq_printf(seq, "%02d:", alrm.time.tm_hour);
		else
			seq_printf(seq, "**:");
		if ((unsigned int)alrm.time.tm_min <= 59)
			seq_printf(seq, "%02d:", alrm.time.tm_min);
		else
			seq_printf(seq, "**:");
		if ((unsigned int)alrm.time.tm_sec <= 59)
			seq_printf(seq, "%02d\n", alrm.time.tm_sec);
		else
			seq_printf(seq, "**\n");

		seq_printf(seq, "alrm_date\t: ");                          //alarm日期
		if ((unsigned int)alrm.time.tm_year <= 200)
			seq_printf(seq, "%04d-", alrm.time.tm_year + 1900);
		else
			seq_printf(seq, "****-");
		if ((unsigned int)alrm.time.tm_mon <= 11)
			seq_printf(seq, "%02d-", alrm.time.tm_mon + 1);
		else
			seq_printf(seq, "**-");
		if (alrm.time.tm_mday && (unsigned int)alrm.time.tm_mday <= 31)
			seq_printf(seq, "%02d\n", alrm.time.tm_mday);
		else
			seq_printf(seq, "**\n");
		seq_printf(seq, "alarm_IRQ\t: %s\n",
				alrm.enabled ? "yes" : "no");                 //alarm IRQ是否使能
		seq_printf(seq, "alrm_pending\t: %s\n",      
				alrm.pending ? "yes" : "no");                 //alarm pending是否使能
		seq_printf(seq, "update IRQ enabled\t: %s\n",
			(rtc->uie_rtctimer.enabled) ? "yes" : "no");          //update IRQ是否使能
		seq_printf(seq, "periodic IRQ enabled\t: %s\n",
			(rtc->pie_enabled) ? "yes" : "no");                   //periodic IRQ是否使能
		seq_printf(seq, "periodic IRQ frequency\t: %d\n",
			rtc->irq_freq);                                       //periodic IRQ频率
		seq_printf(seq, "max user IRQ frequency\t: %d\n",             //最大的频率
			rtc->max_user_freq);
	}

	seq_printf(seq, "24hr\t\t: yes\n");

	if (ops->proc)
		ops->proc(rtc->dev.parent, seq);                               //驱动是否支持proc,一般驱动没有实现。

	return 0;
}

可以使用如下的方法卸载/proc/driver/rtc

代码语言:javascript
复制
void rtc_proc_del_device(struct rtc_device *rtc)
{
	if (is_rtc_hctosys(rtc))
		remove_proc_entry("driver/rtc", NULL);
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 认识rtc节点
  • rtc-proc.c
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档