詳解linux 看門狗驅(qū)動編寫
看門狗是linux驅(qū)動的一個重要環(huán)節(jié)。某些特殊的設(shè)備,有時候需要放在一些環(huán)境惡劣的地方,比如電信設(shè)備。但是,任何軟件都不可能100%沒有bug。如何保證軟件在遇到嚴(yán)重bug、死機的時候也能正常運行呢,那么看門狗就是有效的一種方法??撮T狗一般要求用戶定時喂狗,如果一段時間沒有喂狗的話,那么系統(tǒng)就會自動重啟。今天,我們就來看看這個看門狗驅(qū)動怎么編寫?
1、代碼目錄
drivers/watchdog
2、閱讀目錄下的Kconfig,可以找一個s3c模塊macro
config HAVE_S3C2410_WATCHDOG bool help This will include watchdog timer support for Samsung SoCs. If you want to include watchdog support for any machine, kindly select this in the respective mach-XXXX/Kconfig file. config S3C2410_WATCHDOG tristate "S3C2410 Watchdog" depends on HAVE_S3C2410_WATCHDOG || COMPILE_TEST select WATCHDOG_CORE select MFD_SYSCON if ARCH_EXYNOS help Watchdog timer block in the Samsung SoCs. This will reboot the system when the timer expires with the watchdog enabled. The driver is limited by the speed of the system's PCLK signal, so with reasonably fast systems (PCLK around 50-66MHz) then watchdog intervals of over approximately 20seconds are unavailable. The driver can be built as a module by choosing M, and will be called s3c2410_wdt
3、S3C2410_WATCHDOG主要依賴WATCHDOG_CORE,可以繼續(xù)跟蹤Makefile
obj-$(CONFIG_S3C2410_WATCHDOG) += s3c2410_wdt.o
4、macro只依賴一個s3c2410_wdt.c文件,繼續(xù)查看
static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
s3c2410wdt_resume);
static struct platform_driver s3c2410wdt_driver = {
.probe = s3c2410wdt_probe,
.remove = s3c2410wdt_remove,
.shutdown = s3c2410wdt_shutdown,
.id_table = s3c2410_wdt_ids,
.driver = {
.name = "s3c2410-wdt",
.pm = &s3c2410wdt_pm_ops,
.of_match_table = of_match_ptr(s3c2410_wdt_match),
},
};
module_platform_driver(s3c2410wdt_driver);
5、確認(rèn)driver為platform類型,繼續(xù)在probe函數(shù)中查找有用的code
ret = watchdog_register_device(&wdt->wdt_device);
if (ret) {
dev_err(dev, "cannot register watchdog (%d)\n", ret);
goto err_cpufreq;
}
6、網(wǎng)上繼續(xù)查找,尋找到和watchdog有關(guān)的數(shù)據(jù)結(jié)構(gòu)
static const struct watchdog_info s3c2410_wdt_ident = {
.options = OPTIONS,
.firmware_version = 0,
.identity = "S3C2410 Watchdog",
};
static const struct watchdog_ops s3c2410wdt_ops = {
.owner = THIS_MODULE,
.start = s3c2410wdt_start,
.stop = s3c2410wdt_stop,
.ping = s3c2410wdt_keepalive,
.set_timeout = s3c2410wdt_set_heartbeat,
.restart = s3c2410wdt_restart,
};
static const struct watchdog_device s3c2410_wdd = {
.info = &s3c2410_wdt_ident,
.ops = &s3c2410wdt_ops,
.timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
};
7、找到設(shè)備注冊函數(shù)、函數(shù)結(jié)構(gòu)基本就算結(jié)束了,當(dāng)然有中斷的話,也可以確認(rèn)一下
ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
pdev->name, pdev);
if (ret != 0) {
dev_err(dev, "failed to install irq (%d)\n", ret);
goto err_cpufreq;
}
8、有興趣的話,可以找一個函數(shù)閱讀一下。比如下面這個重啟函數(shù),可以和spec對比者來看
static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
void *data)
{
struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
void __iomem *wdt_base = wdt->reg_base;
/* disable watchdog, to be safe */
writel(0, wdt_base + S3C2410_WTCON);
/* put initial values into count and data */
writel(0x80, wdt_base + S3C2410_WTCNT);
writel(0x80, wdt_base + S3C2410_WTDAT);
/* set the watchdog to go and reset... */
writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
wdt_base + S3C2410_WTCON);
/* wait for reset to assert... */
mdelay(500);
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Apache配置參數(shù)deny和allow的使用實例
這篇文章主要介紹了Apache配置參數(shù)deny和allow的使用實例,需要的朋友可以參考下2015-06-06
linux 配置本地yum源,配置國內(nèi)yum源,配置epel源的步驟
這篇文章主要介紹了linux 配置本地yum源,配置國內(nèi)yum源,配置epel源的步驟,幫助大家更好的配置服務(wù)器,感興趣的朋友可以了解下2020-12-12
紅帽RHEL8和7的區(qū)別對比分享(Centos8與7參照redhat)
這篇文章主要介紹了紅帽RHEL8和7有什么區(qū)別(Centos8與7參照redhat),包括紅帽RHEL8和RHEL7功能區(qū)別對比和RHEL8額外新功能新特性,對紅帽RHEL8和7相關(guān)知識感興趣的朋友跟隨小編一起看看吧2023-01-01
CentOS7.4開機出現(xiàn)welcome to emergency mode的解決方法
CentOS7.4開機出現(xiàn)welcome to emergency mode,報這個錯誤多數(shù)情況下是因為/etc/fstab文件的錯誤。注意一下是不是加載了外部硬盤、存儲器或者是網(wǎng)絡(luò)共享空間,在重啟時沒有加載上導(dǎo)致的2018-09-09
ubuntu中swap(虛擬內(nèi)存)設(shè)置方法
這篇文章主要介紹了ubuntu中swap(虛擬內(nèi)存)設(shè)置方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
詳解CentOS7 安裝 MariaDB 10.2.4的方法
這篇文章主要介紹了CentOS7 安裝 MariaDB 10.2.4的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Linux下的mongodb服務(wù)監(jiān)視腳本(啟動服務(wù))
這篇文章主要介紹了Linux下的mongodb服務(wù)監(jiān)視腳本(啟動服務(wù)),需要的朋友可以參考下2015-10-10

