最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】驱动测试程序中无法打开设备文件

工作和技术 crifan 1727浏览 0评论

【已解决】驱动测试程序中无法打开设备文件

实现了一个简单的字符设备,然后按照网上其它人的教程中的例子,
自己写了个驱动测试文件,内容如下:

————————————————————–
#include <stdio.h>
#include <linux/stat.h>
//#include "linux/stat.h"

#include <sys/types.h>
#include <fcntl.h>

int main(void)
{
int testChg;
int test;

//test = open("/dev/spi", S_IRWXU|S_IRWXG|S_IRWXO);
test = open("/dev/spi", O_WRONLY);
if(test==-1)
   printf("Error when open some device.n");
else
   printf("Test device Open is OK, test=%d.n", test);

printf("Now will open /dev/chg.n");

//testChg = open("/dev/chg", S_IRWXU);
//testChg = open("/dev/chg", S_IRWXU|S_IRWXG|S_IRWXO);

testChg = open("/dev/chg", O_WRONLY);

printf("open result: testChg =%d.n", testChg);

if(testChg == -1)
   return -1;

printf("Open is OK.n");

printf("Now will do ioctl().n");
ioctl(testChg, 1, 0);

printf("ioctl is done.n");

close(testChg);

return 0;

}
————————————————————–

其中注释掉的代码是原先的实现,其中用到了参数SS_IRWXU,对应的linux/stat.h有如下定义:

(注:linux kernel里面的是linux/stat.h中,增加了一些其他组合定义的宏,比如S_IWUGO,表示

#define S_IWUGO   (S_IWUSR|S_IWGRP|S_IWOTH)

而这些组合的定义,对于我当前用到的buildroot中的buildrootbuild_armstaging_dirusrincludelinux 中的stat.h,竟然没有,我之前由于这个错误,搞好了半天,才发现这个问题的。。。)

————————————————————–
#ifdef __KERNEL__
………….
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
………….
#endif

————————————————————–

其意思很明显,是针对某文件或文件夹,定义了不同的用户/组/其他等人的访问权限,

而驱动测试文件里面的,传递给打开设备时候的参数,此处用的是O_WRONLY,
是针对设备文件的权限,其定义在fcntl.h中:
————————————————————–
#define O_ACCMODE 00000003
#define O_RDONLY 00000000
#define O_WRONLY 00000001
#define O_RDWR   00000002

————————————————————–
上面用的O_WRONLY表示可读写。

所以,用被注释掉的那部分代码去测试设备文件的打开,始终都不成功,编译运行测试例子,都是输出:
————————————————————–
# ./chgDemo
Error when open some device.
Now will open /dev/chg.
open result: testChg =-1.

————————————————————–

最后都改成对应的设备文件访问参数O_WRONLY之后,代码运行就正常了,输出:

————————————————————–
# ./chgDemo
Test device Open[ 4063.590000] as352x_afe_charger_ioctl: cmd=1, arg=0
is OK, [ 4063.590000] test, call is connected.
test=3.
Now will open /dev/chg.
open result: testChg =4.
Open is OK.
Now will do ioctl().
ioctl is done.

————————————————————–

转载请注明:在路上 » 【已解决】驱动测试程序中无法打开设备文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.160 seconds, using 22.05MB memory