linux的設備驅動模型,是建立在sysfs和kobject之上的,由總線、設備、驅動、類所組成的關係結構。從本節開始,我們將對linux這一設備驅動模型進行深入分析。
頭文件是include/linux/device.h,實現在drivers/base目錄中。本節要分析的,是其中的設備,主要在core.c中。
struct device {
struct device *parent;
struct device_private *p;
struct kobject kobj;
const char *init_name; /* initial name of the device */
struct device_type *type;
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 *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;
dev_t devt; /* dev_t, creates the sysfs "dev" */
spinlock_t devres_lock;
struct list_head devres_head;
struct klist_node knode_class;
struct class *class;
const struct attribute_group **groups; /* optional groups */
void (*release)(struct device *dev);
};
先來分析下struct device的結構變量。首先是指向父節點的指針parent,kobj是內嵌在device中的kobject,用於把它聯繫到sysfs中。bus是對設備所在總線的指針,driver是對設備所用驅動的指針。還有DMA需要的數據,表示設備號的devt,表示設備資源的devres_head和保護它的devres_lock。指向類的指針class,knode_class是被連入class鏈表時所用的klist節點。group是設備的屬性集合。release應該是設備釋放時調用的函數。
struct device_private {
struct klist klist_children;
struct klist_node knode_parent;
struct klist_node knode_driver;
struct klist_node knode_bus;
void *driver_data;
struct device *device;
};
#define to_device_private_parent(obj) \
container_of(obj, struct device_private, knode_parent)
#define to_device_private_driver(obj) \
container_of(obj, struct device_private, knode_driver)
#define to_device_private_bus(obj) \
container_of(obj, struct device_private, knode_bus)
struct device中有一部分不願意讓外界看到,所以做出struct device_private結構,包括了設備驅動模型內部的鏈接。klist_children是子設備的鏈表,knode_parent是連入父設備的klist_children時所用的節點,knode_driver是連入驅動的設備鏈表所用的節點,knode_bus是連入總線的設備鏈表時所用的節點。driver_data用於在設備結構中存放相關的驅動信息,也許是驅動專門爲設備建立的結構實例。device則是指向struct device_private所屬的device。
下面還有一些宏,to_device_private_parent()是從父設備的klist_children上節點,獲得相應的device_private。to_device_private_driver()是從驅動的設備鏈表上節點,獲得對應的device_private。to_device_private_bus()是從總線的設備鏈表上節點,獲得對應的device_private。
或許會奇怪,爲什麼knode_class沒有被移入struct device_private,或許有外部模塊需要用到它。
/*
* The type of device, "struct device" is embedded in. A class
* or bus can contain devices of different types
* like "partitions" and "disks", "mouse" and "event".
* This identifies the device type and carries type-specific
* information, equivalent to the kobj_type of a kobject.
* If "name" is specified, the uevent will contain it in
* the DEVTYPE variable.
*/
struct device_type {
const char *name;
const struct attribute_group **groups;
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
char *(*devnode)(struct device *dev, mode_t *mode);
void (*release)(struct device *dev);
const struct dev_pm_ops *pm;
};
device竟然有device_type,類似於與kobject相對的kobj_type,之後我們再看它怎麼用。
/* interface for exporting device attributes */
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device *dev, struct device_attribute *attr,
char *buf);
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count);
};
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
這個device_attribute顯然就是device對struct attribute的封裝,新加的show()、store()函數都是以與設備相關的結構調用的。
至於device中其它的archdata、dma、devres,都是作爲設備特有的,我們現在主要關心設備驅動模型的建立,這些會盡量忽略。
下面就來看看device的實現,這主要在core.c中。
int __init devices_init(void)
{
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
if (!devices_kset)
return -ENOMEM;
dev_kobj = kobject_create_and_add("dev", NULL);
if (!dev_kobj)
goto dev_kobj_err;
sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
if (!sysfs_dev_block_kobj)
goto block_kobj_err;
sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
if (!sysfs_dev_char_kobj)
goto char_kobj_err;
return 0;
char_kobj_err:
kobject_put(sysfs_dev_block_kobj);
block_kobj_err:
kobject_put(dev_kobj);
dev_kobj_err:
kset_unregister(devices_kset);
return -ENOMEM;
}
這是在設備驅動模型初始化時調用的device部分初始的函數devices_init()。它乾的事情我們都很熟悉,就是建立sysfs中的devices目錄,和dev目錄。還在dev目錄下又建立了block和char兩個子目錄。因爲dev目錄只打算存放輔助的設備號,所以沒必要使用kset。
static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct device_attribute *dev_attr = to_dev_attr(attr);
struct device *dev = to_dev(kobj);
ssize_t ret = -EIO;
if (dev_attr->show)
ret = dev_attr->show(dev, dev_attr, buf);
if (ret >= (ssize_t)PAGE_SIZE) {
print_symbol("dev_attr_show: %s returned bad count\n",
(unsigned long)dev_attr->show);
}
return ret;
}
static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct device_attribute *dev_attr = to_dev_attr(attr);
struct device *dev = to_dev(kobj);
ssize_t ret = -EIO;
if (dev_attr->store)
ret = dev_attr->store(dev, dev_attr, buf, count);
return ret;
}
static struct sysfs_ops dev_sysfs_ops = {
.show = dev_attr_show,
.store = dev_attr_store,
};
看到這裏是不是很熟悉,dev_sysfs_ops就是device準備註冊到sysfs中的操作函數。dev_attr_show()和dev_attr_store()都會再調用與屬性相關的函數。
static void device_release(struct kobject *kobj)
{
struct device *dev = to_dev(kobj);
struct device_private *p = dev->p;
if (dev->release)
dev->release(dev);
else if (dev->type && dev->type->release)
dev->type->release(dev);
else if (dev->class && dev->class->dev_release)
dev->class->dev_release(dev);
else
WARN(1, KERN_ERR "Device '%s' does not have a release() "
"function, it is broken and must be fixed.\n",
dev_name(dev));
kfree(p);
}
static struct kobj_type device_ktype = {
.release = device_release,
.sysfs_ops = &dev_sysfs_ops,
};
使用的release函數是device_release。在釋放device時,會依次調用device結構中定義的release函數,device_type中定義的release函數,device所屬的class中所定義的release函數,最後會吧device_private結構釋放掉。
static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
{
struct kobj_type *ktype = get_ktype(kobj);
if (ktype == &device_ktype) {
struct device *dev = to_dev(kobj);
if (dev->bus)
return 1;
if (dev->class)
return 1;
}
return 0;
}
static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
{
struct device *dev = to_dev(kobj);
if (dev->bus)
return dev->bus->name;
if (dev->class)
return dev->class->name;
return NULL;
}
static int dev_uevent(struct kset *kset, struct kobject *kobj,
struct kobj_uevent_env *env)
{
struct device *dev = to_dev(kobj);
int retval = 0;
/* add device node properties if present */
if (MAJOR(dev->devt)) {
const char *tmp;
const char *name;
mode_t mode = 0;
add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
name = device_get_devnode(dev, &mode, &tmp);
if (name) {
add_uevent_var(env, "DEVNAME=%s", name);
kfree(tmp);
if (mode)
add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
}
}
if (dev->type && dev->type->name)
add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
if (dev->driver)
add_uevent_var(env, "DRIVER=%s", dev->driver->name);
#ifdef CONFIG_SYSFS_DEPRECATED
if (dev->class) {
struct device *parent = dev->parent;
/* find first bus device in parent chain */
while (parent && !parent->bus)
parent = parent->parent;
if (parent && parent->bus) {
const char *path;
path = kobject_get_path(&parent->kobj, GFP_KERNEL);
if (path) {
add_uevent_var(env, "PHYSDEVPATH=%s", path);
kfree(path);
}
add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
if (parent->driver)
add_uevent_var(env, "PHYSDEVDRIVER=%s",
parent->driver->name);
}
} else if (dev->bus) {
add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
if (dev->driver)
add_uevent_var(env, "PHYSDEVDRIVER=%s",
dev->driver->name);
}
#endif
/* have the bus specific function add its stuff */
if (dev->bus && dev->bus->uevent) {
retval = dev->bus->uevent(dev, env);
if (retval)
pr_debug("device: '%s': %s: bus uevent() returned %d\n",
dev_name(dev), __func__, retval);
}
/* have the class specific function add its stuff */
if (dev->class && dev->class->dev_uevent) {
retval = dev->class->dev_uevent(dev, env);
if (retval)
pr_debug("device: '%s': %s: class uevent() "
"returned %d\n", dev_name(dev),
__func__, retval);
}
/* have the device type specific fuction add its stuff */
if (dev->type && dev->type->uevent) {
retval = dev->type->uevent(dev, env);
if (retval)
pr_debug("device: '%s': %s: dev_type uevent() "
"returned %d\n", dev_name(dev),
__func__, retval);
}
return retval;
}
static struct kset_uevent_ops device_uevent_ops = {
.filter = dev_uevent_filter,
.name = dev_uevent_name,
.uevent = dev_uevent,
};
前面在講到kset時,我們並未關注其中的kset_event_ops結構變量。但這裏device既然用到了,我們就對其中的三個函數做簡單介紹。kset_uevent_ops中的函數是用於管理kset內部kobject的uevent操作。其中filter函數用於阻止一個kobject向用戶空間發送uevent,返回值爲0表示阻止。這裏dev_uevent_filter()檢查device所屬的bus或者class是否存在,如果都不存在,也就沒有發送uevent的必要了。name函數是用於覆蓋kset發送給用戶空間的名稱。這裏dev_uevent_name()選擇使用bus或者class的名稱。uevent()函數是在uevent將被髮送到用戶空間之前調用的,用於向uevent中增加新的環境變量。dev_uevent()的實現很熱鬧,向uevent中添加了各種環境變量。
static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct kobject *top_kobj;
struct kset *kset;
struct kobj_uevent_env *env = NULL;
int i;
size_t count = 0;
int retval;
/* search the kset, the device belongs to */
top_kobj = &dev->kobj;
while (!top_kobj->kset && top_kobj->parent)
top_kobj = top_kobj->parent;
if (!top_kobj->kset)
goto out;
kset = top_kobj->kset;
if (!kset->uevent_ops || !kset->uevent_ops->uevent)
goto out;
/* respect filter */
if (kset->uevent_ops && kset->uevent_ops->filter)
if (!kset->uevent_ops->filter(kset, &dev->kobj))
goto out;
env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
if (!env)
return -ENOMEM;
/* let the kset specific function add its keys */
retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
if (retval)
goto out;
/* copy keys to file */
for (i = 0; i < env->envp_idx; i++)
count += sprintf(&buf[count], "%s\n", env->envp[i]);
out:
kfree(env);
return count;
}
static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
enum kobject_action action;
if (kobject_action_type(buf, count, &action) == 0) {
kobject_uevent(&dev->kobj, action);
goto out;
}
dev_err(dev, "uevent: unsupported action-string; this will "
"be ignored in a future kernel version\n");
kobject_uevent(&dev->kobj, KOBJ_ADD);
out:
return count;
}
static struct device_attribute uevent_attr =
__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
device不僅在kset中添加了對uevent的管理,而且還把uevent信息做成設備的一個屬性uevent。其中show_event()是顯示uevent中環境變量的,store_uevent()是發送uevent的。
static int device_add_attributes(struct device *dev,
struct device_attribute *attrs)
{
int error = 0;
int i;
if (attrs) {
for (i = 0; attr_name(attrs[i]); i++) {
error = device_create_file(dev, &attrs[i]);
if (error)
break;
}
if (error)
while (--i >= 0)
device_remove_file(dev, &attrs[i]);
}
return error;
}
static void device_remove_attributes(struct device *dev,
struct device_attribute *attrs)
{
int i;
if (attrs)
for (i = 0; attr_name(attrs[i]); i++)
device_remove_file(dev, &attrs[i]);
}
static int device_add_groups(struct device *dev,
const struct attribute_group **groups)
{
int error = 0;
int i;
if (groups) {
for (i = 0; groups[i]; i++) {
error = sysfs_create_group(&dev->kobj, groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(&dev->kobj,
groups[i]);
break;
}
}
}
return error;
}
static void device_remove_groups(struct device *dev,
const struct attribute_group **groups)
{
int i;
if (groups)
for (i = 0; groups[i]; i++)
sysfs_remove_group(&dev->kobj, groups[i]);
}
以上四個內部函數是用來向device中添加或刪除屬性與屬性集合的。
device_add_attributes、device_remove_attributes、device_add_groups、device_remove_groups,都是直接通過sysfs提供的API實現。
static int device_add_attrs(struct device *dev)
{
struct class *class = dev->class;
struct device_type *type = dev->type;
int error;
if (class) {
error = device_add_attributes(dev, class->dev_attrs);
if (error)
return error;
}
if (type) {
error = device_add_groups(dev, type->groups);
if (error)
goto err_remove_class_attrs;
}
error = device_add_groups(dev, dev->groups);
if (error)
goto err_remove_type_groups;
return 0;
err_remove_type_groups:
if (type)
device_remove_groups(dev, type->groups);
err_remove_class_attrs:
if (class)
device_remove_attributes(dev, class->dev_attrs);
return error;
}
static void device_remove_attrs(struct device *dev)
{
struct class *class = dev->class;
struct device_type *type = dev->type;
device_remove_groups(dev, dev->groups);
if (type)
device_remove_groups(dev, type->groups);
if (class)
device_remove_attributes(dev, class->dev_attrs);
}
device_add_attrs()實際負責device中的屬性添加。也是幾個部分的集合,包括class中的dev_attrs,device_type中的groups,還有device本身的groups。
device_remove_attrs()則負責對應的device屬性刪除工作。
#define print_dev_t(buffer, dev) \
sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
char *buf)
{
return print_dev_t(buf, dev->devt);
}
static struct device_attribute devt_attr =
__ATTR(dev, S_IRUGO, show_dev, NULL);
這裏又定義了一個名爲dev的屬性,就是顯示設備的設備號。
/**
* device_create_file - create sysfs attribute file for device.
* @dev: device.
* @attr: device attribute descriptor.
*/
int device_create_file(struct device *dev, struct device_attribute *attr)
{
int error = 0;
if (dev)
error = sysfs_create_file(&dev->kobj, &attr->attr);
return error;
}
/**
* device_remove_file - remove sysfs attribute file.
* @dev: device.
* @attr: device attribute descriptor.
*/
void device_remove_file(struct device *dev, struct device_attribute *attr)
{
if (dev)
sysfs_remove_file(&dev->kobj, &attr->attr);
}
/**
* device_create_bin_file - create sysfs binary attribute file for device.
* @dev: device.
* @attr: device binary attribute descriptor.
*/
int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
{
int error = -EINVAL;
if (dev)
error = sysfs_create_bin_file(&dev->kobj, attr);
return error;
}
/**
* device_remove_bin_file - remove sysfs binary attribute file
* @dev: device.
* @attr: device binary attribute descriptor.
*/
void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
{
if (dev)
sysfs_remove_bin_file(&dev->kobj, attr);
}
int device_schedule_callback_owner(struct device *dev,
void (*func)(struct device *), struct module *owner)
{
return sysfs_schedule_callback(&dev->kobj,
(void (*)(void *)) func, dev, owner);
}
這裏的五個函數,也是對sysfs提供的API的簡單封裝。
device_create_file()和device_remove_file()提供直接的屬性文件管理方法。
device_create_bin_file()和device_remove_bin_file()則是提供設備管理二進制文件的方法。
device_schedule_callback_owner()也是簡單地將func加入工作隊列。
static void klist_children_get(struct klist_node *n)
{
struct device_private *p = to_device_private_parent(n);
struct device *dev = p->device;
get_device(dev);
}
static void klist_children_put(struct klist_node *n)
{
struct device_private *p = to_device_private_parent(n);
struct device *dev = p->device;
put_device(dev);
}
如果之前認真看過klist的實現,應該知道,klist_children_get()和klist_children_put()就是在設備掛入和刪除父設備的klist_children鏈表時調用的函數。在父設備klist_children鏈表上的指針,相當於對device的一個引用計數。
struct device *get_device(struct device *dev)
{
return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
}
/**
* put_device - decrement reference count.
* @dev: device in question.
*/
void put_device(struct device *dev)
{
/* might_sleep(); */
if (dev)
kobject_put(&dev->kobj);
}
device中的引用計數,完全交給內嵌的kobject來做。如果引用計數降爲零,自然是調用之前說到的包含甚廣的device_release函數。
void device_initialize(struct device *dev)
{
dev->kobj.kset = devices_kset;
kobject_init(&dev->kobj, &device_ktype);
INIT_LIST_HEAD(&dev->dma_pools);
init_MUTEX(&dev->sem);
spin_lock_init(&dev->devres_lock);
INIT_LIST_HEAD(&dev->devres_head);
device_init_wakeup(dev, 0);
device_pm_init(dev);
set_dev_node(dev, -1);
}
device_initialize()就是device結構的初始化函數,它把device中能初始化的部分全初始化。它的界限在其中kobj的位置與device在設備驅動模型中的位置,這些必須由外部設置。可以看到,調用kobject_init()時,object的kobj_type選擇了device_ktype,其中主要是sysops的兩個函數,還有device_release函數。
static struct kobject *virtual_device_parent(struct device *dev)
{
static struct kobject *virtual_dir = NULL;
if (!virtual_dir)
virtual_dir = kobject_create_and_add("virtual",
&devices_kset->kobj);
return virtual_dir;
}
static struct kobject *get_device_parent(struct device *dev,
struct device *parent)
{
int retval;
if (dev->class) {
struct kobject *kobj = NULL;
struct kobject *parent_kobj;
struct kobject *k;
/*
* If we have no parent, we live in "virtual".
* Class-devices with a non class-device as parent, live
* in a "glue" directory to prevent namespace collisions.
*/
if (parent == NULL)
parent_kobj = virtual_device_parent(dev);
else if (parent->class)
return &parent->kobj;
else
parent_kobj = &parent->kobj;
/* find our class-directory at the parent and reference it */
spin_lock(&dev->class->p->class_dirs.list_lock);
list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
if (k->parent == parent_kobj) {
kobj = kobject_get(k);
break;
}
spin_unlock(&dev->class->p->class_dirs.list_lock);
if (kobj)
return kobj;
/* or create a new class-directory at the parent device */
k = kobject_create();
if (!k)
return NULL;
k->kset = &dev->class->p->class_dirs;
retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
if (retval < 0) {
kobject_put(k);
return NULL;
}
/* do not emit an uevent for this simple "glue" directory */
return k;
}
if (parent)
return &parent->kobj;
return NULL;
}
這裏的get_device_parent()就是獲取父節點的kobject,但也並非就如此簡單。get_device_parent()的返回值直接決定了device將被掛在哪個目錄下。到底該掛在哪,是由dev->class、dev->parent、dev->parent->class等因素綜合決定的。我們看get_device_parent()中是如何判斷的。如果dev->class爲空,表示一切隨父設備,有parent則返回parent->kobj,沒有則返回NULL。如果有dev->class呢,情況就比較複雜了,也許device有着與parent不同的class,也許device還沒有一個parent,等等。我們看具體的情況。如果parent不爲空,而且存在parent->class,則還放在parent目錄下。不然,要麼parent不存在,要麼parent沒有class,很難直接將有class的device放在parent下面。目前的解決方法很簡單,在parent與device之間,再加一層表示class的目錄。如果parent都沒有,那就把/sys/devices/virtual當做parent。class->p->class_dirs就是專門存放這種中間kobject的kset。思路理清後,再結合實際的sysfs,代碼就很容易看懂了。
static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
{
/* see if we live in a "glue" directory */
if (!glue_dir || !dev->class ||
glue_dir->kset != &dev->class->p->class_dirs)
return;
kobject_put(glue_dir);
}
static void cleanup_device_parent(struct device *dev)
{
cleanup_glue_dir(dev, dev->kobj.parent);
}
cleanup_device_parent()是取消對parent引用時調用的函數,看起來只針對這種glue形式的目錄起作用。
static void setup_parent(struct device *dev, struct device *parent)
{
struct kobject *kobj;
kobj = get_device_parent(dev, parent);
if (kobj)
dev->kobj.parent = kobj;
}
setup_parent()就是調用get_device_parent()獲得應該存放的父目錄kobj,並把dev->kobj.parent設爲它。
static int device_add_class_symlinks(struct device *dev)
{
int error;
if (!dev->class)
return 0;
error = sysfs_create_link(&dev->kobj,
&dev->class->p->class_subsys.kobj,
"subsystem");
if (error)
goto out;
/* link in the class directory pointing to the device */
error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
&dev->kobj, dev_name(dev));
if (error)
goto out_subsys;
if (dev->parent && device_is_not_partition(dev)) {
error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
"device");
if (error)
goto out_busid;
}
return 0;
out_busid:
sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
out_subsys:
sysfs_remove_link(&dev->kobj, "subsystem");
out:
return error;
}
device_add_class_symlinks()在device和class直接添加一些軟鏈接。在device目錄下創建指向class的subsystem文件,在class目錄下創建指向device的同名文件。如果device有父設備,而且device不是塊設備分區,則在device目錄下建立一個指向父設備的device鏈接文件。這一點在usb設備和usb接口間很常見。
static void device_remove_class_symlinks(struct device *dev)
{
if (!dev->class)
return;
#ifdef CONFIG_SYSFS_DEPRECATED
if (dev->parent && device_is_not_partition(dev)) {
char *class_name;
class_name = make_class_name(dev->class->name, &dev->kobj);
if (class_name) {
sysfs_remove_link(&dev->parent->kobj, class_name);
kfree(class_name);
}
sysfs_remove_link(&dev->kobj, "device");
}
if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
device_is_not_partition(dev))
sysfs_remove_link(&dev->class->p->class_subsys.kobj,
dev_name(dev));
#else
if (dev->parent && device_is_not_partition(dev))
sysfs_remove_link(&dev->kobj, "device");
sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
#endif
sysfs_remove_link(&dev->kobj, "subsystem");
}
device_remove_class_symlinks()刪除device和class之間的軟鏈接。
static inline const char *dev_name(const struct device *dev)
{
return kobject_name(&dev->kobj);
}
int dev_set_name(struct device *dev, const char *fmt, ...)
{
va_list vargs;
int err;
va_start(vargs, fmt);
err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
va_end(vargs);
return err;
}
dev_name()獲得設備名稱,dev_set_name()設置設備名稱。但這裏的dev_set_name()只能在設備未註冊前使用。device的名稱其實是完全靠dev->kobj管理的。
static struct kobject *device_to_dev_kobj(struct device *dev)
{
struct kobject *kobj;
if (dev->class)
kobj = dev->class->dev_kobj;
else
kobj = sysfs_dev_char_kobj;
return kobj;
}
device_to_dev_kobj()爲dev選擇合適的/sys/dev下的kobject,或者是塊設備,或者是字符設備,或者沒有。
#define format_dev_t(buffer, dev) \
({ \
sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev)); \
buffer; \
})
static int device_create_sys_dev_entry(struct device *dev)
{
struct kobject *kobj = device_to_dev_kobj(dev);
int error = 0;
char devt_str[15];
if (kobj) {
format_dev_t(devt_str, dev->devt);
error = sysfs_create_link(kobj, &dev->kobj, devt_str);
}
return error;
}
static void device_remove_sys_dev_entry(struct device *dev)
{
struct kobject *kobj = device_to_dev_kobj(dev);
char devt_str[15];
if (kobj) {
format_dev_t(devt_str, dev->devt);
sysfs_remove_link(kobj, devt_str);
}
}
device_create_sys_dev_entry()是在/sys/dev相應的目錄下建立對設備的軟鏈接。先是通過device_to_dev_kobj()獲得父節點的kobj,然後調用sysfs_create_link()建立軟鏈接。
device_remove_sys_dev_entry()與其操作正相反,刪除在/sys/dev下建立的軟鏈接。
int device_private_init(struct device *dev)
{
dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
if (!dev->p)
return -ENOMEM;
dev->p->device = dev;
klist_init(&dev->p->klist_children, klist_children_get,
klist_children_put);
return 0;
}
device_private_init()分配並初始化dev->p。至於空間的釋放,是等到釋放設備時調用的device_release()中。
之前的函數比較散亂,或許找不出一個整體的印象。但下面馬上就要看到重要的部分了,因爲代碼終於攢到了爆發的程度!
/**
* device_register - register a device with the system.
* @dev: pointer to the device structure
*
* This happens in two clean steps - initialize the device
* and add it to the system. The two steps can be called
* separately, but this is the easiest and most common.
* I.e. you should only call the two helpers separately if
* have a clearly defined need to use and refcount the device
* before it is added to the hierarchy.
*
* NOTE: _Never_ directly free @dev after calling this function, even
* if it returned an error! Always use put_device() to give up the
* reference initialized in this function instead.
*/
int device_register(struct device *dev)
{
device_initialize(dev);
return device_add(dev);
}
device_register()是提供給外界註冊設備的接口。它先是調用device_initialize()初始化dev結構,然後調用device_add()將其加入系統中。但要注意,在調用device_register()註冊dev之前,有一些dev結構變量是需要自行設置的。這其中有指明設備位置的struct device *parent,struct bus_type *bus, struct class *class,有指明設備屬性的 const char *init_name, struct device_type *type, const struct attribute_group **groups, void (*release)(struct device *dev), dev_t devt,等等。不同設備的使用方法不同,我們留待之後再具體分析。device_initialize()我們已經看過,下面重點看看device_add()是如何實現的。
int device_add(struct device *dev)
{
struct device *parent = NULL;
struct class_interface *class_intf;
int error = -EINVAL;
dev = get_device(dev);
if (!dev)
goto done;
if (!dev->p) {
error = device_private_init(dev);
if (error)
goto done;
}
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
if (!dev_name(dev))
goto name_error;
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
parent = get_device(dev->parent);
setup_parent(dev, parent);
/* use parent numa_node */
if (parent)
set_dev_node(dev, dev_to_node(parent));
/* first, register with generic layer. */
/* we require the name to be set before, and pass NULL */
error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
if (error)
goto Error;
/* notify platform of device entry */
if (platform_notify)
platform_notify(dev);
error = device_create_file(dev, &uevent_attr);
if (error)
goto attrError;
if (MAJOR(dev->devt)) {
error = device_create_file(dev, &devt_attr);
if (error)
goto ueventattrError;
error = device_create_sys_dev_entry(dev);
if (error)
goto devtattrError;
devtmpfs_create_node(dev);
}
error = device_add_class_symlinks(dev);
if (error)
goto SymlinkError;
error = device_add_attrs(dev);
if (error)
goto AttrsError;
error = bus_add_device(dev);
if (error)
goto BusError;
error = dpm_sysfs_add(dev);
if (error)
goto DPMError;
device_pm_add(dev);
/* Notify clients of device addition. This call must come
* after dpm_sysf_add() and before kobject_uevent().
*/
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_ADD_DEVICE, dev);
kobject_uevent(&dev->kobj, KOBJ_ADD);
bus_probe_device(dev);
if (parent)
klist_add_tail(&dev->p->knode_parent,
&parent->p->klist_children);
if (dev->class) {
mutex_lock(&dev->class->p->class_mutex);
/* tie the class to the device */
klist_add_tail(&dev->knode_class,
&dev->class->p->class_devices);
/* notify any interfaces that the device is here */
list_for_each_entry(class_intf,
&dev->class->p->class_interfaces, node)
if (class_intf->add_dev)
class_intf->add_dev(dev, class_intf);
mutex_unlock(&dev->class->p->class_mutex);
}
done:
put_device(dev);
return error;
DPMError:
bus_remove_device(dev);
BusError:
device_remove_attrs(dev);
AttrsError:
device_remove_class_symlinks(dev);
SymlinkError:
if (MAJOR(dev->devt))
device_remove_sys_dev_entry(dev);
devtattrError:
if (MAJOR(dev->devt))
device_remove_file(dev, &devt_attr);
ueventattrError:
device_remove_file(dev, &uevent_attr);
attrError:
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
kobject_del(&dev->kobj);
Error:
cleanup_device_parent(dev);
if (parent)
put_device(parent);
name_error:
kfree(dev->p);
dev->p = NULL;
goto done;
}
device_add()將dev加入設備驅動模型。它先是調用get_device(dev)增加dev的引用計數,然後調用device_private_init()分配和初始化dev->p,調用dev_set_name()設置dev名字。然後是準備將dev加入sysfs,先是用get_device(parent)增加對parent的引用計數(無論是直接掛在parent下還是通過一個類層掛在parent下都要增加parent的引用計數),然後調用setup_parent()找到實際要加入的父kobject,通過kobject_add()加入其下。然後是添加屬性和屬性集合的操作,調用device_create_file()添加uevent屬性,調用device_add_attrs()添加device/type/class預定義的屬性與屬性集合。如果dev有被分配設備號,再用device_create_file()添加dev屬性,並用device_create_sys_dev_entry()在/sys/dev下添加相應的軟鏈接,最後調用devtmpfs_create_node()在/dev下創建相應的設備文件。然後調用device_add_class_symlinks()添加dev與class間的軟鏈接,調用bus_add_device()添加dev與bus間的軟鏈接,並將dev掛入bus的設備鏈表。調用dpm_sysfs_add()增加dev下的power屬性集合,調用device_pm_add()將dev加入dpm_list鏈表。
調用kobject_uevent()發佈KOBJ_ADD消息,調用bus_probe_device()爲dev尋找合適的驅動。如果有parent節點,把dev->p->knode_parent掛入parent->p->klist_children鏈表。如果dev有所屬的class,將dev->knode_class掛在class->p->class_devices上,並調用可能的類設備接口的add_dev()方法。可能對於直接在bus上的設備來說,自然可以調用bus_probe_device()查找驅動,而不與總線直接接觸的設備,則要靠class來發現驅動,這裏的class_interface中的add_dev()方法,就是一個絕好的機會。最後會調用put_device(dev)釋放在函數開頭增加的引用計數。
device_add()要做的事很多,但想想每件事都在情理之中。device是設備驅動模型的基本元素,在class、bus、dev、devices中都有它的身影。device_add()要適應各種類型的設備註冊,自然會越來越複雜。可以說文件開頭定義的內部函數,差不多都是爲了這裏服務的。
void device_unregister(struct device *dev)
{
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
device_del(dev);
put_device(dev);
}
有註冊自然又註銷。device_unregister()就是用於將dev從系統中註銷,並釋放創建時產生的引用計數。
void device_del(struct device *dev)
{
struct device *parent = dev->parent;
struct class_interface *class_intf;
/* Notify clients of device removal. This call must come
* before dpm_sysfs_remove().
*/
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_DEL_DEVICE, dev);
device_pm_remove(dev);
dpm_sysfs_remove(dev);
if (parent)
klist_del(&dev->p->knode_parent);
if (MAJOR(dev->devt)) {
devtmpfs_delete_node(dev);
device_remove_sys_dev_entry(dev);
device_remove_file(dev, &devt_attr);
}
if (dev->class) {
device_remove_class_symlinks(dev);
mutex_lock(&dev->class->p->class_mutex);
/* notify any interfaces that the device is now gone */
list_for_each_entry(class_intf,
&dev->class->p->class_interfaces, node)
if (class_intf->remove_dev)
class_intf->remove_dev(dev, class_intf);
/* remove the device from the class list */
klist_del(&dev->knode_class);
mutex_unlock(&dev->class->p->class_mutex);
}
device_remove_file(dev, &uevent_attr);
device_remove_attrs(dev);
bus_remove_device(dev);
/*
* Some platform devices are driven without driver attached
* and managed resources may have been acquired. Make sure
* all resources are released.
*/
devres_release_all(dev);
/* Notify the platform of the removal, in case they
* need to do anything...
*/
if (platform_notify_remove)
platform_notify_remove(dev);
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cleanup_device_parent(dev);
kobject_del(&dev->kobj);
put_device(parent);
}
device_del()是與device_add()相對的函數,進行實際的將dev從系統中脫離的工作。這其中既有將dev從設備驅動模型各種鏈表中脫離的工作,又有將dev從sysfs的各個角落刪除的工作。大致流程與dev_add()相對,就不一一介紹。
爆發結束,下面來看一些比較輕鬆的函數。
/**
* device_get_devnode - path of device node file
* @dev: device
* @mode: returned file access mode
* @tmp: possibly allocated string
*
* Return the relative path of a possible device node.
* Non-default names may need to allocate a memory to compose
* a name. This memory is returned in tmp and needs to be
* freed by the caller.
*/
const char *device_get_devnode(struct device *dev,
mode_t *mode, const char **tmp)
{
char *s;
*tmp = NULL;
/* the device type may provide a specific name */
if (dev->type && dev->type->devnode)
*tmp = dev->type->devnode(dev, mode);
if (*tmp)
return *tmp;
/* the class may provide a specific name */
if (dev->class && dev->class->devnode)
*tmp = dev->class->devnode(dev, mode);
if (*tmp)
return *tmp;
/* return name without allocation, tmp == NULL */
if (strchr(dev_name(dev), '!') == NULL)
return dev_name(dev);
/* replace '!' in the name with '/' */
*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
if (!*tmp)
return NULL;
while ((s = strchr(*tmp, '!')))
s[0] = '/';
return *tmp;
}
device_get_devnode()返回設備的路徑名。不過似乎可以由device_type或者class定義一些獨特的返回名稱。
static struct device *next_device(struct klist_iter *i)
{
struct klist_node *n = klist_next(i);
struct device *dev = NULL;
struct device_private *p;
if (n) {
p = to_device_private_parent(n);
dev = p->device;
}
return dev;
}
int device_for_each_child(struct device *parent, void *data,
int (*fn)(struct device *dev, void *data))
{
struct klist_iter i;
struct device *child;
int error = 0;
if (!parent->p)
return 0;
klist_iter_init(&parent->p->klist_children, &i);
while ((child = next_device(&i)) && !error)
error = fn(child, data);
klist_iter_exit(&i);
return error;
}
struct device *device_find_child(struct device *parent, void *data,
int (*match)(struct device *dev, void *data))
{
struct klist_iter i;
struct device *child;
if (!parent)
return NULL;
klist_iter_init(&parent->p->klist_children, &i);
while ((child = next_device(&i)))
if (match(child, data) && get_device(child))
break;
klist_iter_exit(&i);
return child;
}
device_for_each_child()對dev下的每個子device,都調用一遍特定的處理函數。
device_find_child()則是查找dev下特點的子device,查找使用特定的match函數。
這兩個遍歷過程都使用了klist特有的遍歷函數,支持遍歷過程中的節點刪除等功能。next_device()則是爲了遍歷方便封裝的一個內部函數。
下面本該是root_device註冊相關的代碼。但經過檢查,linux內核中使用到的root_device很少見,而且在sysfs中也未能找到一個實際的例子。所以root_device即使還未被棄用,也並非主流,我們將其跳過。
與kobject和kset類似,device也爲我們提供了快速device創建方法,下面就看看吧。
static void device_create_release(struct device *dev)
{
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
kfree(dev);
}
struct device *device_create_vargs(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt,
va_list args)
{
struct device *dev = NULL;
int retval = -ENODEV;
if (class == NULL || IS_ERR(class))
goto error;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) {
retval = -ENOMEM;
goto error;
}
dev->devt = devt;
dev->class = class;
dev->parent = parent;
dev->release = device_create_release;
dev_set_drvdata(dev, drvdata);
retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
if (retval)
goto error;
retval = device_register(dev);
if (retval)
goto error;
return dev;
error:
put_device(dev);
return ERR_PTR(retval);
}
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
{
va_list vargs;
struct device *dev;
va_start(vargs, fmt);
dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
va_end(vargs);
return dev;
}
這裏的device_create()提供了一個快速的dev創建註冊方法。只是中間沒有提供設置device_type的方法,或許是這樣的device已經夠特立獨行了,不需要搞出一類來。
static int __match_devt(struct device *dev, void *data)
{
dev_t *devt = data;
return dev->devt == *devt;
}
void device_destroy(struct class *class, dev_t devt)
{
struct device *dev;
dev = class_find_device(class, NULL, &devt, __match_devt);
if (dev) {
put_device(dev);
device_unregister(dev);
}
}
device_destroy()就是與device_create()相對的註銷函數。至於這裏爲什麼會多一個put_device(dev),也很簡單,因爲在class_find_device()找到dev時,調用了get_device()。
struct device *class_find_device(struct class *class, struct device *start,
void *data,
int (*match)(struct device *, void *))
{
struct class_dev_iter iter;
struct device *dev;
if (!class)
return NULL;
if (!class->p) {
WARN(1, "%s called for class '%s' before it was initialized",
__func__, class->name);
return NULL;
}
class_dev_iter_init(&iter, class, start, NULL);
while ((dev = class_dev_iter_next(&iter))) {
if (match(dev, data)) {
get_device(dev);
break;
}
}
class_dev_iter_exit(&iter);
return dev;
}
class_find_device()本來是class.c中的內容,其實現也於之前將的遍歷dev->p->klist_children類似,無非是在klist提供的遍歷方法上加以封裝。但我們這裏列出class_find_device()的實現與使用它的device_destroy(),卻是爲了更好地分析這個調用流程中dev是如何被保護的。它實際上是經歷了三個保護手段:首先在class_dev_iter_next()->klist_next()中,是受到struct klist中 spinlock_t k_lock保護的。在找到下一點並解鎖之前,就增加了struct klist_node中的struct kref n_ref引用計數。在當前的next()調用完,到下一個next()調用之前,都是受這個增加的引用計數保護的。再看class_find_device()中,使用get_device(dev)增加了dev本身的引用計數保護(當然也要追溯到kobj->kref中),這是第三種保護。知道device_destroy()中主動調用put_device(dev)纔去除了這種保護。
本來對dev的保護,應該完全是由dev中的引用計數完成的。但實際上這種保護很多時候是間接完成的。例如這裏的klist中的自旋鎖,klist_node中的引用計數,都不過是爲了保持class的設備鏈表中對dev的引用計數不消失,這是一種間接保護的手段,保證了這中間即使外界主動釋放class設備鏈表對dev的引用計數,dev仍然不會被實際註銷。這種曲折的聯繫,才真正發揮了引用計數的作用,構成設備驅動模型獨特的魅力。
int device_rename(struct device *dev, char *new_name)
{
char *old_device_name = NULL;
int error;
dev = get_device(dev);
if (!dev)
return -EINVAL;
pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
__func__, new_name);
old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
if (!old_device_name) {
error = -ENOMEM;
goto out;
}
error = kobject_rename(&dev->kobj, new_name);
if (error)
goto out;
if (dev->class) {
error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
&dev->kobj, dev_name(dev));
if (error)
goto out;
sysfs_remove_link(&dev->class->p->class_subsys.kobj,
old_device_name);
}
out:
put_device(dev);
kfree(old_device_name);
return error;
}
device_rename()是供設備註冊後改變名稱用的,除了改變/sys/devices下地名稱,還改變了/sys/class下地軟鏈接名稱。前者很自然,但後者卻很難想到。即使簡單的地方,經過重重調試,我們也會驚訝於linux的心細如髮。
static int device_move_class_links(struct device *dev,
struct device *old_parent,
struct device *new_parent)
{
int error = 0;
if (old_parent)
sysfs_remove_link(&dev->kobj, "device");
if (new_parent)
error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
"device");
return error;
#endif
}
device_move_class_links()只是一個內部函數,後面還有操縱它的那隻手。這裏的device_move_class_links顯得很名不副實,並沒用操作class中軟鏈接的舉動。這很正常,因爲在sysfs中軟鏈接是針對kobject來說的,所以即使位置變掉了,軟鏈接還是很很準確地定位。
/**
* device_move - moves a device to a new parent
* @dev: the pointer to the struct device to be moved
* @new_parent: the new parent of the device (can by NULL)
* @dpm_order: how to reorder the dpm_list
*/
int device_move(struct device *dev, struct device *new_parent,
enum dpm_order dpm_order)
{
int error;
struct device *old_parent;
struct kobject *new_parent_kobj;
dev = get_device(dev);
if (!dev)
return -EINVAL;
device_pm_lock();
new_parent = get_device(new_parent);
new_parent_kobj = get_device_parent(dev, new_parent);
pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
__func__, new_parent ? dev_name(new_parent) : "<NULL>");
error = kobject_move(&dev->kobj, new_parent_kobj);
if (error) {
cleanup_glue_dir(dev, new_parent_kobj);
put_device(new_parent);
goto out;
}
old_parent = dev->parent;
dev->parent = new_parent;
if (old_parent)
klist_remove(&dev->p->knode_parent);
if (new_parent) {
klist_add_tail(&dev->p->knode_parent,
&new_parent->p->klist_children);
set_dev_node(dev, dev_to_node(new_parent));
}
if (!dev->class)
goto out_put;
error = device_move_class_links(dev, old_parent, new_parent);
if (error) {
/* We ignore errors on cleanup since we're hosed anyway... */
device_move_class_links(dev, new_parent, old_parent);
if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
if (new_parent)
klist_remove(&dev->p->knode_parent);
dev->parent = old_parent;
if (old_parent) {
klist_add_tail(&dev->p->knode_parent,
&old_parent->p->klist_children);
set_dev_node(dev, dev_to_node(old_parent));
}
}
cleanup_glue_dir(dev, new_parent_kobj);
put_device(new_parent);
goto out;
}
switch (dpm_order) {
case DPM_ORDER_NONE:
break;
case DPM_ORDER_DEV_AFTER_PARENT:
device_pm_move_after(dev, new_parent);
break;
case DPM_ORDER_PARENT_BEFORE_DEV:
device_pm_move_before(new_parent, dev);
break;
case DPM_ORDER_DEV_LAST:
device_pm_move_last(dev);
break;
}
out_put:
put_device(old_parent);
out:
device_pm_unlock();
put_device(dev);
return error;
}
device_move()就是將dev移到一個新的parent下。但也有可能這個parent是空的。大部分操作圍繞在引用計數上,get_device(),put_device()。而且換了新的parent,到底要加到sysfs中哪個目錄下,還要再調用get_device_parent()研究一下。主要的操作就是kobject_move()和device_move_class_links()。因爲在sysfs中軟鏈接是針對kobject來說的,所以即使位置變掉了,軟鏈接還是很很準確地定位,所以在/sys/dev、/sys/bus、/sys/class中的軟鏈接都不用變,這實在是sysfs的一大優勢。除此之外,device_move()還涉及到電源管理的問題,device移動影響到dev在dpm_list上的位置,我們對此不瞭解,先忽略之。
void device_shutdown(void)
{
struct device *dev, *devn;
list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
kobj.entry) {
if (dev->bus && dev->bus->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->bus->shutdown(dev);
} else if (dev->driver && dev->driver->shutdown) {
dev_dbg(dev, "shutdown\n");
dev->driver->shutdown(dev);
}
}
kobject_put(sysfs_dev_char_kobj);
kobject_put(sysfs_dev_block_kobj);
kobject_put(dev_kobj);
async_synchronize_full();
}
這個device_shutdown()是在系統關閉時才調用的。它動用了很少使用的devices_kset,從而可以遍歷到每個註冊到sysfs上的設備,調用相應的總線或驅動定義的shutdown()函數。提起這個,還是在device_initialize()中將dev->kobj->kset統一設爲devices_kset的。原來設備雖然有不同的parent,但kset還是一樣的。這樣我們就能理解/sys/devices下的頂層設備目錄是怎麼來的,因爲沒用parent,就在調用kobject_add()時將kset->kobj當成了parent,所以會直接掛在頂層目錄下。這樣的目錄大致有pci0000:00、virtual等等。
看完了core.c,我有種明白機器人也是由零件組成的的感覺。linux設備驅動模型的大門已經打開了四分之一。隨着分析的深入,我們大概也會越來越明白linux的良苦用心。