自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手教Linux驅(qū)動(dòng)10-Platform總線詳解

系統(tǒng) Linux
linux從2.6起就加入了一套新的驅(qū)動(dòng)管理和注冊(cè)的機(jī)制platform平臺(tái)總線,是一條虛擬的總線,并不是一個(gè)物理的總線。

[[380561]]

platform總線是學(xué)習(xí)linux驅(qū)動(dòng)必須要掌握的一個(gè)知識(shí)點(diǎn)。

本文參考已發(fā)布:Linux 3.14內(nèi)核

一、概念

嵌入式系統(tǒng)中有很多的物理總線:I2c、SPI、USB、uart、PCIE、APB、AHB

linux從2.6起就加入了一套新的驅(qū)動(dòng)管理和注冊(cè)的機(jī)制platform平臺(tái)總線,是一條虛擬的總線,并不是一個(gè)物理的總線。

相比 PCI、USB,它主要用于描述SOC上的片上資源。platform 所描述的資源有一個(gè)共同點(diǎn):在CPU 的總線上直接取址。

平臺(tái)設(shè)備會(huì)分到一個(gè)名稱(用在驅(qū)動(dòng)綁定中)以及一系列諸如地址和中斷請(qǐng)求號(hào)(IRQ)之類的資源。

設(shè)備用platform_device表示,驅(qū)動(dòng)用platform_driver進(jìn)行注冊(cè)。

與傳統(tǒng)的bus/device/driver機(jī)制相比,platform由內(nèi)核進(jìn)行統(tǒng)一管理,在驅(qū)動(dòng)中使用資源,提高了代碼的安全性和可移植性。

二、platform

1. platform總線兩個(gè)最重要的結(jié)構(gòu)體

platform維護(hù)的所有的驅(qū)動(dòng)都必須要用該結(jié)構(gòu)體定義:

platform_driver

  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.  const struct platform_device_id *id_table; 
  9.  bool prevent_deferred_probe; 
  10. }; 

該結(jié)構(gòu)體,用于注冊(cè)驅(qū)動(dòng)到platform總線,

成員 含義
probe 當(dāng)驅(qū)動(dòng)和硬件信息匹配成功之后,就會(huì)調(diào)用probe函數(shù),驅(qū)動(dòng)所有的資源的注冊(cè)和初始化全部放在probe函數(shù)中
remove 硬件信息被移除了,或者驅(qū)動(dòng)被卸載了,全部要釋放,釋放資源的操作就放在該函數(shù)中
struct device_driver driver 內(nèi)核維護(hù)的所有的驅(qū)動(dòng)必須包含該成員,通常driver->name用于和設(shè)備進(jìn)行匹配
const struct platform_device_id *id_table 往往一個(gè)驅(qū)動(dòng)可能能同時(shí)支持多個(gè)硬件,這些硬件的名字都放在該結(jié)構(gòu)體數(shù)組中

我們編寫驅(qū)動(dòng)的時(shí)候往往需要填充以上幾個(gè)成員

platform_device

platform總線用于描述設(shè)備硬件信息的結(jié)構(gòu)體,包括該硬件的所有資源(io,memory、中斷、DMA等等)。

  1. struct platform_device { 
  2.  const char *name
  3.  int  id; 
  4.  bool  id_auto; 
  5.  struct device dev; 
  6.  u32  num_resources; 
  7.  struct resource *resource; 
  8.  
  9.  const struct platform_device_id *id_entry; 
  10.  
  11.  /* MFD cell pointer */ 
  12.  struct mfd_cell *mfd_cell; 
  13.  
  14.  /* arch specific additions */ 
  15.  struct pdev_archdata archdata; 
  16. }; 
成員 含義
const char *name 設(shè)備的名字,用于和驅(qū)動(dòng)進(jìn)行匹配的
struct device dev 內(nèi)核中維護(hù)的所有的設(shè)備必須包含該成員,
u32 num_resources 資源個(gè)數(shù)
struct resource *resource 描述資源

struct device dev->release()必須實(shí)現(xiàn),

其中描述硬件信息的成員struct resource

0x139d0000

  1. struct resource { 
  2.  resource_size_t start;  //表示資源的起始值,            
  3.  resource_size_t end;    //表示資源的最后一個(gè)字節(jié)的地址, 如果是中斷,end和satrt相同 
  4.  const char *name;   // 可不寫   
  5.  unsigned long flags; //資源的類型 
  6.  struct resource *parent, *sibling, *child; 
  7. }; 
  8. flags的類型說明 
  9.  
  10. #define IORESOURCE_MEM  0x00000200    //內(nèi)存 
  11. #define IORESOURCE_IRQ  0x00000400    //中斷 

