#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
MODULE_LICENSE("GPL");
#define DEVICE_NAME "hello"
#define DEVICE_CLASS_NAME"helloclass"
struct cdev mycdev;
dev_t device_node;
static struct class *helloclass;
static __init int hello_init(void)
{
intrest;
rest=alloc_chrdev_region(&device_node,0,1, DEVICE_NAME);
if(rest < 0){
printk("%salloc_chrdev_region failed.\n",__func__);
returnrest;
}
cdev_init(&mycdev,NULL);
mycdev.owner= THIS_MODULE;
cdev_add(&mycdev,device_node, 1);
helloclass =class_create(THIS_MODULE, DEVICE_CLASS_NAME);
if(IS_ERR(helloclass)){
printk("%sclass_create failed.\n",__func__);
}
device_create(helloclass,NULL,device_node,NULL,DEVICE_NAME);
printk(KERN_ALERT"%s -- hello world,are you ok!\n",__func__);
return0;
}
static void hello_exit(void)
{
cdev_del(&mycdev);
device_destroy(helloclass,device_node);
class_destroy(helloclass);
unregister_chrdev_region(device_node,1);
printk(KERN_ALERT"%s -- hello world,are you ok!\n",__func__);
}
module_init(hello_init);
module_exit(hello_exit);
本文介绍了一个简单的Linux字符设备驱动程序实现过程。该程序通过注册字符设备,并创建相应的类和设备节点,实现了设备的基本初始化和退出操作。此外,还展示了如何分配字符设备号以及设备的增删管理。
776

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



