4fc799bd9f7a2cbb6fae8cc8a041eb57ed8dafd7
[cascardo/kernel/samples/proc2/.git] / helloproc.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <linux/module.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22
23 MODULE_LICENSE("GPL");
24
25 static char hello[] = "Hello, world!\n";
26
27 static void *hp_start(struct seq_file *s, loff_t *pos)
28 {
29         loff_t hp_pos = *pos;
30         *pos += 1;
31         if (hp_pos >= sizeof(hello))
32                 return NULL;
33         return (void *) hello[hp_pos];
34 }
35
36 static void hp_stop(struct seq_file *s, void *v)
37 {
38 }
39
40 static void *hp_next(struct seq_file *s, void *v, loff_t *pos)
41 {
42         loff_t hp_pos;
43         hp_pos = *pos;
44         *pos += 1;
45         if (hp_pos >= sizeof(hello))
46                 return NULL;
47         return (void *) hello[hp_pos];
48 }
49
50 static int hp_show(struct seq_file *s, void *v)
51 {
52         seq_putc(s, (char) v);
53         return 0;
54 }
55
56 static const struct seq_operations hp_seq_ops = {
57         .start = hp_start,
58         .stop = hp_stop,
59         .next = hp_next,
60         .show = hp_show,
61 };
62
63 static int hp_open(struct inode *ino, struct file *fp)
64 {
65         return seq_open(fp, &hp_seq_ops);
66 }
67
68 static const struct file_operations hp_fops = {
69         .owner = THIS_MODULE,
70         .open = hp_open,
71         .read = seq_read,
72         .llseek = seq_lseek,
73         .release = seq_release,
74 };
75
76 static __init int hp_init(void)
77 {
78         struct proc_dir_entry *pde;
79         pde = proc_create("hello", 0666, NULL, &hp_fops);
80         if (!pde)
81                 return -ENOMEM;
82         return 0;
83 }
84
85 static __exit void hp_exit(void)
86 {
87         remove_proc_entry("hello", NULL);
88 }
89
90 module_init(hp_init);
91 module_exit(hp_exit);