內(nèi)核管理的所有的驅(qū)動(dòng),都必須包含一個(gè)叫struct device_driver成員, //男性描述的硬件,必須包含struct device結(jié)構(gòu)體成員。 //女性

  1. struct device_driver { 
  2.  const char  *name;       
  3.  struct bus_type  *bus; 
  4.  
  5.  struct module  *owner; 
  6.  const char  *mod_name; /* used for built-in modules */ 
  7.  
  8.  bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 
  9.  
  10.  const struct of_device_id *of_match_table; 
  11.  const struct acpi_device_id *acpi_match_table; 
  12.  
  13.  int (*probe) (struct device *dev); 
  14.  int (*remove) (struct device *dev); 
  15.  void (*shutdown) (struct device *dev); 
  16.  int (*suspend) (struct device *dev, pm_message_t state); 
  17.  int (*resume) (struct device *dev); 
  18.  const struct attribute_group **groups; 
  19.  
  20.  const struct dev_pm_ops *pm; 
  21.  
  22.  struct driver_private *p; 
  23. }; 

其中:

  1. const char  *name

用于和硬件進(jìn)行匹配。

內(nèi)核描述硬件,必須包含struct device結(jié)構(gòu)體成員:

  1. struct device { 
  2.  struct device  *parent; 
  3.  
  4.  struct device_private *p; 
  5.  
  6.  struct kobject kobj; 
  7.  const char  *init_name; /* initial name of the device */ 
  8.  const struct device_type *type; 
  9.  
  10.  struct mutex  mutex; /* mutex to synchronize calls to 
  11.       * its driver. 
  12.       */ 
  13.  
  14.  struct bus_type *bus;  /* type of bus device is on */ 
  15.  struct device_driver *driver; /* which driver has allocated this 
  16.         device */ 
  17.  void  *platform_data; /* Platform specific data, device 
  18.         core doesn't touch it */ 
  19.  struct dev_pm_info power; 
  20.  struct dev_pm_domain *pm_domain; 
  21.  
  22. #ifdef CONFIG_PINCTRL 
  23.  struct dev_pin_info *pins; 
  24. #endif 
  25.  
  26. #ifdef CONFIG_NUMA 
  27.  int  numa_node; /* NUMA node this device is close to */ 
  28. #endif 
  29.  u64  *dma_mask; /* dma mask (if dma'able device) */ 
  30.  u64  coherent_dma_mask;/* Like dma_mask, but for 
  31.           alloc_coherent mappings as 
  32.           not all hardware supports 
  33.           64 bit addresses for consistent 
  34.           allocations such descriptors. */ 
  35.  
  36.  struct device_dma_parameters *dma_parms; 
  37.  
  38.  struct list_head dma_pools; /* dma pools (if dma'ble) */ 
  39.  
  40.  struct dma_coherent_mem *dma_mem; /* internal for coherent mem 
  41.           override */ 
  42. #ifdef CONFIG_DMA_CMA 
  43.  struct cma *cma_area;  /* contiguous memory area for dma 
  44.         allocations */ 
  45. #endif 
  46.  /* arch specific additions */ 
  47.  struct dev_archdata archdata; 
  48.  
  49.  struct device_node *of_node; /* associated device tree node */ 
  50.  struct acpi_dev_node acpi_node; /* associated ACPI device node */ 
  51.  
  52.  dev_t   devt; /* dev_t, creates the sysfs "dev" */ 
  53.  u32   id; /* device instance */ 
  54.  
  55.  spinlock_t  devres_lock; 
  56.  struct list_head devres_head; 
  57.  
  58.  struct klist_node knode_class; 
  59.  struct class  *class; 
  60.  const struct attribute_group **groups; /* optional groups */ 
  61.  
  62.  void (*release)(struct device *dev); 
  63.  struct iommu_group *iommu_group; 
  64.  
  65.  bool   offline_disabled:1; 
  66.  bool   offline:1; 
  67. }; 

其中:

  1. void (*release)(struct device *dev); 

不能為空。

2. 如何注冊(cè)

要用注冊(cè)一個(gè)platform驅(qū)動(dòng)的步驟

1)注冊(cè)驅(qū)動(dòng)platform_device_register

  1. /** 
  2.  * platform_device_register - add a platform-level device 
  3.  * @pdev: platform device we're adding 
  4.  */ 
  5. int platform_device_register(struct platform_device *pdev) 
  6.  device_initialize(&pdev->dev); 
  7.  arch_setup_pdev_archdata(pdev); 
  8.  return platform_device_add(pdev); 

