Linux Platform devices 平臺設備驅動

    設備總線驅動模型:http://blog.csdn.NET/lizuobin2/article/details/51570196

    本文主要參考:http://www.wowotech.net/device_model/platform_device.html


    platform平臺設備驅動是基於設備總線驅動模型的,它只不過是將 device 進一步封裝成爲 platform_device,將 device_driver 進一步封裝成爲 platform_device_driver,前面已經分析過設備總線驅動模型,關於device 與 device_driver 的註冊過程以及它們在sysfs文件系統中的層次關係就不在分析,本文重點分析platform平臺設備驅動與設備總線驅動模型相比較新增添的那些東西。


    在Linux設備模型的抽象中,存在着一類稱作“Platform Device”的設備,內核是這樣描述它們的(Documentation/driver-model/platform.txt):

    Platform devices are devices that typically appear as autonomous entities in the system. This includes legacy port-based devices and host bridges to peripheral buses, and most controllers integrated into system-on-chip platforms.  What they usually have in common is direct addressing from a CPU bus.  Rarely, a platform_device will be connected through a segment of some other kind of bus; but its registers will still be directly addressable.

   

     概括來說,Platform設備包括:基於端口的設備(已不推薦使用,保留下來只爲兼容舊設備,legacy);連接物理總線的橋設備;集成在SOC平臺上面的控制器;連接在其它bus上的設備(很少見)。等等。
    這些設備有一個基本的特徵:可以通過CPU bus直接尋址(例如在嵌入式系統常見的“寄存器”)。因此,由於這個共性,內核在設備模型的基礎上(device和device_driver),對這些設備進行了更進一步的封裝,抽象出paltform bus、platform device和platform driver,以便驅動開發人員可以方便的開發這類設備的驅動。
    可以說,paltform設備對linux驅動工程師是非常重要的,因爲我們編寫的大多數設備驅動,都是爲了驅動plaftom設備。


platform_bus_type
    我們知道,在設備總線驅動模型的中,BUS像一個月老一樣,通過它的match函數,將註冊到bus中的device與driver進行配對,那麼每一個不同的bus 都有自己的match函數,我們來看看platform_bus_type.

  1. struct bus_type platform_bus_type = {  
  2.     .name       = "platform",  
  3.     .dev_attrs  = platform_dev_attrs,  
  4.     .match      = platform_match,  
  5.     .uevent     = platform_uevent,  
  6.     .pm     = &platform_dev_pm_ops,  
  7. };  
  1. static int platform_match(struct device *dev, struct device_driver *drv)  
  2. {  
  3.     struct platform_device *pdev = to_platform_device(dev);  
  4.     struct platform_driver *pdrv = to_platform_driver(drv);  
  5.   
  6.     /* match against the id table first */  
  7.     if (pdrv->id_table)  
  8.         return platform_match_id(pdrv->id_table, pdev) != NULL;  
  9.   
  10.     /* fall-back to driver name match */  
  11.     return (strcmp(pdev->name, drv->name) == 0);  
  12. }  

    如果platform_device_driver中定義了id_table,則調用 platform_match_id 進行匹配

    舉個例子:

  1. static struct platform_device_id s3c24xx_driver_ids[] = {  
  2.     {  
  3.         .name       = "s3c2410-i2c",  
  4.         .driver_data    = TYPE_S3C2410,  
  5.     }, {  
  6.         .name       = "s3c2440-i2c",  
  7.         .driver_data    = TYPE_S3C2440,  
  8.     }, { },  
  9. };  
  1. struct platform_device s3c_device_i2c0 = {  
  2.     .name         = "s3c2410-i2c",  
  3. #ifdef CONFIG_S3C_DEV_I2C1  
  4.     .id       = 0,  
  5. #else  
  6.     .id       = -1,  
  7. #endif  
  8.     .num_resources    = ARRAY_SIZE(s3c_i2c_resource),  
  9.     .resource     = s3c_i2c_resource,  
  10. };  
  1. static const struct platform_device_id *platform_match_id(struct platform_device_id *id, struct platform_device *pdev)  
  2. {  
  3.     while (id->name[0]) {  
  4.         if (strcmp(pdev->name, id->name) == 0) {  
  5.             pdev->id_entry = id;  
  6.             return id;  
  7.         }  
  8.         id++;  
  9.     }  
  10.     return NULL;  
  11. }  
    顯然,platform_match_id 的作用就是遍歷整個 Id_table 數組,尋找是否有與 platform_device->name 同名的,如果有,則返回這個 Platform_device_id ,使用Id_table 打破了原本設備總線驅動模型,一個 device 只能用與一個 device_driver 配對的侷限性。現在一個platform_device_driver 可以與多個platform_device配對。

    如果沒有,則只是根據 platform_device_driver->name 與 platform_device->name 進行比較,這也就是老師爲啥在寫平臺設備驅動程序的時候經常說,“將驅動註冊到內核中去,如果有同名設備,則調用driver->probe函數....”。


