QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 685|回复: 0

初学驱动编程,哪位牛人可以帮我看看这个小程序吗?

[复制链接]
发表于 2004-2-16 17:59:55 | 显示全部楼层 |阅读模式
用的是RH9,核心2.4.20-8
程序如下


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/malloc.h>
#include <asm/uaccess.h>
#include <asm/page.h>
#include <linux/ermo.h>
#include <linux/config.h>

#define MYDRV_CLS IO('c',0x01)
char mybuf[100];
int mydrv_major=99;
devfs_handle_t dev_handle;
ssize_t mydrv_read(struct file *filp,char *buf,size_t count,loff_t *f_pos)
{
int length=strlen(mybuf);
if (count>99)count=99;
count=length-*f_pos;
if(copy_to_user(buf,mybuf,count)){
  printk("error reading,copy_to_user \n");
  return -EFAULT;
}
*f_pos+=count;
return count;
}
static ssize_t mydrv_write(struct file *filp,const char *buf,size_t count,loff_t *p_pos)
{
int num;
num=count<100?count:100;
if(copy_from_user(mybuf,buf,num))
return -EFAULT;
printk("mydrv_write succeed! \n");
return num;
}
static int mydrv_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
  case MYDRV_CLS;
   mybuf[0]=0x0;
   return(0);
   default;
    return(-EINVAL);
   }
}
#define MAX_MYDRV_DEV 2
int mydrv_open(struct inode *inode,struct file *filp)
{
unsigned int dev=MINOR(inode->i_rdev);
if(mydrv_num)
  return -1;
if(dev>=MAX_MYDRV_DEV)
return -ENODEV;
filp->f_op=&mydrv_ops;
printk("open success\n");
MOD_INC_USE_COUNT;
return 0;
}
int mydrv_release(struct inode *inode,struct file *filp){
MOD_DEC_USE_COUNT;
return 0;
}

struct file_operations mydrv_ops={
open:mydrv_open,
read:mydrv_read,
write:mydrv_write,
ioctl:mydrv_ioctl
release:mydrv_release,
};
int init_module(void)
{
int result;
printk("inting...\n");
result=devfs_register_chrdev(mydrv_major,"mydrv",&mydrv_ops);
if(result<0)
{
  printk(KERN_WARNING"mydrv:unable to get major %d\n",mydrv_major);
  return result;
}
dev_handle=devfs_register(NULL,"mydrv",DEVFS_FL_DEFAULT,99,0);
printk("succeed in getting buffer \n");
printk("%s \n",mybuf);
return 0;
}
void cleanup_module(void)
{
devfs_unregister_chrdev(mydrv_major,"mydrv");
devfs_unregister(dev_handle);
printk("exiting..\n");
}



编译就成来这样:

[root@Hinatia Program2]# gcc -c mydrv.c -O2 -D__KERNEL__ -DMODULE -I /usr/src/linux-2. 4.20-8/include
In file included from mydrv.c:5:
/usr/src/linux-2.4.20-8/include/linux/malloc.h:4:2: #error linux/malloc.h is deprecate             d, use linux/slab.h instead.
mydrv.c:8:24: linux/ermo.h: 没有那个文件或目录
mydrv.c:14: parse error before "dev_handle"
mydrv.c:14: warning: data definition has no type or storage class
mydrv.c: In function `mydrv_ioctl':
mydrv.c:40: parse error before ';' token
mydrv.c:43: parse error before ';' token
mydrv.c:41: warning: unreachable code at beginning of switch statement
mydrv.c: In function `mydrv_open':
mydrv.c:51: `mydrv_num' undeclared (first use in this function)
mydrv.c:51: (Each undeclared identifier is reported only once
mydrv.c:51: for each function it appears in.)
mydrv.c:55: `mydrv_ops' undeclared (first use in this function)
mydrv.c: At top level:
mydrv.c:65: `mydrv_ops' used prior to declaration
mydrv.c:70: parse error before "release"
mydrv.c: In function `init_module':
mydrv.c:82: `DEVFS_FL_DEFAULT' undeclared (first use in this function)



我快被这作业搞疯乐.哪位牛人能帮帮我?这几个到底是啥子错误阿?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-8 23:07 , Processed in 0.042601 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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