本文假设各位看官已经了解看门狗的工作原理,而且手上有2440的datasheet,可以随时查看看门狗相关的寄存器。我在这里仅仅专注于对驱动程序的分析,望见谅~
借用网上的朋友☆&寒 烟☆的一幅描述看门狗驱动程序的结构框图,总结得还是相当到位的,这里妄自copy如下(如有冒犯原作者请立刻通知,立删并道歉),各位可以按照结构图来按图索骥,更好地掌握驱动程序里面稍稍复杂的关系。
看过结构框图,想必对程序的构成有一个大致的认识了。下面开始贴上代码并分析之
watchdog_init & watchdog_exit
- static int __init watchdog_init(void)
- {
- printk(banner);
- return platform_driver_register(&s3c2410wdt_driver);
- }
- static void __exit watchdog_exit(void)
- {
- platform_driver_unregister(&s3c2410wdt_driver);
- }
- module_init(watchdog_init);
- module_exit(watchdog_exit);
上面两个函数分别是整个驱动的入口和出口,分别完成注册platform设备和注销platform设备的动作,其操作的platform设备由结构体s3c2410wdt_driver描述,代码如下
结构体s3c2410wdt_driver
- static struct platform_driver s3c2410wdt_driver = {
- .probe = s3c2410wdt_probe,
- .remove = __devexit_p(s3c2410wdt_remove),
- .shutdown = s3c2410wdt_shutdown,
- .suspend = s3c2410wdt_suspend,
- .resume = s3c2410wdt_resume,
- .driver = {
- .owner = THIS_MODULE,
- .name = "s3c2410-wdt",
- },
- };
对应文章一开始的框图,上述结构体绑定了5个函数和一些自身的信息。5个函数分别实现的功能如下。
s3c2410wdt_remove
- static int __devexit s3c2410wdt_remove(struct platform_device *dev)
- {
- release_resource(wdt_mem); //释放获取的watchdog平台设备的IO资源
- kfree(wdt_mem);
- wdt_mem = NULL;
- free_irq(wdt_irq->start, dev); //释放中断(重要)
- wdt_irq = NULL;
- clk_disable(wdt_clock); //释放看门狗时钟
- clk_put(wdt_clock);
- wdt_clock = NULL;
- iounmap(wdt_base); //释放内存映射地址
- misc_deregister(&s3c2410wdt_miscdev); //注销混杂设备
- return 0;
- }
s3c2410wdt_shutdown
- static void s3c2410wdt_shutdown(struct platform_device *dev)
- {
- s3c2410wdt_stop();
- }
- static void s3c2410wdt_stop(void)
- {
- spin_lock(&wdt_lock); //自旋锁
- __s3c2410wdt_stop();
- spin_unlock(&wdt_lock);
- }
- static void __s3c2410wdt_stop(void)
- {
- unsigned long wtcon;
- //这里就是直接对看门狗寄存器进行操作,至于如何赋值、赋什么值
- //大家自己查阅一下数据手册收获更多!
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
- writel(wtcon, wdt_base + S3C2410_WTCON);
- }
s3c2410wdt_suspend
- static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
- {
- /* 保存看门狗状态,之后关闭,即暂停功能 */
- wtcon_save = readl(wdt_base + S3C2410_WTCON);
- wtdat_save = readl(wdt_base + S3C2410_WTDAT);
- /* Note that WTCNT doesn't need to be saved. */
- s3c2410wdt_stop();
- return 0;
- }
s3c2410wdt_resume
- static int s3c2410wdt_resume(struct platform_device *dev)
- {
- /* 重装看门狗状态 */
- writel(wtdat_save, wdt_base + S3C2410_WTDAT);
- writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
- writel(wtcon_save, wdt_base + S3C2410_WTCON);
- printk(KERN_INFO PFX "watchdog %sabled/n",
- (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
- return 0;
- }
s3c2410wdt_probe
- static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
- {
- struct resource *res; //定义一个资源,用来保存获取的watchdog的IO资源
- struct device *dev;
- unsigned int wtcon;
- int started = 0;
- int ret;
- int size;
- DBG("%s: probe=%p/n", __func__, pdev);
- dev = &pdev->dev;
- wdt_dev = &pdev->dev;
- /* get the memory region for the watchdog timer */
- //获取watchdog平台设备所使用的IO端口资源,注意这个IORESOURCE_MEM标志
- //和watchdog平台设备定义中的一致
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (res == NULL) {
- dev_err(dev, "no memory resource specified/n");
- return -ENOENT;
- }
- size = (res->end - res->start) + 1;
- //request_mem_region标记这段物理地址自己使用了
- wdt_mem = request_mem_region(res->start, size, pdev->name);
- if (wdt_mem == NULL) {
- dev_err(dev, "failed to get memory region/n");
- ret = -ENOENT;
- goto err_req;
- }
- //把watchdog的io端口映射到内存虚拟地址,size为映射地址的长度
- wdt_base = ioremap(res->start, size);
- if (wdt_base == NULL) {
- dev_err(dev, "failed to ioremap() region/n");
- ret = -EINVAL;
- goto err_req;
- }
- DBG("probe: mapped wdt_base=%p/n", wdt_base);
- //获取中断号
- wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- if (wdt_irq == NULL) {
- dev_err(dev, "no irq resource specified/n");
- ret = -ENOENT;
- goto err_map;
- }
- //申请中断
- ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
- if (ret != 0) {
- dev_err(dev, "failed to install irq (%d)/n", ret);
- goto err_map;
- }
- //申请时钟
- wdt_clock = clk_get(&pdev->dev, "watchdog");
- if (IS_ERR(wdt_clock)) {
- dev_err(dev, "failed to find watchdog clock source/n");
- ret = PTR_ERR(wdt_clock);
- goto err_irq;
- }
- //时钟获取后要使能后才可以使用,clk_enable定义在arch/arm/plat-s3c/clock.c中
- clk_enable(wdt_clock);
- /* see if we can actually set the requested timer margin, and if
- * not, try the default value */
- if (s3c2410wdt_set_heartbeat(tmr_margin)) {
- started = s3c2410wdt_set_heartbeat(
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- if (started == 0)
- dev_info(dev,
- "tmr_margin value out of range, default %d used/n",
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- else
- dev_info(dev, "default timer value is out of range, "
- "cannot start/n");
- }
- //把wdt又注册为一个杂项设备,注意结构体s3c2410wdt_miscdev
- ret = misc_register(&s3c2410wdt_miscdev);
- if (ret) {
- dev_err(dev, "cannot register miscdev on minor=%d (%d)/n",
- WATCHDOG_MINOR, ret);
- goto err_clk;
- }
- if (tmr_atboot && started == 0) {
- dev_info(dev, "starting watchdog timer/n");
- s3c2410wdt_start();
- } else if (!tmr_atboot) {
- /* if we're not enabling the watchdog, then ensure it is
- * disabled if it has been left running from the bootloader
- * or other source */
- s3c2410wdt_stop();
- }
- /* print out a statement of readiness */
- wtcon = readl(wdt_base + S3C2410_WTCON);
- dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled/n",
- (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
- (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
- (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
- return 0;
- err_clk:
- clk_disable(wdt_clock);
- clk_put(wdt_clock);
- err_irq:
- free_irq(wdt_irq->start, pdev);
- err_map:
- iounmap(wdt_base);
- err_req:
- release_resource(wdt_mem);
- kfree(wdt_mem);
- return ret;
- }
留意到,当注册wdt为杂项设备时,参数里的结构体s3c2410wdt_miscdev,代码如
- static struct miscdevice s3c2410wdt_miscdev = {
- .minor = WATCHDOG_MINOR,
- .name = "watchdog",
- .fops = &s3c2410wdt_fops,
- };
哈哈,看到这个东东,应该很熟悉了吧,在之前的一些列驱动里都有接触过的结构体。其中我们关注.fops,它为我们提供了若干工具函数。
- static const struct file_operations s3c2410wdt_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .write = s3c2410wdt_write,
- .unlocked_ioctl = s3c2410wdt_ioctl,
- .open = s3c2410wdt_open,
- .release = s3c2410wdt_release,
- };
s3c2410wdt_open
- static int s3c2410wdt_open(struct inode *inode, struct file *file)
- {
- //试着获取信号量(即:加锁),如果获取不成功,说明其他进程此时占用了,就返回忙
- if (test_and_set_bit(0, &open_lock))
- return -EBUSY;
- //如果内核配置了CONFIG_WATCHDOG_NOWAYOUT项,则使模块使用计数加1
- if (nowayout)
- __module_get(THIS_MODULE);
- expect_close = 0;
- /* start the timer */
- s3c2410wdt_start();
- //表示返回的这个设备文件是不可以被seek操作的,nonseekable_open定义在fs.h中
- return nonseekable_open(inode, file);
- }
- static void s3c2410wdt_start(void)
- {
- //整个函数直接操作寄存器,嘿嘿,还是不解释
- unsigned long wtcon;
- spin_lock(&wdt_lock); //尝试取得自旋锁
- __s3c2410wdt_stop();
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
- if (soft_noboot) {
- wtcon |= S3C2410_WTCON_INTEN;
- wtcon &= ~S3C2410_WTCON_RSTEN;
- } else {
- wtcon &= ~S3C2410_WTCON_INTEN;
- wtcon |= S3C2410_WTCON_RSTEN;
- }
- DBG("%s: wdt_count=0x%08x, wtcon=%08lx/n",
- __func__, wdt_count, wtcon);
- writel(wdt_count, wdt_base + S3C2410_WTDAT);
- writel(wdt_count, wdt_base + S3C2410_WTCNT);
- writel(wtcon, wdt_base + S3C2410_WTCON);
- spin_unlock(&wdt_lock);
- }
s3c2410wdt_release
- static int s3c2410wdt_release(struct inode *inode, struct file *file)
- {
- /*
- * Shut off the timer.
- * Lock it in if it's a module and we set nowayout
- */
- // 如果判断到当前操作状态是可以关闭看门狗定时器时就关闭,否则就是“喂狗”状态
- if (expect_close == 42)
- s3c2410wdt_stop();
- else {
- dev_err(wdt_dev, "Unexpected close, not stopping watchdog/n");
- s3c2410wdt_keepalive(); //喂狗
- }
- expect_close = 0;
- clear_bit(0, &open_lock);
- return 0;
- }
s3c2410wdt_write
- static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
- size_t len, loff_t *ppos)
- {
- /*
- * Refresh the timer.
- */
- if (len) {
- if (!nowayout) {
- size_t i;
- /* In case it was set long ago */
- expect_close = 0;
- for (i = 0; i != len; i++) {
- char c;
- if (get_user(c, data + i))
- return -EFAULT;
- if (c == 'V') //写入字符 V容许关闭看门狗,但前提还是
- //不配置CONFIG_WATCHDOG_NOWAYOUT
- expect_close = 42;
- }
- }
- s3c2410wdt_keepalive(); //喂狗
- }
- return len;
- }
s3c2410wdt_ioctl
- static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- void __user *argp = (void __user *)arg;
- int __user *p = argp;
- int new_margin;
- switch (cmd) {
- case WDIOC_GETSUPPORT: //获取看门狗的支持信息,wdt_ident定义在上面
- return copy_to_user(argp, &s3c2410_wdt_ident,
- sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
- case WDIOC_GETSTATUS: //获取看门狗状态
- case WDIOC_GETBOOTSTATUS:
- return put_user(0, p);
- case WDIOC_KEEPALIVE: //喂狗命令
- s3c2410wdt_keepalive();
- return 0;
- case WDIOC_SETTIMEOUT: //设置定时器溢出时间值命令(以秒为单位)
- if (get_user(new_margin, p))
- return -EFAULT;
- if (s3c2410wdt_set_heartbeat(new_margin))
- return -EINVAL;
- s3c2410wdt_keepalive();
- return put_user(tmr_margin, p);
- case WDIOC_GETTIMEOUT: //读取定时器默认溢出时间值命令
- return put_user(tmr_margin, p);
- default:
- return -ENOTTY;
- }
- }
到这里为止,看门狗驱动函数已经全部介绍完毕,可能有朋友跟我一样,一开始会迷惑,到底看门狗算是什么设备吖。它究竟是平台设备(platform)、混杂设备(misc)、还是字符设备(char)呢。这里我摘录一段网友的解释,视点比较独特,深入浅出,摘录如下:
曾看到这样一段话,说一粒花生,如果我们把它看作字符设备,当我们煮着炖着吃了,它就成了菜中的一个混杂设备,但是了它总是从挂在花生秧的根部上的一员长来了,因此它是可以作为一个花生秧苗中一个独立的设备而存在,所以就也可以定义为一个平台设备。这说明什么呢?字符设备是对其本质的描述,说明是串行,顺序访问的(而不是缓冲随机的),混杂设备是存放这个字符设备的容器,即这个设备是被丢在使用了同一设备号的混杂设备里面,平台设备则是描述了设备的一种特性或者说属性。换句话说就是这个设备属于平台的独立模块,完全是一种附加的属性。