#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#if 1
static int major = 0;
static int key_val = 0x65666768;
ssize_t hello_write(struct file *file,const char __user *buf,size_t size, loff_t *offset)
{
int err;
printk("Hello_write\n");
copy_from_user(&key_val,buf,4);
return 4;
}
ssize_t hello_read(struct file *file , char __user * buf, size_t size, loff_t *offset)
{
int err;
copy_to_user(buf,&key_val,4);
return 4;
}
static struct file_operations hello_funcs={
.owner = THIS_MODULE,
.read =hello_read,
.write =hello_write,
};
static struct class * qmcy;
static struct device * device;
int __init hello_init(void)
{
printk("Hello QMCY enter\n");
major = register_chrdev(0,"QMCY",&hello_funcs);
qmcy = class_create(THIS_MODULE,"QMCY"); //sys/class/QMCY
device_create(qmcy,NULL,MKDEV(major,0),NULL,"QMCY");//dev/QMCY
return 0;
}
void __exit hello_exit(void)
{
printk("Hello QMCY exit\n");
device_destroy(qmcy,MKDEV(major,0));
unregister_chrdev(major,"QMCY");
class_destroy(qmcy);
}
module_init(hello_init);
module_exit(hello_exit);
#else
int init_module()
{
printk("Hello first driver\n");
return 0;
}
void cleanup_module()
{
printk("Hello first exit\n");
}
#endif
MODULE_LICENSE("GPL");
Makefile文件内容
KERNEL= $(shell uname -r)
KERNEL_DIR=/lib/modules/$(KERNEL)/build
all:
make -C $(KERNEL_DIR) M=`pwd` modules
clean:
make -C $(KERNEL_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m +=hello.o
make 之后
生成hello.ko
insmod hello.ko
rmmod.ko

被折叠的 条评论
为什么被折叠?



