patch-2_6_7-vs1_9_1_12
[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         int rc;
181         if (!capable(CAP_SYS_ADMIN))
182                 return -EACCES;
183
184         rc = mf_setVmlinuxChunk(buffer, count, file->f_pos, (u64)data);
185         if (rc < 0)
186                 return rc;
187
188         file->f_pos += count;
189
190         return count;                   
191 }
192
193 static int __init mf_proc_init(void)
194 {
195         struct proc_dir_entry *mf_proc_root;
196         struct proc_dir_entry *ent;
197         struct proc_dir_entry *mf;
198         char name[2];
199         int i;
200
201         mf_proc_root = proc_mkdir("iSeries/mf", NULL);
202         if (!mf_proc_root)
203                 return 1;
204
205         name[1] = '\0';
206         for (i = 0; i < 4; i++) {
207                 name[0] = 'A' + i;
208                 mf = proc_mkdir(name, mf_proc_root);
209                 if (!mf)
210                         return 1;
211
212                 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
213                 if (!ent)
214                         return 1;
215                 ent->nlink = 1;
216                 ent->data = (void *)(long)i;
217                 ent->read_proc = proc_mf_dump_cmdline;
218                 ent->write_proc = proc_mf_change_cmdline;
219
220                 if (i == 3)     /* no vmlinux entry for 'D' */
221                         continue;
222
223                 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
224                 if (!ent)
225                         return 1;
226                 ent->nlink = 1;
227                 ent->data = (void *)(long)i;
228 #if 0
229                 if (i == 3) {
230                         /*
231                          * if we had a 'D' vmlinux entry, it would only
232                          * be readable.
233                          */
234                         ent->read_proc = proc_mf_dump_vmlinux;
235                         ent->write_proc = NULL;
236                 } else
237 #endif
238                 {
239                         ent->write_proc = proc_mf_change_vmlinux;
240                         ent->read_proc = NULL;
241                 }
242         }
243
244         ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
245         if (!ent)
246                 return 1;
247         ent->nlink = 1;
248         ent->data = (void *)0;
249         ent->read_proc = proc_mf_dump_side;
250         ent->write_proc = proc_mf_change_side;
251
252         ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
253         if (!ent)
254                 return 1;
255         ent->nlink = 1;
256         ent->data = (void *)0;
257         ent->read_proc = proc_mf_dump_src;
258         ent->write_proc = proc_mf_change_src;
259
260         return 0;
261 }
262
263 __initcall(mf_proc_init);