我试图在内核空间中实现ioctl,以便将一些日期写入寄存器,在cmd的ioctl中我会崩溃。
下面是我的代码:
内核端:
static struct file_operations fops = {
.compat_ioctl = device_ioctl
};
int device_ioctl(struct inode *inode, struct file *filep,
unsigned int cmd, unsigned long arg)
{
int len = 200;
printk (KERN_INFO "In Device_ioctl !!\n");
switch(cmd)
{
case IOCTL_WRITE_REG:
write_ioctl((unsigned long *)arg);
break;
default:
printk (KERN_INFO "default\n");
return -ENOTTY;
}
printk (KERN_INFO "device_ioctl out\n");
return len;
}用户端
#define IOCTL_WRITE_REG _IOW(MAJOR_NUM, 1, int *)
void write_to_device(int write_fd)
{
int retval;
unsigned int to_write1 = 1;
retval = ioctl(write_fd, IOCTL_WRITE_REG, &to_write1);
if(retval < 0)
{
printf("fd: %d, write error: %d\n", write_fd, errno);
exit(-1);
}
}它没有进入device_ioctl函数,我哪里出错了?
发布于 2013-03-26 04:34:28
有几件事我碰巧注意到:
unlocked_ioctl而不是compat_ioctl。compat_ioctl允许32位用户空间程序在64位内核上调用ioctl调用.unlocked_ioctl)。预期的签名是:
long (*unlocked_ioctl) (struct * filep,unsigned,unsigned );我还没有真正地编译这段代码,但我认为这应该是可行的:
static struct file_operations fops = {
.unlocked_ioctl = device_ioctl
};
long device_ioctl(struct file *filep,
unsigned int cmd,
unsigned long arg)
{
int len = 200;
printk (KERN_INFO "In Device_ioctl !!\n");
switch(cmd)
{
case IOCTL_WRITE_REG:
write_ioctl((unsigned long *)arg);
break;
default:
printk (KERN_INFO "default\n");
return -ENOTTY;
}
printk (KERN_INFO "device_ioctl out\n");
return len;
}https://stackoverflow.com/questions/15628996
复制相似问题