1、前言
Linux內核中的設備驅動模型,是建立在sysfs設備文件系統和kobject上的,由總線(bus)、設備(device)、驅動(driver)和類(class)所組成的關系結構,在底層,Linux系統中的每個設備都有一個device結構體的實例,本文將對Linux內核的device結構體以及相關結構進行簡要分析。
2、device結構體
在Linux內核源碼中,struct device結構體的定義在include/linux/device.h中,實現的主要方法在drivers/base/core.c文件中,device結構體的定義如下所示:
struct device { 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 * 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 */ void *driver_data; /* Driver data, set and get with dev_set/get_drvdata */ struct dev_links_info links; struct dev_pm_info power; struct dev_pm_domain *pm_domain; #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN struct irq_domain *msi_domain; #endif #ifdef CONFIG_PINCTRL struct dev_pin_info *pins; #endif #ifdef CONFIG_GENERIC_MSI_IRQ struct list_head msi_list; #endif #ifdef CONFIG_NUMA int numa_node; /* NUMA node this device is close to */ #endif const struct dma_map_ops *dma_ops; 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. */ unsigned long dma_pfn_offset; 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 */ #ifdef CONFIG_DMA_CMA struct cma *cma_area; /* contiguous memory area for dma allocations */ #endif /* arch specific additions */ struct dev_archdata archdata; struct device_node *of_node; /* associated device tree node */ struct fwnode_handle *fwnode; /* firmware device node */ dev_t devt; /* dev_t, creates the sysfs "dev" */ u32 id; /* device instance */ 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 iommu_group *iommu_group; struct iommu_fwspec *iommu_fwspec; bool offline_disabled:1; bool offline:1; bool of_node_reused:1; };
部分結構體成員解釋:
parent:指向設備的“父”設備,它所連接的設備,在大多數情況下,父設備是某種總線或主機控制器,如果該成員為NULL,則該設備為頂級設備;
p:用於保存設備驅動核心部分的私有數據;
kobj:嵌入的struct kobject對象實例;
init_name:設備的初始名稱
type:設備的類型,用於標識設備類型並攜帶特定類型信息;
mutex:用於同步的互斥鎖;
bus:該設備所處於的總線;
driver:該設備所分配的驅動程序;
platform_data:設備中特定的平台數據;
driver_data:指向驅動程序特定的私有數據;
of_node:與設備數相聯系的結構體指針;
devt:用於表示設備的設備號;
devres_lock:保護設備資源的自旋鎖;
devres_head:設備資源的雙向鏈表頭;
knode_class:接入class鏈表時所需要的klist節點;
class:指向設備所屬class的指針;
groups:該設備的屬性集合;
release:函數指針,當設備需要釋放時調用此函數。
device結構體中有一部分成員不願意被外界看到,所以抽象出了struct device_private這個結構體,該結構體包括了設備驅動模型內部的鏈接,結構體定義如下:
struct device_private { struct klist klist_children; struct klist_node knode_parent; struct klist_node knode_driver; struct klist_node knode_bus; struct list_head deferred_probe; struct device *device; };
部分結構體成員解釋:
klist_children:子設備的klist鏈表;
knode_parent:接入父設備的klist_children時所需要的klist節點;
knode_driver:接入驅動的設備鏈表時所需要的klist節點;
knode_bus:接入總線的設備鏈表時所需要的klist節點;
device:回指struct device結構體的指針。
device結構體中包含了一個struct device_type結構體的指針,用於描述設備的類型,該結構體定義如下:
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, umode_t *mode, kuid_t *uid, kgid_t *gid); void (*release)(struct device *dev); const struct dev_pm_ops *pm; };
該結構體功能類似於kobj_type。
還有一個設備屬性結構體,名稱為struct device_attribute,是對struct attribute的進一步封裝,並提供了設備屬性的讀寫函數指針,結構體定義如下:
/* 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); };
其它的一些struct device結構體成員,例如archdata、dma和devres等,是一些設備特有的東西,暫時不討論,本文主要關心設備驅動模型的基本建立。
3、device的實現
接下來對device的實現進行簡單分析,實現的方法主要在文件core.c中:
int __init devices_init(void) { devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);///sys/devices目錄 if (!devices_kset) return -ENOMEM; dev_kobj = kobject_create_and_add("dev", NULL);///sys/dev目錄 if (!dev_kobj) goto dev_kobj_err; sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);///sys/dev/block目錄 if (!sysfs_dev_block_kobj) goto block_kobj_err; sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);///sys/dev/char目錄 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; }
devices_init()函數是在設備驅動模型初始化時調用的部分初始函數,它實現的功能是建立sysfs中的devices目錄和dev目錄,然后在dev目錄下建立block和char兩個子目錄,block和char目錄用來存放設備號文件。
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 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 = kobj_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 = kobj_to_dev(kobj); ssize_t ret = -EIO; if (dev_attr->store) ret = dev_attr->store(dev, dev_attr, buf, count); return ret; } static const struct sysfs_ops dev_sysfs_ops = { .show = dev_attr_show, .store = dev_attr_store, };
to_dev_attr()宏定義用來獲取struct device_attribute結構體的首地址,dev_sysfs_ops結構體的內容就是device注冊到sysfs設備文件系統的操作函數,dev_attr_show()和dev_attr_store()函數會調用struct device_attribute結構體內的讀寫屬性相關函數。
static void device_release(struct kobject *kobj) { struct device *dev = kobj_to_dev(kobj); struct device_private *p = dev->p; /* * Some platform devices are driven without driver attached * and managed resources may have been acquired. Make sure * all resources are released. * * Drivers still can add resources into device after device * is deleted but alive, so release devres here to avoid * possible memory leak. */ devres_release_all(dev); 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 const void *device_namespace(struct kobject *kobj) { struct device *dev = kobj_to_dev(kobj); const void *ns = NULL; if (dev->class && dev->class->ns_type) ns = dev->class->namespace(dev); return ns; } static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) { struct device *dev = kobj_to_dev(kobj); if (dev->class && dev->class->get_ownership) dev->class->get_ownership(dev, uid, gid); } static struct kobj_type device_ktype = { .release = device_release, .sysfs_ops = &dev_sysfs_ops, .namespace = device_namespace, .get_ownership = device_get_ownership, };
當struct device結構體內的實例kobject引用計數到0時會調用device_release()函數來釋放掉device,函數調用時,會先通過kobj指針獲取device的首地址,然后判斷device下的release()是否存在,如果存在則調用,否則,依次判斷device下device_type下的release()和device下class下的dev_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 = kobj_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 = kobj_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 = kobj_to_dev(kobj); int retval = 0; /* add device node properties if present */ if (MAJOR(dev->devt)) { const char *tmp; const char *name; umode_t mode = 0; kuid_t uid = GLOBAL_ROOT_UID; kgid_t gid = GLOBAL_ROOT_GID; add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); if (name) { add_uevent_var(env, "DEVNAME=%s", name); if (mode) add_uevent_var(env, "DEVMODE=%#o", mode & 0777); if (!uid_eq(uid, GLOBAL_ROOT_UID)) add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); if (!gid_eq(gid, GLOBAL_ROOT_GID)) add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); kfree(tmp); } } 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); /* Add common DT information about the device */ of_device_uevent(dev, env); /* 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 function 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 const struct kset_uevent_ops device_uevent_ops = { .filter = dev_uevent_filter, .name = dev_uevent_name, .uevent = dev_uevent, };
kset_uevent_ops結構體內的函數是用於管理kset內部的kobject的uevent操作的,其中,filter()函數用於阻止一個kobject向用戶空間發送uevent,當函數的返回值為0時表示阻止,在上面的dev_uevent_filter()函數中檢查了device所屬的bus或者class是否存在,如果都不存在,則返回0,也就是沒有發送uevent的必要了,name()函數用於覆蓋kset發送給用戶空間的名稱,在上面的dev_uevent_name()函數選擇使用device所屬的bus或者class的名稱,uevent()函數是在uevent將被發送到用戶空間之前進行調用的,用於向uevent中增加新的環境變量。
static ssize_t uevent_show(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 uevent_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { if (kobject_synth_uevent(&dev->kobj, buf, count)) dev_err(dev, "uevent: failed to send synthetic uevent\n"); return count; } static DEVICE_ATTR_RW(uevent);
device不僅在kset中添加了對uevent的管理,還把uevent信息封裝成設備的一個屬性文件uevent,其權限為擁有者可讀寫,其中uevent_show()函數用於在用戶空間顯示uevent中的環境變量,uevent_store()函數則用於將uevent屬性寫入到內核空間。
static int device_add_attrs(struct device *dev) { struct class *class = dev->class; const struct device_type *type = dev->type; int error; if (class) { error = device_add_groups(dev, class->dev_groups); if (error) return error; } if (type) { error = device_add_groups(dev, type->groups); if (error) goto err_remove_class_groups; } error = device_add_groups(dev, dev->groups); if (error) goto err_remove_type_groups; if (device_supports_offline(dev) && !dev->offline_disabled) { error = device_create_file(dev, &dev_attr_online); if (error) goto err_remove_dev_groups; } return 0; err_remove_dev_groups: device_remove_groups(dev, dev->groups); err_remove_type_groups: if (type) device_remove_groups(dev, type->groups); err_remove_class_groups: if (class) device_remove_groups(dev, class->dev_groups); return error; } static void device_remove_attrs(struct device *dev) { struct class *class = dev->class; const struct device_type *type = dev->type; device_remove_file(dev, &dev_attr_online); device_remove_groups(dev, dev->groups); if (type) device_remove_groups(dev, type->groups); if (class) device_remove_groups(dev, class->dev_groups); }
device_add_attrs()負責device中的屬性添加,包括幾個部分的集合,分別是class中groups、device_type中的groups還有device本身的groups,device_remove_attrs()則是相反的操作,負責刪除device的屬性。
static ssize_t dev_show(struct device *dev, struct device_attribute *attr, char *buf) { return print_dev_t(buf, dev->devt); } static DEVICE_ATTR_RO(dev);
這里定義了一個名為dev的屬性文件,其權限為擁有者只能讀,函數實現的功能為顯示設備的設備號。
/** * device_create_file - create sysfs attribute file for device. * @dev: device. * @attr: device attribute descriptor. */ int device_create_file(struct device *dev, const struct device_attribute *attr) { int error = 0; if (dev) { WARN(((attr->attr.mode & S_IWUGO) && !attr->store), "Attribute %s: write permission without 'store'\n", attr->attr.name); WARN(((attr->attr.mode & S_IRUGO) && !attr->show), "Attribute %s: read permission without 'show'\n", attr->attr.name); 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, const 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, const 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, const struct bin_attribute *attr) { if (dev) sysfs_remove_bin_file(&dev->kobj, attr); }
上面這些函數是對sysfs提供的API進行簡單的封裝,其中device_create_file()和device_remove_file()提供直接的設備屬性文件管理方法,device_create_bin_file()和device_remove_bin_file()則是提供設備管理二進制文件的方法。
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_children_get()和klist_children_put()函數是當設備掛入和刪除父設備的klist_children鏈表時調用的函數,相當於對設備的kobject引用計數的操作。
/** * get_device - increment reference count for device. * @dev: device. * * This simply forwards the call to kobject_get(), though * we do take care to provide for the case that we get a NULL * pointer passed in. */ struct device *get_device(struct device *dev) { return dev ? kobj_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); }
get_device()函數和put_device()用於dev的引用計數,通過內嵌的kobject來實現,當引用計數為0時,將會調用前面分析到的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); mutex_init(&dev->mutex); lockdep_set_novalidate_class(&dev->mutex); spin_lock_init(&dev->devres_lock); INIT_LIST_HEAD(&dev->devres_head); device_pm_init(dev); set_dev_node(dev, -1); #ifdef CONFIG_GENERIC_MSI_IRQ INIT_LIST_HEAD(&dev->msi_list); #endif INIT_LIST_HEAD(&dev->links.consumers); INIT_LIST_HEAD(&dev->links.suppliers); dev->links.status = DL_DEV_NO_DRIVER; }
device_initialize()函數是設備在sysfs中注冊的第一個階段,用於將struct device結構體進行初始化,主要是對結構體內的一些成員進行初始化,結構體內嵌的kobject下的kset配置為devices_kset,調用kobject_init()函數設置device_ktype和sysfs_ops結構中的兩個函數和device_release()函數,另外還有一些特定資源需要的成員的初始化。
static struct kobject *get_device_parent(struct device *dev, struct device *parent) { if (dev->class) { struct kobject *kobj = NULL; struct kobject *parent_kobj; struct kobject *k; #ifdef CONFIG_BLOCK /* block disks show up in /sys/block */ if (sysfs_deprecated && dev->class == &block_class) { if (parent && parent->class == &block_class) return &parent->kobj; return &block_class.p->subsys.kobj; } #endif /* * 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 && !dev->class->ns_type) return &parent->kobj; else parent_kobj = &parent->kobj; mutex_lock(&gdp_mutex); /* find our class-directory at the parent and reference it */ spin_lock(&dev->class->p->glue_dirs.list_lock); list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) if (k->parent == parent_kobj) { kobj = kobject_get(k); break; } spin_unlock(&dev->class->p->glue_dirs.list_lock); if (kobj) { mutex_unlock(&gdp_mutex); return kobj; } /* or create a new class-directory at the parent device */ k = class_dir_create_and_add(dev->class, parent_kobj); /* do not emit an uevent for this simple "glue" directory */ mutex_unlock(&gdp_mutex); return k; } /* subsystems can specify a default root directory for their devices */ if (!parent && dev->bus && dev->bus->dev_root) return &dev->bus->dev_root->kobj; if (parent) return &parent->kobj; return NULL; }
函數get_device_parent()用於獲取父節點的kobject,get_device_parent()的返回值直接決定了device將被掛在哪個目錄下,設備最終掛在的目錄,是由多個因素綜合決定的。
static int device_add_class_symlinks(struct device *dev) { struct device_node *of_node = dev_of_node(dev); int error; if (of_node) { error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); if (error) dev_warn(dev, "Error %d creating of_node link\n",error); /* An error here doesn't warrant bringing down the device */ } if (!dev->class) return 0; error = sysfs_create_link(&dev->kobj, &dev->class->p->subsys.kobj, "subsystem"); if (error) goto out_devnode; if (dev->parent && device_is_not_partition(dev)) { error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); if (error) goto out_subsys; } #ifdef CONFIG_BLOCK /* /sys/block has directories and does not need symlinks */ if (sysfs_deprecated && dev->class == &block_class) return 0; #endif /* link in the class directory pointing to the device */ error = sysfs_create_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); if (error) goto out_device; return 0; out_device: sysfs_remove_link(&dev->kobj, "device"); out_subsys: sysfs_remove_link(&dev->kobj, "subsystem"); out_devnode: sysfs_remove_link(&dev->kobj, "of_node"); return error; }
device_add_class_symlinks()函數用於在device和class直接添加一些軟鏈接,在device目錄下創建指向class的subsystem文件,在class目錄下創建指向device的同名文件,如果device有父設備,而且device不是塊設備分區時,則在device目錄下創建一個指向父設備的device鏈接文件。
static void device_remove_class_symlinks(struct device *dev) { if (dev_of_node(dev)) sysfs_remove_link(&dev->kobj, "of_node"); if (!dev->class) return; if (dev->parent && device_is_not_partition(dev)) sysfs_remove_link(&dev->kobj, "device"); sysfs_remove_link(&dev->kobj, "subsystem"); #ifdef CONFIG_BLOCK if (sysfs_deprecated && dev->class == &block_class) return; #endif sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); }
device_remove_class_symlinks()函數則是相反操作,用於刪除device和class之間建立的軟鏈接。
/** * dev_set_name - set a device name * @dev: device * @fmt: format string for the device's name */ 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_set_name()函數用於設置device的名稱,該函數只能在設備未注冊之前使用,名稱是通過dev->kobject進行管理的。
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選擇合適的/sys/dev下的kobject或者字符設備或者NULL。
#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); INIT_LIST_HEAD(&dev->p->deferred_probe); 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. * * For more information, see the kerneldoc for device_initialize() * and device_add(). * * 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()函數進行結構體的變量初始化,然后調用device_add()函數將device添加到系統中,但是需要注意的是,在調用device_register()注冊device之前,有一些device結構體變量需要自己設置,其中有指明設備位置的struct device *parent、struct bus_type *bus、struct class *class等,有指明設備屬性的const char *init_name、struct device_type *type、const struct attribute_group **groups、dev_t devt和release()函數等,不同的設備使用的方法不同。
接下來分析device_add()函數的實現:
/** * device_add - add device to device hierarchy. * @dev: device. * * This is part 2 of device_register(), though may be called * separately _iff_ device_initialize() has been called separately. * * This adds @dev to the kobject hierarchy via kobject_add(), adds it * to the global and sibling lists for the device, then * adds it to the other relevant subsystems of the driver model. * * Do not call this routine or device_register() more than once for * any device structure. The driver model core is not designed to work * with devices that get unregistered and then spring back to life. * (Among other things, it's very hard to guarantee that all references * to the previous incarnation of @dev have been dropped.) Allocate * and register a fresh new struct device instead. * * NOTE: _Never_ directly free @dev after calling this function, even * if it returned an error! Always use put_device() to give up your * reference instead. */ int device_add(struct device *dev) { struct device *parent; struct kobject *kobj; struct class_interface *class_intf; int error = -EINVAL; struct kobject *glue_dir = NULL; dev = get_device(dev); //增加device的引用計數 if (!dev) goto done; if (!dev->p) { error = device_private_init(dev); //分配和初始化dev->p 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; } /* 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); if (!dev_name(dev)) { error = -EINVAL; goto name_error; } pr_debug("device: '%s': %s\n", dev_name(dev), __func__); parent = get_device(dev->parent); //增加對parent的引用計數,無parent時返回NULL kobj = get_device_parent(dev, parent); //獲取父kobject if (IS_ERR(kobj)) { error = PTR_ERR(kobj); goto parent_error; } if (kobj) dev->kobj.parent = kobj; //設置dev->kobj的父kobject /* use parent numa_node */ if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) 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); //將device內嵌的kobj加入到kobject層次結構中 if (error) { glue_dir = get_glue_dir(dev); goto Error; } /* notify platform of device entry */ if (platform_notify) platform_notify(dev); error = device_create_file(dev, &dev_attr_uevent); //添加uevent屬性文件 if (error) goto attrError; error = device_add_class_symlinks(dev); //dev與class軟鏈接創建 if (error) goto SymlinkError; error = device_add_attrs(dev); //添加屬性 if (error) goto AttrsError; error = bus_add_device(dev); //將設備添加到總線上,創建dev與bus間的軟鏈接 if (error) goto BusError; error = dpm_sysfs_add(dev); //增加dev下的power屬性集合 if (error) goto DPMError; device_pm_add(dev); if (MAJOR(dev->devt)) { //主設備號存在 error = device_create_file(dev, &dev_attr_dev); //添加dev屬性 if (error) goto DevAttrError; error = device_create_sys_dev_entry(dev); //在/sys/dev下添加相應的軟鏈接 if (error) goto SysEntryError; devtmpfs_create_node(dev); //在/dev下添加相應的設備節點 } /* Notify clients of device addition. This call must come * after dpm_sysfs_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); //kobject發布KOBJ_ADD消息到用戶空間 bus_probe_device(dev); //為device尋找合適的驅動 if (parent) klist_add_tail(&dev->p->knode_parent, &parent->p->klist_children); //如果父節點存在,將掛入到klist_children鏈表 if (dev->class) { //如果device所屬的class存在 mutex_lock(&dev->class->p->mutex); /* tie the class to the device */ klist_add_tail(&dev->knode_class, &dev->class->p->klist_devices); //節點插入鏈表 /* notify any interfaces that the device is here */ list_for_each_entry(class_intf, &dev->class->p->interfaces, node) if (class_intf->add_dev) class_intf->add_dev(dev, class_intf); mutex_unlock(&dev->class->p->mutex); } done: put_device(dev); //減少device的引用計數 return error; SysEntryError: if (MAJOR(dev->devt)) device_remove_file(dev, &dev_attr_dev); DevAttrError: device_pm_remove(dev); dpm_sysfs_remove(dev); DPMError: bus_remove_device(dev); BusError: device_remove_attrs(dev); AttrsError: device_remove_class_symlinks(dev); SymlinkError: device_remove_file(dev, &dev_attr_uevent); attrError: kobject_uevent(&dev->kobj, KOBJ_REMOVE); glue_dir = get_glue_dir(dev); kobject_del(&dev->kobj); Error: cleanup_glue_dir(dev, glue_dir); parent_error: put_device(parent); name_error: kfree(dev->p); dev->p = NULL; goto done; }
函數device_add()用於將dev添加到設備驅動模型中去,它先調用get_device()來增加dev的引用計數,然后調用device_private_init()進行dev->p的分配和初始化,調用dev_set_name()對dev的名字進行設置,接下來,要做的是准備將dev添加到sysfs設備文件系統中去,首先是調用get_device()增加對paren的引用計數(無論是直接掛在parent下還是通過一個類層掛在parent下都要增加parent的引用計數),然后調用get_device_parent()找到實際要加入的父kobject,並調用kobject_add()將dev->kobj加入到dev->kobj.parent的層次結構中去,接下來是完成屬性和屬性集合的添加,調用device_create_file()添加uevent屬性文件,然后調用device_add_class_symlinks()在dev下創建一個軟鏈接subsystem,指向相對應的class,然后繼續調用device_add_attrs()添加屬性和屬性集合,調用bus_add_device()添加設備的總線屬性,在dev與bus之間創建軟鏈接,並將dev掛入到總線的設備鏈表中去,dpm_sysfs_add()用於增加dev下的power屬性集合,調用device_pm_add()將設備添加到dpm_list鏈表中去。如果設備被分配了主設備號,調用device_create_file()添加dev屬性文件,然后調用device_create_sys_dev_entry()在/sys/dev下創建相應的軟鏈接,調用devtmpfs_create_node()在/dev下添加對應的設備節點文件。函數開始調用kobject_uevent()向用戶空間發布KOBJ_ADD消息通知,並調用bus_probe_device()為設備探測尋找合適的驅動程序,如果設備有父節點的話,則把dev->p->knode_parent掛入到parent->p->klist_children鏈表中,如果設備有所屬的class,則將dev->knode_class掛入class->p>class_devices上,並調用可能的類設備接口add_dev()函數,對於直接在bus上的設備來講,可以調用bus_probe_device()來查找驅動程序,但是不與bus直接接觸的設備,則靠class來去尋找驅動,便使用了class_interface內的add_dev()方式,函數最后調用put_device()減少在開頭增加的引用計數並返回。
/** * device_unregister - unregister device from system. * @dev: device going away. * * We do this in two parts, like we do device_register(). First, * we remove it from all the subsystems with device_del(), then * we decrement the reference count via put_device(). If that * is the final reference count, the device will be cleaned up * via device_release() above. Otherwise, the structure will * stick around until the final reference to the device is dropped. */ 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從系統中注銷,並減少創建時產生的引用計數,當引用計數為0時,將銷毀dev。
/** * device_del - delete device from system. * @dev: device. * * This is the first part of the device unregistration * sequence. This removes the device from the lists we control * from here, has it removed from the other driver model * subsystems it was added to in device_add(), and removes it * from the kobject hierarchy. * * NOTE: this should be called manually _iff_ device_add() was * also called manually. */ void device_del(struct device *dev) { struct device *parent = dev->parent; struct kobject *glue_dir = NULL; 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); dpm_sysfs_remove(dev); //將sysfs下的power屬性集合移除 if (parent) //如果存在父節點 klist_del(&dev->p->knode_parent); //將設備節點從父節點鏈表中移除 if (MAJOR(dev->devt)) { //如果分配了主設備號 devtmpfs_delete_node(dev); //將/dev下的設備節點文件移除 device_remove_sys_dev_entry(dev); ///sys/dev下的軟鏈接取消 device_remove_file(dev, &dev_attr_dev); //將屬性文件移除 } if (dev->class) { device_remove_class_symlinks(dev); mutex_lock(&dev->class->p->mutex); /* notify any interfaces that the device is now gone */ list_for_each_entry(class_intf, &dev->class->p->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->mutex); } device_remove_file(dev, &dev_attr_uevent); //移除uevent屬性文件 device_remove_attrs(dev); //屬性及屬性集合移除 bus_remove_device(dev); //總線上移除設備 device_pm_remove(dev); //將dev從dpm_list中移除 driver_deferred_probe_del(dev); //驅動移除 device_remove_properties(dev); device_links_purge(dev); /* Notify the platform of the removal, in case they * need to do anything... */ if (platform_notify_remove) platform_notify_remove(dev); if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus_notifier, BUS_NOTIFY_REMOVED_DEVICE, dev); kobject_uevent(&dev->kobj, KOBJ_REMOVE); //kobject發布KOBJ_REMOVE消息到用戶空間 glue_dir = get_glue_dir(dev); kobject_del(&dev->kobj); //將內嵌的kobj從層次結構中移除 cleanup_glue_dir(dev, glue_dir); put_device(parent); //父節點引用計數減1操作 }
函數device_del()是與device_add()相對的函數,device_add()將設備添加到系統中,device_del()則是將設備從系統中移除,包括了將dev從設備驅動模型的各種klist鏈表中進行脫離,又將dev從sysfs的各個地方創建的文件進行刪除的工作。
static struct device *prev_device(struct klist_iter *i) //返回前一個設備 { struct klist_node *n = klist_prev(i); struct device *dev = NULL; struct device_private *p; if (n) { p = to_device_private_parent(n);//返回device_private結構體的首地址 dev = p->device;//將struct device結構體地址返回 } return dev; } 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; }
內部函數prev_device()和next_device()用於device的klist的鏈表遍歷,prev_device()將返回前一個設備,next_device()將返回下一個設備。
/** * device_get_devnode - path of device node file * @dev: device * @mode: returned file access mode * @uid: returned file owner * @gid: returned file group * @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, umode_t *mode, kuid_t *uid, kgid_t *gid, 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, uid, gid); 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 '/' */ s = kstrdup(dev_name(dev), GFP_KERNEL); if (!s) return NULL; strreplace(s, '!', '/'); return *tmp = s; }
函數device_get_devnode()用於返回設備的路徑名。
/** * device_for_each_child - device child iterator. * @parent: parent struct device. * @fn: function to be called for each device. * @data: data for the callback. * * Iterate over @parent's child devices, and call @fn for each, * passing it @data. * * We check the return of @fn each time. If it returns anything * other than 0, we break out and return that value. */ 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) //正序遍歷klist_children鏈表 error = fn(child, data); klist_iter_exit(&i); return error; } int device_for_each_child_reverse(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 = prev_device(&i)) && !error) //逆序遍歷klist_children鏈表 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()函數和device_for_each_child_reverse()函數對父設備下的子設備進行遍歷,並都調用一個特定的函數fn()進行處理,device_find_child()函數則是查找特定的子設備,查找使用特定match()函數進行匹配。
接下來,繼續分析動態創建struct device的方法,其原理和kobject和kset的動態創建類似:
static void device_create_release(struct device *dev) { pr_debug("device: '%s': %s\n", dev_name(dev), __func__); kfree(dev); } static struct device * device_create_groups_vargs(struct class *class, struct device *parent, dev_t devt, void *drvdata, const struct attribute_group **groups, const char *fmt, va_list args) { struct device *dev = NULL; int retval = -ENODEV; if (class == NULL || IS_ERR(class))//判斷class指針是否有效 goto error; dev = kzalloc(sizeof(*dev), GFP_KERNEL);//為dev動態分配內存 if (!dev) { retval = -ENOMEM; goto error; } device_initialize(dev);//設備初始化 dev->devt = devt; dev->class = class; dev->parent = parent; dev->groups = groups; dev->release = device_create_release; dev_set_drvdata(dev, drvdata); retval = kobject_set_name_vargs(&dev->kobj, fmt, args);//設置kobject的name if (retval) goto error; retval = device_add(dev);//將設備添加到sysfs層次系統 if (retval) goto error; return dev; error: put_device(dev); return ERR_PTR(retval);//將異常指針返回 } /** * device_create_vargs - creates a device and registers it with sysfs * @class: pointer to the struct class that this device should be registered to * @parent: pointer to the parent struct device of this new device, if any * @devt: the dev_t for the char device to be added * @drvdata: the data to be added to the device for callbacks * @fmt: string for the device's name * @args: va_list for the device's name * * This function can be used by char device classes. A struct device * will be created in sysfs, registered to the specified class. * * A "dev" file will be created, showing the dev_t for the device, if * the dev_t is not 0,0. * If a pointer to a parent struct device is passed in, the newly created * struct device will be a child of that device in sysfs. * The pointer to the struct device will be returned from the call. * Any further sysfs files that might be required can be created using this * pointer. * * Returns &struct device pointer on success, or ERR_PTR() on error. * * Note: the struct class passed to this function must have previously * been created with a call to class_create(). */ struct device *device_create_vargs(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, va_list args) { return device_create_groups_vargs(class, parent, devt, drvdata, NULL, fmt, args); } 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; } struct device *device_create_with_groups(struct class *class, struct device *parent, dev_t devt, void *drvdata, const struct attribute_group **groups, const char *fmt, ...) { va_list vargs; struct device *dev; va_start(vargs, fmt); dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, fmt, vargs); va_end(vargs); return dev; }
在上面代碼中,device_create_release()和device_create_groups_vargs()屬於兩個內部的靜態函數,第一個函數用於釋放device分配的內核空間,由於dev是動態分配的,而第二個函數則是用來動態創建一個device,函數首先對傳入的class進行判斷,然后對device進行內存分配,分配成功后就是開始調用device_initialize()對設備初始化,並手動對device的一些成員進行賦值,然后調用kobject_set_name_vargs()對device中嵌入的kobject進行名稱設置,最后,則是調用device_add()函數將動態創建的device添加到sysfs層次系統,而device_create_vargs()、device_create()和device_create_with_groups()都是對device_create_groups_vargs()的進一步封裝,device_create()和device_create_with_groups()的區別在於創建的時候是否要創建device的組屬性文件。
static int __match_devt(struct device *dev, const void *data) { const dev_t *devt = data; return dev->devt == *devt;//設備號匹配 } /** * device_destroy - removes a device that was created with device_create() * @class: pointer to the struct class that this device was registered with * @devt: the dev_t of the device that was previously registered * * This call unregisters and cleans up a device that was created with a * call to device_create(). */ void device_destroy(struct class *class, dev_t devt) { struct device *dev; dev = class_find_device(class, NULL, &devt, __match_devt);//在class下尋找設備 if (dev) { put_device(dev);//減少引用計數 device_unregister(dev);//注銷設備 } }
device_create()用於動態創建一個設備,而device_destroy()函數則用來銷毀一個device_create()創建出來的設備,__match_devt()屬於內部的靜態函數,用於class_find_device()函數尋找需要銷毀的device,主要是通過設備號進行匹配而尋找,之所以需要使用put_device()減少引用計數,是因為使用class_find_device()中調用了get_device()增加了引用計數。
int device_rename(struct device *dev, const char *new_name) { struct kobject *kobj = &dev->kobj; char *old_device_name = NULL; int error; dev = get_device(dev); if (!dev) return -EINVAL; dev_dbg(dev, "renaming to %s\n", new_name); old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); if (!old_device_name) { error = -ENOMEM; goto out; } if (dev->class) { error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, kobj, old_device_name, new_name, kobject_namespace(kobj)); if (error) goto out; } error = kobject_rename(kobj, new_name); if (error) goto out; out: put_device(dev); kfree(old_device_name); return error; }
函數device_rename()是當設備在sysfs中注冊后,用來改變設備的名稱用的,首先改變/sys/class目錄下的軟鏈接的名稱,然后使用kobject_rename()將device下嵌入的kobject進行重新命名。
/** * device_shutdown - call ->shutdown() on each device to shutdown. */ void device_shutdown(void) { struct device *dev, *parent; spin_lock(&devices_kset->list_lock); /* * Walk the devices list backward, shutting down each in turn. * Beware that device unplug events may also start pulling * devices offline, even as the system is shutting down. */ while (!list_empty(&devices_kset->list)) { dev = list_entry(devices_kset->list.prev, struct device, kobj.entry); /* * hold reference count of device's parent to * prevent it from being freed because parent's * lock is to be held */ parent = get_device(dev->parent); get_device(dev); /* * Make sure the device is off the kset list, in the * event that dev->*->shutdown() doesn't remove it. */ list_del_init(&dev->kobj.entry); spin_unlock(&devices_kset->list_lock); /* hold lock to avoid race with probe/release */ if (parent) device_lock(parent); device_lock(dev); /* Don't allow any more runtime suspends */ pm_runtime_get_noresume(dev); pm_runtime_barrier(dev); if (dev->class && dev->class->shutdown_pre) { if (initcall_debug) dev_info(dev, "shutdown_pre\n"); dev->class->shutdown_pre(dev); } if (dev->bus && dev->bus->shutdown) { if (initcall_debug) dev_info(dev, "shutdown\n"); dev->bus->shutdown(dev); } else if (dev->driver && dev->driver->shutdown) { if (initcall_debug) dev_info(dev, "shutdown\n"); dev->driver->shutdown(dev); } device_unlock(dev); if (parent) device_unlock(parent); put_device(dev); put_device(parent); spin_lock(&devices_kset->list_lock); } spin_unlock(&devices_kset->list_lock); }
函數device_shutdown()用來關閉sysfs上的每個設備,它在系統關閉時才會進行調用,在函數內,使用了devices_kset這個頂層kset,所在的目錄為/sys/devices,因此,函數調用會遍歷到注冊的到sysfs上的每個設備,調用設備相應的總線或驅動定義的shutdown()函數,每個設備雖然可以有不同的parent,但是kset還是一樣的,當在調用kobject_add()函數時,將devices_kset這個kset->kobj設置成parent,那么新添加的kobject就會掛在/sys/devices頂層目錄下,例如virtual目錄等。
4、小結
在內核中,struct device結構體的實現非常地復雜,它是Linux內核設備驅動模型的基礎,為了適應越來越復雜的情景,以及提高設備的驅動性能,其實現將會越來越復雜,對其分析點到為止。
參考:
《LINUX設備驅動程序(第三版)》
https://blog.csdn.net/qb_2008/article/details/6847133
https://blog.csdn.net/abo8888882006/article/details/5424363