LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 739|回复: 13

简单驱动出错

[复制链接]
发表于 2005-5-31 16:13:55 | 显示全部楼层 |阅读模式
我加入一个file_operations结构后就出现unknown field 'read' specified in initializer错误,没搞明白!
  1. #ifndef __KERNEL__
  2. #define __KERNEL__
  3. #endif
  4. #ifndef MODULE
  5. #define MODULE
  6. #endif

  7. #define __NO_VERSION__

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

  10. MODULE_LICENSE("GPL");
  11. #ifdef CONFIG_SMP
  12. #define __SMP__
  13. #endif

  14. #include <linux/kernel.h> /* printk()*/
  15. #include <linux/fs.h>/*file_operations*/

  16. static ssize_t hello_read();
  17. static ssize_t hello_write();
  18. static int hello_open();
  19. static int hello_release();

  20. struct file_operations hello_fops={
  21. read: hello_read,};
  22. /*上面这行出现unknown field 'read' specified in initializer错误,是什么原因?*/
  23. ///////////////////////////////////////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////////////////////////////////////////
  25. static ssize_t hello_read(struct file *filp,char *buf, size_t len, loff_t *ppos)
  26. {
  27.         return 0;
  28. }

  29. static ssize_t hello_write(struct file *filp,char *buf, size_t len,loff_t *ppos){
  30.         return 0;
  31. }

  32. static int hello_open(struct inode *inode,struct file *filp ){
  33.         //MOD_INC_USE_COUNT;
  34.         return 0;
  35. }

  36. static int hello_release( struct inode *inode,struct file *filp){
  37.         //MOD_DEC_USE_COUNT;
  38.         return 0;
  39. }

  40. int init_module(void){
  41.         printk("Hello,World!\n");
  42.         return 0; /* 如果初始工作失败,就返回非0 */
  43. }

  44. static void exit_module(void){
  45.         printk("Bye!\n");
  46. }
复制代码
发表于 2005-5-31 16:18:53 | 显示全部楼层
注意include目录是/usr/src/linux/include,不是/usr/include
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-31 16:29:16 | 显示全部楼层
这个应该没错,内核版本是2.4.20-8,Makefile文件如下
  1. OBJS = hello.o
  2. CFLAG =
  3. CC = gcc
  4. KERNELDIR = /usr/src/linux2.4.20-8
  5. #include $(KERNELDIR)/.config

  6. all:$(OBJS)
  7. hello.o:hello.c
  8.         gcc -D__KERNEL__ -D_MOUDLE -I$(KERNELDIR)/include -c $^ -o $@
  9. clean:
  10.         rm -f $(OBJS)
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-5-31 17:11:28 | 显示全部楼层
你把static 去了看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-31 17:23:58 | 显示全部楼层
一样的!
回复 支持 反对

使用道具 举报

发表于 2005-5-31 18:23:42 | 显示全部楼层
到头文件里看看file_operations的定义有没有问题。这个太诡异了
回复 支持 反对

使用道具 举报

发表于 2005-5-31 23:58:03 | 显示全部楼层
改成这样看看

struct file_operations hello_fops={
.read= hello_read};
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-1 09:07:30 | 显示全部楼层
这个是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-1 16:36:49 | 显示全部楼层
我把struct file_operations hello_fops={
read: hello_read,};改成struct file_operations *hello_fops={NULL,NULL,hello_read,NULL};之后编译通过,不知道是不是我GCC版本的原因,我的GCC是3.2.2?真是什么也不懂啊!
又出现了新问题,编译通过用insmod hello.o加载模块,显示“hello,world!”应该加载成功喽?可是我的/proc/device文件下并没有出现相应的设备名hello和设备号37,这又是什么原因呢?
  1. #ifndef __KERNEL__
  2. #define __KERNEL__
  3. #endif
  4. #ifndef MODULE
  5. #define MODULE
  6. #endif

  7. #define __NO_VERSION__

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

  10. MODULE_LICENSE("GPL");
  11. #ifdef CONFIG_SMP
  12. #define __SMP__
  13. #endif

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


  18. #define MAJOR_ID 37


  19. static ssize_t hello_read();
  20. static ssize_t hello_write();
  21. static int hello_open();
  22. static int hello_release();

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

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

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

  33. static int hello_open(struct inode *inode,struct file *filp ){
  34.         //MOD_INC_USE_COUNT;
  35.         return 0;
  36. }

  37. static int hello_release( struct inode *inode,struct file *filp){
  38.         //MOD_DEC_USE_COUNT;
  39.         return 0;
  40. }

  41. int __init hello_init(void){

  42.         int result;                
  43.         result=register_chrdev(MAJOR_ID,"hello",hello_fops);
  44.         if(result<0){
  45.                 printk("CAN NOT GET MAJOR!");
  46.         }       
  47.         printk("Hello,World!\n");       
  48.         return 0;
  49. }

  50. static void __exit hello_exit(void){
  51.         unregister_chrdev(MAJOR_ID,"hello");
  52.         printk("Bye!\n");
  53. }

  54. module_init(hello_init);
  55. module_exit(hello_exit);
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-6-1 16:48:47 | 显示全部楼层
看/proc/modules
你的gcc有点老。
回复 支持 反对

使用道具 举报

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

本版积分规则

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