我有
#define IOCTL_ALLOC_MSG _IO(MAJOR_NUM, 0)
#define IOCTL_DEALLOC_MSG _IO(MAJOR_NUM, 1)
在头文件中。
在驱动程序文件中我写道:
struct file_operations memory_fops = {
unlocked_ioctl: device_ioctl,
open: memory_open,
release: memory_release
};
int memory_init(void) {
int result;
/* Registering device */
result = register_chrdev(MAJOR_NUM, "memory", &memory_fops);
if (result < 0) {
printk("<1>memory: cannot obtain major number %dn", MAJOR_NUM);
return result;
}
allocfunc();
printk("<1>Inserting memory modulen");
return 0;
}
int device_ioctl(struct inode *inode, /* see include/linux/fs.h */
struct file *file, /* ditto */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param)
{
/*
* Switch according to the ioctl called
*/
printk ( "<l> inside ioctl n" );
switch (ioctl_num) {
case IOCTL_ALLOC_MSG:
allocfunc();
break;
case IOCTL_DEALLOC_MSG:
deallocfunc();
break;
}
return 0;
}
我创建了这样的字符文件
mknod /dev/memory c 60 0
应用调用失败
int main(int argc, char *argv[]) {
FILE * memfile;
/* Opening the device parlelport */
memfile=fopen("memory","r+");
if ( memfile <0) {
printf ( " cant open file n");
return -1;
}
/* We remove the buffer from the file i/o */
int ret_val;
if ( argc > 1 ) {
if ( strcmp (argv[1], "mem" ) ==0 ) {
ret_val = ioctl(memfile, IOCTL_ALLOC_MSG);
if (ret_val < 0) {
printf("ioctl failed. Return code: %d, meaning: %sn", ret_val, strerror(errno));
return -1;
}
}
当我运行该应用程序时,我得到"IOCTL 失败。返回码:-1,意思是:无效的参数"在:strerror(errno)
printk:
Inserting memory module
仅供参考,我尝试了"/dev/memory"内存"不同的名称和主要数字组合 - 但徒劳无功。
您正在向ioctl()
函数传递FILE*
,而它需要一个文件描述符,即int
。
它至少应该生成一个大警告,说您正在将指针转换为没有强制转换的整数,不是吗?
有两个明显的解决方案:
- 使用
fileno()
函数从FILE*
获取文件描述符。它应该是类似ioctl(fileno(memfile), IOCTL_ALLOC_MSG)
. - 使用
open()
而不是fopen()
。如果您正在编写低级代码,这是首选解决方案,因为您可以避免FILE*
强加的额外抽象层(所有缓冲内容等)。
我将假设将fopen("memory")
更改为fopen("/dev/memory1")
将解决代码的初始问题。
@SunEric 还在对您的问题的评论中指出,您在驱动程序的初始化函数 (memory_init()
) 中调用了 allocFunc()
,但这似乎是您希望您的IOCTL_ALLOC_MSG
做的事情。 这很可能是您需要解决的下一个问题。