2017-02-14 530 views
0

我有以下chardev定义:入门上的ioctl ENOTTY针对Linux内核模块

.H

#define MAJOR_NUM 245 
#define MINOR_NUM 0 
#define IOCTL_MY_DEV1 _IOW(MAJOR_NUM, 0, unsigned long) 
#define IOCTL_MY_DEV2 _IOW(MAJOR_NUM, 1, unsigned long) 
#define IOCTL_MY_DEV3 _IOW(MAJOR_NUM, 2, unsigned long) 

模块的.c

static long device_ioctl(
        struct file* file, 
        unsigned int ioctl_num, 
        unsigned long ioctl_param) 
{ 
    ... 
} 

static int device_open(struct inode* inode, struct file* file) 
{ 
    ... 
} 

static int device_release(struct inode* inode, struct file* file) 
{ 
    ... 
} 

struct file_operations Fops = { 
    .open=device_open, 
    .unlocked_ioctl= device_ioctl, 
    .release=device_release 
}; 

static int __init my_dev_init(void) 
{ 
    register_chrdev(MAJOR_NUM, "MY_DEV", &Fops); 
    ... 
} 
module_init(my_dev_init); 

我的用户代码

ioctl(fd, IOCTL_MY_DEV1, 1); 

始终以相同错误失败:ENOTTY

Inappropriate ioctl for device

我已经看到了类似的问题: 即

Linux kernel module - IOCTL usage returns ENOTTY

Linux Kernel Module/IOCTL: inappropriate ioctl for device

但对我来说

+0

您是否验证过您的'device_ioctl()'是否被调用? (在那里放一个printk调用) – nos

+0

@nos我的'device_ioctl()'没有被调用,只用'printk'放置一个空的主体 – Mugen

+0

你没有很好的注册ioctl的调用版本...试试锁定的版本。 –

回答

1

ENOTTY是由内核发出他们的解决方案没有奏效当你的设备驱动程序没有注册一个ioctl函数来调用。我担心你的功能没有很好的注册,可能是因为你已经在struct file_operations结构的.unlocked_ioctl字段中注册了它。

如果将其注册到函数的锁定版本中,可能会得到不同的结果。最可能的原因是,该inode被锁定为ioctl调用(因为它应该是,以避免同时readwrite业务竞争条件相同的设备)

对不起,我得给Linux源代码树进不去为使用的字段的正确名称,但肯定你可以自己找到它。