ioctl呼叫以一种奇怪的方式工作
我是Linux驱动程序新手,正在学习ioctl调用。 我制作了一个简单的测试程序来测试命令,如下所示 内核模块:ioctl呼叫以一种奇怪的方式工作,c,linux,device-driver,C,Linux,Device Driver,我是Linux驱动程序新手,正在学习ioctl调用。 我制作了一个简单的测试程序来测试命令,如下所示 内核模块: #include<linux/module.h> #include<linux/kernel.h> #include<linux/init.h> #include<linux/ioctl.h> #include<linux/cdev.h> #include<linux/fs.h> #i
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/ioctl.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/device.h>
MODULE_LICENSE("GPL");
static char kernel[20];
struct class *c_class;
dev_t dev;
struct cdev mcdev;
static int my_open(struct inode* inode, struct file *file)
{
printk("opened\n");
return 0;
}
static ssize_t my_write(struct file * file,const char * user,size_t count,loff_t * offset)
{
copy_from_user(kernel, user, count);
printk("%s\n",kernel);
return 0;
}
/*
check for command 2 as unsigned int
*/
static int ioctl_call(struct file *f, unsigned int cmd, unsigned long arg)
{
printk("cmd = %c\n",cmd);
switch(cmd)
{
case 1:
printk("case11\n");
break;
case 2:
printk("case12\n");
break;
default:
printk("case3\n");
break;
}
return 0;
}
static struct file_operations fops = {
.open = my_open,
.write = my_write,
.unlocked_ioctl = ioctl_call,
.owner = THIS_MODULE,
};
static int __init inin(void)
{
printk("sdfsdfsdf\n");
alloc_chrdev_region(&dev, 0, 1, "hello");
cdev_init(&mcdev, &fops);
cdev_add(&mcdev, dev, 1);
c_class = class_create(THIS_MODULE, "claaass");
device_create(c_class, NULL, dev, NULL, "devieee");
return 0;
}
module_init(inin);
static void __exit outpu(void)
{
device_destroy(c_class, dev);
class_destroy(c_class);
cdev_del(&mcdev);
unregister_chrdev_region(dev, 1);
}
module_exit(outpu);
#包括
#包括
#包括
#包括
#包括
#包括
#包括
模块许可证(“GPL”);
静态字符内核[20];
结构类*c_类;
发展与发展;
结构cdev-mcdev;
静态int my_open(结构索引节点*索引节点,结构文件*文件)
{
printk(“打开的”\n);
返回0;
}
静态ssize\u t my\u write(结构文件*文件,常量字符*用户,大小计数,loff\u t*偏移量)
{
从用户复制用户(内核、用户、计数);
printk(“%s\n”,内核);
返回0;
}
/*
检查命令2是否为无符号整数
*/
静态int ioctl_调用(结构文件*f、无符号int cmd、无符号长参数)
{
printk(“cmd=%c\n”,cmd);
开关(cmd)
{
案例1:
printk(“案例11\n”);
打破
案例2:
printk(“案例12\n”);
打破
违约:
printk(“案例3\n”);
打破
}
返回0;
}
静态结构文件\u操作fops={
.open=my_open,
.write=my_write,
.unlocked_ioctl=ioctl_调用,
.owner=此_模块,
};
静态int\uu init inin(void)
{
printk(“sdfsdfsdf\n”);
alloc_chrdev_区域(&dev,0,1,“你好”);
cdev_init(&mcdev,&fops);
cdev_添加(和mcdev,开发,1);
c_class=class_create(该_模块,“claaass”);
设备创建(c_类,NULL,dev,NULL,“设备”);
返回0;
}
模块初始化(inin);
静态无效\uuuu退出输出(无效)
{
设备销毁(c类,开发);
破坏级(c级);
cdev_del(和mcdev);
注销chrdev_地区的注册(dev,1);
}
模块_出口(输出);
应用程序:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<linux/ioctl.h>
int main()
{
int fd,a;
char to_kernel[100]="hello how are you";
fd = open("/dev/devieee", O_WRONLY);
if( fd < 0)
{
printf("Unable to open the device\n");
exit(0);
}
write(fd,to_kernel,20);
if(!(a=ioctl(fd, 2, to_kernel)))
printf("a = %d called\n",a);
printf("outside a = %d",a);
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
int-fd,a;
char to_kernel[100]=“你好”;
fd=打开(“/dev/deviceee”,仅限O_wr);
如果(fd<0)
{
printf(“无法打开设备\n”);
出口(0);
}
写入(fd,到_内核,20);
if(!(a=ioctl(fd,2,to_内核)))
printf(“a=%d称为\n”,a);
printf(“外部a=%d”,a);
返回0;
}
我想在驱动程序中打印案例2,但它从未调用ioctl,如果我在ioctl应用程序中放入任何其他内容,它将打印该案例,但不会打印2。请正确格式化代码。完全编辑代码需要花费太多的精力。。但是每天我都会做一些事情,你没有遵循,尽管我不认为这可以解释为什么1和2在你的代码中会有不同的行为。在应用程序中,
ioctl
调用返回什么值?我检查了它,它是0,这意味着ioctl成功返回。有人尝试过吗。。。它的完整代码。