class,device,DEVICE_ATTR和sysfs

来源:互联网 发布:手机编写javascript 编辑:程序博客网 时间:2024/06/07 00:13
从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.hstruct 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.hstruct 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, ...)

0 0
原创粉丝点击