快速写一个驱动程序并由User程序调用

KernelSpace

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/mm.h>

#define DEMO_NAME "zhouhehe"

static dev_t dev;
static struct cdev *demo_cdev;
static signed count = 1;

static int zhouhehe_open(struct inode *inode, struct file *file)
{
	int major = MAJOR(inode->i_rdev);
   
	int minor = MINOR(inode->i_rdev);

	printk("%s: major=%d, minor=%d\n",__func__,major,minor);
return 0;
}


static ssize_t zhouhehe_read(struct file *file, char __user *buf,size_t lbuf,loff_t *ppos)
{
	printk("%s enter\n",__func__);

	return 0;

}

static ssize_t zhouhehe_write(struct file *file, const char __user *buf,size_t count,loff_t *f_pos)
{
	printk("%s enter\n",__func__);

	return 0;

}


static const struct file_operations demodrv_fops = {
	.owner = THIS_MODULE,
	.open = zhouhehe_open,
	.read = zhouhehe_read,
	.write = zhouhehe_write
};

struct class *zhouhehe_class;

static int __init simple_char_init(void)
{
	int ret;
	
	printk("zhouhehe----simple_char_init\n");
	
	ret = alloc_chrdev_region(&dev,0,count,DEMO_NAME);
	if(ret)
	{
		printk("failed to allocate char device region\n");
		return ret;
	}

	demo_cdev = cdev_alloc();
	if(!demo_cdev) 
	{
		printk("cdev_alloc failed\n");
		goto unregister_chrdev;
	}
	
	cdev_init(demo_cdev,&demodrv_fops);
	ret = cdev_add(demo_cdev,dev,count);
	if(ret)
	{
		printk("cdev_add failed\n");
		goto cdev_fail;
	}
	zhouhehe_class = class_create(THIS_MODULE,DEMO_NAME);
	device_create(zhouhehe_class,NULL,dev,NULL,DEMO_NAME);
	
	printk("successed register char device: %s\n",DEMO_NAME);
	printk("Major number = %d,minor number = %d\n",MAJOR(dev),MINOR(dev));
	
	return 0;
	
cdev_fail:
	cdev_del(demo_cdev);
unregister_chrdev:
	unregister_chrdev_region(dev,count);

	return ret;

} 


static void __exit simple_char_exit(void)
{
	printk("removing device\n");

	if(demo_cdev)
		cdev_del(demo_cdev);

	unregister_chrdev_region(dev,count);

}

module_init(simple_char_init);
module_exit(simple_char_exit);

MODULE_LICENSE("GPL");

Userspace

# include <stdio.h>

# include <fcntl.h>

# include <unistd.h>

#include <sys/mman.h>
#include <stdlib.h>

#define DEMO_DEV_NAME "/dev/zhouhehe"


int count = 0;
int parse_paras(int argc,char *argv[])       
{      
	int ch;       
	opterr=0;       
	while((ch=getopt(argc,argv,"n:"))!=-1)       
	{       
		printf("optind:%d\n",optind);       
		printf("optarg:%s\n",optarg);       
		printf("ch:%c\n",ch);       
		switch(ch)     
		{       
			case 'n':      
				printf("option n:%s\n",optarg);
				count = atoi(optarg);
				printf("=====count=%d\n",count);
				break;
			default:       
				printf("Knowed option:%c\n",ch);
		}
		printf("optopt+%c\n",optopt);       
	}
 
	return 0;
}


int main(int argc, char *argv[]) 
{
	char buffer[64];
	int fd;
	int i = 0;
	
	parse_paras(argc,argv);
	
	printf("zhouhehe=============111111111\n");

	
	for(i=0;i<count;i++)
	{
		printf("main[%d] start...\n",i);
		fd = open(DEMO_DEV_NAME,O_RDONLY);
		if(fd<0) 
		{
			printf("open device %s failed\n",DEMO_DEV_NAME);
			return -1;
		}
		printf("zhouhehe=============test[%d]\n",i);

		read(fd,buffer,64);

		close(fd);
	}
	free(paddr);

	printf("main test done!!!\n");

	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码改变世界ctw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值