device-driver:bus/kobject/kset

bus

bus是驱动模型的主要一环,衔接了driver和device,特别地,bus负责管理挂载在上面的所有driver和devices, 大部分的情况是: 系统初始化时候,xxx-bus-core注册了一个虚拟总线,比如 i2c总线, 然后soc厂家在这个总线上, 注册自己的i2c-host, 一个host,一个driver,多个client devices.

简略版本的bus-type:

struct bus_type {
	const char		*name;
	const char		*dev_name;
	//common functions
	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	void (*remove)(struct device *dev);
	//power management
	void (*shutdown)(struct device *dev);
	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);	
};

驱动owner一般很少创建自己的bus,系统基本已经造好了轮子, 直接使用即可.
比如下面的i2c总线,是kernel已经创建好的.
soc驱动开发者, 可以调用i2c_add_adapter( )去注册一个自己的i2c host bus.
调用层次:
i2c_add_adapter( )
i2c_register_adapter() ->
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);

struct bus_type i2c_bus_type = {
	.name		= "i2c",
	.match		= i2c_device_match,
	.probe		= i2c_device_probe,
	.remove		= i2c_device_remove,
	.shutdown	= i2c_device_shutdown,
};

下面是一个bus的常规kenrel创建过程:
注册一个device,作为顶层的device, 所有从属于这个bus的设备, 当前的platform_bus device节点都是他们的parent, 即 /sys/devices/platform
同时,注册一个platform到bus目录下, 来作为总线,管理隶属于platform的driver和devices, 即 /sys/bus/platform.

struct device platform_bus = {
	.init_name	= "platform",
};
struct bus_type platform_bus_type = {
	.name		= "platform",
	.match		= platform_match,
	.uevent		= platform_uevent,
	.probe		= platform_probe,
	.remove		= platform_remove,
	.shutdown	= platform_shutdown,
	.pm		= &platform_dev_pm_ops,
};
int __init platform_bus_init(void)
{
	int error;
	error = device_register(&platform_bus);
	error =  bus_register(&platform_bus_type);
	return error;
}

Summary

kobject
#include <linux/kobject.h>
包含文件中包含了对kobject的定义,以及相关的结构和函数。
void kobject_init(struct kobject *kobj);
int kobject_set_name(struct kobject *kobj,const char *format,…);
kobject的初始化函数。

struct kobject *kobject_get(struct kobject *kobj);
void kobject_put(struct kobject *kobj);
管理kobject引用计数的函数。

struct kobj_type *get_ktype(struct kobject *kobj);
对包含kobject的结构类型的描述,使用get_ktype获得与指定kobject相关的
kobjtype.

int kobject_add(struct kobject *kobj);
extern int kobject_register(struct kobject *kobj);
void kobject_del(struct kobject *kobj);
void kobject_unregister(struct kobject *kobj);
kobject add向系统添加kobject,处理kset成员关系,sysfs表述以及产生热插拔事
件。kobject_register函数是kobject_init和kobject_add的组合。使用kobject del
删除一个kobject,或者使用kobject_unregister函数,它是kobject_del和kobject_put
的组合。

kset的初始化和注册函数。
void kset_init(struct kset *kset);
int kset_add(struct kset *kset);
int kset_register(struct kset *kset);
void kset_unregister(struct kset *kset);

sysfs操作

#include <linux/sysfs.h>
包含sysfs声明的包含文件。

  • int sysfs_create_file(struct kobject *kobj,struct attribute *attr);
    int sysfs_remove_file(struct kobject *kobj,struct attribute *attr);
    int sysfs_create_bin file(struct kobject *kobj,struct bin attribute *attr);
    int sysfs_remove_bin_file(struct kobject *kobj,struct bin_attribute *attr);
    int sysfs_create_link(struct kobject *kobj,struct kobject *target,char
    *name):
    void sysfs_remove_link(struct kobject *kobj,char *name);
    添加或删除与kobject相关属性文件的函数。
    总线、设备和驱动程序

  • int bus_register(struct bus_type *bus);
    void bus_unregister(struct bus_type *bus);
    在设备模型中实现总线注册和注销的函数。
    int bus_for_each dev(struct bus_type *bus,struct device *start,void *data,
    int (*fn)(struct device *void *))
    int bus for each drv(struct bus type *bus,struct device driver *start,void *para)
    这些函数分别遍历附属于指定总线的每个设备和驱动程序。
    BUS_ATTR(name,mode,show,store);
    int bus_create_file(struct bus_type *bus,struct bus_attribute *attr);
    void bus_remove_file(struct bus_type *bus,struct bus_attribute *attr);
    使用宏BUS ATTR声明了一个bus_attribute结构,使用上面的两个函数可对
    该结构进行添加和删除。

  • int device_register(struct device *dev);
    void device_unregister(struct device *dev);
    处理设备注册的函数。
    DEVICE_ATTR(name,mode,show,store);
    int device_create_file(struct device *device,struct device_attribute *entry);
    void device_remove_file(struct device *dev,struct device_attribute *attr);
    处理设备属性的宏和函数。

  • int driver_register(struct device_driver *drv);
    void driver_unregister(struct device_driver *drv);
    注册和注销设备驱动程序的函数。
    DRIVER_ATTR(name,mode,show,store);
    int driver_create_file(struct device_driver *drv,struct driver_attribute
    *attr);
    void driver_remove_file(struct device_driver *drv,struct driver_attribute
    *attr);
    管理驱动程序属性的宏和函数。

  • struct class_simple *class_simple_create(struct module *owner,char *name);
    void class_simple_destroy(struct class_simple *cs);
    struct class_device *class_simple_device_add(struct class_simple *cs,dev_t
    devnum,struct device *device,const char *fmt,…);
    void class_simple_device_remove(dev_t dev);
    int class_simple_set_hotplug(struct class_simple *cs,int (*hotplug)(struct
    class_device *dev,char **envp,int num_envp,char *buffer,int
    buffer_size));
  • 实现class_simple接口的函数;它们管理了包含dev属性和其他内容在内的简
    单类人口。
    int class_register(struct class *cls);
    void class_unregister(struct class *cls);
    注册和注销类。
    CLASS_ATTR(name,mode,show,store);
    int class_create_file(struct class *cls,const struct class_attribute *attr);
    void class_remove file(struct class *cls,const struct class_attribute *attr);
    处理类属性的常用宏和函数。
    int class_device_register(struct class_device *cd);
    void class_device_unregister(struct class_device *cd);
    int class_device_rename(struct class_device *cd,char *new_name);
    CLASS_DEVICE_ATTR(name,mode,show,store);
    int class_device_create_file(struct class_device *cls,const struct
    class_deviceattribute *attr);
    void class_device_remove_file(struct class_device *cls,const struct
    class_device_attribute *attr);
    实现类设备接口的函数和宏。
    int class_interface_register(struct class_interface *intf);
    void class_interface_unregister(struct class_interface *intf);
    向类添加(或者删除)接口的函数。

固件

  • #include <linux/firmware.h>
    int request_firmware(const struct firmware **fw,char *name,struct device
    *device);
    int request_firmware nowait(struct module *module,char *name,struct device
    *device, void *context, void (*cont)(const struct firmware *fw,void
    *context));
    void release firmware(struct firmware *fw);
    内核中实现固件加载的接口函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值