Linux下面一个简单的虚拟platform驱动

/* 
 * Yao.GUET
 * http://blog.csdn.net/Yao_GUET
 * Linux下面一个简单的虚拟platform驱动
 */

在Linux之中,约定如果设备不属于任何的总线,则可以把它注册为虚拟的platform设备。

下面就简单来学习一下一个简单的platform设备是怎么创建出来的。


一般注册platform驱动的步骤是:

1,platform_device_create注册一个设备

2,platform_driver_create注册一个驱动。

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    =   {
    }
};

platform_device和platform_driver的名字必须一致


然后在chrdev_probe中完成注册一个字符设备。一般注册字符设备的流程如下:

1,alloc_chrdev_region分配一个未使用的设备号

2,cdev_init和cdev_add使用(1)分配到的设备号添加一个字符设备


如果到这里就结束了,我们就需要使用分配到的设备号手动去建立/dev下面的设备节点,,,

在这里可以使用class_create和device_create让udev deamon自动为我们创建设备节点

3,class_create(THIS_MODULE, "chrdev");

4,device_create(chrdev_class, NULL, chrdev_devno, NULL, "chrdev");

当使用insmod把模块加载到系统之后,就会在/dev下面自动创建名为"chrdev"的设备节点,模块名字应该尽量跟注册驱动的名字一致,不然可能会遇到不可测的问题


源文件:chrdev.c

