Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97740
  • 博文数量: 23
  • 博客积分: 516
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-29 16:45
文章分类
文章存档

2012年(11)

2011年(12)

分类: LINUX

2012-03-22 13:26:00

proc 文件系统是一种内核和内核模块用来向进程 (process) 发送信息的机制 (所以叫做 proc)。这个伪文件系统让你可以和内核内部数据结构进行交互,获取 有关进程的有用信息,在运行中 (on the fly) 改变设置 (通过改变内核参数)。 与其他文件系统不同,proc 存在于内存之中而不是硬盘上。

proc 文件系统可以被用于收集有用的关于系统和运行中的内核的信息。下面是一些重要 的文件:

  • /proc/cpuinfo - CPU 的信息 (型号, 家族, 缓存大小等)
  • /proc/meminfo - 物理内存、交换空间等的信息
  • /proc/mounts - 已加载的文件系统的列表
  • /proc/devices - 可用设备的列表
  • /proc/filesystems - 被支持的文件系统
  • /proc/modules - 已加载的模块
  • /proc/version - 内核版本
  • /proc/cmdline - 系统启动时输入的内核命令行参数

proc文件系统可用于查看硬件的寄存器状态,并可以对其进行写操作。以下实例用于模拟利用proc文件系统对寄存器进行读写操作
#include
#include
#include
#include
#include

#include

#define MAX_BUFFER_SIZE        256
#define    AXP192_PROC_FILE    "driver/axp192"
static struct proc_dir_entry *axp192_proc_file;
static int index;
static u8 buff[MAX_BUFFER_SIZE];

static void axp192_write(int index, u8 reg_val)
{
    buff[index] = reg_val;
}

static void axp192_read(int index, u8 *reg_val)
{
    *reg_val = buff[index];
}

static ssize_t axp192_proc_read(struct file *filp,
        char *buffer, size_t length, loff_t *offset)
{
    u8 reg_val;

    if ((index < 0) || (index > MAX_BUFFER_SIZE))
        return 0;

    axp192_read(index, ®_val);
    printk(KERN_INFO "register 0x%x: 0x%x\n", index, reg_val);
    return 0;
}

static ssize_t axp192_proc_write(struct file *filp,
        const char *buff, size_t len, loff_t *off)
{
    u8 reg_val;
    char messages[256], vol[256];

    if (len > 256)
        len = 256;

    if (copy_from_user(messages, buff, len))
        return -EFAULT;

    if ('-' == messages[0]) {
        /* set the register index */
        memcpy(vol, messages+1, len-1);
        index = (u8) simple_strtoul(vol, NULL, 16);
    } else {
        /* set the register value */
        reg_val = (u8)simple_strtoul(messages, NULL, 16);
        axp192_write(index, reg_val & 0xFF);
    }

    return len;
}

static struct file_operations axp192_proc_ops = {
    .read = axp192_proc_read,
    .write = axp192_proc_write,
};

static void create_axp192_proc_file(void)
{
    printk("setup proc control interface\n");
    axp192_proc_file = create_proc_entry(AXP192_PROC_FILE, 0644, NULL);
    if (axp192_proc_file) {
        //axp192_proc_file->owner = THIS_MODULE;
        axp192_proc_file->proc_fops = &axp192_proc_ops;
    } else
        printk(KERN_INFO "proc file create failed!\n");
}

static void remove_axp192_proc_file(void)
{
    remove_proc_entry(AXP192_PROC_FILE, NULL);
}


static int __init axp192_init(void)
{
    printk("---AXP192_init\n");
    create_axp192_proc_file();
    return 0;
}

static void __exit axp192_exit(void)
{
    remove_axp192_proc_file();
}

module_init(axp192_init);
module_exit(axp192_exit);

MODULE_DESCRIPTION("Axp192 Driver");
MODULE_LICENSE("GPL");

在虚拟机中insmod成功后,在/proc/driver下将会生成名为axp192的文件,改变其属性对其操作。

访问寄存器0x21

echo "-0x21" > axp192                   

cat axp192

由于没有设置值,故默认值为0    所以:register 0x21: 0x0

改变寄存器0x21的值,设为0x78

echo "0x78" > axp192                   

cat axp192

设置后,观察:register 0x21: 0x78

阅读(4543) | 评论(0) | 转发(1) |
0

上一篇:linux同步机制应用

下一篇:没有了

给主人留下些什么吧!~~