源文件
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
static int g_simple_cdev_major_no;
static struct class *g_simple_cdev_class;
static struct device *g_simple_cdev_device;
static int simple_cdev_open(struct inode *inode, struct file *file)
{
return 0;
}
static int simple_cdev_release(struct inode *inode, struct file *file)
{
return 0;
}
static int simple_cdev_mmap(struct file *file, struct vm_area_struct *vma)
{
return 0;
}
/* File operations struct for character device */
static const struct file_operations g_simple_cdev_fops = {
.owner = THIS_MODULE,
.open = simple_cdev_open,
.release = simple_cdev_release,
.mmap = simple_cdev_mmap,
};
static int __init simple_cdev_init(void)
{
g_simple_cdev_major_no = register_chrdev(0, "simple_cdev", &g_simple_cdev_fops);
g_simple_cdev_class = class_create(THIS_MODULE, "simple_cdev");
g_simple_cdev_device = device_create(g_simple_cdev_class, NULL,
MKDEV(g_simple_cdev_major_no, 0), NULL, "simple_cdev");
return 0;
}
static void __exit simple_cdev_exit(void)
{
printk(KERN_INFO "Goodbye world\n");
}
MODULE_LICENSE("GPL");
module_init(simple_cdev_init);
module_exit(simple_cdev_exit);
- 由regiser_chrdev自动申请某个设备号
- 由class_create和device_create自动生成字符设备结点/dev/simple_cdev,设备号从regiset_chrdev获取
Makefile
obj-m := simple_cdev.o
PWD := $(shell pwd)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean