- 论坛徽章:
- 0
|
前正在学习制作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);
} |
|