动手写linux驱动(2)--Apple的学习笔记

一,前言

动手写驱动系列将从简单到复杂,我在复习的同时,做了笔记,从小牛到大牛就是这样一步步走过来的。等我将来回头看还是很有成就感的,同时给要系统学习的同学一个学习路线的参考方向。
在动手写linux驱动(1)--Apple的学习笔记后,今天在工程3中添加ioctl和llseek。在工程4中将设备创建为3个并且驱动中添加互斥锁。
代码工程放在我gitee上https://gitee.com/applecai/linux-driver-study

二,问题记录

  1. unlocked_ioctl对应的宏定义参考了ioctl-number.rst,找了个没有用的0xB7。
  2. 添加llseek,主要应用了ppos参数。一开始当个命令测试了,即打开文件操作单独命令后关闭文件,所以ppos一直为0。因为打开后就关闭fd。后来修改了代码,实现连续操作。
  3. 多设备创建后无法释放导致卸载模块再重新加载后出现oop,这是因为__exit中的参数设置不对。
  4. APP程序调用open返回失败。因为setup_dev那个函数传的参数中index应该从6开始。虽然表明看sys/class及device都是6,7,8。但是cdev_add的inode是不对的。
  5. APP程序调用参数为dev7或者dev8不正确会死。但dev6则正确,原因是kmalloc申请空间的时候没有乘以3。

三,测试效果

工程3的效果:
APP中的lseek操作为retvalue = lseek(fd, -6, APPLEPAPER_CUR_POS); 写入后指针为18,此句代码的含义是指针从当前位置向前移动6个位置。我写入Apple's usr data!向前移动6个位置后再开始读取,测试通过如下:

# ./applepaper3 /dev/applepaper6 
goto file applepaperApp3.c,line 88[   63.427014] applepaper is set to zero

start to write usrdata:Apple's usr data!
[   63.434079] written 18 bytes(s) from 0
[   63.441267] filp->f_pos=12
[   63.441280] read 18 bytes(s) from 12
In the end,read data:data!

retvalue = lseek(fd, 8, APPLEPAPER_BEGIN_POS);写入后指针为18,此句代码的含义是指针从开始位置向后移动8个位置。我写入Apple's usr data!则从第8个位置后再开始读取,测试通过如下:

# ./applepaper3 /dev/applepaper6 
goto file applepaperApp3.c,line 88[   90.183172] applepaper is set to zero

start to write usrdata:Apple's usr data!
[   90.189684] written 18 bytes(s) from 0
[   90.198539] filp->f_pos=8
[   90.198562] read 18 bytes(s) from 8
In the end,read data:usr data!

工程4的效果
工程4包括2个锁的宏定义(打开1个即可),一种是open/close的时候使用锁,代表文件不能同时打开。将宏定义FILE_LOCK设置为1即可APP和driver都设置为1。效果如下,说明要等第一次运行close后才能再次打开设备文件。

# ./applepaper4 /dev/applepaper6 &
# goto file applepaperApp4.c,line 88[  140.730968] applepaper is set to zero

start to write usrdata:Apple's usr data!
[  140.737775] written 18 bytes(s) from 0
[  140.746198] filp->f_pos=8
[  140.746219] read 18 bytes(s) from 8
In the end,read data:usr data!
./applepaper4App running times:1
 /dev/applepaperApp running times:2
6
App running times:3
App running times:4
App running times:5
App running times:6[  170.756126] applepaper is set to zero

goto file applepaperApp4.c,line 88
start to write usrdata:App[  170.760564] written 18 bytes(s) from 0
le's usr data!
[  170.771010] filp->f_pos=8
[  170.771030] read 18 bytes(s) from 8
In the end,read data:usr data!
[  175.522953] random: crng init done
App running times:1
App running times:2
App running times:3
App running times:4
App running times:5
App running times:6
[1]+  Done                       ./applepaper4 /dev/applepaper6

还有一个宏定义FILEOP_LOCK设置为1,代表读写即擦除过程加锁。driver中设置宏定义即可。效果如下,做了个sh脚本./run.sh让一个在后台运行,一个在前台运行。这个看起来不明显。

# insmod applepaper4.ko
[   21.523194] applepaper4: loading out-of-tree module taints kernel.
[   21.530767] Registered character driver
[   21.536164] Registered character driver
[   21.540895] Registered character driver
# ./applepaper4 /dev/applepaper6
goto file applepaperApp4.c,line 88[   33.450871] applepaper is set to zero

start to write usrdata:Apple's usr data!
[   33.457846] written 18 bytes(s) from 0
[   33.466101] filp->f_pos=8
[   33.466118] read 18 bytes(s) from 8
In the end,read data:usr data!
# ./run.sh
run one in backgroup,run other in console
goto file applepaperApp4.c,line 88[   38.516497] applepaper is set to zero

start to write usrdata:Apple's usr data!
[   38.525298] applepaper is set to zero
goto file applepaperApp4.c,line 88
start to write usrdata:Apple[   38.530707] written 18 bytes(s) from 0
's usr data!
[   38.541227] written 18 bytes(s) from 0
[   38.545911] filp->f_pos=8
[   38.545928] read 18 bytes(s) from 8
In the end,read data:usr data!
# [   38.555957] filp->f_pos=8
[   38.555979] read 18 bytes(s) from 8
In the end,read data:usr data!

你可能感兴趣的:(动手写linux驱动(2)--Apple的学习笔记)