Chinaunix
标题:
疑难问题:字符驱动中的IOCTL无法被调用,请高手看看!
[打印本页]
作者:
zyhlovewd
时间:
2011-11-13 11:44
标题:
疑难问题:字符驱动中的IOCTL无法被调用,请高手看看!
前正在学习制作LINUX下的字符驱动,在驱动中将ioctl和mmap驱动都实现了。通过在linux应用程序中调用ioctl等系统调用去获取驱动中的ioctl提供的相应资源,但执行后发现驱动中的ioctl方法
并未被调用,相当奇怪的问题。请各位高手帮忙一下,不甚感谢!
内核中实现如下:
static int 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_SIZE 0x100000UL
#define MAP_MASK (MAP_SIZE - 1)
void *vPhyMem = NULL;
int main()
{
int fd,
i,
nwrite,
nread;
char *buf = "hello\n";
char cReadBuf[6] = {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);
}
作者:
xx_20111111
时间:
2011-11-14 10:45
回复
1#
zyhlovewd
你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。
作者:
zyhlovewd
时间:
2011-11-14 14:12
回复 zyhlovewd
你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。
xx_20111111 发表于 2011-11-14 10:45
你好,每次IOCTL的返回值都是0, ops_ioctl的值总是4096.就算我实现IOCTL,将arg的值改变,ops_ioctl仍然没有改变!
作者:
chen_q07
时间:
2011-11-18 15:03
我也刚学,互勉~~!
作者:
cao527121128
时间:
2011-11-20 12:56
ioctl(fd, (unsigned long)2, (unsigned long *)&ops_ioctl);
这段是干嘛的?应用程序中ioctl的模型:
ioctl(fd,unsigned long cmd,arg)
作者:
hujinzhi
时间:
2011-12-01 16:53
你把第二个参数cmd换个数字试试看,一般做ioctl这个方法时,都会使用幻数的!
作者:
CN薰様
时间:
2011-12-01 23:21
第二个参数不要用2。可以用linux/ioctl.h里面提供的IOC宏
2好像被2.6的内核干掉了。以前我喜欢直接用0x01, 0x02,后来全改成IOW/IOR了
作者:
zyhlovewd
时间:
2011-12-14 07:10
回复
6#
hujinzhi
嗯,已经解决啦。换了个数字就成功调用啦。貌似数字2不能使用。
作者:
falloutmx
时间:
2011-12-23 13:57
我也是发现2不能用,但是不知道为什么2不让用了,哪个高手指点下?
作者:
hypnoscs
时间:
2012-04-13 17:00
记得好像是定义两个宏,然后系统会随机生成那个幻数,记不太清楚了,回去查查
作者:
neuliudapeng
时间:
2012-04-16 09:20
ioctl(fd, (unsigned long)2, (unsigned long *)&ops_ioctl);
这句话有问题,参数形式没有跟驱动程序定义的函数对应起来
作者:
cjdao
时间:
2012-04-17 08:53
关于这个问题<ldd>第六章里说得很清楚呀,去看看吧
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2