#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("first_drv_open\n");
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("first_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,
};
int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv"); //mdev 根据这些信息自动创建
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
return 0;
}
static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
Makefile :
KERN_DIR = /home/youliang/share/work/kernel/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += first_drv.o
1. 主设备号
(1)主设备号可以自动获取也可以手工指定
手工指定
register_chrdev(“主设备号”, "first_drv", &first_drv_fops); //cat /proc/devices 下没有分配的主设备号
自动获取
major = register_chrdev(0, "first_drv", &first_drv_fops); // register_chrdev 返回主设备号
2. 设备节点
1. 手工创建 mknod /dev/xxx c 主设备号 次设备号
2. 通过 busybox mdev 命令自动创建
firstdrv_class = class_create(THIS_MODULE, "firstdrv"); //mdev 根据这些信息自动创建
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
阅读(1347) | 评论(0) | 转发(0) |