xiang324
驱动牛犊
驱动牛犊
  • 注册日期2004-03-23
  • 最后登录2004-04-23
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1219回复:2

急救

楼主#
更多 发布于:2004-04-12 09:29
兄弟我是linux新手,现在正在作设备驱动程序原码如下:
#define MODULE
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/version.h>
char kernel_version [] = UTS_RELEASE;

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>

unsigned int test_major = 0;

static int read_test(struct inode *node, struct file *file, char *buf, int count)
{
int left;
if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT )
return -EFAULT;

for(left = count ; left > 0 ; left--)
{
__put_user(1,buf,1);
buf++;
}

return count;
}

static int write_test(struct inode *inode, struct file *file, const char *buf, int count)
{
return count;
}

static int open_test(struct inode *inode, struct file *file )
{
MOD_INC_USE_COUNT;
return 0;
}

static void release_test(struct inode *inode, struct file *file )
{
MOD_DEC_USE_COUNT;
}

struct file_operations test_fops = {
NULL,
read_test,
write_test,
NULL, /* test_readdir */
NULL,
NULL, /* test_ioctl */
NULL, /* test_mmap */
open_test,
release_test, NULL, /* test_fsync */
NULL, /* test_fasync */
/* nothing more, fill with NULLs */
NULL,
NULL,
NULL,
};



int init_module(void)
{
int result;
result = register_chrdev(0, \"test\", &test_fops);
if (result < 0) {
printk(KERN_INFO \"test: can\'t get major number \");
return result;
}

if (test_major == 0) test_major = result; /* dynamic */

return 0;
}



void cleanup_module(void)

{

unregister_chrdev(test_major, \"test\");

}
编译命令如下:
gcc test.c -DMODULE -D__KERNEL__ -o test.o
结果出现很多源文件错误,如在/usr/incude/linux/fs.h中找不到file_operation数据结构......
那位大虾给兄弟指点一下迷津
rainyss
驱动牛犊
驱动牛犊
  • 注册日期2004-04-05
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-04-14 05:35
你的驱动种序编译出来也装不上去._NO_VERSION不能随便乱用的.
zhouai
驱动牛犊
驱动牛犊
  • 注册日期2004-04-15
  • 最后登录2006-04-21
  • 粉丝0
  • 关注0
  • 积分60分
  • 威望6点
  • 贡献值0点
  • 好评度6点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-04-15 21:06
写一个makefile吧
CC=gcc
MODCFLAGS:=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8(为你所用的版本号)/include
test.o:test.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c test.c
然后在目录下make就可以了!
游客

返回顶部