疑难问题:字符驱动中的IOCTL无法被调用,请高手看看!
前正在学习制作LINUX下的字符驱动,在驱动中将ioctl和mmap驱动都实现了。通过在linux应用程序中调用ioctl等系统调用去获取驱动中的ioctl提供的相应资源,但执行后发现驱动中的ioctl方法并未被调用,相当奇怪的问题。请各位高手帮忙一下,不甚感谢!
内核中实现如下:
staticint test_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
int abc;
printk("This is ioctl driver begin\n");
get_user(abc, (unsigned long*)arg);
put_user(10, (unsigned long*)arg);
printk("This is ioctl driver\n");
return 0;
}
static const struct file_operations chr_fops = {
.owner = THIS_MODULE,
.read = test_read,
.write= test_write,
.ioctl= test_ioctl,
.mmap=test_mmap,
.open = test_open,
.release= test_release,
};
应用程序调用如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
//#include <unistd.h>
#define MEM_DIY 0x2
#define MAP_SIZE0x100000UL
#define MAP_MASK(MAP_SIZE - 1)
void *vPhyMem = NULL;
int main()
{
int fd,
i,
nwrite,
nread;
char *buf = "hello\n";
char cReadBuf = {0};
unsigned long ops_ioctl = 0 ;
fd = open("/dev/fs", O_RDWR);
if(0 >= fd)
{
perror("open");
exit(1);
}
else
printf("Open success \n");
nwrite = write(fd, buf, strlen(buf));
if(nwrite < 0)
{
perror("Write");
exit(1);
}
else
printf("Write sucess \n");
nread = read(fd, cReadBuf, 6);
if(0 > nread)
{
perror("Read");
exit(1);
}
else
printf("Read is %s \n", cReadBuf);
ioctl(fd, (unsigned long)2, (unsigned long *)&ops_ioctl);
printf("ioctl do %d \n", ops_ioctl);
close(fd);
exit(0);
} 回复 1# zyhlovewd
你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。 回复zyhlovewd
你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。
xx_20111111 发表于 2011-11-14 10:45 http://bbs.chinaunix.net/images/common/back.gif
你好,每次IOCTL的返回值都是0, ops_ioctl的值总是4096.就算我实现IOCTL,将arg的值改变,ops_ioctl仍然没有改变! 我也刚学,互勉~~! ioctl(fd, (unsigned long)2, (unsigned long *)&ops_ioctl);
这段是干嘛的?应用程序中ioctl的模型:
ioctl(fd,unsigned long cmd,arg) 你把第二个参数cmd换个数字试试看,一般做ioctl这个方法时,都会使用幻数的! 第二个参数不要用2。可以用linux/ioctl.h里面提供的IOC宏
2好像被2.6的内核干掉了。以前我喜欢直接用0x01, 0x02,后来全改成IOW/IOR了 回复 6# hujinzhi
嗯,已经解决啦。换了个数字就成功调用啦。貌似数字2不能使用。 我也是发现2不能用,但是不知道为什么2不让用了,哪个高手指点下? 记得好像是定义两个宏,然后系统会随机生成那个幻数,记不太清楚了,回去查查
页:
[1]
2