1、驱动程序代码(C语言)
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
MODULE_LICENSE("Dual BSD/GPL");
//主设备号
#define MAJOR_NUM 252
static ssize_t globalvar_read(struct file *, char *, size_t, loff_t*);
static ssize_t globalvar_write(struct file *, const char *, size_t, loff_t*);
//初始化字符设备驱动的 file_operations 结构体
struct file_operations globalvar_fops =
{
.read=globalvar_read,
.write=globalvar_write
};
//“globalvar”设备的全局变量
static int global_var = 0;
static int globalvar_init(void)
{
int ret;
//注册设备驱动
ret = register_chrdev(MAJOR_NUM,"globalvar",&globalvar_fops);
if (ret)
{
printk(KERN_EMERG "globalvar register failure\n");
}
else
{
printk(KERN_EMERG "globalvar register success\n");
}
return ret;
}
static void globalvar_exit(void)
{
int ret;
//注销设备驱动
ret = unregister_chrdev(MAJOR_NUM, "globalvar");
if (ret)
{
printk(KERN_EMERG "globalvar unregister failure\n");
}
else
{
printk(KERN_EMERG "globalvar unregister success\n");
}
}
static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off)
{
//将 global_var 从内核空间复制到用户空间
if (copy_to_user(buf, &global_var, sizeof(int)))
{
return - EFAULT;
}
return sizeof(int);
}
static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t
*off)
{
//将用户空间的数据复制到内核空间的 global_var
if (copy_from_user(&global_var, buf, sizeof(int)))
{
return - EFAULT;
}
return sizeof(int);
}
module_init(globalvar_init);
module_exit(globalvar_exit);
2、驱动程序Makefile
obj-m += globalvar.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
3、编译驱动程序
[root@localhost globalvar]# make
make -C /lib/modules/2.6.18-164.el5/build M=/root/globalvar modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-164.el5-i686'
Building modules, stage 2.
MODPOST
make[1]: Leaving directory `/usr/src/kernels/2.6.18-164.el5-i686'
[root@localhost globalvar]#
4、加载驱动程序
[root@localhost globalvar]# insmod globalvar.ko
Message from syslogd@ at Thu Jun 13 12:05:09 2013 ...
localhost kernel: globalvar register success[root@localhost globalvar]#
5、查看设备
[root@localhost globalvar]# cat /proc/devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
6 lp
7 vcs
10 misc
13 input
14 sound
21 sg
29 fb
116 alsa
128 ptm
136 pts
162 raw
180 usb
189 usb_device
216 rfcomm
252 globalvar
254 pcmcia
Block devices:
1 ramdisk
3 ide0
8 sd
9 md
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
253 device-mapper
254 mdp
[root@localhost globalvar]#
6、创建设备节点
[root@localhost globalvar]# mknod /dev/globalvar c 252 0
[root@localhost globalvar]#
查看
[root@localhost globalvar]# ls -l /dev/g*
crw-r--r-- 1 root root 252, 0 Jun 13 12:08 /dev/globalvar
srwx------ 1 root root 0 Jun 13 10:05 /dev/gpmctl
[root@localhost globalvar]#
7、应用程序源码(C语言)
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
main()
{
int fd, num;
// 打开“/dev/globalvar ”
fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR);
if (fd != -1 )
{
//初次读 globalvar
read(fd, &num, sizeof(int));
printf("The globalvar is %d\n", num);
//写globalvar
printf("Please input the num written to globalvar\n");
scanf("%d", &num);
write(fd, &num, sizeof(int));
//再次读 globalvar
read(fd, &num, sizeof(int));
printf("The globalvar is %d\n", num);
// 关闭“/dev/globalvar ”
close(fd);
}
else
{
printf("Device open failure\n");
}
}
8、应用程序编译与测试
[root@localhost test]# gcc -o globalvartest.o -c globalvartest.c
[root@localhost test]# ./globalvartest.o
bash: ./globalvartest.o: Permission denied
[root@localhost test]# ./globalvartest.o
bash: ./globalvartest.o: cannot execute binary file
[root@localhost test]# gcc -o globalvartest.o globalvartest.c
[root@localhost test]# ./globalvartest.o
The globalvar is 0
Please input the num written to globalvar
100
The globalvar is 100
[root@localhost test]# ./globalvartest.o
The globalvar is 100
Please input the num written to globalvar
121
The globalvar is 121
[root@localhost test]#
9、小结:
OK。通过测试!