pletform_device 中的 id 的作用:

    if (pdev->id != -1)      /* 如果不是-1 對name編號 */  
        dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);  
    else                             /* -1時直接是名字 */
        dev_set_name(&pdev->dev, pdev->name); 


從device封裝而來的platform_device

  1. struct platform_device {  
  2.     const char  * name;  
  3.     int     id;  
  4.     struct device   dev;  
  5.     u32     num_resources;  
  6.     struct resource * resource;  
  7.   
  8.     struct platform_device_id   *id_entry;  
  9.   
  10.     /* arch specific additions */  
  11.     struct pdev_archdata    archdata;  
  12. };    
    name,設備的名稱,該名稱在設備註冊時,會拷貝到dev.init_name中。
    dev,真正的設備,通過 container_of ,就能找到整個platform_device ,訪問其它成員,如後面要提到的 resource 
    num_resources、resource,該設備的資源描述,由struct resource(include/linux/ioport.h)結構抽象。 
    在Linux中,系統資源包括I/O、Memory、Register、IRQ、DMA、Bus等多種類型。這些資源大多具有獨佔性,不允許多個設備同時使用,因此Linux內核提供了一些API,用於分配、管理這些資源。 
    當某個設備需要使用某些資源時,只需利用struct resource組織這些資源(如名稱、類型、起始、結束地址等),並保存在該設備的resource指針中即可。然後在設備probe時,設備需求會調用資源管理接口,分配、使用這些資源。而內核的資源管理邏輯,可以判斷這些資源是否已被使用、是否可被使用等等。

  1. struct resource {  
  2.     resource_size_t start;  
  3.     resource_size_t end;  
  4.     const char *name;  
  5.     unsigned long flags;  
  6.     struct resource *parent, *sibling, *child;  
  7. };  
  1. static struct resource led_resource[] = {   //jz2440的參數,驅動未測試   
  2.     [0] = {  
  3.         .start = 0x56000010,  
  4.         .end   = 0x56000010 + 8 - 1,  
  5.         .flags = IORESOURCE_MEM,  
  6.     },  
  7.     [1] = {  
  8.         .start = 5,  
  9.         .end   = 5,  
  10.         .flags = IORESOURCE_IRQ,  
  11.     },  
  12. };  
  13. static struct platform_device led_dev = {  
  14.     .name = "myled",    //設備名字 與 驅動相匹配  
  15.     .id   = -1,  
  16.     .num_resources = ARRAY_SIZE(led_resource),  
  17.     .resource = led_resource,  
  18.       
  19.     .dev = {  
  20.         .release = led_release,  
  21.         //.devt = MKDEV(252, 1),  
  22.     },  
  23. };  


從 device_driver 封裝而來的platform_device_dirver

  1. struct platform_driver {  
  2.     int (*probe)(struct platform_device *);  
  3.     int (*remove)(struct platform_device *);  
  4.     void (*shutdown)(struct platform_device *);  
  5.     int (*suspend)(struct platform_device *, pm_message_t state);  
  6.     int (*resume)(struct platform_device *);  
  7.     struct device_driver driver;  
  8.     struct platform_device_id *id_table;  
  9. };  
  1. int platform_driver_register(struct platform_driver *drv)  
  2. {  
  3.     drv->driver.bus = &platform_bus_type;  
  4.     if (drv->probe)  
  5.         drv->driver.probe = platform_drv_probe;  
  6.     if (drv->remove)  
  7.         drv->driver.remove = platform_drv_remove;  
  8.     if (drv->shutdown)  
  9.         drv->driver.shutdown = platform_drv_shutdown;  
  10.   
  11.     return driver_register(&drv->driver);  
  12. }  
    struct platform_driver結構和struct device_driver非常類似,上邊的platform_drv_probe、platform_drv_remove、platform_drv_shutdown,只不過稍作轉換調用platform_driver中的probe、remove、shutdown函數,舉個例子稍微看一下

  1. static int platform_drv_probe(struct device *_dev)  
  2. {  
  3.     struct platform_driver *drv = to_platform_driver(_dev->driver);  
  4.     struct platform_device *dev = to_platform_device(_dev);  
  5.   
  6.     return drv->probe(dev);  
  7. }  


