ioctl失败“没有这样的文件或目录”

时间:2013-01-30 00:33:21

标签: linux-device-driver embedded-linux ioctl

我正在尝试控制自定义电路板上的LED。我已经编写了设备驱动程序并成功加载了它。我在/ dev目录中创建了一个具有正确主编号的设备文件。然后我在用户平面上编写了一个程序来控制板上的LED,但是我的ioctl失败了,错误号为2(没有这样的文件或目录),返回的文件描述符是3.下面是用户空间中的应用程序。 / p>

int main(int argc, char **argv)
{
    struct  s_LEDControl sLedControlParam;  
    int     ledNumber;
    int     command;
    int     fDDevice;
    int     ioctlReturn;

    ledNumber   = atoi(argv[1]);
    command     = atoi(argv[2]);

    /* Prepare the message to be sent to Kernel Space */
    sLedControlParam.led = ledNumber;
    sLedControlParam.cmd = command;

    /* Open the device */
    fDDevice = open("/dev/newdevice_drv", O_RDWR);

    if(fDDevice == -1)
    {
         printf("ERROR: Cannot open the device /dev/newdevice_drv\n");
         exit(0);
    }

    ioctlReturn = ioctl(fDDevice, c_controlLED1, &sLedControlParam);

    if(ioctlReturn != 0)
    {
         printf("ERROR: ioctl failed Reason:%s FD:%d\n", strerror(errno), fDDevice);
    }

    close(fDDevice);
    return 0;
}

为了检查文件描述符是否有问题,我将一个虚拟文件描述符传递给ioctl,我得到了正确的错误号。我是linux和设备驱动程序的新手,任何帮助都将不胜感激。

上述C程序交叉编译到PPC环境。

以下是驱动程序代码的一部分

MODULE_LICENSE("XXX");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_SUPPORTED_DEVICE("XXXX");

/** Assign device functions to file_operation structure */
struct file_operations fops= {
        .owner=THIS_MODULE,
        .open=device_open,
        .release=device_release,
        .read=device_read,
        .write=device_write,
        .unlocked_ioctl=device_ioctl,
        .llseek=NULL,
};

static int device_ioctl( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
        enum e_FPGACommand e_cmd;
        e_cmd = (enum e_FPGACommand)cmd;

        printk("DEBUG: device_ioctl entered\n");

        switch(e_cmd)
        {
            case c_controlLED1: controlLED1_ioctl(arg);
            break;

            default: printk("ERROR: Invalid command %d\n", e_cmd);
                     return INVALID_IOCTL;
            break;
        }

        printk("DEBUG: device_ioctl exited\n");

        return OK;
}

static int controlLED1_ioctl(unsigned int arg)
{
    struct s_LEDControl *sLedControlParam;
    int result;

    sLedControlParam = (struct s_LEDControl *)kmalloc(sizeof(struct s_LEDControl), GFP_KERNEL);

    if(sLedControlParam == GLO_NULL)
    {
        printk("ERROR: Memory allocation failed in controlLED1_ioctl\n");
        return FAILURE_MEMORY_ALLOCATION;
    }

    result = copy_from_user(sLedControlParam, (struct s_LEDControl *)arg, sizeof(struct s_LEDControl));

    if(result > 0) 
    {
        printk("ERROR: copy_from_user in controlLED1_ioctl returned unfinished bytes\n");
        kfree(sLedControlParam);
        return FAILURE_COPY_FROM_USER;
    }

    if(sLedControlParam->cmd == On)
    {
        /* On - Do something */
    }

    return OK;
}

我无法看到任何错误消息,当设备驱动程序加载时,我得到所有打印件。当我尝试运行我的应用程序时,我可以看到设备已打开并立即关闭,但无法从我的ioctl功能中看到任何打印件。

1 个答案:

答案 0 :(得分:2)

选中answer

你有

static int device_ioctl( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)

但他说必须

static long device_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)

我还没有验证,但也许有帮助。我只是好奇,因为它之前从未见过unlocked_ioctl。