免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11319 | 回复: 11
打印 上一主题 下一主题

疑难问题:字符驱动中的IOCTL无法被调用,请高手看看! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-13 11:44 |只看该作者 |倒序浏览
前正在学习制作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);

}

论坛徽章:
0
2 [报告]
发表于 2011-11-14 10:45 |只看该作者
回复 1# zyhlovewd


    你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。

论坛徽章:
0
3 [报告]
发表于 2011-11-14 14:12 |只看该作者
回复  zyhlovewd


    你查看一下ioctl的调用返回是不是成功了,成功为0,出错则为负数。
xx_20111111 发表于 2011-11-14 10:45



    你好,每次IOCTL的返回值都是0, ops_ioctl的值总是4096.就算我实现IOCTL,将arg的值改变,ops_ioctl仍然没有改变!

论坛徽章:
0
4 [报告]
发表于 2011-11-18 15:03 |只看该作者
我也刚学,互勉~~!

论坛徽章:
0
5 [报告]
发表于 2011-11-20 12:56 |只看该作者
ioctl(fd, (unsigned long)2, (unsigned long *)&ops_ioctl);
这段是干嘛的?应用程序中ioctl的模型:
ioctl(fd,unsigned long cmd,arg)

论坛徽章:
0
6 [报告]
发表于 2011-12-01 16:53 |只看该作者
你把第二个参数cmd换个数字试试看,一般做ioctl这个方法时,都会使用幻数的!

论坛徽章:
2
2015年亚洲杯之阿曼
日期:2015-03-23 18:11:212015亚冠之大阪钢巴
日期:2015-09-07 13:54:16
7 [报告]
发表于 2011-12-01 23:21 |只看该作者
第二个参数不要用2。可以用linux/ioctl.h里面提供的IOC宏

2好像被2.6的内核干掉了。以前我喜欢直接用0x01, 0x02,后来全改成IOW/IOR了

论坛徽章:
0
8 [报告]
发表于 2011-12-14 07:10 |只看该作者
回复 6# hujinzhi


嗯,已经解决啦。换了个数字就成功调用啦。貌似数字2不能使用。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:51:16
9 [报告]
发表于 2011-12-23 13:57 |只看该作者
我也是发现2不能用,但是不知道为什么2不让用了,哪个高手指点下?

论坛徽章:
0
10 [报告]
发表于 2012-04-13 17:00 |只看该作者
记得好像是定义两个宏,然后系统会随机生成那个幻数,记不太清楚了,回去查查
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP