一个简单驱动程序

实现自动创建/dev/xx节点

#include <linux/clk.h>
#include <linux/err.h>
#include <linux/gfp.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/stat.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <asm/div64.h>
#include <linux/device.h>
#include <linux/cdev.h>
static int  test_open(struct inode *nd , struct file *fp)
{	
	printk(KERN_INFO "test_open()\n");
	return 0;


}
static int  test_ioctl(struct file *fp, unsigned int fd, unsigned long lg)
{
	printk(KERN_INFO "test_ioctl()\n");
	return 0;


}


static struct file_operations test_fops = {
	
	.owner =	THIS_MODULE,
	.open = test_open,
	.compat_ioctl = test_ioctl,




};




static int __init test_driver_init(void)
{
	int ret;
	struct class *myclass;
	struct cdev mycdev;
	dev_t mydev;
	int major;
	myclass =  class_create(THIS_MODULE, "rcar_test");
	if(myclass == NULL)
	{
		printk("class_create() fail\n");
	}
	
	printk(KERN_INFO "test_driver_init()\n");
	
	
	
	major = register_chrdev(0,"rcar_test", &test_fops);
	if(major < 0)
	{
		printk( "test_driver_init() fail\n");
	}
	mydev = MKDEV(major, 0); //转换dev_t 


	//cdev_init(&mycdev, &test_fops);
	//mycdev.owner = THIS_MODULE;
	
	//cdev_add(&mycdev, mydev, 1);


	device_create(myclass,NULL,mydev,NULL,"rcar_test");//创建/sys/class/设备节点和/dev/xxx


	return 0;


}


static void __exit test_driver_exit(void)
{
	printk(KERN_INFO " test_driver_exit()\n");
	unregister_chrdev(0,"rcar_test");
}


module_init(test_driver_init);
module_exit(test_driver_exit);


MODULE_AUTHOR("XXX <XXX@XXXX.com>");
MODULE_DESCRIPTION("XXX XXXX test driver");
MODULE_LICENSE("GPL v2");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值