2) 注冊(cè)設(shè)備platform_driver_register

  1. #define platform_driver_register(drv) \ 
  2.  __platform_driver_register(drv, THIS_MODULE) 

三、舉例

1. 開發(fā)步驟

platform 總線下驅(qū)動(dòng)的開發(fā)步驟是:

設(shè)備

需要實(shí)現(xiàn)的結(jié)構(gòu)體是:platform_device 。

1)初始化 resource 結(jié)構(gòu)變量

2)初始化 platform_device 結(jié)構(gòu)變量

3)向系統(tǒng)注冊(cè)設(shè)備:platform_device_register。

以上三步,必須在設(shè)備驅(qū)動(dòng)加載前完成,即執(zhí)行platform_driver_register()之前,原因是驅(qū)動(dòng)注冊(cè)時(shí)需要匹配內(nèi)核中所有已注冊(cè)的設(shè)備名。

platform_driver_register()中添加device到內(nèi)核最終還是調(diào)用的device_add函數(shù)。

Platform_device_add和device_add最主要的區(qū)別是多了一步insert_resource(p, r),即將platform資源(resource)添加進(jìn)內(nèi)核,由內(nèi)核統(tǒng)一管理。

驅(qū)動(dòng)

驅(qū)動(dòng)注冊(cè)中,需要實(shí)現(xiàn)的結(jié)構(gòu)體是:platform_driver 。

在驅(qū)動(dòng)程序的初始化函數(shù)中,調(diào)用了platform_driver_register()注冊(cè) platform_driver 。

需要注意的是:platform_driver 和 platform_device 中的 name 變量的值必須是相同的【在不考慮設(shè)備樹情況下,關(guān)于設(shè)備樹,后面會(huì)寫新的文章詳細(xì)講述】 。

這樣在 platform_driver_register() 注冊(cè)時(shí),會(huì)將當(dāng)前注冊(cè)的 platform_driver 中的 name 變量的值和已注冊(cè)的所有 platform_device 中的 name 變量的值進(jìn)行比較,只有找到具有相同名稱的 platform_device 才能注冊(cè)成功。

當(dāng)注冊(cè)成功時(shí),會(huì)調(diào)用 platform_driver 結(jié)構(gòu)元素 probe 函數(shù)指針。

實(shí)例1

本例比較簡(jiǎn)單,只用于測(cè)試platform_driver 和platform_device是否可以匹配成功。

左邊是platform_device結(jié)構(gòu)體注冊(cè)的代碼,右邊是platform_driver結(jié)構(gòu)體注冊(cè)的代碼。

platform_driver 定義和注冊(cè):

  1. 1 #include <linux/init.h> 
  2.  2 #include <linux/module.h> 
  3.  3 #include <linux/platform_device.h> 
  4.  4 #include <linux/ioport.h> 
  5.  5  
  6.  6 static int hello_probe(struct platform_device *pdev) 
  7.  7 { 
  8.  8     printk("match ok \n"); 
  9.  9     return 0; 
  10. 10 } 
  11. 11 static  int hello_remove(struct platform_device *pdev) 
  12. 12 { 
  13. 13     printk("hello_remove \n"); 
  14. 14     return 0; 
  15. 15 } 
  16. 16 static struct platform_driver hello_driver = 
  17. 17 { 
  18. 18     .probe = hello_probe, 
  19. 19     .driver.name = "duang"
  20. 20     .remove = hello_remove,      
  21. 21 }; 
  22. 22 static int hello_init(void) 
  23. 23 { 
  24. 24     printk("hello_init \n"); 
  25. 25     return platform_driver_register(&hello_driver); 
  26. 26 } 
  27. 27 static void hello_exit(void) 
  28. 28 { 
  29. 29     printk("hello_exit \n"); 
  30. 30     platform_driver_unregister(&hello_driver); 
  31. 31     return
  32. 32 } 
  33. 33 MODULE_LICENSE("GPL"); 
  34. 34 module_init(hello_init); 
  35. 35 module_exit(hello_exit); 

platform_device定義和注冊(cè):

  1. 1 #include <linux/init.h>                                                                                                                                                         
  2.   2 #include <linux/module.h> 
  3.   3 #include <linux/platform_device.h> 
  4.   4 #include <linux/ioport.h> 
  5.   5  
  6.   6 static void hello_release(struct device *dev) 
  7.   7 { 
  8.   8      return
  9.   9 } 
  10.  10 static struct platform_device hello_device = 
  11.  11 { 
  12.  12     .name = "duang"
  13.  13     .id = -1, 
  14.  14     .dev.release = hello_release, 
  15.  15 }; 
  16.  16  
  17.  17  
  18.  18 static int hello_init(void) 
  19.  19 { 
  20.  20     printk("hello_init \n"); 
  21.  21     return platform_device_register(&hello_device); 
  22.  22  
  23.  23 } 
  24.  24 static void hello_exit(void) 
  25.  25 { 
  26.  26     printk("hello_exit \n"); 
  27.  27     platform_device_unregister(&hello_device); 
  28.  28     return
  29.  29 } 
  30.  30 MODULE_LICENSE("GPL"); 
  31.  31 module_init(hello_init); 
  32.  32 module_exit(hello_exit); 

