下面的代码是主要用来测试 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);
~
~
~