菜鸟求教
为linux加载最简单hello.ko驱动时,并没有输出printk提示信息,但是卸载它时,却提示了printk提示信息.程序如下,
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT " Hello World enter\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT " Hello World exit\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Song Baohua");
MODULE_DESCRIPTION("A simple Hello World Module");
MODULE_ALIAS("a simplest module");
编译后生成hello.ko文件,copy到NFS网络文件系统里。运行
insmod ./hello.ko 没有任何提示信息,但是运行
rmmod hello 却可以提示信息: Hello World exit
求教这是什么原因啊,第一次学写驱动。
另外我跟着韦东山的学写字符驱动设备额,也遇见类似的问题。加载后,我
cat /proc/device 中并没法有发现为加载的主设备号,但是
lsmod里显示模块是加载成功了的,请问这是什么原因啊?是不是为的内核移植的时候有问题啊?就指点迷津
程序如下
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("first_dry_open\n");
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("irst_drv_write\n");
return 0;
}
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open,
.write = first_drv_write,
};
static int first_drv_init(void)
{
register_chrdev(111, "first_drv", &first_drv_fops); // 注册, 告诉内核, 之前已经查看过 主设备号并没有被占用
return 0;
}
static void first_drv_exit(void)
{
unregister_chrdev(111, "first_drv"); // 卸载
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");