我有
#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 %d\n", MAJOR_NUM);
return result;
}
allocfunc();
printk("<1>Inserting memory module\n");
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: %s\n", ret_val, strerror(errno));
return -1;
}
}
当我运行应用程序时,我得到“ioctl 失败。返回代码:-1,意思是:无效参数”在:strerror(errno)
打印:
Inserting memory module
仅供参考,我尝试了“/dev/memory”“memory”不同的名称和主要数字组合 - 但徒劳无功。