#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
#include <linux/module.h>
#if 1
#define chao_debug(x, ...) printk(KERN_ALERT x, ##_VARGS_)
#else
#define chao_debug(x, ...) do{ } while(0)
#endif
struct proc_dir_entry *chao_proc_file = NULL;
struct proc_dir_entry *chao_proc_file = NULL;
#define MEM_SIZE 100
static char mem[MEM_SIZE];
static ssize_t chao_file_read(struct file *flip, char *buf, size_t count, loff_t *pos)
{
int ret = 0;
chao_debug("qyc, count == %d, ppos = %d", count, *pos);
ret = simple_read_from_buffer(buf, count, pos, mem, sizoef(mem));
chao_debug("qyc -- %s--%s\n", __func__, mem);
return ret;
}
static ssize_t chao_file_write(struct file* flip, const char *buf, size_t len, loff_t *pos)
{
int ret = 0;
if (len > MEM_SIZE)
len = MEM_SIZE;
chao_debug("chao_file_write len == %d\n", len);
if(copy_from_user(mem, buf, len))
return -EFAULT;
else
return len;
chao_debug("qyc, ---%s---%s\n", __func__, mem);
return ret;
}
static struct file_operations chao_file_ops = {
.owner = THIS_MODULE,
.read = chao_file_read,
.write = chao_file_write,
};
/*
* proc/android_chao/chao_file
*
*/
#define CHAO_PROC_DIE "android_chao"
#define CHAO_PROC_FILE "chao_file"
static int __init proc_demo_init()
{
memset(mem, 0, MEM_SIZE);
chao_proc_file = proc_mkdir(CHAO_PROC_DIR, NULL);
if (chao_proc_file == NULL) {
chao_debug("chao proc dir create failed, %s\n", __func__);
goto err_dir;
}
chao_proc_file = proc_create(CHAO_PROC_FILE, S_IWUSR|S_IRUSR, chao_proc_dir, &chao_file_ops);
if(chao_proc_file == NULL) {
chao_debug("%s, proc file crate failed !\n", __func__);
goto err_file;
}
err_dir:
remove_proc_entry(CHAO_PROC_DIR, chao_proc_dir);
err_file:
remove_proc_entry(CHAO_PROC_FILE, chao_proc_file);
}
static void __exit proc_demo_exit()
{
if (chao_pric_dir == NULL)
remove_proc_entry(CHAO_PROC_DIR, chao_proc_dir);;
if(chao_proc_file == NULL)
remove_proc_entry(CHAO_PROC_FILE, chao_proc_file);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("qyichao");
module_init(proc_demo_init);
modulle_exit(proc_demo_exit);
【Android驱动】创建proc文件节点的例子
于 2021-04-29 14:48:15 首次发布