Overview
Linux Driver Model里面有抽象出Bus,device和driver。Device和Driver都要归属于某一个bus,bus会根据device或driver的注册来遍历匹配的driver或device。
1. bus_register()主要是在sysfs中bus下创建对应的目录,并初始化bus structure中的一些成员。
2. driver_register()主要是在sysfs对应的bus目录下创建对应的目录,同时遍历bus下的device,通过bus的match function进行匹配,如果bus的 match为NULL,则默认match。然后再看device是否已经有bind driver,如果没有则优先调用bus的probe,如果bus的probe为NULL,则调用driver的probe function,同时把device加入到driver的device list,把driver也赋值给device->drv。
3. device_register()会创建sysfs目录,同时遍历bus下的driver,通过bus的match function进行匹配,如果bus的 match为NULL,则默认match。然后再看device是否已经有bind driver,如果没有则优先调用bus的probe,如果bus的probe为NULL,则调用driver的probe function。同时把device加入到driver的device list,把driver也赋值给device->drv。
Bus
A bus is a channel between the processor and one or more devices. For the purposes of the device model, all devices are connected via a bus, even if it is an internal, virtual, "platform" bus. Buses can plug into each other. A USB controller is usually a PCI device, for example. The device model represents the actual connections between buses and the devices they control. A bus is represented by the bus_type structure. It contains the name, the default attributes, the bus' methods, PM operations, and the driver core's private data.
struct bus_type {
const char *name; //The name of the bus
const char *dev_name; //Used for subsystems to enumerate devices like ("foo%u", dev->id).
struct device *dev_root; //Default device to use as the parent.
int (*match)(struct device *dev, struct device_driver *drv);
int (*probe)(struct device *dev);
const struct dev_pm_ops *pm;
const struct iommu_ops *iommu_ops;
struct subsys_private *p;
...
};
The subsys_private structure is the one that is the actual kobject allowing struct bus_type/class to be statically allocated safely. Nothing outside of the * driver core should ever touch these fields.
struct subsys_private {
struct kset subsys; //the struct kset that defines this subsystem
struct kset *devices_kset; //the subsystem's 'devices' directory
struct list_head interfaces;
struct mutex mutex;
struct kset *drivers_kset;
struct klist klist_devices;
struct klist klist_drivers;
struct blocking_notifier_head bus_notifier;
unsigned int drivers_autoprobe:1;
struct bus_type *bus;
struct kset glue_dirs;
struct class *class;
};
bus_register()
Register a driver-core subsystem. Once we have that, we register the bus with the kobject
infrastructure, then register the children subsystems it has:the devices and drivers that belong to the subsystem.
int bus_register(struct bus_type *bus){
struct subsys_private *priv;
priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
priv->bus = bus;
bus->p = priv;
retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);// set the kset's kobject name
priv->subsys.kobj.kset = bus_kset; //setting the kobject's kset as bus_kset, so the directory will under bus directory in sysfs.
priv->subsys.kobj.ktype = &bus_ktype; //setting the ktype
priv->drivers_autoprobe = 1;
retval = kset_register(&priv->subsys);//初始化kset,同时create dir in sysfs,这是会在sysfs 中bus目录下创建一个新的目录
retval = bus_create_file(bus, &bus_attr_uevent); //在新create的目录下创建uevent文件
priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj);//在目录下创建devices目录
priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj);//创建drivers目录
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); //list initialize
klist_init(&priv->klist_drivers, NULL, NULL); //list init
retval = add_probe_files(bus); //创建probe file
return 0;
}
Device driver
The basic device driver structure.
The device driver-model tracks all of the drivers known to the system.The main reason for this tracking is to enable the driver core to match up drivers with new devices. Once drivers are known objects within the system, however, a number of other things become possible. Device drivers can export information and configuration variables that are independent of any specific device.
struct device_driver {
const char *name; //Name of the device driver
struct bus_type *bus; //The bus which the device of this driver belongs to
struct module *owner; //Type of the probe (synchronous or asynchronous) to use.
enum probe_type probe_type; //Type of the probe (synchronous or asynchronous) to use.
int (*probe) (struct device *dev); //Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device.
struct driver_private *p;
};
struct driver_private {
struct kobject kobj;
struct klist klist_devices;
struct klist_node knode_bus;
struct module_kobject *mkobj;
struct device_driver *driver;
};
driver_register()
Register driver with bus.
int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other;
other = driver_find(drv->name, drv->bus);//Find if a driver already registered by kobject name
if (other) {
return -EBUSY;
}
ret = bus_add_driver(drv);
ret = driver_add_groups(drv, drv->groups);
kobject_uevent(&drv->p->kobj, KOBJ_ADD);
return ret;
}
int bus_add_driver(struct device_driver *drv)
{
struct bus_type *bus;
struct driver_private *priv;
bus = bus_get(drv->bus); //increase the reference count of drv->bus->p->subsys->kobject
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
klist_init(&priv->klist_devices, NULL, NULL); //Initialize devices list
priv->driver = drv;
drv->p = priv;
priv->kobj.kset = bus->p->drivers_kset; //set the driver kobject's kset as the bus drivers_set
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, "%s", drv->name); //Due to the parent is NULL, so the drivers kobject parent is the kset's object which is bus->drivers_set. So create a dir under sysfs bus/*/drivers/
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);//add driver in bus drivers list
if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
}
module_add_driver(drv->owner, drv);
error = driver_create_file(drv, &driver_attr_uevent); //create uevent file under driver directory in sysfs
return 0;
}
int driver_attach(struct device_driver *drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
}
* bus_for_each_dev - device iterator.
* @bus: bus type.
* @start: device to start iterating from.
* @data: data for the callback.
* @fn: function to be called for each device.
int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *, void *))
{
struct klist_iter i;
struct device *dev;
int error = 0;
klist_iter_init_node(&bus->p->klist_devices, &i, (start ? &start->p->knode_bus : NULL));
while ((dev = next_device(&i)) && !error) //iterate the bus device list
error = fn(dev, data); //invoke __driver_attach() function
klist_iter_exit(&i);
return error;
}
static int __driver_attach(struct device *dev, void *data)
{
struct device_driver *drv = data;
if (!driver_match_device(drv, dev))
return 0;
device_lock(dev); //mutex_lock(&dev->mutex)
//这里可以看到只有当device的driver为NULL时,也即是device还没有bind对应的driver时,才会继续执行driver的probe
if (!dev->driver)
driver_probe_device(drv, dev);
device_unlock(dev);
return 0;
}
static inline int driver_match_device(struct device_driver *drv, struct device *dev)
{
//如果driver的bus有match function,那么调用bus的match function,否则默认认为match, //如果不match,则直接跳到下一个device
return drv->bus->match ? drv->bus->match(dev, drv) : 1;
}
//driver_probe_device - attempt to bind device & driver together
int driver_probe_device(struct device_driver *drv, struct device *dev)
{
int ret = 0;
ret = really_probe(dev, drv);
return ret;
}
static int really_probe(struct device *dev, struct device_driver *drv) {
dev->driver = drv;//赋值device driver field
driver_sysfs_add(dev); //在sysfs中创建link file
//如果device的bus有support probe function,那么就调用bus的probe function,如果bus的probe function为NULL,那么就调用driver的probe function
if (dev->bus->probe) {
ret = dev->bus->probe(dev);
} else if (drv->probe) {
ret = drv->probe(dev);
}
driver_bound(dev);
return ret;
}
//将device加入到driver的klist_devices中
static void driver_bound(struct device *dev) {
klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
}
device_register()
struct device {
//The device's "parent" device, the device to which it is attached. * In most cases, a parent //device is some sort of bus or host * controller. If parent is NULL, the device, is a top-level //device, * which is not usually what you want.
struct device *parent;
struct device_private *p;
struct kobject kobj;
const char *init_name; /* initial name of the device */
const struct device_type *type;
struct mutex mutex; /* mutex to synchronize calls to
struct bus_type *bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated thisdevice */
void *driver_data; /* Driver data, set and get with dev_set/get_drvdata */
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;
dev_t devt; /* dev_t, creates the sysfs "dev" */
u32 id; /* device instance */
struct class *class;
};
int device_register(struct device *dev)
{
device_initialize(dev);
return device_add(dev);
}
void device_initialize(struct device *dev)
{
dev->kobj.kset = devices_kset; //sys/devices
kobject_init(&dev->kobj, &device_ktype);
mutex_init(&dev->mutex);
...
}
int device_add(struct device *dev)
{
//set device name
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
/* subsystems can specify simple device enumeration */
if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
bus_probe_device(dev);
}
void bus_probe_device(struct device *dev)
{
struct bus_type *bus = dev->bus;
if (bus->p->drivers_autoprobe)
device_initial_probe(dev);
}
void device_initial_probe(struct device *dev)
{
__device_attach(dev, true);
}
static int __device_attach(struct device *dev, bool allow_async)
{
ret = bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
}
static int __device_attach_driver(struct device_driver *drv, void *_data)
{
struct device_attach_data *data = _data;
struct device *dev = data->dev;
if (!driver_match_device(drv, dev))
return 0;
return driver_probe_device(drv, dev);//又回到driver_register调用的函数去调用probe
}