MISC设备使用代码框架
#include <linux/init.h>
#include <linux/printk.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
/*============================= 模块信息=========================================*/
#define HI_NAME "HelloWorld"
#define HI_MOD_VER "V1.0.0"
/*=============================MISC设备驱动=======================================*/
#define HI_MISC_DEV_NAME "hi1"
static int hi_misc_open(struct inode *inode, struct file *file)
{
return 0;
}
static long hi_misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
return 0;
}
static const struct file_operations hi_misc_fops = {
.owner = THIS_MODULE,
.open = hi_misc_open,
.unlocked_ioctl = hi_misc_ioctl,
};
static struct miscdevice hi_misc_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = HI_MISC_DEV_NAME,
.fops = &hi_misc_fops,
};
/*=============================模块框架接口========================================*/
static int __init hi_mod_init(void)
{
#if 1
int ret;
ret = misc_register(&hi_misc_dev);
if(ret < 0){
printk("hi misc_register failed(ret=%d)!\n", ret);
}
#endif
return 0;
}
static void __exit hi_mod_exit(void)
{
#if 1
misc_deregister(&hi_misc_dev);
#endif
}
/*=============================模块框架===========================================*/
module_init(hi_mod_init);
module_exit(hi_mod_exit);
MODULE_AUTHOR("momo");
MODULE_DESCRIPTION("HelloWorld driver using misc dev.");
MODULE_VERSION( HI_MOD_VER " (" __DATE__ " " __TIME__ ")");
MODULE_LICENSE("GPL");