該程序只用于測(cè)試platform框架是否可以成功匹配,struct platform_device hello_device 并沒有設(shè)置任何硬件信息。

Makfile

  1. 1 ifneq ($(KERNELRELEASE),)                                                                                                                                                       
  2. 2 obj-m:=device.o driver.o 
  3. else 
  4. 4 KDIR :=/lib/modules/$(shell uname -r)/build 
  5. 5 PWD  :=$(shell pwd) 
  6. all
  7. 7     make -C $(KDIR) M=$(PWD) modules 
  8. 8 clean: 
  9. 9     rm -f *.ko *.o *.mod.o *.symvers *.cmd  *.mod.c *.order 
  10. 0 endif 

該makefile可以同時(shí)將兩個(gè)C文件編譯成ko文件。

編譯:

編譯

編譯生成的文件:

在這里插入圖片描述

加載模塊

  1. 清空log信息 
  2. sudo dmesg -c 

匹配成功

實(shí)例2

給結(jié)構(gòu)體platform_device 增加硬件信息,并在內(nèi)核中能夠讀取出來。本例向結(jié)構(gòu)體hello_device 增加信息如下:

基址寄存器地址0x139d0000,該地址的空間是0x4

中斷號(hào)199 【注意】 實(shí)際的內(nèi)核中會(huì)把外設(shè)的中斷號(hào)根據(jù)HW id(通常soc廠商設(shè)備soc的時(shí)候會(huì)給每一個(gè)中斷源定義好唯一的ID)計(jì)算出一個(gè)新的中斷號(hào),該中斷號(hào)會(huì)被cpu所識(shí)別。

device.c

  1. struct resource res[]={ 
  2.  [0] ={ 
  3.   .start = 0x139d0000, 
  4.   .end  = 0x139d0000 + 0x3, 
  5.   .flags = IORESOURCE_MEM, 
  6.  }, 
  7.  
  8.  [1] ={ 
  9.   .start = 199, 
  10.   .end  = 199, 
  11.   .flags = IORESOURCE_IRQ, 
  12.  },  
  13. }; 
  14. static struct platform_device hello_device =  
  15.  .name = "duang"
  16.  .id = -1, 
  17.  .dev.release = hello_release,  
  18.  .num_resources = ARRAY_SIZE(res), 
  19.  .resource = res, 
  20. }; 

driver.c

  1. static int hello_probe(struct platform_device *pdev) 
  2.  printk("match ok \n"); 
  3.  
  4.  printk("mem = %x \n",pdev->resource[0].start); 
  5.  printk("irq = %d \n",pdev->resource[1].start); 
  6.  
  7.  //注冊(cè)中斷、申請(qǐng)內(nèi)存 
  8.  return 0; 

重新編譯,卸載第一個(gè)例子的模塊,并清除log:

  1. make 
  2. sudo rmmod device  
  3. sudo rmmod driver 
  4. sudo dmesg -c 

執(zhí)行

由結(jié)果可知,probe函數(shù)正確讀取到了硬件信息。

四、platform_device是如何管理的?

1. 沒有設(shè)備樹

在沒有設(shè)備樹的時(shí)候,以三星Cortex-A8 s5pc100為例,硬件信息放在以下位置

  1. arch\arm\mach-s5pc100\Mach-smdkc100.c 
  2. arch\arm\plat-samsung\ 

注冊(cè)platform_device

platform_device定義

該數(shù)組存放了,內(nèi)核啟動(dòng)需要初始化的硬件的信息。

2. 如果有設(shè)備樹

內(nèi)核會(huì)有設(shè)備初始化的完整代碼,會(huì)在內(nèi)核啟動(dòng)的時(shí)候把設(shè)備樹信息解析初始化,把硬件信息初始化到對(duì)應(yīng)的鏈表中。在總線匹配成功后,會(huì)把硬件的信息傳遞給probe()函數(shù)。

四、總線相關(guān)的其他的知識(shí)點(diǎn)

1. 內(nèi)核總線相關(guān)結(jié)構(gòu)體變量

