Linux内核模块和Linux fs 与 sysfs 二

主要参考整理《Linux设备驱动开发详解》

2. kobject 内核对象----设备模型的核心部分:

    kobject 结构体: 间: /linux/include/linux/kobject.h

struct kobject { const char *name; //对象名称 struct kref kref; //对象引用计数 struct list_head entry; //用于将kobject 挂接到kset链表 struct kobject *parent; //指向kobject的父指针 struct kset *kset; //指向kset的指针 struct kobj_type *ktype; //指向kobject 的类型 struct sysfs_dirent *sd; unsigned int state_initialized:1; unsigned int state_in_sysfs:1; unsigned int state_add_uevent_sent:1; unsigned int state_remove_uevent_sent:1; };

   

    kobj_type 结构体:

   struct kobj_type { void (*release)(struct kobject *kobj); //release 释放函数 struct sysfs_ops *sysfs_ops; //sysfs 属性操作 struct attribute **default_attrs; //默认的属性 }

  

    sys_ops 结构体 :

     struct kobj_attribute { struct attribute attr; //属性 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf); //显示属性 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count); //保存属性 }

   操作 kobject 的函数 :

   初始化kobject:

                void kobject_init(struct kobject *kobj, struct kobj_type *ktype);

   设置kobject 的名称:

                int kobject_set_name(struct kobject *kobj, const char *name, ...)
       __attribute__((format(printf, 2, 3)));

   清除 kobject :

                 void kobject_del(struct kobject *kobj);

  。。。。。。。。。

   具体的操作可以在使用的时候参考。

 

3. kset 结构体 : kset 是具有相同类型的kobject的集合,其结构体如下:

struct kset { struct list_head list; //用于链接该kset中所有kobject 的链表头 spinlock_t list_lock; // struct kobject kobj; //链接的kobject 个体 struct kset_uevent_ops *uevent_ops; //事件操作 }

    注: 形象地来说: kset 类似于一个具有相同属性的链表集合,kobject  则是一个个单独的个体;而当koject 进入链表或者退出链表是会产生一些事件(kset_uevent_ops)

    kset_uevent_ops 结构体;

struct kset_uevent_ops { int (*filter)(struct kset *kset, struct kobject *kobj); //用于过滤一些事件 const char *(*name)(struct kset *kset, struct kobject *kobj); // int (*uevent)(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env); }; //环境变量的设置

 

  4. subsystem内核对象子系统: subsystem是一系列kset的集合,描述系统中某一类设备子系统。

 

  5. Linux设备模型组件:

           1) 系统中的任一设备在设备模型(udev)中都是通过一个 device 对象描述,其结构如下:

struct device { struct klist klist_children; //设备列表中的孩子列表 struct klist_node knode_parent; /* node in sibling list */ struct klist_node knode_driver; //驱动节点 struct klist_node knode_bus; //总线节点 struct device *parent; //父指针 struct kobject kobj; char bus_id[BUS_ID_SIZE]; /* position on parent bus */ struct device_type *type; unsigned uevent_suppress:1; struct semaphore sem; /* semaphore to synchronize calls to * its driver. */ struct bus_type *bus; /* type of bus device is on */ struct device_driver *driver; /* which driver has allocated this device */ void *driver_data; /* data private to the driver */ void *platform_data; /* Platform specific data, device core doesn't touch it */ struct dev_pm_info power; #ifdef CONFIG_NUMA int numa_node; /* NUMA node this device is close to */ #endif u64 *dma_mask; /* dma mask (if dma'able device) */ u64 coherent_dma_mask;/* Like dma_mask, but for alloc_coherent mappings as not all hardware supports 64 bit addresses for consistent allocations such descriptors. */ struct device_dma_parameters *dma_parms; struct list_head dma_pools; /* dma pools (if dma'ble) */ struct dma_coherent_mem *dma_mem; /* internal for coherent mem override */ /* arch specific additions */ struct dev_archdata archdata; spinlock_t devres_lock; struct list_head devres_head; struct list_head node; struct class *class; dev_t devt; /* dev_t, creates the sysfs "dev" */ struct attribute_group **groups; /* optional groups */ void (*release)(struct device *dev); }

