|
#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 的值,但一直显示是乱码。各们大哥帮帮忙,是什么原因。 |
|