- 论坛徽章:
- 0
|
- 1 #ifndef _KERNEL_
- 2 #define _KERNEL_
- 3 #endif
- 4
- 5 #ifndef MODULE
- 6 #define MODULE
- 7 #endif
- 8
- 9 #include <linux/module.h>
- 10 #include <linux/fs.h>
- 11
- 12 static ssize_t myfile_read(struct file *file, char *buf, size_t count, loff_t *offset) ;
- 13 static ssize_t myfile_write(struct file *file, const char *buf, size_t count, loff_t *offse t) ;
- 14 static int myfile_open(struct inode *inod, struct file *file) ;
- 15 static int myfile_flush(struct file *file) ;
- 16 static int myfile_release(struct inode *inod, struct file *file) ;
- 17
- 18 const static unsigned int major = 126 ;
- 19
- 20 static struct file_operations myfile_fops = {
- 21 read: myfile_read,
- 22 write: myfile_write,
- 23 open: myfile_open,
- 24 flush: myfile_flush,
- 25 release: myfile_release,
- 26 } ;
- 27
- 28
- 29 static int myfile_open(struct inode *inod, struct file *file)
- 30 {
- 31 MOD_INC_USE_COUNT;
- 32 return 0;
- 33 }
- 34
- 35 static int myfile_release(struct inode *inod, struct file *file)
- 36 {
- 37 MOD_DEC_USE_COUNT;
- 38 return 0;
- 39 }
- 40
- 41 static ssize_t myfile_read(struct file *file, char *buf, size_t count, loff_t *offset)
- 42 {
- 43 printk("<4>read %d char success.\n", count) ;
- 44 return count ;
- 45 }
- 46
- 47 static ssize_t myfile_write(struct file *file, const char *buf, size_t count, loff_t *offse t)
- 48 {
- 49 printk("<4>write %d char success.\n", count) ;
- 50 return count ;
- 51 }
- 52
- 53 static int myfile_flush(struct file *file)
- 54 {
- 55 printk("<4>flush file success.\n") ;
- 56 return 0;
- 57 }
- 58
- 59 static int module_init(void)
- 60 {
- 61 int regStatus ;
- 62
- 63 regStatus = register_chrdev(major, "myfile", &myfile_fops) ;
- 64 if (regStatus)
- 65 {
- 66 printk("<4>cann't register device with kernel.\n") ;
- 67 return regStatus ;
- 68 }
- 69 else
- 70 {
- 71 printk("<4>device registered with major %d.\n", major) ;
- 72 }
- 73
- 74 printk("<4>Hello world!\n") ;
- 75 return 0;
- 76 }
- 77
- 78 static void module_exit(void)
- 79 {
- 80 unregister_chrdev(major, "myfile") ;
- 81 printk("<4>Bye!\n") ;
- 82 }
- 83
- 84 MODULE_LICENSE("GPL");
复制代码
编译的时候这样的错误:
hello.c:59: error: two or more data types in declaration of `init_module'
hello.c: In function `__init_module_inline':
hello.c:59: error: syntax error before "void"
hello.c: At top level:
hello.c:63: error: initializer element is not constant
hello.c:63: warning: data definition has no type or storage class
hello.c:64: error: syntax error before "if"
hello.c:74: error: syntax error before string constant
hello.c:74: warning: data definition has no type or storage class
hello.c:78: error: two or more data types in declaration of `cleanup_module'
hello.c: In function `__cleanup_module_inline':
hello.c:78: error: syntax error before "void"
hello.c: At top level:
hello.c:81: error: syntax error before string constant
hello.c:81: warning: data definition has no type or storage class |
|