proc虛擬文件系統也可以創建虛擬文件節點,實現用戶空間與內核空間的交互。在驅動中創建節點,可以實現對硬件的控制。proc_create函數原型(在kernel-3.10/include/linux/proc_fs.h文件)如下所示:
static inline struct proc_dir_entry *proc_create(
const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops)
{
return proc_create_data(name, mode, parent, proc_fops, NULL);
}
name:表示你要創建的設備節點的名稱,可隨意命名即可;
mode:表示節點的權限,一般賦值0644;
parent:表示父節點,如果直接在proc目錄創建節點,直接賦值NULL即可;
proc_fops:表示與節點相關聯的file_operations;
如下代碼是我實現的一個test程序,可供參考學習proc_create的使用:
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#define BUFSIZE 1024
static char *buf;
static unsigned int len;
/***********************
* file_operations->open
* 無操作
***********************/
static int test_proc_open(struct inode *inode, struct file *file)
{
return 0;
}
/************************
* file_operations->read
* 可以在adb工具進入機器的pro目錄,執行adb shell && cd proc && cat tets_rw,
* 即可讀出節點test_rw的內容是12345
************************/
static ssize_t test_proc_read(struct file *file,
char __user *buffer,size_t count, loff_t *f_pos)
{
if(*f_pos > 0)
return 0;
printk("---start read---\n");
printk("the string is >>>>> %s\n", buf);
if(copy_to_user(buffer, buf, len))
return -EFAULT;
*f_pos = *f_pos + len;
return len;
}
/************************
* file_operations->write
* 可以在adb工具進入機器的pro目錄,
* 執行adb shell && cd proc && echo 12345 > tets_rw,即可把12345寫入節點test_rw
************************/
static ssize_t test_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *f_pos)
{
if(count <= 0)
return -EFAULT;
printk("---start write---\n");
len = count > BUFSIZE ? BUFSIZE : count;
// kfree memory by kmalloc before
if(buf != NULL)
kfree(buf);
buf = (char*)kmalloc(len+1, GFP_KERNEL);
if(buf == NULL)
{
printk("test_proc_create kmalloc fail!\n");
return -EFAULT;
}
//memset(buf, 0, sizeof(buf));
memset(buf, 0, len+1);
if(copy_from_user(buf, buffer, len))
return -EFAULT;
printk("test_proc_write writing :%s",buf);
return len;
}
static struct file_operations test_fops = {
.owner = THIS_MODULE,
.open = test_proc_open,
// .release = single_release,
.read = test_proc_read,
// .llseek = seq_lseek,
.write = test_proc_write,
};
static int __init test_init(void)
{
struct proc_dir_entry* file;
//創建proc文件並關聯file_operations
file = proc_create("test_rw", 0644, NULL, &test_fops);
if (!file)
return -ENOMEM;
printk("test_rw init success!\n");
return 0;
}
static void __exit test_exit(void)
{
remove_proc_entry("test_rw", NULL);
printk("test_exit\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("caizd");
MODULE_DESCRIPTION("Proc_create Test Driver");
MODULE_LICENSE("GPL");
代碼編進kernel之後,可以在adb工具驗證是否可行。
C:\Users\asus>adb shell
root@inwatch_portal:/ # cd proc
cd proc
root@inwatch_portal:/proc # echo 12345 > test_rw
echo 12345 > test_rw
root@inwatch_portal:/proc # cat test_rw
cat test_rw
12345
root@inwatch_portal:/proc #
---------- 愛生活,愛安卓,愛Linux ----------