读写文件节点---echo与read、write均可

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/uaccess.h>
 
 
 
MODULE_LICENSE("GPL");
 
#define DEVICE_NAME ("my_dev")
#define GLOBALMEM_SIZE    512    /*全局内存最大4K字节*/
 
 
int MAJOR_NUM = 666;
char g_val[20];
struct device *dev;
struct class *myclass;
 
 
static ssize_t global_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
	int ret;
	char val[20];
	printk(KERN_ERR "###### global_read \n");
	
	sprintf(val, "%s\n", g_val);
	//ssize_t simple_read_from_buffer(void __user *to, size_t count,loff_t *ppos, const void *from, size_t available);
	ret = simple_read_from_buffer(buf, len, off, val, strlen(val));
	return ret;
}
 
static ssize_t global_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
	int  ret = 0;
	char val[20];	
	int i =0, j = 0;
	unsigned long p = *off;
	unsigned int count = len;
 
	memset(val, 0, 20);
	
	/*分析和获取有效的写长度*/
	if (p >= GLOBALMEM_SIZE)
	{
		return count ?  - ENXIO: 0;		
	}
	if (count > GLOBALMEM_SIZE - p)
	{
		count = GLOBALMEM_SIZE - p;
	}
	
	if(copy_from_user(val, buf, count))
	{
		ret = -EFAULT;
	}
	else
	{
		*off += count;
		ret = count;
	}	
	memset(g_val, 0, 20);
	
	for(i = 0; i < strlen(val); i++)
	{
		if(val[i] >= '0' && val[i] <= '9')
		{
			g_val[j] = val[i];
			j++;
		}
	}	
	return ret;
}
 
struct file_operations fileops = {
	.read = global_read,
	.write = global_write,
};
 
static int __init globalvar_init(void)
{
	int ret;
	memset(g_val, 0, 20);
	
	printk(KERN_ERR "golabvar_init \n");
	
	ret = register_chrdev(MAJOR_NUM, "my_driver", &fileops);
	if(ret)
	{
		printk(KERN_ERR "register_chrdev fail \n");
	}
	else
	{
		printk(KERN_ERR "register_chrdev sucess \n");
		//注册一个类,使mdev可以在"/dev/"目录下 面建立设备节点
		myclass = class_create(THIS_MODULE, DEVICE_NAME);
		//创建一个设备节点,节点名为DEVICE_NAME
		dev = device_create(myclass, NULL, MKDEV(MAJOR_NUM, 0), NULL, DEVICE_NAME);
		if(!dev)
		{
			printk(KERN_ERR "device_create faile \n");
		}
		
	}	
	
	return 0;
} 
 
static void __exit globalvar_exit(void)
{
	printk("golabvar_exit \n");
	device_destroy(myclass, MKDEV(MAJOR_NUM, 0));
	class_destroy(myclass);
	unregister_chrdev(MAJOR_NUM, "globalvar");
}
 
module_init(globalvar_init);
module_exit(globalvar_exit);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值