详解Linux2.6内核中基于platform机制的驱动模型 (经典) (2)

2    device和platform_device
Plarform device会有一个名字用于driver binding(在注册driver的时候会查找driver的目标设备的bus位置,这个过程称为driver binding),另外IRQ以及地址空间等资源也要给出 。

platform_device结构体用来描述设备的名称、资源信息等。该结构被定义在

中,定义原型如下:

16struct platform_device {
  17        const char      * name; //定义平台设备的名称,此处设备的命名应和相应驱动程序命名一致

18        int             id;
  19        struct device   dev;
  20        u32             num_resources;
  21        struct resource * resource;  //定义平台设备的资源
  22};

在这个结构里封装了struct device及struct resource。可知:platform_device由device派生而来,是一种特殊的device。

下面来看一下platform_device结构体中最重要的一个成员struct resource * resource。struct resource被定义在 /ioport.h#L18中,定义原型如下:
  14/*
  15 * Resources are tree-like, allowing
  16 * nesting etc..
  17 */
  18struct resource {
  19        resource_size_t start;  //定义资源的起始地址
  20        resource_size_t end;  //定义资源的结束地址
  21        const char *name; //定义资源的名称
  22        unsigned long flags; 定义资源的类型,比如MEM,IO,IRQ,DMA类型
  23        struct resource *parent, *sibling, *child;
  24};

这个结构表示设备所拥有的资源,即I/O端口、I/O映射内存、中断及DMA等。这里的地址指的是物理地址。

另外还需要注意platform_device中的device结构,它详细描述了设备的情况,其为所有设备的基类,定义如下:

422struct device {
 423        struct klist            klist_children;
 424        struct klist_node       knode_parent;   /* node in sibling list */
 425        struct klist_node       knode_driver;
 426        struct klist_node       knode_bus;
 427        struct device           *parent;
 428
 429        struct kobject kobj;
 430        char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
 431        struct device_type      *type;
 432        unsigned                is_registered:1;
 433        unsigned                uevent_suppress:1;
 434
 435        struct semaphore        sem;    /* semaphore to synchronize calls to
 436                                         * its driver.
 437                                         */
 438
 439        struct bus_type *bus;           /* type of bus device is on */
 440        struct device_driver *driver;   /* which driver has allocated this
 441                                           device */
 442        void            *driver_data;   /* data private to the driver */
 443        void            *platform_data; /* Platform specific data, device
 444                                           core doesn\'t touch it */
 445        struct dev_pm_info      power;
 446
 447#ifdef CONFIG_NUMA
 448        int             numa_node;      /* NUMA node this device is close to */
 449#endif
 450        u64             *dma_mask;      /* dma mask (if dma\'able device) */
 451        u64             coherent_dma_mask;/* Like dma_mask, but for
 452                                             alloc_coherent mappings as
 453                                             not all hardware supports
 454                                             64 bit addresses for consistent
 455                                             allocations such descriptors. */
 456
 457        struct device_dma_parameters *dma_parms;
 458
 459        struct list_head        dma_pools;      /* dma pools (if dma\'ble) */
 460
 461        struct dma_coherent_mem *dma_mem; /* internal for coherent mem
 462                                             override */
 463        /* arch specific additions */
 464        struct dev_archdata     archdata;
 465
 466        spinlock_t              devres_lock;
 467        struct list_head        devres_head;
 468
 469        /* class_device migration path */
 470        struct list_head        node;
 471        struct class            *class;
 472        dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
 473        struct attribute_group  **groups;       /* optional groups */
 474
 475        void    (*release)(struct device *dev);
 476};
 477

3    device_register和platform_device_register

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjjds.html