Platform Device提供的API
  1. /* include/linux/platform_device.h */  
  2. extern int platform_device_register(struct platform_device *);  
  3. extern void platform_device_unregister(struct platform_device *);  
  4.    
  5. extern void arch_setup_pdev_archdata(struct platform_device *);  
  6. extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);  
  7. extern int platform_get_irq(struct platform_device *, unsigned int);  
  8. extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned intconst char *);  
  9. extern int platform_get_irq_byname(struct platform_device *, const char *);  
  10. extern int platform_add_devices(struct platform_device **, int);  
  11.    
  12. extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);  
  13.    
  14. static inline struct platform_device *platform_device_register_resndata(  
  15.                 struct device *parent, const char *name, int id,  
  16.                 const struct resource *res, unsigned int num,  
  17.                 const void *data, size_t size)  
  18.    
  19. static inline struct platform_device *platform_device_register_simple(  
  20.                 const char *name, int id,  
  21.                 const struct resource *res, unsigned int num)  
  22.    
  23. static inline struct platform_device *platform_device_register_data(  
  24.                 struct device *parent, const char *name, int id,  
  25.                 const void *data, size_t size)  
  26.    
  27. extern struct platform_device *platform_device_alloc(const char *name, int id);  
  28. extern int platform_device_add_resources(struct platform_device *pdev,  
  29.                                          const struct resource *res,  
  30.                                          unsigned int num);  
  31. extern int platform_device_add_data(struct platform_device *pdev,  
  32.                                     const void *data, size_t size);  
  33. extern int platform_device_add(struct platform_device *pdev);  
  34. extern void platform_device_del(struct platform_device *pdev);  
  35. extern void platform_device_put(struct platform_device *pdev);  
    platform_device_register、platform_device_unregister,Platform設備的註冊/註銷接口,和底層的device_register等接口類似。
    arch_setup_pdev_archdata,設置platform_device變量中的archdata指針。
    platform_get_resource、platform_get_irq、platform_get_resource_byname、platform_get_irq_byname,通過這些接口,可以獲取platform_device變量中的resource信息,以及直接獲取IRQ的number等等。
    platform_device_register_full、platform_device_register_resndata、platform_device_register_simple、platform_device_register_data,其它形式的設備註冊。調用者只需要提供一些必要的信息,如name、ID、resource等,Platform模塊就會自動分配一個struct platform_device變量,填充內容後,註冊到內核中。
    platform_device_alloc,以name和id爲參數,動態分配一個struct platform_device變量。
    platform_device_add_resources,向platform device中增加資源描述。
    platform_device_add_data,向platform device中添加自定義的數據(保存在pdev->dev.platform_data指針中)。
    platform_device_add、platform_device_del、platform_device_put,其它操作接口。

Platform Driver提供的API
    platform_driver_registe、platform_driver_unregister,platform driver的註冊、註銷接口。
    platform_driver_probe,主動執行probe動作。
    platform_set_drvdata、platform_get_drvdata,設置或者獲取driver保存在device變量中的私有數據。

懶人API
  1. extern struct platform_device *platform_create_bundle(  
  2.       struct platform_driver *driver, int (*probe)(struct platform_device *),  
  3.       struct resource *res, unsigned int n_res,  
  4.       const void *data, size_t size);  
    只要提供一個platform_driver(要把driver的probe接口顯式的傳入),並告知該設備佔用的資源信息,platform模塊就會幫忙分配資源,並執行probe操作。對於那些不需要熱拔插的設備來說,這種方式是最省事的了。  