       device描述了系统中设备的相关信息以及相互间的关系,以及使用到的总线和使用到得驱动程序。

       Linux中还提供了一些操作device设备的方法:
extern int __must_check device_register(struct device *dev);
extern void device_unregister(struct device *dev);
extern void device_initialize(struct device *dev);
extern int __must_check device_add(struct device *dev);
extern void device_del(struct device *dev);
extern int device_for_each_child(struct device *dev, void *data,
       int (*fn)(struct device *dev, void *data));
extern struct device *device_find_child(struct device *dev, void *data,
    int (*match)(struct device *dev, void *data));
extern int device_rename(struct device *dev, char *new_name);
extern int device_move(struct device *dev, struct device *new_parent);

  注意: 在linux中, device结构体一般不会单独使用,而是结合device使用: 例如在wm8350中 见:linux/include/linux/mfs/wm8350/core.h

 struct wm8350 { int rev; /* chip revision */ struct device *dev; //device 结构体 /* device IO */ union { struct i2c_client *i2c_client; struct spi_device *spi_device; }; int (*read_dev)(struct wm8350 *wm8350, char reg, int size, void *dest); int (*write_dev)(struct wm8350 *wm8350, char reg, int size, void *src); u16 *reg_cache; /* Interrupt handling */ struct work_struct irq_work; struct mutex irq_mutex; /* IRQ table mutex */ struct wm8350_irq irq[WM8350_NUM_IRQ]; int chip_irq; /* Client devices */ struct wm8350_codec codec; struct wm8350_gpio gpio; struct wm8350_hwmon hwmon; struct wm8350_pmic pmic; struct wm8350_power power; struct wm8350_rtc rtc; struct wm8350_wdt wdt; }

  

    2)系统中的每个驱动程序都有一个device_driver对象描述:

struct device_driver { const char *name; //设备驱动程序的名称 struct bus_type *bus; //总线 struct module *owner; const char *mod_name; /* used for built-in modules */ int (*probe) (struct device *dev); //设备的探测函数 int (*remove) (struct device *dev); void (*shutdown) (struct device *dev); int (*suspend) (struct device *dev, pm_message_t state); int (*resume) (struct device *dev); struct attribute_group **groups; struct driver_private *p; }

 

       3) 系统中的总线用 struct bus_type 表示

 struct bus_type { const char *name; //总线名称 struct bus_attribute *bus_attrs; //总线属性 struct device_attribute *dev_attrs; //设备属性 struct driver_attribute *drv_attrs; //设备驱动程序属性 int (*match)(struct device *dev, struct device_driver *drv); int (*uevent)(struct device *dev, struct kobj_uevent_env *env); int (*probe)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*suspend_late)(struct device *dev, pm_message_t state); int (*resume_early)(struct device *dev); int (*resume)(struct device *dev); struct bus_type_private *p; }

 

      4) 系统中的设备类用 struct class ,表示某一类设备。

       struct class { const char *name; //类名 struct module *owner; struct kset subsys; // struct list_head devices; //class_device 链表 struct list_head interfaces; // class_interface 链表 struct kset class_dirs; struct semaphore sem; /* locks children, devices, interfaces */ struct class_attribute *class_attrs; //类属性 struct device_attribute *dev_attrs; //设备属性 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); //事件 void (*class_release)(struct class *class); void (*dev_release)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); };

        5)  设备类的驱动程序: struct class_device :

        6)  当发生设备加入或者删除时会调用 class_interface 的成员函数:

            struct class_interface { struct list_head node; struct class *class; int (*add_dev) (struct device *, struct class_interface *); void (*remove_dev) (struct device *, struct class_interface *); };

       7) 属性:

          

你可能感兴趣的:(linux,struct,list,Class,interface,linux内核)