class,device,DEVICE_ATTR和sysfs

從linux內核2.6的某個版本之後,devfs不復存在,udev成爲devfs的替代。udev是應用層的東東,不要試圖在內核的配置選項裏找到它;在驅動初始化的代碼裏調用class_create爲該設備創建一個class,再爲每個設備調用 class_device_create創建對應的設備。
大致用法如下:

1. struct class *myclass = class_create(THIS_MODULE, “my_device_driver”); //在/sys/class/ 下創建“my_device_driver”類目錄 

2. struce device *my_dev =device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
    //這樣的module被加載時,udev daemon就會自動在/dev/和/sys/class/my_device_driver/下創建my_device設備文件。
    linux2.6的寫法是:class_device_create();
 
3. device_create_file(my_dev, &dev_attr_emmc_checksum_done); //在/sys/class/my_device_driver/my_device/下創建屬性文件?
/*
 * device_create_file -create sysfs attribute file for device.
 * @dev: device.
 * @attr: device attribute descriptor.
 */
 
4. dev_attr_emmc_checksum_done 是用DEVICE_ATTR宏來定義的。
static DEVICE_ATTR(emmc_checksum_pass, S_IRUGO | S_IWUSR | S_IWGRP,\
  emmc_checksum_pass_show, emmc_checksum_pass_store);
當通過adb shell來運行cat命令時會調用show函數,通過echo命令會調用store函數。
 
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
    class_create -create a struct class structure
    @owner: pointer to the module that is to "own" this struct class
    @name: pointer to a string for the name of this class.
在/sys/class/下創建類目錄
class_device_create() 
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_create(struct class        *cls,
                                         struct class_device *parent,
                                         dev_t               devt,
                                         struct device       *device,
                                         const char          *fmt, ...)
    class_device_create -creates a class device and registers it with sysfs
    @cls: pointer to the struct class that this device should be registered to.
    @parent: pointer to the parent struct class_device of this new device, if any.
    @devt: the dev_t for the char device to be added.
    @device: a pointer to a struct device that is assiociated with this class device.
    @fmt: string for the class device's name
在linux 3.4中該函數換成了device_create()
structdevice*device_create(struct class *class, struct device *parent,
        dev_t devt, void *drvdata, const char *fmt, ...)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章