platform_device_register和platform_driver_register

简介: http://www.linuxidc.com/Linux/2012-01/51725.htm #include #include #include #include #include #include #include #include #include #...

http://www.linuxidc.com/Linux/2012-01/51725.htm

 

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/module.h>  
#include <linux/fs.h>  
#include <linux/errno.h>  
#include <linux/mm.h>  
#include <linux/cdev.h>  
#include <linux/platform_device.h>  

static int chrdev_open(struct inode *inode, struct file *file) 
{  
    printk(KERN_ALERT "chrdev open!\n");  
    return 0;  
}  
  
static int chrdev_release(struct inode *inode, struct file *file) 
{  
    printk(KERN_ALERT "chrdev release!\n");  
    return 0;  
}  
  
int temp_data = 0;
static int chrdev_ioctl(struct inode *inode, struct file *file,  unsigned int cmd, unsigned long arg) 
{    
    switch(cmd)
    {
        case 0x100:
            if(copy_from_user(&temp_data,  (int *)arg, sizeof(int))) 
                return -EFAULT;
            break;
        
        case 0x101:
            if(copy_to_user( (int *)arg, &temp_data, sizeof(int))) 
                return -EFAULT;
            break;
    }

    printk(KERN_ALERT "chrdev_ioctl: 0x%x temp_data=%d\n", cmd, temp_data);  
    return 0;
}  
  
// Kernel interface  
static struct file_operations chrdev_fops = {  
    .owner      =   THIS_MODULE,  
    .unlocked_ioctl      =   chrdev_ioctl,  
    .open       =   chrdev_open,  
    .release    =   chrdev_release,  
};  
  
#define CHRDEV_NAME     "miscdriver"  

static struct class *chrdev_class = NULL;  
static struct device *chrdev_device = NULL;  
static dev_t chrdev_devno;  
static struct cdev chrdev_cdev;  
  
static int chrdev_probe(struct platform_device *dev) 
{  
    int ret = 0, err = 0;  
      
    printk(KERN_ALERT "chrdev probe!\n");  
      
    // alloc character device number  
    ret = alloc_chrdev_region(&chrdev_devno, 0, 1, CHRDEV_NAME);  
    if (ret) {  
        printk(KERN_ALERT " alloc_chrdev_region failed!\n");  
        goto PROBE_ERR;  
    }  
    printk(KERN_ALERT " major:%d minor:%d\n", MAJOR(chrdev_devno), MINOR(chrdev_devno));  
      
    // add a character device  
    cdev_init(&chrdev_cdev, &chrdev_fops);  
    chrdev_cdev.owner = THIS_MODULE;  
    err = cdev_add(&chrdev_cdev, chrdev_devno, 1);  
    if (err) {  
        printk(KERN_ALERT " cdev_add failed!\n");  
        goto PROBE_ERR;  
    }  
      
    // create the device class  
    chrdev_class = class_create(THIS_MODULE, CHRDEV_NAME);  
    if (IS_ERR(chrdev_class)) {  
        printk(KERN_ALERT " class_create failed!\n");  
        goto PROBE_ERR;  
    }  
      
    // create the device node in /dev  
    chrdev_device = device_create(chrdev_class, NULL, chrdev_devno,  
        NULL, CHRDEV_NAME);  
    if (NULL == chrdev_device) {  
        printk(KERN_ALERT " device_create failed!\n");  
        goto PROBE_ERR;  
    }  
      
    printk(KERN_ALERT " chrdev probe ok!\n");  
    return 0;  
      
PROBE_ERR:  
    if (err)  
        cdev_del(&chrdev_cdev);  
    if (ret)   
        unregister_chrdev_region(chrdev_devno, 1);  
    return -1;  
}  
  
static int chrdev_remove (struct platform_device *dev) 
{  
    printk(KERN_ALERT " chrdev remove!\n");  
      
    cdev_del(&chrdev_cdev);  
    unregister_chrdev_region(chrdev_devno, 1);  
      
    device_destroy(chrdev_class, chrdev_devno);  
    class_destroy(chrdev_class);  
    return 0;  
}  
  
// platform_device and platform_driver must has a same name!  
// or it will not work normally  
static struct platform_driver chrdev_platform_driver = {  
    .probe  =   chrdev_probe,  
    .remove =   chrdev_remove,  
    .driver =   {  
        .name   =   CHRDEV_NAME,  
        .owner  =   THIS_MODULE,  
    },  
};  
  
static struct platform_device chrdev_platform_device = {  
    .name   =   CHRDEV_NAME,  
    .id     =   0,  
    .dev    =   {  
    }  
};  
  
  
static __init int chrdev_init(void) 
{  
    int ret = 0;  
    printk(KERN_ALERT "chrdev init!\n");  
      
    ret = platform_device_register(&chrdev_platform_device);  
    if (ret) {  
        printk(KERN_ALERT " platform_device_register failed!\n");  
        return ret;  
    }  
      
    ret = platform_driver_register(&chrdev_platform_driver);  
    if (ret) {  
        printk(KERN_ALERT " platform_driver_register failed!\n");  
        return ret;  
    }  
    printk(KERN_ALERT " chrdev_init ok!\n");  
    return ret;  
}  
  
static __exit void chrdev_exit(void) 
{  
    printk(KERN_ALERT "chrdev exit!\n");  
    platform_driver_unregister(&chrdev_platform_driver);  
}  
  
module_init(chrdev_init);  
module_exit(chrdev_exit); 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Decly");

  

目录
相关文章
|
9月前
|
Linux
device_node转换成platform_device
device_node转换成platform_device
82 0
module_platform_driver源码分析
module_platform_driver源码分析
重装驱动:Failed to initialize NVML: Driver/library version mismatch
重装驱动:Failed to initialize NVML: Driver/library version mismatch
543 0
|
Oracle 关系型数据库 Linux
ASMFD (ASM Filter Driver) Support on OS Platforms (Certification Matrix). (文档 ID 2034681.1)
1) Starting with Oracle Grid Infrastructure 12C Release 1 (12.1.0.2), Oracle ASM Filter Driver (Oracle ASMFD) is installed with an Oracle Grid Infrastructure installation.
2645 0
|
Linux
关于platform_device和platform_driver的匹配【转】
转自:http://blog.csdn.net/dfysy/article/details/5959451 版权声明:本文为博主原创文章,未经博主允许不得转载。 说句老实话,我不太喜欢现在Linux 2.6这套bus, platform, device,device driver 的模式。
863 0
ONVIF Device Manager v2.2.146
对接ONVIF使用软件,用于对接支持onvif协议的IPC厂家设置 http://download.csdn.net/detail/li_dabo/9761415
1208 0
|
芯片
I.MX6 PMU MMPF0100 driver porting
/************************************************************************** * I.MX6 MMPF0100 driver porting * 说明: * 虽然主板上有MMPF0100芯片,却没有注册设备并使用该PMU驱动,真是浪费, * 当然因为需要,所以将PMU的驱动注册起来。
947 0
OK335xS CAN device register and deiver match hacking
/************************************************************************* * OK335xS CAN device register and deiver match hacking * 声明: * 本文主要是跟踪CAN设备的注册、和驱动的匹配方式,了解CAN的注册流程。
900 0
|
BI SoC
I.MX6 ar1020 SPI device driver hacking
/************************************************************************************ * I.
795 0