[cpp]  view plain  copy
  1. #include <linux/module.h>  
  2. #include <linux/init.h>  
  3. #include <linux/platform_device.h>  
  4. #include <linux/cdev.h>  
  5. #include <linux/fs.h>  
  6.   
  7. /*  
  8.  * Yao.GUET 
  9.  * http://blog.csdn.net/Yao_GUET 
  10.  * a simple platform character driver 
  11.  */  
  12.   
  13. MODULE_LICENSE("Dual BSD/GPL");  
  14.   
  15. static int chrdev_open(struct inode *inode, struct file *file) {  
  16.     printk(KERN_ALERT "chrdev open!\n");  
  17.     return 0;  
  18. }  
  19.   
  20. static int chrdev_release(struct inode *inode, struct file *file) {  
  21.     printk(KERN_ALERT "chrdev release!\n");  
  22.     return 0;  
  23. }  
  24.   
  25. static int chrdev_ioctl(struct inode *inode, struct file *file,  
  26.     unsigned int cmd, unsigned long arg) {  
  27.     printk(KERN_ALERT "chrdev release!\n");  
  28.     return 0;  
  29. }  
  30.   
  31. // Kernel interface  
  32. static struct file_operations chrdev_fops = {  
  33.     .owner      =   THIS_MODULE,  
  34.     .ioctl      =   chrdev_ioctl,  
  35.     .open       =   chrdev_open,  
  36.     .release    =   chrdev_release,  
  37. };  
  38.   
  39.   
  40. #define CHRDEV_NAME "chrdev"  
  41. // driver interface  
  42. static struct class *chrdev_class = NULL;  
  43. static struct device *chrdev_device = NULL;  
  44. static dev_t chrdev_devno;  
  45. static struct cdev chrdev_cdev;  
  46.   
  47. static int chrdev_probe(struct platform_device *dev) {  
  48.     int ret = 0, err = 0;  
  49.       
  50.     printk(KERN_ALERT "chrdev probe!\n");  
  51.       
  52.     // alloc character device number  
  53.     ret = alloc_chrdev_region(&chrdev_devno, 0, 1, CHRDEV_NAME);  
  54.     if (ret) {  
  55.         printk(KERN_ALERT " alloc_chrdev_region failed!\n");  
  56.         goto PROBE_ERR;  
  57.     }  
  58.     printk(KERN_ALERT " major:%d minor:%d\n", MAJOR(chrdev_devno), MINOR(chrdev_devno));  
  59.       
  60.     cdev_init(&chrdev_cdev, &chrdev_fops);  
  61.     chrdev_cdev.owner = THIS_MODULE;  
  62.     // add a character device  
  63.     err = cdev_add(&chrdev_cdev, chrdev_devno, 1);  
  64.     if (err) {  
  65.         printk(KERN_ALERT " cdev_add failed!\n");  
  66.         goto PROBE_ERR;  
  67.     }  
  68.       
  69.     // create the device class  
  70.     chrdev_class = class_create(THIS_MODULE, CHRDEV_NAME);  
  71.     if (IS_ERR(chrdev_class)) {  
  72.         printk(KERN_ALERT " class_create failed!\n");  
  73.         goto PROBE_ERR;  
  74.     }  
  75.       
  76.     // create the device node in /dev  
  77.     chrdev_device = device_create(chrdev_class, NULL, chrdev_devno,  
  78.         NULL, CHRDEV_NAME);  
  79.     if (NULL == chrdev_device) {  
  80.         printk(KERN_ALERT " device_create failed!\n");  
  81.         goto PROBE_ERR;  
  82.     }  
  83.       
  84.     printk(KERN_ALERT " chrdev probe ok!\n");  
  85.     return 0;  
  86.       
  87. PROBE_ERR:  
  88.     if (err)  
  89.         cdev_del(&chrdev_cdev);  
  90.     if (ret)   
  91.         unregister_chrdev_region(chrdev_devno, 1);  
  92.     return -1;  
  93. }  
  94.   
  95. static int chrdev_remove (struct platform_device *dev) {  
  96.     printk(KERN_ALERT " chrdev remove!\n");  
  97.       
  98.     cdev_del(&chrdev_cdev);  
  99.     unregister_chrdev_region(chrdev_devno, 1);  
  100.       
  101.     device_destroy(chrdev_class, chrdev_devno);  
  102.     class_destroy(chrdev_class);  
  103.     return 0;  
  104. }  
  105.   
  106. // platform_device and platform_driver must has a same name!  
  107. // or it will not work normally  
  108. static struct platform_driver chrdev_platform_driver = {  
  109.     .probe  =   chrdev_probe,  
  110.     .remove =   chrdev_remove,  
  111.     .driver =   {  
  112.         .name   =   CHRDEV_NAME,  
  113.         .owner  =   THIS_MODULE,  
  114.     },  
  115. };  
  116.   
  117. static struct platform_device chrdev_platform_device = {  
  118.     .name   =   CHRDEV_NAME,  
  119.     .id     =   0,  
  120.     .dev    =   {  
  121.     }  
  122. };  
  123.   
  124.   
  125. static __init int chrdev_init(void) {  
  126.     int ret = 0;  
  127.     printk(KERN_ALERT "chrdev init!\n");  
  128.       
  129.     ret = platform_device_register(&chrdev_platform_device);  
  130.     if (ret) {  
  131.         printk(KERN_ALERT " platform_device_register failed!\n");  
  132.         return ret;  
  133.     }  
  134.       
  135.     ret = platform_driver_register(&chrdev_platform_driver);  
  136.     if (ret) {  
  137.         printk(KERN_ALERT " platform_driver_register failed!\n");  
  138.         return ret;  
  139.     }  
  140.     printk(KERN_ALERT " chrdev_init ok!\n");  
  141.     return ret;  
  142. }  
  143.   
  144. static __exit void chrdev_exit(void) {  
  145.     printk(KERN_ALERT "chrdev exit!\n");  
  146.     platform_driver_unregister(&chrdev_platform_driver);  
  147. }  
  148.   
  149. module_init(chrdev_init);  
  150. module_exit(chrdev_exit);  

Makefile:

[html]  view plain  copy
  1. ### Makefile  
  2. obj-m :chrdev.o  
  3.   
  4. KERNEL_DIR := /lib/modules/$(shell uname -r)/build  
  5. PWD := $(shell pwd)  
  6.   
  7. default:  
  8.     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules  
  9.   
  10. clean:  
  11.     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值