【学习笔记】看门狗子系统

子系统提供了一系列操作,而不需要用户再次编写,只需要向相应的结构体填写设备信息。

 struct watchdog_device {
         int id;
         struct cdev cdev;
         struct device *dev;
         struct device *parent;
         const struct watchdog_info *info;
         const struct watchdog_ops *ops;
         unsigned int bootstatus;
         unsigned int timeout;
         unsigned int min_timeout;
         unsigned int max_timeout;
         void *driver_data;
         struct mutex lock;
         unsigned long status;
 /* Bit numbers for status flags */
 #define WDOG_ACTIVE             0       /* Is the watchdog running/active */
 #define WDOG_DEV_OPEN           1       /* Opened via /dev/watchdog ? */
 #define WDOG_ALLOW_RELEASE      2       /* Did we receive the magic char ? */
 #define WDOG_NO_WAY_OUT         3       /* Is 'nowayout' feature set ? */
 #define WDOG_UNREGISTERED       4       /* Has the device been unregistered */ };

主要函数:注册,注销。

extern int watchdog_register_device(struct watchdog_device *);
extern void watchdog_unregister_device(struct watchdog_device *);

自定义结构体的初始化:

#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)


    static const struct watchdog_info s3c2410_wdt_ident = {
                    .options          =     OPTIONS,
                    .firmware_version = 0,
                    .identity         = "S3C2410 Watchdog",
            };


    static struct watchdog_ops s3c2410wdt_ops = {
                    .owner = THIS_MODULE,
                    .start = s3c2410wdt_start,                  <-- 开启wdt
                    .stop = s3c2410wdt_stop,                    <-- 停止wdt
                    .ping = s3c2410wdt_keepalive,               <-- 喂狗
                    .set_timeout = s3c2410wdt_set_heartbeat,    <-- 设置超时;
            };

    static struct watchdog_device s3c2410_wdd = {
                    .info = &s3c2410_wdt_ident,
                    .ops = &s3c2410wdt_ops,
            };
//watchdog/watchdog_core.c:
    int watchdog_register_device(struct watchdog_device *wdd)
        ret = watchdog_dev_register(wdd);
        devno = wdd->cdev.dev;
        wdd->dev = device_create(watchdog_class, wdd->parent, devno, NULL, "watchdog%d", wdd->id);

        watchdog/watchdog_dev.c
                static const struct file_operations watchdog_fops = {
                        .owner      = THIS_MODULE,
                        .write      = watchdog_write,
                        .unlocked_ioctl = watchdog_ioctl,
                        .open       = watchdog_open,
                        .release    = watchdog_release,
                };

        static struct miscdevice watchdog_miscdev = {
                        .minor      = WATCHDOG_MINOR,
                        .name       = "watchdog",
                        .fops       = &watchdog_fops,
                };

                int watchdog_dev_register(struct watchdog_device *watchdog) {
                    if (watchdog->id == 0) {
                        watchdog_miscdev.parent = watchdog->parent;
                        err = misc_register(&watchdog_miscdev);
                        ...
                        old_wdd = watchdog;
                    }

                    devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
                    cdev_init(&watchdog->cdev, &watchdog_fops);
                    watchdog->cdev.owner = watchdog->ops->owner;

                    /* Add the device */
                    err  = cdev_add(&watchdog->cdev, devno, 1);

                }

        static long watchdog_ioctl(struct file *file, unsigned int cmd,
                                    unsigned long arg)
        {
                WDIOC_GETSUPPORT:
                    return copy_to_user(argp, wdd->info,
                            sizeof(struct watchdog_info)) ? -EFAULT : 0;
                WDIOC_GETSTATUS:

                WDIOC_GETBOOTSTATUS:

                WDIOC_SETOPTIONS:
                    if (val & WDIOS_DISABLECARD) {
                            err = watchdog_stop(wdd);
                            if (err < 0)
                                    return err;
                    }
                    if (val & WDIOS_ENABLECARD) {
                            err = watchdog_start(wdd);
                            if (err < 0)
                                    return err;
                    }

                WDIOC_KEEPALIVE:
                    if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
                        return -EOPNOTSUPP;
                    watchdog_ping(wdd);

                WDIOC_SETTIMEOUT:
                    err = watchdog_set_timeout(wdd, val);
                    watchdog_ping(wdd);

                WDIOC_GETTIMEOUT:

                WDIOC_GETTIMELEFT:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值