內(nèi)核維護(hù)的所有的總線都需要用以下結(jié)構(gòu)體注冊(cè)一個(gè)變量。

  1. struct bus_type { 
  2.  const char  *name
  3.  const char  *dev_name; 
  4.  struct device  *dev_root; 
  5.  struct device_attribute *dev_attrs; /* use dev_groups instead */ 
  6.  const struct attribute_group **bus_groups; 
  7.  const struct attribute_group **dev_groups; 
  8.  const struct attribute_group **drv_groups; 
  9.  
  10.  int (*match)(struct device *dev, struct device_driver *drv); 
  11.  int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 
  12.  int (*probe)(struct device *dev);    
  13.  int (*remove)(struct device *dev); 
  14.  void (*shutdown)(struct device *dev); 
  15.  
  16.  int (*online)(struct device *dev); 
  17.  int (*offline)(struct device *dev); 
  18.  
  19.  int (*suspend)(struct device *dev, pm_message_t state); 
  20.  int (*resume)(struct device *dev); 
  21.  
  22.  const struct dev_pm_ops *pm; 
  23.  
  24.  struct iommu_ops *iommu_ops; 
  25.  
  26.  struct subsys_private *p; 
  27.  struct lock_class_key lock_key; 
  28. }; 

platform總線變量的定義struct bus_type platform_bus_type定義如下:

  1. struct bus_type platform_bus_type = { 
  2.  .name  = "platform"
  3.  .dev_groups = platform_dev_groups, 
  4.  .match  = platform_match, 
  5.  .uevent  = platform_uevent, 
  6.  .pm  = &platform_dev_pm_ops, 
  7. }; 

其中最重要的成員是**.match**。

當(dāng)有設(shè)備的硬件信息注冊(cè)到platform_bus_type 總線的時(shí)候,會(huì)遍歷所有platform總線維護(hù)的驅(qū)動(dòng), 通過名字來匹配,如果相同,就說明硬件信息和驅(qū)動(dòng)匹配,就會(huì)調(diào)用驅(qū)動(dòng)的platform_driver ->probe函數(shù),初始化驅(qū)動(dòng)的所有資源,讓該驅(qū)動(dòng)生效。

當(dāng)有設(shè)備的驅(qū)動(dòng)注冊(cè)到platform_bus_type 總線的時(shí)候,會(huì)遍歷所有platform總線維護(hù)的硬件信息, 通過名字來匹配,如果相同,就說明硬件信息和驅(qū)動(dòng)匹配,就會(huì)調(diào)用驅(qū)動(dòng)的platform_driver ->probe函數(shù),初始化驅(qū)動(dòng)的所有資源,讓該驅(qū)動(dòng)生效。

注冊(cè)位置

  1. drivers\base\Platform.c 

platform_bus_type的注冊(cè)

五、注冊(cè)代碼流程詳解

捋架構(gòu)的好處,就是可以幫助我們定位問題

1. match函數(shù)何時(shí)被調(diào)用到?

2. probe函數(shù)何時(shí)被調(diào)用到

以下是上述兩個(gè)問題代碼的調(diào)用流程:

代碼調(diào)用流程

后面我們會(huì)再詳細(xì)介紹設(shè)備樹。

本文轉(zhuǎn)載自微信公眾號(hào)「一口Linux 」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系一口Linux 公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 一口Linux
相關(guān)推薦

2020-09-27 06:59:59

IO系統(tǒng)Linux

2020-09-04 10:14:02

Linux驅(qū)動(dòng)7內(nèi)核

2021-04-12 12:00:13

Linux運(yùn)維Linux系統(tǒng)

2022-09-14 17:12:15

flowable源碼DEMO

2009-11-06 10:44:31

Visual Stud

2020-11-10 09:00:00

JavaMule ESB開發(fā)

2011-01-10 14:41:26

2010-08-18 09:15:45

路由器網(wǎng)絡(luò)診斷

2009-10-21 10:47:03

Siliverligh

2010-07-07 10:24:46

Python多線程

2010-04-02 16:51:09

虛擬機(jī)安裝linux

2020-09-23 07:00:00

Selenium We架構(gòu)

2020-02-21 19:54:09

HTTPS 配置手把手教

2009-11-23 14:48:44

SUSE linux

2010-09-14 09:24:27

家庭無線網(wǎng)絡(luò)

2010-09-02 10:50:17

時(shí)間同步服務(wù)器

2010-08-26 09:24:59

路由器網(wǎng)絡(luò)診斷

2020-02-21 10:45:06

運(yùn)維架構(gòu)技術(shù)

2009-12-11 09:04:10

Windows搭建Li

2022-01-17 07:50:37

Linux Patch項(xiàng)目
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)