linux设备驱动框架

#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>

#define HELLO_MAJOR 250

/*2.实现操作硬件的方法*/
static int hello_open(struct inode *inode, struct file *file)
{
/*初始化设备*/
printk(KERN_INFO "%s()-%d:hello open\n",__func__,__LINE__);

return 0;
}

static ssize_t hello_read(struct file *file, char __user *buf, size_t size, loff_t *opps)
{
printk(KERN_INFO "%s()-%d:hello read\n",__func__,__LINE__);
return size;
}

static ssize_t hello_write(struct file *file, const char __user *buf, size_t size, loff_t *opps)
{
/*操作设备*/
printk(KERN_INFO "%s()-%d:hello write\n",__func__,__LINE__);

return size;
}

/*1.构建struct file_operations结构体*/
struct file_operations hello_fops ={
.owner =THIS_MODULE,
.open =hello_open,
.write =hello_write,
.read =hello_read,
};

static int __init hello_init(void)
{
int ret;

/*1.注册操作方法/申请主设备号
*param1:主设备号
*param2:驱动模块的名字
*param3:操作方法
*/
ret=register_chrdev(HELLO_MAJOR, "hello_module", &hello_fops);
if(ret<0){
printk(KERN_ERR "register device major failed!\n");
return -ENODEV;
}

printk(KERN_INFO "%s()-%d:hello init\n",__func__,__LINE__);

return 0;
}

static void __exit hello_exit(void)
{
unregister_chrdev(HELLO_MAJOR, "hello_module");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

posted on 2013-12-26 21:41  weat  阅读(235)  评论(0编辑  收藏  举报