'How to mmap memory for miscdevice? Why my driver's mmap() is not called?
Driver's mmap() entry point not getting called.
This is the source code of my device driver:
struct miscdevice my_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "mymma",
.fops = &my_fops,
};
static const struct file_operations my_fops = {
.owner = THIS_MODULE,
.mmap = my_mmap,
};
static int __init my_module_init(void)
{
return my_init();
}
static void __exit my_module_exit(void)
{
my_exit();
}
int my_init(void)
{
int ret =0;
if ((ret = misc_register(&my_dev)))
{
printk(KERN_ERR "Unable to register \"my mma\" misc device\n");
return ret;
}
printk("kernel module installed\n");
return ret;
}
But my driver's mmap() entry point is not getting called.
This is the user space program calling it:
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(){
int fd=open("/dev/mymma",O_RDONLY);
if(fd<0)
exit(0);
printf("helllo\n");
int N=5;
int *ptr = mmap ( NULL, N*sizeof(int),
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0 );
if(ptr == MAP_FAILED){
printf("Mapping Failed\n");
return 1;
}
for(int i=0; i<N; i++)
ptr[i] = i*10;
for(int i=0; i<N; i++)
printf("[%d] ",ptr[i]);
printf("\n");
int err = munmap(ptr, 10*sizeof(int));
if(err != 0){
printf("UnMapping Failed\n");
return 1;
}
return 0;
}
Solution 1:[1]
Provide the mmap() entry point of your driver.
I can notice that the device node is opened RDONLY but you are calling mmap() with PROT_READ/WRITE. Moreover MAP_ANONYMOUS makes mmap() ignore the file descriptor: you are merely allocating some space in memory. That is why you don't reach your driver.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
