QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2269|回复: 1

一个内核驱动模块 的问题。

[复制链接]
发表于 2007-1-18 16:37:15 | 显示全部楼层 |阅读模式
#define TEST_MAJOR 251
#define TEST_NAME "TEST_CHAR"
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
MODULE_LICENSE("Dual BSD/GPL");
struct data{
        double name;
        int num;
};

static char test_val;

static struct data *kbuf;
static ssize_t test_read(struct file *filp,char __user *buf,size_t count,loff_t *l)
{
        int retval=0;

        if(copy_to_user(buf,kbuf,sizeof(struct data))){
                retval = -EFAULT;

        }
        kfree(kbuf);
        return sizeof(test_val);
}

static ssize_t test_write(struct file *file,const char __user *buf,size_t count,loff_t *l)
{
        int retval=0;
        kbuf = (struct data *)kmalloc(sizeof(*kbuf), GFP_KERNEL);
        if (!kbuf)
                return -ENOMEM;
        memset(kbuf, 0, sizeof(struct data));
        if(copy_from_user(kbuf,buf,sizeof(struct data))){
                retval = -EFAULT;

        }
        //memcpy((char *)kbuf,&test_val,sizeof(struct data));
        //printk(KERN_INFO "shmem_write = %s\n",(char *)kbuf);
        printk("shmem_write = %f  %d\n", kbuf->name,kbuf->num);

        return sizeof(test_val);
}

static int test_open(struct inode *inode,struct file *filp)
{
        try_module_get(THIS_MODULE);
        return 0;
}
static int test_release(struct inode *inode,struct file *filp)
{
        module_put(THIS_MODULE);
        return 0;
}
struct file_operations testchar_fops={
        .read = test_read,
        .write = test_write,
        .open = test_open,
        .release = test_release
};

static int hello_init(void)
{
        register_chrdev(TEST_MAJOR,TEST_NAME,& testchar_fops);
        return 0;
}
static void hello_exit(void)
{
        unregister_chrdev(TEST_MAJOR,TEST_NAME);
}
module_init(hello_init);
module_exit(hello_exit);

这年模块能够运行成功,但是我想打印出 kbuf->name,kbuf->num 的值,但一直显示是乱码。各们大哥帮帮忙,是什么原因。
发表于 2007-5-8 22:33:21 | 显示全部楼层
printk("shmem_write = %f %d\n", kbuf->name,kbuf->num);

应该是 %s 的吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 21:20 , Processed in 0.165478 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表