有 DRIVER_ATTR 和 DEVICE_ATTR 兩種
Device drivers can export attributes via their sysfs directories.
Drivers can declare attributes using a DRIVER_ATTR macro that works
identically to the DEVICE_ATTR macro.
macro定義如下
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
#define DRIVER_ATTR(_name, _mode, _show, _store) \
struct driver_attribute driver_attr_##_name = \
__ATTR(_name, _mode, _show, _store)
例如:
static DRIVER_ATTR(i2c, S_IWUSR | S_IRUGO, show_register_value, store_register_value);
這些mode的值,定義在kernel/include/linux/stat.h
S_IRUGO=(S_IRUSR | S_IRGRP | S_IROTH)
S_IRUSR:00400 表示user read
S_IRGRP:00040 表示group read
S_IROTH:00004 表示other read
通常會定義attribute的路徑,例如
#define BLUETOOTH_POWER_PATH "/sys/module/bluetooth_power/parameters/power"
#define SE123_POWER_PATH "/sys/class/misc/ssiscan/power_en"
然後會有個function如下去enable power
static int set_se655_power(int on) {
int ret = -1;
int sz;
const char buffer = (on ? 'Y' : 'N');
int fd = open(SE655_POWER_PATH, O_WRONLY);
這些mode的值,定義在kernel/include/asm-generic/fcntl.h
#define O_RDONLY 00000000
#define O_WRONLY 00000001
在kernel/include/linux/device.h裡面定義了以下幾種attribute類型
Bus_attribute
Device_attribute
Driver_attribute
Class_attribute
Dev_ext_attribute
Bin_attribute
Reference:
http://www.bitscn.com/os/linux/200904/158631_7.html
沒有留言:
張貼留言