Linux下面一個簡單的虛擬platform驅動

在Linux之中,約定若是設備不屬於任何的總線,則能夠把它註冊爲虛擬的platform設備。 html

下面就簡單來學習一下一個簡單的platform設備是怎麼建立出來的。 node

通常註冊platform驅動的步驟是: linux

1,platform_device_create註冊一個設備 shell

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

platform_device和platform_driver的名字必須一致 spa


而後在chrdev_probe中完成註冊一個字符設備。通常註冊字符設備的流程以下: .net

1,alloc_chrdev_region分配一個未使用的設備號 orm

2,cdev_init和cdev_add使用(1)分配到的設備號添加一個字符設備 htm


若是到這裏就結束了,咱們就須要使用分配到的設備號手動去創建/dev下面的設備節點,,,

在這裏能夠使用class_create和device_create讓udev deamon自動爲咱們建立設備節點

3,class_create(THIS_MODULE, "chrdev");

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

當使用insmod把模塊加載到系統以後,www.linuxidc.com 就會在/dev下面自動建立名爲"chrdev"的設備節點,模塊名字應該儘可能跟註冊驅動的名字一致,否則可能會遇到不可測的問題


源文件:chrdev.c

[cpp]
  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);  
  151. [cpp]
    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]
    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  
    [html]  view plain copy print ?
    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  
相關文章
相關標籤/搜索