1.应用层code
# include <fcntl.h>
# include <stdio.h>
# include <string.h>
# include <sys/select.h>
# define DATA_NUM ( 64 )
int main ( ) {
int fd, i;
int r_len, w_len;
fd_set fdset;
char buf[ DATA_NUM] = "hello world" ;
memset ( buf, 0 , DATA_NUM) ;
fd = open ( "/dev/hello" , O_RDWR) ;
printf ( "%d\n" , fd) ;
if ( fd == - 1 ) {
printf ( "open file error\n" ) ;
return - 1 ;
}
else {
printf ( "open success\n" ) ;
}
w_len = write ( fd, buf, DATA_NUM) ;
r_len = read ( fd, buf, DATA_NUM) ;
printf ( "w_len %d r_len %d\n" , w_len, r_len) ;
return 0 ;
}
2.设备文件
代码中的open的“/dev/hello”是个设备文件
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
ls: 无法访问'/dev/hello' : 没有那个文件或目录
现在还没有创建这个设备文件
大部分驱动可以自动创建设备文件
也可以手动的创建设备文件
可以使用mknod命令创建设备文件
mknod [ OPTION] .. . NAME TYPE [ MAJOR MINOR]
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo mknod /dev/hello c 232 0
[ sudo] zhaoxr 的密码:
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crw-r--r-- 1 root root 232 , 0 4 月 24 22 :39 /dev/hello
查看驱动 lsmod
lsmod查看的是insmod的.ko文件
这个.ko文件的名字来自makefile文件的驱动名称
cat /proc/devices
查看列出字符和块设备的主设备号,以及分配到这些设备号的设备名称。
这个设备名称来自register_chrdev_region的第三个参数
3.应用层程序调用驱动过程
3.1 编写驱动helloDev.c
# include <linux/module.h>
# include <linux/moduleparam.h>
# include <linux/cdev.h>
# include <linux/fs.h>
# include <linux/wait.h>
# include <linux/poll.h>
# include <linux/sched.h>
# include <linux/slab.h>
# define BUFFER_MAX ( 10 )
# define OK ( 0 )
# define ERROR ( - 1 )
struct cdev * gDev = NULL ;
struct file_operations * gFile = NULL ;
dev_t devNum;
unsigned int subDevNum = 1 ;
int reg_major= 232 ;
int reg_minor = 0 ;
char * buffer = NULL ;
int flag = 0 ;
int hello_open ( struct inode * p, struct file * f) {
printk ( KERN_EMERG "hello_open\r\n" ) ;
return 0 ;
}
ssize_t hello_write ( struct file * f, const char __user * u, size_t s, loff_t * l) {
printk ( KERN_EMERG "hello_write\r\n" ) ;
return 0 ;
}
ssize_t hello_read ( struct file * f, char __user * u, size_t s, loff_t * l) {
printk ( KERN_EMERG "hello_read\r\n" ) ;
return 0 ;
}
int hello_init ( void ) {
devNum = MKDEV ( reg_major, reg_minor) ;
if ( OK == register_chrdev_region ( devNum, subDevNum, "helloworld" ) ) {
printk ( KERN_EMERG "register_chrdev_region ok\n" ) ;
}
else {
printk ( KERN_EMERG "register_chrdev_region error\n" ) ;
return ERROR;
}
printk ( KERN_EMERG "hello driver init\n" ) ;
gDev = kzalloc ( sizeof ( struct cdev ) , GFP_KERNEL) ;
gFile = kzalloc ( sizeof ( struct file_operations ) , GFP_KERNEL) ;
gFile-> open = hello_open;
gFile-> read = hello_read;
gFile-> write = hello_write;
gFile-> owner = THIS_MODULE;
cdev_init ( gDev, gFile) ;
cdev_add ( gDev, devNum, 3 ) ;
return 0 ;
}
void __exit hello_exit ( void ) {
cdev_del ( gDev) ;
unregister_chrdev_region ( devNum, subDevNum) ;
return ;
}
module_init ( hello_init) ;
module_exit ( hello_exit) ;
MODULE_LICENSE ( "GPL" ) ;
3.2 编写驱动的makefile
ifneq ( $( KERNELRELEASE) , )
obj- m : = helloDev. o
else
PWD : = $( shell pwd)
$( info "$(PWD)" )
KDIR : = / lib/ modules/ $( shell uname - r) / build
$( info "$(KDIR)" )
all:
make - C $( KDIR) M= $( PWD)
clean:
rm - rf * . o * . ko * . mod. c * . symvers * . c~ * ~ * . mod * . order
endif
3.3 编写应用层程序main.c
# include <fcntl.h>
# include <stdio.h>
# include <string.h>
# include <sys/select.h>
# include <sys/types.h>
# include <sys/stat.h>
# define DATA_NUM ( 64 )
int main ( ) {
int fd, i;
int r_len, w_len;
fd_set fdset;
char buf[ DATA_NUM] = "hello world" ;
memset ( buf, 0 , DATA_NUM) ;
fd = open ( "/dev/hello" , O_RDWR) ;
printf ( "%d\n" , fd) ;
if ( fd == - 1 ) {
printf ( "open file error\n" ) ;
return - 1 ;
}
else {
printf ( "open success\n" ) ;
}
w_len = write ( fd, buf, DATA_NUM) ;
r_len = read ( fd, buf, DATA_NUM) ;
printf ( "w_len %d r_len %d\n" , w_len, r_len) ;
return 0 ;
}
3.4 编写应用层makefile
out : main. o
gcc - o out main. o
main. o : main. c
gcc - c main. c
clean :
rm - rf * . o out
3.5编译驱动和加载驱动
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ make
"/home/zhaoxr/xiangrui.zhao/linux_kernel"
"/lib/modules/5.15.0-69-generic/build"
make -C /lib/modules/5.15.0-69-generic/build M = /home/zhaoxr/xiangrui.zhao/linux_kernel
make[ 1 ] : 进入目录“/usr/src/linux-headers-5.15.0-69-generic”
CC [ M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.o
MODPOST /home/zhaoxr/xiangrui.zhao/linux_kernel/Module.symvers
CC [ M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.mod.o
LD [ M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko
BTF [ M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko
Skipping BTF generation for /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko due to unavailability of vmlinux
make[ 1 ] : 离开目录“/usr/src/linux-headers-5.15.0-69-generic”
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls
helloDev.c helloDev.ko helloDev.mod helloDev.mod.c helloDev.mod.o helloDev.o Makefile modules.order Module.symvers user
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo insmod helloDev.ko
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22 :47:40 .. .
kernel:[ 169201.833551 ] register_chrdev_region ok
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22 :47:40 .. .
kernel:[ 169201.833556 ] hello driver init
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ lsmod
Module Size Used by
helloDev 16384 0
rfcomm 81920 4
ccm 20480 6
3.6 创建驱动对应的设备文件
利用主次设备号,创建驱动对应的设备文件,并设置权限,以方便应用层访问
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo mknod /dev/hello c 232 0
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crw-r--r-- 1 root root 232 , 0 4 月 25 22 :52 /dev/hello
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo chmod 777 /dev/hello
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crwxrwxrwx 1 root root 232 , 0 4 月 25 22 :52 /dev/hello
3.7 编译应用层程序并执行
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ make
gcc -c main.c
main.c: In function ‘main’:
main.c:28:13: warning: implicit declaration of function ‘write’; did you mean ‘fwrite’? [ -Wimplicit-function-declaration]
28 | w_len = write( fd, buf, DATA_NUM) ;
| ^~~~~
| fwrite
main.c:29:13: warning: implicit declaration of function ‘read’; did you mean ‘fread’? [ -Wimplicit-function-declaration]
29 | r_len = read( fd, buf, DATA_NUM) ;
| ^~~~
| fread
gcc -o out main.o
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ ls
main.c main.o Makefile out
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ ./out
3
open success
w_len 0 r_len 0
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22 :54:55 .. .
kernel:[ 169637.294848 ] hello_open
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22 :54:55 .. .
kernel:[ 169637.295049 ] hello_write
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22 :54:55 .. .
kernel:[ 169637.295056 ] hello_read
4.write的调用过程
应用层调用write,首先会调用C库函数,然后通过系统调用进入到内核里面。
内核中关于write的系统调用
在linux源代码的fs目录下有read_write.c文件
首先是SYSCALL_DEFINE
SYSCALL_DEFINE3 ( write, unsigned int , fd, const char __user * , buf,
size_t , count)
{
struct fd f = fdget_pos ( fd) ;
ssize_t ret = - EBADF;
if ( f. file) {
loff_t pos = file_pos_read ( f. file) ;
ret = vfs_write ( f. file, buf, count, & pos) ;
if ( ret >= 0 )
file_pos_write ( f. file, pos) ;
fdput_pos ( f) ;
}
return ret;
}
然后是vfs_write
ssize_t vfs_write ( struct file * file, const char __user * buf, size_t count, loff_t * pos)
{
ssize_t ret;
if ( ! ( file-> f_mode & FMODE_WRITE) )
return - EBADF;
if ( ! ( file-> f_mode & FMODE_CAN_WRITE) )
return - EINVAL;
if ( unlikely ( ! access_ok ( VERIFY_READ, buf, count) ) )
return - EFAULT;
ret = rw_verify_area ( WRITE, file, pos, count) ;
if ( ! ret) {
if ( count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
file_start_write ( file) ;
ret = __vfs_write ( file, buf, count, pos) ;
if ( ret > 0 ) {
fsnotify_modify ( file) ;
add_wchar ( current, ret) ;
}
inc_syscw ( current) ;
file_end_write ( file) ;
}
return ret;
}
然后是__vfs_write
ssize_t __vfs_write ( struct file * file, const char __user * p, size_t count,
loff_t * pos)
{
if ( file-> f_op-> write)
return file-> f_op-> write ( file, p, count, pos) ;
else if ( file-> f_op-> write_iter)
return new_sync_write ( file, p, count, pos) ;
else
return - EINVAL;
}
由于fd对应的file->f_op->write存在,因此会调用到我们自己驱动编写的hello_write