Linux驱动(四) 驱动注册HelloWorld

2440阅读 0评论2018-01-09 qq526665621
分类:LINUX

#include 《linux/init.h》
#include 《linux/module.h》
#include 《linux/platform_device.h》
#include 《linux/miscdevice.h》
#include 《linux/linux/fs.h》


#define DRIVER_NAME "hello_cdl"

MODULE_LICENSE("Dual BSD/GPL"); //权限
MODULE_AUTHOR("CDL");   //作者

static int hello_open(struct inode *inode, struct file *file)
{
    printk(KERN_EMERG "Hello World open enter!\n");
    return 0;
}

static int hello_release(struct inode *inode, struct file *file)
{
    printk(KERN_EMERG "Hello World release enter!\n");
    return 0;
}

static long hello_ioctl(struct file *file, unsigned int cmd, unsigned long data)
{
    printk(KERN_EMERG "Hello World ioctl enter!\n");
    printk(KERN_EMERG "ioctl cmd[%d] arg[%d]!\n", data);
    return 0;    
}
struct file_operations hello_ops = {
    .owner = THIS_MODULE,
    .open = hello_open,
    .release = hello_release,
    .unlocked_ioctl = hello_ioctl,
};

struct miscdevice hello_misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DRIVER_NAME,
    .fops = &hello_ops,
};

static int hello_probe(struct platform_device * pdev)
{
    printk(KERN_EMERG "Hello World prob enter!\n");
    misc_register(&hello_misc);  //杂项设备注册设备节点
    return 0;
}

static int hello_remove(struct platform_device * pdev)
{
    printk(KERN_EMERG "Hello World remove enter!\n");
    misc_deregister(&hello_misc);
    return 0;
}

static void hello_shutdown(struct platform_device *pdev)
{
    printk(KERN_EMERG "Hello World shutdown enter!\n");
    return;
}

static int hello_suspend(struct platform_device *pdev, pm_message_t state)
{
    printk(KERN_EMERG "Hello World suspend enter!\n");
    return 0;
}

static int hello_resume(struct platform_device * pdev)
{
    printk(KERN_EMERG "Hello World resume enter!\n");
    return 0;
}

struct platform_driver hello_driver = {
    .probe = hello_probe,
    .remove = hello_remove,
    .shutdown = hello_shutdown,
    .suspend = hello_suspend,
    .resume = hello_resume,
    .driver = {
        .name = DRIVER_NAME,
        .owner = THIS_MODULE,
    },
};

static int __init hello_init(void)
{
    int driverstate = 0;
    printk(KERN_EMERG "Hello World enter!\n");
    driverstate = platform_driver_register(&hello_driver);         //注册设备驱动
    printk(KERN_EMERG "Init Driverstat %d!\n", driverstate);
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_EMERG "Hello World exit!\n");
    platform_driver_unregister(&hello_driver);
    return;   
}

module_init(hello_init); //驱动加载初始化
module_exit(hello_exit) //驱动卸载退出
上一篇:Ubuntu64位支持32编译工具
下一篇:Linux驱动(一) 总线 驱动 设备 关系图