ioremap和iounmap -- 内核态地址映射
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/io.h> /* for ioremap and iounmap */
static int __init hello_init(void)
{
void * v_addr = NULL;
unsigned long p_addr = 0xfe000000;
unsigned long size = 0x1000;
/* 将物理地址映射为虚拟地址 */
v_addr = ioremap(p_addr, size);
if (NULL == v_addr)
{
printk("ioremap 0x%lx error\n", p_addr);
}
else
{
/* 使用虚拟地址访问物理空间,访问结束后,释放本次映射 */
printk("*(unsigned int *)v_addr = 0x%x\n",
*(unsigned int *)v_addr);
iounmap(v_addr);
}
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zengxiaolong ");
MODULE_DESCRIPTION("A sample driver");
MODULE_SUPPORTED_DEVICE("testdevice");