Linux异步通知机制-系统掉电保存文件
资源内容介绍
掉电保存驱动与应用 #include <linux/module.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/of_device.h>#include <linux/of.h>#include <linux/of_gpio.h>#include <linux/delay.h>#include <linux/miscdevice.h>#include <linux/fs.h>#define DEVICE_NAME "poweroffSave"static struct fasync_struct *async;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){kill_fasync(&async,SIGIO,POLL_IN);return (IRQ_HANDLED);}static int poweroff_fasync (int fd, struct file *filp,int mode){return fasync_helper(fd,filp,mode,&async);}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);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;}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");