簡單一例:

    開發板:Mini2440

    內核版本:2.6.32.2

  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/device.h>  
  6. #include <linux/interrupt.h>  
  7. #include <linux/sched.h>   
  8. #include <linux/irq.h>  
  9. #include <asm/uaccess.h>  
  10. #include <linux/input.h>  
  11. #include <linux/platform_device.h>  
  12. // 設備資源  
  13. static struct resource led_resource[] = {   //jz2440的參數,驅動未測試   
  14.     [0] = {  
  15.         .start = 0x56000010,  
  16.         .end   = 0x56000010 + 8 - 1,  
  17.         .flags = IORESOURCE_MEM,  
  18.     },  
  19.     [1] = {  
  20.         .start = 5,  
  21.         .end   = 5,  
  22.         .flags = IORESOURCE_IRQ,  
  23.     },  
  24. };  
  25.   
  26. static void led_release(struct device *dev){  
  27.   
  28. }  
  29.   
  30. // 創建一個設備  
  31. static struct platform_device led_dev = {  
  32.     .name = "myled",    //設備名字 與 驅動相匹配  
  33.     .id   = -1,  
  34.     .num_resources = ARRAY_SIZE(led_resource),  
  35.     .resource = led_resource,  
  36.       
  37.     .dev = {  
  38.         .release = led_release,  
  39.         //.devt = MKDEV(252, 1),  
  40.     },  
  41. };  
  42.   
  43. static int led_dev_init(void){  
  44.   
  45.     //向bus註冊led_dev match drv鏈表進行配對  
  46.     platform_device_register(&led_dev);  
  47.     return 0;  
  48. }  
  49.   
  50. static void led_dev_exit(void){  
  51.     platform_device_unregister(&led_dev);  
  52. }  
  53.   
  54. module_init(led_dev_init);  
  55. module_exit(led_dev_exit);  
  56. MODULE_LICENSE("GPL");  
  57.       

  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/device.h>  
  6. #include <linux/interrupt.h>  
  7. #include <linux/sched.h>   
  8. #include <linux/irq.h>  
  9. #include <asm/uaccess.h>  
  10.   
  11. #include <linux/platform_device.h>  
  12. #include <linux/io.h>  
  13.   
  14. static int major;  
  15.   
  16. static struct class *cls;  
  17. static struct device *dev;  
  18.   
  19. static volatile unsigned long *gpio_con;  
  20. static volatile unsigned long *gpio_dat;  
  21. static int pin;  
  22.   
  23. static int led_open(struct inode *inode, struct file *file){  
  24.   
  25.     *gpio_con &= ~(0x03 << (pin*2));  
  26.     *gpio_con |=  (0x01 << (pin*2));  
  27.     return 0;  
  28. }  
  29.   
  30. static ssize_t led_write(struct file *file, const char __user *buf,  
  31.     size_t count, loff_t *ppos){  
  32.   
  33.     int val;  
  34.     copy_from_user(&val, buf, count);  
  35.   
  36.     if(val == 1){  
  37.           
  38.         *gpio_dat &= ~(1<<pin);  
  39.     }else{  
  40.       
  41.         *gpio_dat &=  (1<<pin);  
  42.     }  
  43.   
  44.     return 0;  
  45. }  
  46.   
  47. static struct file_operations led_fops = {  
  48.   
  49.     .owner = THIS_MODULE,  
  50.     .open  = led_open,  
  51.     .write = led_write,  
  52. };  
  53.   
  54. static int led_probe(struct platform_device *pdev){  
  55.   
  56.     struct resource *res;  
  57.     // 最後一個參數 0 表示第1個該類型的資源  
  58.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  59.     gpio_con = ioremap(res->start, res->end - res->start + 1);  
  60.     gpio_dat = gpio_con + 1;  
  61.   
  62.     res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);  
  63.     pin = res->start;  
  64.   
  65.     printk("led_probe, found led\n");  
  66.   
  67.     // 註冊設備驅動 創建設備節點  
  68.     major = register_chrdev(0, "myled", &led_fops);  
  69.     // 創建類  
  70.     cls = class_create(THIS_MODULE, "myled");  
  71.     // 創建設備節點  
  72.     dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");  
  73.   
  74.     return 0;  
  75. }  
  76.   
  77. static int led_remove(struct platform_device *pdev){  
  78.   
  79.     printk("led_remove, remove led\n");   
  80.     // 刪除設備節點  
  81.     device_unregister(dev);  
  82.     // 銷燬類  
  83.     class_destroy(cls);  
  84.     // 取消註冊設備驅動  
  85.     unregister_chrdev(major, "myled");  
  86.     // 取消內存映射  
  87.     iounmap(gpio_con);  
  88.   
  89.     return 0;  
  90. }  
  91.   
  92. struct platform_driver led_drv = {  
  93.   
  94.     .probe  = led_probe,    //匹配到dev之後調用probe  
  95.     .remove = led_remove,  
  96.     .driver = {  
  97.         .name = "myled",  
  98.     },  
  99. };  
  100.   
  101. static int led_drv_init(void){  
  102.   
  103.     platform_driver_register(&led_drv);  
  104.     return 0;  
  105. }  
  106.   
  107. static void led_drv_exit(void){  
  108.       
  109.     platform_driver_unregister(&led_drv);  
  110. }  
  111.   
  112. module_init(led_drv_init);  
  113. module_exit(led_drv_exit);  
  114. MODULE_LICENSE("GPL");  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章