fedora编写驱动实例

转自http://blog.csdn.net/xiaodong60606/article/details/45716715


#include <linux/module.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/uaccess.h> 

#define HELLO_MAJOR 250
#define HELLO_MINOR 0
#define NUMBER_OF_DEVICES 2

struct class *hello_class;
static struct cdev cdev;
dev_t devno;

static ssize_t hello_read(struct file *file, char __user *buf, size_t count,
				loff_t *ppos)
{
	char *str = "hello world";

	copy_to_user(buf,str,strlen(str));
	*(buf + strlen(str)) = '\n';
	return count;
}

static ssize_t hello_open(struct inode *inode,struct file *file)
{
	return 0; 
}

static const struct file_operations hello_fops = {
		.open = hello_open,
		.read = hello_read,
	    .owner = THIS_MODULE,
};

static int __init hello_init(void)
{
	int ret;
	devno = MKDEV(HELLO_MAJOR,HELLO_MINOR);

	if(HELLO_MAJOR){
		ret = register_chrdev_region(devno,NUMBER_OF_DEVICES,"chrdev");
	}else{
		ret = alloc_chrdev_region(&devno, 0, NUMBER_OF_DEVICES, "chrdev");
	}
	if(ret < 0){
		printk("%s register chrdev error\n",__func__);
		return ret;
	}

	hello_class = class_create(THIS_MODULE,"hello_char_calss");
	if(IS_ERR(hello_class)){
		printk("%s create class error\n",__func__);
		return -1;
	}

	device_create(hello_class, NULL, devno, NULL, "chrdev");
	
	
	cdev_init(&cdev, &hello_fops);
	cdev.owner = THIS_MODULE;
	cdev_add(&cdev, devno, NUMBER_OF_DEVICES);

	return 0;
}


static void __exit hello_exit(void)
{
	printk("%s",__func__);
	cdev_del(&cdev);
	device_destroy(hello_class,devno);
	class_destroy(hello_class);
	unregister_chrdev_region(devno,NUMBER_OF_DEVICES);

}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL"); 

Makefie

见上一篇。


测试

#include <stdio.h>
#include <fcntl.h>

int main(void)
{
	int fd;
	int i;	
	char buf[50];

	fd = open("/dev/chrdev",O_RDWR);
	if(fd < 0){
		printf("can't open dev\n");
		return -1;
	}

	read(fd,buf,11);
	
	printf("%s",buf);

	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值