当前位置: 技术问答>linux和unix
Linux驱动菜鸟提问
来源: 互联网 发布时间:2016-06-21
本文导语: 模仿书上的例子写了一个Linux驱动程序,代码如下: #include #include #include #include #include #include #include unsigned int test_major=0;//动态分配 char *rmsg="read msg from kernel"; static int read_test(struct inode *node,struct fi...
模仿书上的例子写了一个Linux驱动程序,代码如下:
#include
#include
#include
#include
#include
#include
#include
unsigned int test_major=0;//动态分配
char *rmsg="read msg from kernel";
static int read_test(struct inode *node,struct file *file,char *buf,int count)
{
int left;
if(access_ok(VERIFY_WRITE,buf,count)==-EFAULT)
return -EFAULT;
for(left=count;left>0;left--)
{
//__put_user(1,buf,1);
copy_to_user(buf,rmsg,1);
buf++;
rmsg++;
}
return count;
}
static int write_test(struct inode *inode,struct file *file,const char *buf,int count)
{
printk(KERN_INFO "write msgn");
return count;
}
static int open_test(struct inode *inode,struct file *file)
{
return 0;
}
static void release_test(struct inode *inode,struct file *file)
{
}
struct file_operations test_fops={
NULL,
read_test,
write_test,
NULL,
NULL,
NULL,
NULL,
open_test,
release_test,
NULL,
NULL,
};
int init_moduletest(void)
{
int result;
result=register_chrdev(0,"test",&test_fops);
if(result