linux sys 文件系统中 系统 设备的操作

本文提供了一段用于测试 Linux 内核中 sys_devicesys_class 操作的代码,包括读写功能。该代码创建了一个名为 mxc_gpio 的 sysdev_class,并注册了四个设备实例。通过 show 和 store 函数实现了对 mxc_gpio 的属性读取和设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面的代码是主要用来测试 linux 内核中对 sys_device sysdev_class 的操作,包括读和写;

在代码的最后会画出linux 内核里面有关struct attribute 和它的继承类的框图. 代码是可以直接

那来测试使用

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>

#include <linux/sysdev.h>
MODULE_LICENSE("Dual BSD/GPL");

static char mybuf[1024];
static struct sysdev_class mxc_gpio_sysclass = {
        .name = "mxc_gpio",
        .suspend = NULL,
        .resume = NULL,
};

static struct sys_device mxc_gpio_devices[] = {
        [0] = {
                .id = 0,
                .cls = &mxc_gpio_sysclass,
        },
        [1] = {
                .id = 1,
                .cls = &mxc_gpio_sysclass,
        },
        [2] = {
                .id = 2,
                .cls = &mxc_gpio_sysclass,
        },
        [3] = {
                .id = 3,
                .cls = &mxc_gpio_sysclass,
        }
};

ssize_t gpio_show(struct sys_device *dev, struct sysdev_attribute *attr, char * buf){
        ssize_t status;
        status = sprintf (buf, "test gpio:%s", mybuf);
        return status;
}
ssize_t gpio_store (struct sys_device *dev, struct sysdev_attribute *attr,
                        const char *buf, size_t len) {
        ssize_t status;
        status = sprintf (mybuf, "%s/n", buf);
        return status;
}

static SYSDEV_ATTR(show, 0400, gpio_show, NULL);
static SYSDEV_ATTR(store, 0200, NULL, gpio_store);

static int __init my_sys_device_init(void) {
        int i;
        int ret = -1;
        memset ((void *)mybuf, 0, sizeof(mybuf));
        strcpy (mybuf, "default value/n");

        ret = sysdev_class_register(&mxc_gpio_sysclass);
        for (i = 0; i < ARRAY_SIZE(mxc_gpio_devices); i++) {
                ret = sysdev_register(&mxc_gpio_devices[i]);
        }
        sysdev_create_file (&mxc_gpio_devices[0], &attr_show);
        sysdev_create_file (&mxc_gpio_devices[0], &attr_store);
        return ret;
}

static void __exit my_sys_device_end (void) {
        int i;
        sysdev_remove_file (&mxc_gpio_devices[0], &attr_show);
        sysdev_remove_file (&mxc_gpio_devices[0], &attr_store);
        for (i = 0; i < ARRAY_SIZE(mxc_gpio_devices); i++) {
                sysdev_unregister(&mxc_gpio_devices[i]);
        }
        sysdev_class_unregister(&mxc_gpio_sysclass);
}

module_init(my_sys_device_init);
module_exit(my_sys_device_end);

~                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                              
~           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值