kernel 于ioctl申请书

ioctl经营无纸装置频繁使用的类型。同时这是一个非常实用的方法进程调试。

这里正在进行wdt该文章继续

static long at91_wdt_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
void __user *argp = (void __user *)arg;
int __user *p = argp;
int new_value;


switch (cmd) {
case WDIOC_GETSUPPORT:
return copy_to_user(argp, &at91_wdt_info,
sizeof(at91_wdt_info)) ? -EFAULT : 0;
case WDIOC_GETSTATUS:
case WDIOC_GETBOOTSTATUS:
return put_user(0, p);
case WDIOC_SETOPTIONS:
if (get_user(new_value, p))
return -EFAULT;
if (new_value & WDIOS_DISABLECARD)
at91_wdt_stop();
if (new_value & WDIOS_ENABLECARD)
at91_wdt_start();
return 0;
case WDIOC_KEEPALIVE:
at91_wdt_reload(); /* pat the watchdog */
return 0;
case WDIOC_SETTIMEOUT:
if (get_user(new_value, p))
return -EFAULT;
if (at91_wdt_settimeout(new_value))
return -EINVAL;
/* Enable new time value */
at91_wdt_start();
/* Return current value */
return put_user(wdt_time, p);
case WDIOC_GETTIMEOUT:
return put_user(wdt_time, p);
default:
return -ENOTTY;
}
}

这里先定义两个一个最底层的被调用接口。在这里面初始化了几个命令,这些命令就能够直接调用你自己的底层函数了

之后面临的就是怎么让这个ioctl被上层可一调用  

这里有一个系统设置的结构体

static const struct file_operations at91wdt_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.unlocked_ioctl = at91_wdt_ioctl,
.open = at91_wdt_open,
.release = at91_wdt_close,
.write = at91_wdt_write,
};

把ioctl赋给unlocked  

再把这个结构体赋给miscdevice结构体   这里就能够看出来他是一个杂项设备  不是block  或者字符设备了

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

这里再把它注冊给系统就好了

static int __devinit at91wdt_probe(struct platform_device *pdev)
{
int res;


if (at91wdt_miscdev.parent)
return -EBUSY;
at91wdt_miscdev.parent = &pdev->dev;


res = misc_register(&at91wdt_miscdev);
if (res)
return res;


printk(KERN_INFO "AT91 Watchdog Timer enabled (%d seconds%s) ",
wdt_time, nowayout ?

", nowayout" : "");
return 0;
}

上面就是这个设备备注測时的调用   能够看到開始就把这个ioctl的父结构体赋给系统了 

后面系上层系统将能够看到的功能    它可以通过驱动前来层

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/lcchuguo/p/4904632.html