Linux驱动分析之RTC框架

简介: 当Linux内核启动时,它会从RTC中读取时间与日期,作为基准值。然后通过软件来维护系统时间和日期。Linux系统中提供了RTC核心层,对于驱动开发者而言,操作起来就变得很简单了。我们来看看整体框架。

前言

当Linux内核启动时,它会从RTC中读取时间与日期,作为基准值。然后通过软件来维护系统时间和日期。Linux系统中提供了RTC核心层,对于驱动开发者而言,操作起来就变得很简单了。我们来看看整体框架。


驱动框架

下面是整体框架图

640.png

与RTC核心有关的文件有:

文件

描述

/drivers/rtc/class.c

这个文件向linux设备模型核心注册了一个类RTC,然后向驱动程序提供了注册/注销接口

/drivers/rtc/rtc-dev.c

这个文件定义了基本的设备文件操作函数,如:open,read等

/drivers/rtc/interface.c

这个文件主要提供了用户程序与RTC驱动的接口函数,用户程序一般通过ioctl与RTC驱动交互,这里定义了每个ioctl命令需要调用的函数

/drivers/rtc/rtc-sysfs.c

与sysfs有关

/drivers/rtc/rtc-proc.c

与proc文件系统有关

/include/linux/rtc.h

定义了与RTC有关的数据结构



重要结构体

  • rtc_device
//RTC设备structrtc_device {
structdevicedev;
structmodule*owner;
intid;
conststructrtc_class_ops*ops; //rtc操作函数structmutexops_lock;
structcdevchar_dev;
unsignedlongflags;
unsignedlongirq_data;
spinlock_tirq_lock;
wait_queue_head_tirq_queue;
structfasync_struct*async_queue;
intirq_freq;
intmax_user_freq;
structtimerqueue_headtimerqueue;
structrtc_timeraie_timer; 
structrtc_timeruie_rtctimer;
structhrtimerpie_timer; /* sub second exp, so needs hrtimer */intpie_enabled;
structwork_structirqwork;
/* Some hardware can't support UIE mode */intuie_unsupported;
longset_offset_nsec;
boolregistered;
structnvmem_device*nvmem;
/* Old ABI support */boolnvram_old_abi;
structbin_attribute*nvram;
time64_trange_min;
timeu64_trange_max;
time64_tstart_secs;
time64_toffset_secs;
boolset_start_time;
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMULstructwork_structuie_task;
structtimer_listuie_timer;
/* Those fields are protected by rtc->irq_lock */unsignedintoldsecs;
unsignedintuie_irq_active:1;
unsignedintstop_uie_polling:1;
unsignedintuie_task_active:1;
unsignedintuie_timer_active:1;
#endif};

上面的结构体表示一个RTC设备,比较简单,主要就是中断信息,字符设备对象,操作函数等。


  • rtc_class_ops
//RTC操作函数structrtc_class_ops {
int (*ioctl)(structdevice*, unsignedint, unsignedlong);
int (*read_time)(structdevice*, structrtc_time*);
int (*set_time)(structdevice*, structrtc_time*);
int (*read_alarm)(structdevice*, structrtc_wkalrm*);
int (*set_alarm)(structdevice*, structrtc_wkalrm*);
int (*proc)(structdevice*, structseq_file*);
int (*set_mmss64)(structdevice*, time64_tsecs);
int (*set_mmss)(structdevice*, unsignedlongsecs);
int (*read_callback)(structdevice*, intdata);
int (*alarm_irq_enable)(structdevice*, unsignedintenabled);
int (*read_offset)(structdevice*, long*offset);
int (*set_offset)(structdevice*, longoffset);
};

就是一些设置时间和读取时间,以及闹钟等接口函数。


  • rtc_time
//时间结构体structrtc_time {
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
};


API函数

// 注册RTC classstaticstructrtc_device*rtc_device_register(constchar*name,
structdevice*dev,
conststructrtc_class_ops*ops,
structmodule*owner)
structrtc_device*devm_rtc_device_register(structdevice*dev,
constchar*name,
conststructrtc_class_ops*ops,
structmodule*owner)
//注销RTC      staticvoidrtc_device_unregister(structrtc_device*rtc)     
voiddevm_rtc_device_unregister(structdevice*dev, structrtc_device*rtc)


总结

RTC也是字符设备驱动,只是进行了封装,封装完之后我们调用起来其实就很简单了。只要实现好接口函数,填充好结构体,然后进行注册即可。


相关文章
|
2月前
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
388 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
2月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
174 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
2月前
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
245 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
2月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
221 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
324 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
3月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
424 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
586 0
|
3月前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
148 0
|
3月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
196 0
|
4月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-2025073001 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025073001 (Linux, Windows) - 专业渗透测试框架
182 0