当前位置: 首页 > 工具软件 > switchClass > 使用案例 >

switch_class

罗浩然
2023-12-01

switch 类

1. init/exit:

static int __init switch_class_init(void)
    {
        return create_switch_class();   //新建一个switch类
    }
    static void __exit switch_class_exit(void)
    {
        class_destroy(switch_class);    //销毁一个switch类
    }
    module_init(switch_class_init);
    module_exit(switch_class_exit);
2. create_switch_class ( )

struct class *switch_class;         //声明两个全局变量
    static atomic_t device_count;

    static int create_switch_class(void)
    {
        if (!switch_class) {
            switch_class = class_create(THIS_MODULE, "switch");   //给this module创建一个switch类.
            if (IS_ERR(switch_class))
                return PTR_ERR(switch_class);
            atomic_set(&device_count, 0);                  //初始化一个原子变量,用来保护临界区。
        }
        return 0;
    }
3. switch_dev_register(): 设备注册函数,提供给别的驱动调用,用来注册switch设备。

int switch_dev_register(struct switch_dev *sdev)
    {
        int ret;
        if (!switch_class) {
            ret = create_switch_class();     //如果没有switch类,则创建一个。
            if (ret < 0)
                return ret;
        }
        sdev->index = atomic_inc_return(&device_count);   //原子变量+1,以获得原子锁,进入临界区操作。
        sdev->dev = device_create(switch_class, NULL,     
            MKDEV(0, sdev->index), NULL, sdev->name);     //创建一个名为sdev->name,设备号为MKDEV(0, sdev->index)的switch类设备。

        if (IS_ERR(sdev->dev))        //如果创建不成功就返回。
            return PTR_ERR(sdev->dev);
        ret = device_create_file(sdev->dev, &dev_attr_state);    //为设备添加 sysfs attribute:state。
        if (ret < 0)
            goto err_create_file_1;
        ret = device_create_file(sdev->dev, &dev_attr_name);    //为设备添加 sysfs attribute:name
        if (ret < 0)
            goto err_create_file_2;
        dev_set_drvdata(sdev->dev, sdev);   //给设备初始化。
        sdev->state = 0;
        return 0;
    err_create_file_2:
        device_remove_file(sdev->dev, &dev_attr_state);
    err_create_file_1:
        device_destroy(switch_class, MKDEV(0, sdev->index));
        printk(KERN_ERR "switch: Failed to register driver %s\n", sdev->name);
        return ret;
    }
    EXPORT_SYMBOL_GPL(switch_dev_register);
4. 注册完一个switch设备之后,就可以给该switch设备设置状态了, 即: switch_set_state()

void switch_set_state(struct switch_dev *sdev, int state)
    {
        char name_buf[120];
        char state_buf[120];
        char *prop_buf;
        char *envp[3];
        int env_offset = 0;
        int length;
        if (sdev->state != state) {
            sdev->state = state;        //如果设备状态不等于state,则赋值state.
            prop_buf = (char *)get_zeroed_page(GFP_KERNEL);    //获得一页初始化为0的内存 prop_buf。
            if (prop_buf) {
                length = name_show(sdev->dev, NULL, prop_buf);   //输出设备名 到prop_buf 中.
                if (length > 0) {
                    if (prop_buf[length - 1] == '\n')
                        prop_buf[length - 1] = 0;
                    snprintf(name_buf, sizeof(name_buf),
                        "SWITCH_NAME=%s", prop_buf);            //打印switch name
                    envp[env_offset++] = name_buf;
                }
                length = state_show(sdev->dev, NULL, prop_buf);
                if (length > 0) {
                    if (prop_buf[length - 1] == '\n')
                        prop_buf[length - 1] = 0;
                    snprintf(state_buf, sizeof(state_buf),    //打印switch state
                        "SWITCH_STATE=%s", prop_buf);
                    envp[env_offset++] = state_buf;
                }
                envp[env_offset] = NULL;
                kobject_uevent_env(&sdev->dev->kobj, KOBJ_CHANGE, envp); //给该switch设备传输一份带envp[]的数据。
                free_page((unsigned long)prop_buf);  //释放申请的内存。
            } else {
                printk(KERN_ERR "out of memory in switch_set_state\n");
                kobject_uevent(&sdev->dev->kobj, KOBJ_CHANGE);   //notify userspace by ending an uevent 通知用户空间,uevent操作结束。
            }
        }
    }
    EXPORT_SYMBOL_GPL(switch_set_state);
  5. name 和 state 属性操作

static ssize_t state_show(struct device *dev, struct device_attribute *attr,
            char *buf)
    {
        struct switch_dev *sdev = (struct switch_dev *)
            dev_get_drvdata(dev);
        if (sdev->print_state) {
            int ret = sdev->print_state(sdev, buf);   //打印state
            if (ret >= 0)
                return ret;
        }
        return sprintf(buf, "%d\n", sdev->state);
    }
    static ssize_t name_show(struct device *dev, struct device_attribute *attr,
            char *buf)
    {
        struct switch_dev *sdev = (struct switch_dev *)
            dev_get_drvdata(dev);
        if (sdev->print_name) {
            int ret = sdev->print_name(sdev, buf);   //打印name
            if (ret >= 0)
                return ret;
        }
        return sprintf(buf, "%s\n", sdev->name);
    }
    static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, state_show, NULL);    //给用户空间提供接口
    static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, name_show, NULL);

6. 一个重要的数据结构,描述switch设备的。

struct switch_dev {
        const char    *name;
        struct device    *dev;   
        int        index;
        int        state;
        ssize_t    (*print_name)(struct switch_dev *sdev, char *buf);
        ssize_t    (*print_state)(struct switch_dev *sdev, char *buf);
    };

 

 类似资料:

相关阅读

相关文章

相关问答