LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: haohao_h

简单驱动出错

[复制链接]
发表于 2005-6-1 16:59:56 | 显示全部楼层
把这几个改成正确的定义
static ssize_t hello_read();
static ssize_t hello_write();
static int hello_open();
static int hello_release();
回复 支持 反对

使用道具 举报

发表于 2005-6-1 21:44:55 | 显示全部楼层
Post by haohao_h
这个是c99语法,据说具有更好的兼容性,我试了,还是不行!很奇怪,我把struct file_operations hello_fops={
read: hello_read,};
改成struct file_operations *hello_fops=NULL;就不出错了,在/usr/include目录下的fs.h文件没有file_operations结构的定义,说明我的include目录应该是正确的吧!file_operations结构我看不出有什么问题

  1. /*
  2. * NOTE:
  3. * read, write, poll, fsync, readv, writev can be called
  4. *   without the big kernel lock held in all filesystems.
  5. */
  6. struct file_operations {
  7.         struct module *owner;
  8.         loff_t (*llseek) (struct file *, loff_t, int);
  9.         ssize_t (*read) (struct file *, char *, size_t, loff_t *);
  10.         ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
  11.         int (*readdir) (struct file *, void *, filldir_t);
  12.         unsigned int (*poll) (struct file *, struct poll_table_struct *);
  13.         int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  14.         int (*mmap) (struct file *, struct vm_area_struct *);
  15.         int (*open) (struct inode *, struct file *);
  16.         int (*flush) (struct file *);
  17.         int (*release) (struct inode *, struct file *);
  18.         int (*fsync) (struct file *, struct dentry *, int datasync);
  19.         int (*fasync) (int, struct file *, int);
  20.         int (*lock) (struct file *, int, struct file_lock *);
  21.         ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
  22.         ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
  23.         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  24.         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  25. };
复制代码

晕你仔细看看我是怎么写的!
我可不是你那样写的!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-2 08:17:26 | 显示全部楼层
这个是fh.h里struct file_operations的定义!
回复 支持 反对

使用道具 举报

发表于 2005-6-2 08:53:47 | 显示全部楼层
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif

#define __NO_VERSION__

#include <linux/config.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
#ifdef CONFIG_SMP
#define __SMP__
#endif

#include <linux/kernel.h> /* printk()*/
#include <linux/fs.h>/*file_operations*/
#include <linux/init.h>/*moudle_init moudle_exit*/
//#include <asm/uaccess.h>/**/


#define MAJOR_ID 37


static ssize_t hello_read(struct file *filp,char *buf, size_t len, loff_t *ppos);
static ssize_t hello_write(struct file *filp,char *buf, size_t len,loff_t *ppos);
static int hello_open(struct inode *inode,struct file *filp);
static int hello_release(struct inode *inode,struct file *filp);

struct file_operations *hello_fops={NULL,NULL,hello_read,hello_write,NULL};

static ssize_t hello_read(struct file *filp,char *buf, size_t len, loff_t *ppos)
{
        printk("Hello,read!\n");
        return 0;
}

static ssize_t hello_write(struct file *filp,char *buf, size_t len,loff_t *ppos){
        printk("Hello,write!\n");
        return 0;
}

static int hello_open(struct inode *inode,struct file *filp ){
        //MOD_INC_USE_COUNT;
        return 0;
}

static int hello_release( struct inode *inode,struct file *filp){
        //MOD_DEC_USE_COUNT;
        return 0;
}

int __init hello_init(void){

        int result;                
        result=register_chrdev(MAJOR_ID,"hello",hello_fops);
        if(result<0){
                printk("CAN NOT GET MAJOR!");
        }       
        printk("Hello,World!\n");       
        return 0;
}

static void __exit hello_exit(void){
        unregister_chrdev(MAJOR_ID,"hello");
        printk("Bye!\n");
}

module_init(hello_init);
module_exit(hello_exit);
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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