在linux下使用class的功能
硬/软件环境:s3c2440/linux-2.6.36/busybox-1.18.4/arm-linux-gcc 4.4.3
下面是模块的c代码:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <asm/io.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhanglong");
//----------------------------
struct class *zl_class;
struct class_device *test_cla_dev;
struct zl_device {
struct device dev;
dev_t no;
};
struct zl_device zl_dev = {
.no = MKDEV(52,0),
};
int test_init(void)
{
int ret = 0;
zl_class = class_create(THIS_MODULE, "zl_class");
test_cla_dev = device_create(zl_class, NULL, zl_dev.no, &(zl_dev.dev), "zldev_name");
return ret;
}
void test_exit(void)
{
class_destroy(zl_class);
}
module_init(test_init);
module_exit(test_exit);
|
下面是Makefile文件内容:
#KERNEL = /home/zl/my2440-2.6.13
KERNEL = /media/STUDY/linux/kernel/my2440-2.6.36
#KERNEL = /lib/modules/$(shell uname -r)/build
default:
make -C $(KERNEL) M=$(shell pwd) modules
clean:
make -C $(KERNEL) M=$(shell pwd) modules clean
modules_install:
make -C $(KERNEL) M=$(shell pwd) modules_install INSTALL_MOD_PATH=/home/zl/s3c2440_nfs
depmod -a -b /home/zl/s3c2440_nfs 2.6.36-my2440
obj-m += driver.o
|
执行make命令后,“insmod driver.ko”插入模块后
在/sys/class目录下有相应名称的文件夹zl_class生成(执行class_create函数后)
在/sys/class/zl_class目录下有zldev_name文件夹生成(执行device_create函数后)
由于已经启用了mdev, /dev目录下会有名为zldev_name的设备文件生成,其设备号是device_create函数中传入的设备号。
/sys/devices/virtual #
/sys/devices/virtual # ls -l zl_class/zldev_name/
total 0
-r--r--r-- 1 root root 4096 May 16 14:12 dev
drwxr-xr-x 2 root root 0 May 16 14:13 power
lrwxrwxrwx 1 root root 0 May 16 14:13 subsystem -> ../../../../class/zl_class
-rw-r--r-- 1 root root 4096 May 16 14:13 uevent
/sys/devices/virtual #
/sys/class #
/sys/class # ls -l zl_class/zldev_name/
total 0
-r--r--r-- 1 root root 4096 May 16 14:12 dev
drwxr-xr-x 2 root root 0 May 16 14:13 power
lrwxrwxrwx 1 root root 0 May 16 14:13 subsystem -> ../../../../class/zl_class
-rw-r--r-- 1 root root 4096 May 16 14:13 uevent
/sys/class #
/dev #
/dev # ls -l zldev_name
crw-rw---- 1 root root 52, 0 May 16 14:12 zldev_name
/dev #
阅读(1483) | 评论(0) | 转发(0) |