|
1 |
| -#include < linux/module.h > |
2 |
| -#include <linux/kernel.h> |
| 1 | +#include <linux/module.h> |
| 2 | +#include <linux/kernel.h> |
| 3 | +#include <linux/slab.h> |
| 4 | +#include <linux/string.h> |
| 5 | +#include <linux/timer.h> |
| 6 | +#include <linux/major.h> |
| 7 | +#include <linux/fs.h> |
| 8 | +#include <linux/err.h> |
| 9 | +#include <linux/ioctl.h> |
| 10 | +#include <linux/init.h> |
3 | 11 | #include <linux/proc_fs.h>
|
| 12 | +#include <asm-generic/uaccess.h> |
| 13 | +#include <linux/types.h> |
| 14 | +#include <linux/bug.h> |
| 15 | +#include <linux/io.h> |
| 16 | +#include <asm/page.h> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +#ifndef __GFP_WAIT |
| 21 | +#define __GFP_WAIT __GFP_RECLAIM |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef GFP_IOFS |
| 25 | +#define GFP_IOFS STP_ALLOC_FLAGS |
| 26 | +#endif |
4 | 27 |
|
5 | 28 | #ifdef CONFIG_PROC_FS
|
6 | 29 |
|
7 |
| -#define procfs_name "procfs_hello_world" |
8 | 30 | #define procfs_msg "Hello World\n"
|
| 31 | +#define procfs_name "procfs_hello_world" |
| 32 | +#define procfs_perms 0644 |
| 33 | +#define procfs_parent NULL |
9 | 34 |
|
10 |
| -struct proc_dir_entry *my_pFile; |
| 35 | +static struct file_operations procfs_fops; |
| 36 | +static char *message; |
| 37 | +static int read_p; |
11 | 38 |
|
12 |
| -static int |
13 |
| -pfs_hw_read(char *page, |
14 |
| - char **start, |
15 |
| - off_t off, int buffer_length, int *eof, void *data) |
| 39 | + |
| 40 | +static ssize_t |
| 41 | +pfs_hw_read(struct file *file, |
| 42 | + char __user * buf, size_t count, loff_t * offp) |
16 | 43 | {
|
17 |
| - int ret; |
| 44 | + ssize_t len = strlen(message); |
18 | 45 |
|
19 |
| - if (off > 0) { |
20 |
| - ret = 0; |
21 |
| - } else { |
22 |
| - /* totally ignore the size of the buffer */ |
23 |
| - ret = sprintf(page, procfs_msg); |
| 46 | + read_p = !read_p; |
| 47 | + if (read_p) { |
| 48 | + return 0; |
24 | 49 | }
|
| 50 | + printk("proc called read %ld\n", count); |
| 51 | + printk("proc called len %ld\n", len); |
| 52 | + printk("about to copy %s\n", message); |
| 53 | + |
| 54 | + /* |
| 55 | + * should be len instead of 0, but that causes an oops |
| 56 | + */ |
25 | 57 |
|
26 |
| - return ret; |
| 58 | + /* @todo figure out why copy_to_user so upset wich nonzero */ |
| 59 | + copy_to_user(buf, message, 0); |
| 60 | + |
| 61 | + return 0; |
27 | 62 | }
|
28 | 63 |
|
29 | 64 | static int
|
30 |
| -pfs_hw_create_entry() |
| 65 | +pfs_hw_open(struct inode *sp_inode, struct file *sp_file) |
31 | 66 | {
|
32 |
| - my_pFile = create_proc_entry(procfs_name, 0644, NULL); |
| 67 | + printk("proc called open\n"); |
33 | 68 |
|
34 |
| - if (my_pFile == NULL) { |
35 |
| - remove_proc_entry(procfs_name, &proc_root); |
36 |
| - printk(KERN_ALERT "Error creating:/proc/%s\n", procfs_name); |
| 69 | + read_p = 1; |
| 70 | + |
| 71 | + //message = kmalloc(sizeof(char) * 1024, __GFP_WAIT | __GFP_IO | __GFP_FS); |
| 72 | + message = kmalloc(1024, GFP_KERNEL); |
| 73 | + |
| 74 | + if (message == NULL) { |
| 75 | + printk("ERROR, pfs_hw_init"); |
37 | 76 | return -ENOMEM;
|
38 | 77 | }
|
39 |
| - my_pFile->read_proc = procfile_read; |
40 |
| - my_pFile->owner = THIS_MODULE; |
41 |
| - my_pFile->mode = S_IFREG | S_IRUGO; |
42 |
| - my_pFile->uid = 0; |
43 |
| - my_pFile->gid = 0; |
44 |
| - my_pFile->size = 37; |
| 78 | + strcpy(message, procfs_msg); |
| 79 | + return 0; |
| 80 | +} |
45 | 81 |
|
| 82 | +static int |
| 83 | +pfs_hw_release(struct inode *sp_inode, struct file *sp_file) |
| 84 | +{ |
| 85 | + printk("proc called release\n"); |
| 86 | + kfree(message); |
46 | 87 | return 0;
|
47 | 88 |
|
48 | 89 | }
|
49 | 90 |
|
50 | 91 |
|
51 |
| -static int __init |
52 |
| -pfs_hw_init() |
| 92 | +static __init int |
| 93 | +pfs_hw_create_entry(void) |
53 | 94 | {
|
54 |
| - int ret_val; |
| 95 | + printk("/proc/%s create\n", procfs_name); |
| 96 | + /* procfs_fops.owner = THIS_MODULE; */ |
| 97 | + procfs_fops.open = pfs_hw_open; |
| 98 | + procfs_fops.read = pfs_hw_read; |
| 99 | + procfs_fops.release = pfs_hw_release; |
55 | 100 |
|
56 |
| - ret_val = pfs_hw_create_entry(); |
57 |
| - return ret_val; |
| 101 | + if (!proc_create(procfs_name, procfs_perms, procfs_parent, &procfs_fops)) { |
| 102 | + printk(KERN_ALERT "Error creating:/proc/%s\n", procfs_name); |
| 103 | + remove_proc_entry(procfs_name, procfs_parent); |
| 104 | + return -ENOMEM; |
| 105 | + } |
| 106 | + return 0; |
58 | 107 | }
|
59 | 108 |
|
60 | 109 | static void __exit
|
61 |
| -pfs_hw_exit() |
| 110 | +pfs_hw_exit(void) |
62 | 111 | {
|
63 |
| - remove_proc_entry(procfs_name, &proc_root); |
| 112 | + remove_proc_entry(procfs_name, procfs_parent); |
64 | 113 | printk(KERN_INFO "/proc/%s removed\n", procfs_name);
|
65 | 114 | }
|
66 | 115 |
|
67 |
| -module_init(pfs_hw_init) |
68 |
| -module_exit(pfs_hw_exit) |
| 116 | +module_init(pfs_hw_create_entry); |
| 117 | +module_exit(pfs_hw_exit); |
69 | 118 | #else
|
70 | 119 | #error "Set CONFIG_PROC_FS=y in you .config"
|
71 | 120 | #endif /* CONFIG_PROC_FS */
|
72 | 121 |
|
73 | 122 | MODULE_AUTHOR("d-grossman");
|
74 | 123 | MODULE_DESCRIPTION("procfs hello world");
|
75 |
| - |
76 |
| -MODULE_LICENSE("GPL"); |
|
0 commit comments