Linux异步通知机制-系统掉电保存文件
立即下载
资源介绍:
掉电保存驱动与应用
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "poweroffSave"
static struct fasync_struct *async;
/*poweroffSave struct save dts device node and irq num/line*/
struct poweroffSave
{
const struct device_node *devNode;
int irq;
};
struct poweroffSave poweroff;
/*irq fun:
* irq run , driver send async siganl to app
* */
static irqreturn_t powerOff_irq(int irq, void *devid)
{
/*SIGIO:signal POLL_IN:only read*/
kill_fasync(&async,SIGIO,POLL_IN);
return (IRQ_HANDLED);
}
static int poweroff_fasync (int fd, struct file *filp,int mode)
{
/*init fasync_struct*/
return fasync_helper(fd,filp,mode,&async);
}
/*sys call function:fasync*/
static struct file_operations poweroff_ops = {
.owner = THIS_MODULE,
.fasync = poweroff_fasync,
};
static struct miscdevice poweroff_ops_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &poweroff_ops,
};
static int poweroffSave_probe(struct platform_device *device)
{
int ret;
poweroff.devNode = of_find_node_by_path("/poweroffSave");
if(poweroff.devNode == NULL)
{
printk("poweroffSave node find fail\r\n");
return -1;
}
of_property_read_u32_array(poweroff.devNode,"irq",&poweroff.irq,1);
/*request irq:ensure dts to irq line、irq mode*/
ret = request_irq(poweroff.irq,powerOff_irq,IRQ_TYPE_EDGE_FALLING,"poweroffIRQ",&poweroff);
if(ret < 0)
{
printk("Failed to request irq %d\r\n",poweroff.irq);
return ret;
}
/*register misc devcie*/
ret = misc_register(&poweroff_ops_dev);
if(ret < 0)
{
printk("regitser misc_device error\r\n");
return -1;
}
return 0;
}
static int poweroffSave_remove(struct platform_device *device)
{
misc_deregister(&poweroff_ops_dev);
free_irq(poweroff.irq,&poweroff);
return 0;
}
static struct of_device_id poweroffSave_id[] = {
{.compatible = "poweroffSave"},
{}
};
static struct platform_driver powerOffSave_driver =
{
.probe = poweroffSave_probe,
.remove = poweroffSave_remove,
.driver ={
.owner = THIS_MODULE,
.name = "poweroffSave",
.of_match_table = poweroffSave_id,
},
};
static int __init poweroffsave_init(void)
{
int err;
err = platform_driver_register(&powerOffSave_driver);
return 0;
}
static void __exit poweroffsave_exit(void)
{
platform_driver_unregister(&powerOffSave_driver);
}
module_init(poweroffsave_init);
module_exit(poweroffsave_exit);
MODULE_LICENSE("GPL");