struct device 類(lèi)型
系統(tǒng)中的任一設(shè)備在設(shè)備模型中都由一個(gè) device 對(duì)象描述隧哮,其對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu) struct device
struct device {
struct kobject kobj;
struct device *parent;
struct device_private *p;
const char *init_name; /* initial name of the device */
const struct device_type *type;
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_drvdata/dev_get_drvdata */
....
struct dev_links_info links;
struct dev_pm_info power;
struct dev_pm_domain *pm_domain;
const struct dma_map_ops *dma_ops;
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;
u64 bus_dma_mask; /* upstream dma_mask constraint */
unsigned long dma_pfn_offset;
struct device_dma_parameters *dma_parms;
......
};
device 內(nèi)嵌一個(gè) kobject 對(duì)象,用于實(shí)現(xiàn)引用計(jì)數(shù)的管理并且通過(guò)它實(shí)現(xiàn) sysfs 的層級(jí)結(jié)構(gòu)
driver 域指向管理該設(shè)備的驅(qū)動(dòng)程序?qū)ο螅琩river_data 是提供給驅(qū)動(dòng)程序的數(shù)據(jù)
bus 域描述設(shè)備連接的總線類(lèi)型
內(nèi)核提供了相應(yīng)的函數(shù)枕面,操作device 對(duì)象:
device_reigster 函數(shù)將一個(gè)新的 device 對(duì)象插入設(shè)備模型护盈,并且自動(dòng)在 /sys/devices 下創(chuàng)建一個(gè)新的目錄
device_unregister 完成相反的操作
device_reigster 中最終是調(diào)用 kobject_add 這些底層函數(shù)躲雅,將 device 中包含的 kobject 注冊(cè)到整個(gè) kobject 的層級(jí)結(jié)構(gòu)中
sysfs 相關(guān)API
使用默認(rèn)目錄
sysfs 默認(rèn)掛載在 "sys" 目錄下,對(duì)于內(nèi)核態(tài)是 "kobject"屠缭,對(duì)于用戶態(tài)箍鼓,映射在總線(bus) 對(duì)應(yīng)的目錄下崭参,一般的設(shè)備驅(qū)動(dòng)呵曹,我們使用的是 platform 虛擬總線,因此會(huì)映射在 /sys/platform/device/xxx 下
設(shè)備驅(qū)動(dòng)創(chuàng)建時(shí)何暮,就會(huì)創(chuàng)建該目錄奄喂,因此不需要手動(dòng)創(chuàng)建
在bus/devices 下創(chuàng)建文件:
static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
自定義目錄
在 /sys 目錄下創(chuàng)建自定義目錄,創(chuàng)建目錄使用下面的接口:
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
struct kobject *kobj;
int retval;
kobj = kobject_create();
if (!kobj)
return NULL;
retval = kobject_add(kobj, parent, "%s", name);
if (retval) {
pr_warn("%s: kobject_add error: %d\n", __func__, retval);
kobject_put(kobj);
kobj = NULL;
}
return kobj;
}
name 表示目錄名稱
parent 表示父目錄海洼,NULL 為默認(rèn)在 /sys 目錄下創(chuàng)建
創(chuàng)建和釋放文件
內(nèi)核 kobject 中的 attribute 映射到用戶態(tài)就是文件跨新,attribute 分為下面三類(lèi)結(jié)構(gòu):
struct attribute {
const char *name;
umode_t mode;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
bool ignore_lockdep:1;
struct lock_class_key *key;
struct lock_class_key skey;
#endif
};
struct attribute_group {
const char *name;
umode_t (*is_visible)(struct kobject *,
struct attribute *, int);
umode_t (*is_bin_visible)(struct kobject *,
struct bin_attribute *, int);
struct attribute **attrs;
struct bin_attribute **bin_attrs;
};
struct bin_attribute {
struct attribute attr;
size_t size;
void *private;
ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
char *, loff_t, size_t);
ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
char *, loff_t, size_t);
int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
struct vm_area_struct *vma);
};
- struct attribute :屬性的通用結(jié)構(gòu),其他部分在使用時(shí)可以將 struct attribute 嵌入大到更大的屬性結(jié)構(gòu)中
- struct bin_attribute:為二進(jìn)制屬性專門(mén)設(shè)計(jì)坏逢,在 sysfs 中表現(xiàn)為二進(jìn)制文件域帐,大多數(shù)設(shè)備參數(shù)的映射
- struct attribute_group:提供一組屬性的集合,集中管理 attribute 和 bin_attribute
sysfs 提供了下面的接口是整,將屬性關(guān)聯(lián)到對(duì)應(yīng)的 kobject 上:
static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
static inline int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, const void *ns)
int __must_check sysfs_create_bin_file(struct kobject *kobj, const struct bin_attribute *attr);
int __must_check sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);
struct attribute 包含了一個(gè) mode 屬性肖揣,即文件的訪問(wèn)權(quán)限,可讀浮入,可寫(xiě)龙优,可執(zhí)行,可以用已經(jīng)定義的宏表示
(S_IWUSR | S_IRUGO)事秀,也可以用數(shù)字表示(0600)
宏的含義如下
S_IRUSR:用戶讀權(quán)限
S_IWUSR:用戶寫(xiě)權(quán)限
S_IRGRP:用戶組讀權(quán)限
S_IWGRP:用戶組寫(xiě)權(quán)限
S_IROTH:其他組都權(quán)限
S_IWOTH:其他組寫(xiě)權(quán)限
attribute 初始化宏
sysfs 接口提供了一組宏定義來(lái)初始化上面的 struct attibute 結(jié)構(gòu)
#define __ATTR(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
.show = _show, \
.store = _store, \
}
#define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), \
.mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
.show = _show, \
.store = _store, \
}
#define __ATTR_RO(_name) { \
.attr = { .name = __stringify(_name), .mode = 0444 }, \
.show = _name##_show, \
}
#define __ATTR_RO_MODE(_name, _mode) { \
.attr = { .name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
.show = _name##_show, \
}
#define __ATTR_WO(_name) { \
.attr = { .name = __stringify(_name), .mode = 0200 }, \
.store = _name##_store, \
}
#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
#define __ATTR_NULL { .attr = { .name = NULL } }
屬性的讀寫(xiě)方法
struct kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
char *buf);
ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count);
};
- show 函數(shù)在讀操作時(shí)被調(diào)用彤断,它會(huì)拷貝 attr 提供的屬性值到 buffer 指定的緩沖區(qū)中,緩沖區(qū)的大小為 PAGE_SIZE 字節(jié)易迹,如果讀取成功宰衙,則返回實(shí)際寫(xiě)入 buffer 的字節(jié)數(shù),如果失敗睹欲,返回負(fù)的錯(cuò)誤碼
- store 函數(shù)在寫(xiě)操作時(shí)被調(diào)用菩浙,它會(huì)從 buffer 中讀取 size 大小的字節(jié),并將其保存在 attr 所表示的屬性結(jié)構(gòu)體變量中句伶,如果成功劲蜻,則將返回實(shí)際從buffer中讀取的字節(jié)數(shù),如果失敗考余,返回負(fù)的錯(cuò)誤碼
device 中對(duì)sysfs的封裝
/* 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)
#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = \
__ATTR_PREALLOC(_name, _mode, _show, _store)
#define DEVICE_ATTR_RW(_name) \
struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
#define DEVICE_ATTR_RO(_name) \
struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
#define DEVICE_ATTR_WO(_name) \
struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
extern int device_create_file(struct device *device, const struct device_attribute *entry);
extern void device_remove_file(struct device *dev, const struct device_attribute *attr);
struct device 結(jié)構(gòu)中包含了 kobject 對(duì)象先嬉,相應(yīng)的 include/linux/Device.h 包含了 struct device_attribute 結(jié)構(gòu)體
定義對(duì)應(yīng)的 device_attribute 結(jié)構(gòu),使用 device_create_file 接口楚堤,就可以向 device 目錄下添加對(duì)應(yīng)的文件
DEVICE_ATTR 開(kāi)頭的宏是對(duì) __ATTR 宏的進(jìn)一步封裝