ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / mf_proc.c
1 /*
2  * mf_proc.c
3  * Copyright (C) 2001 Kyle A. Lucke  IBM Corporation
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19 #include <linux/init.h>
20 #include <asm/uaccess.h>
21 #include <asm/iSeries/mf.h>
22
23 static int proc_mf_dump_cmdline(char *page, char **start, off_t off,
24                 int count, int *eof, void *data)
25 {
26         int len = count;
27         char *p;
28     
29         len = mf_getCmdLine(page, &len, (u64)data);
30    
31         p = page + len - 1;
32         while (p > page) {
33                 if ((*p == 0) || (*p == ' '))
34                         --p;
35                 else
36                         break;
37         }
38         if (*p != '\n') {
39                 ++p;
40                 *p = '\n';
41         }
42         ++p;
43         *p = 0;
44         len = p - page;
45     
46         len -= off;                     
47         if (len < count) {              
48                 *eof = 1;               
49                 if (len <= 0)           
50                         return 0;       
51         } else                          
52                 len = count;            
53         *start = page + off;            
54         return len;                     
55 }
56
57 #if 0
58 static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,
59                 int count, int *eof, void *data)
60 {
61         int sizeToGet = count;
62
63         if (!capable(CAP_SYS_ADMIN))
64                 return -EACCES;
65
66         if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {
67                 if (sizeToGet != 0) {
68                         *start = page + off;
69                         return sizeToGet;
70                 }
71                 *eof = 1;
72                 return 0;
73         }
74         *eof = 1;
75         return 0;
76 }
77 #endif
78
79 static int proc_mf_dump_side(char *page, char **start, off_t off,
80                 int count, int *eof, void *data)
81 {
82         int len;
83         char mf_current_side = mf_getSide();
84
85         len = sprintf(page, "%c\n", mf_current_side);
86
87         if (len <= (off + count))
88                 *eof = 1;
89         *start = page + off;
90         len -= off;
91         if (len > count)
92                 len = count;
93         if (len < 0)
94                 len = 0;
95         return len;                     
96 }
97
98 static int proc_mf_change_side(struct file *file, const char __user *buffer,
99                 unsigned long count, void *data)
100 {
101         char stkbuf[10];
102
103         if (!capable(CAP_SYS_ADMIN))
104                 return -EACCES;
105
106         if (count > (sizeof(stkbuf) - 1))
107                 count = sizeof(stkbuf) - 1;
108         if (copy_from_user(stkbuf, buffer, count))
109                 return -EFAULT;
110         stkbuf[count] = 0;
111         if ((*stkbuf != 'A') && (*stkbuf != 'B') &&
112             (*stkbuf != 'C') && (*stkbuf != 'D')) {
113                 printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");
114                 return -EINVAL;
115         }
116
117         mf_setSide(*stkbuf);
118
119         return count;
120 }
121
122 static int proc_mf_dump_src(char *page, char **start, off_t off,
123                 int count, int *eof, void *data)
124 {
125         int len;
126
127         mf_getSrcHistory(page, count);
128         len = count;
129         len -= off;                     
130         if (len < count) {              
131                 *eof = 1;               
132                 if (len <= 0)           
133                         return 0;       
134         } else                          
135                 len = count;            
136         *start = page + off;            
137         return len;                     
138 }
139
140 static int proc_mf_change_src(struct file *file, const char __user *buffer,
141                 unsigned long count, void *data)
142 {
143         char stkbuf[10];
144
145         if (!capable(CAP_SYS_ADMIN))
146                 return -EACCES;
147
148         if ((count < 4) && (count != 1)) {
149                 printk(KERN_ERR "mf_proc: invalid src\n");
150                 return -EINVAL;
151         }
152
153         if (count > (sizeof(stkbuf) - 1))
154                 count = sizeof(stkbuf) - 1;
155         if (copy_from_user(stkbuf, buffer, count))
156                 return -EFAULT;
157
158         if ((count == 1) && (*stkbuf == '\0'))
159                 mf_clearSrc();
160         else
161                 mf_displaySrc(*(u32 *)stkbuf);
162
163         return count;                   
164 }
165
166 static int proc_mf_change_cmdline(struct file *file, const char *buffer,
167                 unsigned long count, void *data)
168 {
169         if (!capable(CAP_SYS_ADMIN))
170                 return -EACCES;
171
172         mf_setCmdLine(buffer, count, (u64)data);
173
174         return count;                   
175 }
176
177 static int proc_mf_change_vmlinux(struct file *file, const char *buffer,
178                 unsigned long count, void *data)
179 {
180         if (!capable(CAP_SYS_ADMIN))
181                 return -EACCES;
182
183         mf_setVmlinuxChunk(buffer, count, file->f_pos, (u64)data);
184         file->f_pos += count;
185
186         return count;                   
187 }
188
189 static int __init mf_proc_init(void)
190 {
191         struct proc_dir_entry *mf_proc_root;
192         struct proc_dir_entry *ent;
193         struct proc_dir_entry *mf;
194         char name[2];
195         int i;
196
197         mf_proc_root = proc_mkdir("iSeries/mf", NULL);
198         if (!mf_proc_root)
199                 return 1;
200
201         name[1] = '\0';
202         for (i = 0; i < 4; i++) {
203                 name[0] = 'A' + i;
204                 mf = proc_mkdir(name, mf_proc_root);
205                 if (!mf)
206                         return 1;
207
208                 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
209                 if (!ent)
210                         return 1;
211                 ent->nlink = 1;
212                 ent->data = (void *)(long)i;
213                 ent->read_proc = proc_mf_dump_cmdline;
214                 ent->write_proc = proc_mf_change_cmdline;
215
216                 if (i == 3)     /* no vmlinux entry for 'D' */
217                         continue;
218
219                 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
220                 if (!ent)
221                         return 1;
222                 ent->nlink = 1;
223                 ent->data = (void *)(long)i;
224 #if 0
225                 if (i == 3) {
226                         /*
227                          * if we had a 'D' vmlinux entry, it would only
228                          * be readable.
229                          */
230                         ent->read_proc = proc_mf_dump_vmlinux;
231                         ent->write_proc = NULL;
232                 } else
233 #endif
234                 {
235                         ent->write_proc = proc_mf_change_vmlinux;
236                         ent->read_proc = NULL;
237                 }
238         }
239
240         ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
241         if (!ent)
242                 return 1;
243         ent->nlink = 1;
244         ent->data = (void *)0;
245         ent->read_proc = proc_mf_dump_side;
246         ent->write_proc = proc_mf_change_side;
247
248         ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
249         if (!ent)
250                 return 1;
251         ent->nlink = 1;
252         ent->data = (void *)0;
253         ent->read_proc = proc_mf_dump_src;
254         ent->write_proc = proc_mf_change_src;
255
256         return 0;
257 }
258
259 __initcall(mf_proc_init);