应用程序调用hello驱动,写入数据是打印写入的数据为0,这种问题该怎么排查呀老师 @百问网-赵老师

驱动代码如下 :

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>

static int major=0;
//static int minor=0;
static struct class* hello_class;
static char data[1024]={‘\0’};
static struct device * hello_device;
//ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
//copy_from_user(void *to, const void __user *from, unsigned long n)
//copy_to_user(void __user *to, const void from, unsigned long n)
static ssize_t hello_read(struct file
file, char __user buf, size_t size, loff_t offset)
{
ssize_t ret;
printk(“%s %s linre %d\n”,FILE, FUNCTION, LINE);

    ret=copy_to_user(buf,&data,sizeof(data));
    return ret;

}

//ssize_t (*write) (struct file *, const char __user *, size_t, loff_t );
static ssize_t hello_write(struct file
file , const char __user buf, size_t size , loff_t offset)
{
ssize_t ret;
printk(“%s %s linre %d\n”,FILE, FUNCTION, LINE);
ret=copy_from_user(&data,buf,1024);
return ret;
}
//int (*open) (struct inode , struct file );
static int hello_open(struct inode
node , struct file
file )
{
printk(“%s %s linre %d\n”,FILE, FUNCTION, LINE);

    return 0;

}
//int (*release) (struct inode , struct file );
static int hello_close(struct inode
node, struct file
file)
{
printk(“%s %s linre %d\n”,FILE, FUNCTION, LINE);

    return 0;

}
static struct file_operations hello_test=
{
.owner=THIS_MODULE,
.open=hello_open,
.read=hello_read,
.write=hello_write,
.release=hello_close
};

static int __init hello_init(void)
{
major=register_chrdev(0,“hello_testone”,&hello_test);
if(major>=0)
{
printk(“major device number %d\n”,major);
}
else
{
printk(“register device faild!\n”);
}
hello_class=class_create(hello_test.owner,“hello_class”);
if(hello_class==NULL)
{
unregister_chrdev(major,“hello_testone”);
printk(“class create faild,hello_class is NULL!\n”);
return -1;
}
hello_device=device_create(hello_class, NULL, MKDEV(major, 0), NULL, “hello”);///dev/hello
if(!hello_device)
{
class_destroy(hello_class);
return -1;
}
return 0;
}

//static void __exit el3_cleanup_module(void)
static void __exit hello_exit(void)
{

    device_destroy(hello_class,MKDEV(major,0));
    class_destroy(hello_class);
    unregister_chrdev(major,"hello_testone");
    return;

}
module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE(“GPL”);

应用程序代码如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
int sum,fd=0;
fd=open(“/dev/hello”,O_RDWR);
sum=write(fd,“hello”,sizeof(“hello”));
printf(“sum is %d\n”,sum);
close(fd);

    return 0;

}

应用程序打印结果、内核驱动程序打印消息如下:
[178666.609374] major device number 240
[179407.090905] drivers/hello_driver.c hello_open linre 46
[179407.090972] drivers/hello_driver.c hello_write linre 39
[179407.092510] drivers/hello_driver.c hello_close linre 54
[179465.444650] drivers/hello_driver.c hello_open linre 46
[179465.444757] drivers/hello_driver.c hello_write linre 39
[179465.447752] drivers/hello_driver.c hello_close linre 54
[180138.350290] drivers/hello_driver.c hello_open linre 46
[180138.350392] drivers/hello_driver.c hello_write linre 39
[180138.353261] drivers/hello_driver.c hello_close linre 54

[root@100ask:/mnt]# ./a.out
sum is 0