Merge git://git.kernel.org/pub/scm/linux/kernel/git/brodo/cpufreq-2.6
[sfrench/cifs-2.6.git] / arch / mips / lasat / sysctl.c
1 /*
2  * Thomas Horsten <thh@lasat.com>
3  * Copyright (C) 2000 LASAT Networks A/S.
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  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  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17  *
18  * Routines specific to the LASAT boards
19  */
20 #include <linux/types.h>
21 #include <asm/lasat/lasat.h>
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/sysctl.h>
26 #include <linux/stddef.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/ctype.h>
30 #include <linux/string.h>
31 #include <linux/net.h>
32 #include <linux/inet.h>
33 #include <linux/mutex.h>
34 #include <asm/uaccess.h>
35
36 #include "sysctl.h"
37 #include "ds1603.h"
38
39 static DEFINE_MUTEX(lasat_info_mutex);
40
41 /* Strategy function to write EEPROM after changing string entry */
42 int sysctl_lasatstring(ctl_table *table, int *name, int nlen,
43                 void *oldval, size_t *oldlenp,
44                 void *newval, size_t newlen, void **context)
45 {
46         int r;
47         mutex_lock(&lasat_info_mutex);
48         r = sysctl_string(table, name,
49                           nlen, oldval, oldlenp, newval, newlen, context);
50         if (r < 0) {
51                 mutex_unlock(&lasat_info_mutex);
52                 return r;
53         }
54         if (newval && newlen) {
55                 lasat_write_eeprom_info();
56         }
57         mutex_unlock(&lasat_info_mutex);
58         return 1;
59 }
60
61
62 /* And the same for proc */
63 int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
64                        void *buffer, size_t *lenp, loff_t *ppos)
65 {
66         int r;
67         mutex_lock(&lasat_info_mutex);
68         r = proc_dostring(table, write, filp, buffer, lenp, ppos);
69         if ( (!write) || r) {
70                 mutex_unlock(&lasat_info_mutex);
71                 return r;
72         }
73         lasat_write_eeprom_info();
74         mutex_unlock(&lasat_info_mutex);
75         return 0;
76 }
77
78 /* proc function to write EEPROM after changing int entry */
79 int proc_dolasatint(ctl_table *table, int write, struct file *filp,
80                        void *buffer, size_t *lenp, loff_t *ppos)
81 {
82         int r;
83         mutex_lock(&lasat_info_mutex);
84         r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
85         if ( (!write) || r) {
86                 mutex_unlock(&lasat_info_mutex);
87                 return r;
88         }
89         lasat_write_eeprom_info();
90         mutex_unlock(&lasat_info_mutex);
91         return 0;
92 }
93
94 static int rtctmp;
95
96 #ifdef CONFIG_DS1603
97 /* proc function to read/write RealTime Clock */
98 int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
99                        void *buffer, size_t *lenp, loff_t *ppos)
100 {
101         int r;
102         mutex_lock(&lasat_info_mutex);
103         if (!write) {
104                 rtctmp = ds1603_read();
105                 /* check for time < 0 and set to 0 */
106                 if (rtctmp < 0)
107                         rtctmp = 0;
108         }
109         r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
110         if ( (!write) || r) {
111                 mutex_unlock(&lasat_info_mutex);
112                 return r;
113         }
114         ds1603_set(rtctmp);
115         mutex_unlock(&lasat_info_mutex);
116         return 0;
117 }
118 #endif
119
120 /* Sysctl for setting the IP addresses */
121 int sysctl_lasat_intvec(ctl_table *table, int *name, int nlen,
122                     void *oldval, size_t *oldlenp,
123                     void *newval, size_t newlen, void **context)
124 {
125         int r;
126         mutex_lock(&lasat_info_mutex);
127         r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
128         if (r < 0) {
129                 mutex_unlock(&lasat_info_mutex);
130                 return r;
131         }
132         if (newval && newlen) {
133                 lasat_write_eeprom_info();
134         }
135         mutex_unlock(&lasat_info_mutex);
136         return 1;
137 }
138
139 #ifdef CONFIG_DS1603
140 /* Same for RTC */
141 int sysctl_lasat_rtc(ctl_table *table, int *name, int nlen,
142                     void *oldval, size_t *oldlenp,
143                     void *newval, size_t newlen, void **context)
144 {
145         int r;
146         mutex_lock(&lasat_info_mutex);
147         rtctmp = ds1603_read();
148         if (rtctmp < 0)
149                 rtctmp = 0;
150         r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
151         if (r < 0) {
152                 mutex_unlock(&lasat_info_mutex);
153                 return r;
154         }
155         if (newval && newlen) {
156                 ds1603_set(rtctmp);
157         }
158         mutex_unlock(&lasat_info_mutex);
159         return 1;
160 }
161 #endif
162
163 #ifdef CONFIG_INET
164 static char lasat_bcastaddr[16];
165
166 void update_bcastaddr(void)
167 {
168         unsigned int ip;
169
170         ip = (lasat_board_info.li_eeprom_info.ipaddr &
171                 lasat_board_info.li_eeprom_info.netmask) |
172                 ~lasat_board_info.li_eeprom_info.netmask;
173
174         sprintf(lasat_bcastaddr, "%d.%d.%d.%d",
175                         (ip      ) & 0xff,
176                         (ip >>  8) & 0xff,
177                         (ip >> 16) & 0xff,
178                         (ip >> 24) & 0xff);
179 }
180
181 static char proc_lasat_ipbuf[32];
182 /* Parsing of IP address */
183 int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
184                        void *buffer, size_t *lenp, loff_t *ppos)
185 {
186         int len;
187         unsigned int ip;
188         char *p, c;
189
190         if (!table->data || !table->maxlen || !*lenp ||
191             (*ppos && !write)) {
192                 *lenp = 0;
193                 return 0;
194         }
195
196         mutex_lock(&lasat_info_mutex);
197         if (write) {
198                 len = 0;
199                 p = buffer;
200                 while (len < *lenp) {
201                         if(get_user(c, p++)) {
202                                 mutex_unlock(&lasat_info_mutex);
203                                 return -EFAULT;
204                         }
205                         if (c == 0 || c == '\n')
206                                 break;
207                         len++;
208                 }
209                 if (len >= sizeof(proc_lasat_ipbuf)-1)
210                         len = sizeof(proc_lasat_ipbuf) - 1;
211                 if (copy_from_user(proc_lasat_ipbuf, buffer, len))
212                 {
213                         mutex_unlock(&lasat_info_mutex);
214                         return -EFAULT;
215                 }
216                 proc_lasat_ipbuf[len] = 0;
217                 *ppos += *lenp;
218                 /* Now see if we can convert it to a valid IP */
219                 ip = in_aton(proc_lasat_ipbuf);
220                 *(unsigned int *)(table->data) = ip;
221                 lasat_write_eeprom_info();
222         } else {
223                 ip = *(unsigned int *)(table->data);
224                 sprintf(proc_lasat_ipbuf, "%d.%d.%d.%d",
225                         (ip      ) & 0xff,
226                         (ip >>  8) & 0xff,
227                         (ip >> 16) & 0xff,
228                         (ip >> 24) & 0xff);
229                 len = strlen(proc_lasat_ipbuf);
230                 if (len > *lenp)
231                         len = *lenp;
232                 if (len)
233                         if(copy_to_user(buffer, proc_lasat_ipbuf, len)) {
234                                 mutex_unlock(&lasat_info_mutex);
235                                 return -EFAULT;
236                         }
237                 if (len < *lenp) {
238                         if(put_user('\n', ((char *) buffer) + len)) {
239                                 mutex_unlock(&lasat_info_mutex);
240                                 return -EFAULT;
241                         }
242                         len++;
243                 }
244                 *lenp = len;
245                 *ppos += len;
246         }
247         update_bcastaddr();
248         mutex_unlock(&lasat_info_mutex);
249         return 0;
250 }
251 #endif /* defined(CONFIG_INET) */
252
253 static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
254                                      void *oldval, size_t *oldlenp,
255                                      void *newval, size_t newlen,
256                                      void **context)
257 {
258         int r;
259
260         mutex_lock(&lasat_info_mutex);
261         r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
262         if (r < 0) {
263                 mutex_unlock(&lasat_info_mutex);
264                 return r;
265         }
266
267         if (newval && newlen)
268         {
269                 if (name && *name == LASAT_PRID)
270                         lasat_board_info.li_eeprom_info.prid = *(int*)newval;
271
272                 lasat_write_eeprom_info();
273                 lasat_init_board_info();
274         }
275         mutex_unlock(&lasat_info_mutex);
276
277         return 0;
278 }
279
280 int proc_lasat_eeprom_value(ctl_table *table, int write, struct file *filp,
281                        void *buffer, size_t *lenp, loff_t *ppos)
282 {
283         int r;
284         mutex_lock(&lasat_info_mutex);
285         r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
286         if ( (!write) || r) {
287                 mutex_unlock(&lasat_info_mutex);
288                 return r;
289         }
290         if (filp && filp->f_dentry)
291         {
292                 if (!strcmp(filp->f_dentry->d_name.name, "prid"))
293                         lasat_board_info.li_eeprom_info.prid = lasat_board_info.li_prid;
294                 if (!strcmp(filp->f_dentry->d_name.name, "debugaccess"))
295                         lasat_board_info.li_eeprom_info.debugaccess = lasat_board_info.li_debugaccess;
296         }
297         lasat_write_eeprom_info();
298         mutex_unlock(&lasat_info_mutex);
299         return 0;
300 }
301
302 extern int lasat_boot_to_service;
303
304 #ifdef CONFIG_SYSCTL
305
306 static ctl_table lasat_table[] = {
307         {LASAT_CPU_HZ, "cpu-hz", &lasat_board_info.li_cpu_hz, sizeof(int),
308          0444, NULL, &proc_dointvec, &sysctl_intvec},
309         {LASAT_BUS_HZ, "bus-hz", &lasat_board_info.li_bus_hz, sizeof(int),
310          0444, NULL, &proc_dointvec, &sysctl_intvec},
311         {LASAT_MODEL, "bmid", &lasat_board_info.li_bmid, sizeof(int),
312          0444, NULL, &proc_dointvec, &sysctl_intvec},
313         {LASAT_PRID, "prid", &lasat_board_info.li_prid, sizeof(int),
314          0644, NULL, &proc_lasat_eeprom_value, &sysctl_lasat_eeprom_value},
315 #ifdef CONFIG_INET
316         {LASAT_IPADDR, "ipaddr", &lasat_board_info.li_eeprom_info.ipaddr, sizeof(int),
317          0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
318         {LASAT_NETMASK, "netmask", &lasat_board_info.li_eeprom_info.netmask, sizeof(int),
319          0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
320         {LASAT_BCAST, "bcastaddr", &lasat_bcastaddr,
321                 sizeof(lasat_bcastaddr), 0600, NULL,
322                 &proc_dostring, &sysctl_string},
323 #endif
324         {LASAT_PASSWORD, "passwd_hash", &lasat_board_info.li_eeprom_info.passwd_hash, sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
325          0600, NULL, &proc_dolasatstring, &sysctl_lasatstring},
326         {LASAT_SBOOT, "boot-service", &lasat_boot_to_service, sizeof(int),
327          0644, NULL, &proc_dointvec, &sysctl_intvec},
328 #ifdef CONFIG_DS1603
329         {LASAT_RTC, "rtc", &rtctmp, sizeof(int),
330          0644, NULL, &proc_dolasatrtc, &sysctl_lasat_rtc},
331 #endif
332         {LASAT_NAMESTR, "namestr", &lasat_board_info.li_namestr, sizeof(lasat_board_info.li_namestr),
333          0444, NULL, &proc_dostring, &sysctl_string},
334         {LASAT_TYPESTR, "typestr", &lasat_board_info.li_typestr, sizeof(lasat_board_info.li_typestr),
335          0444, NULL, &proc_dostring, &sysctl_string},
336         {0}
337 };
338
339 #define CTL_LASAT 1     // CTL_ANY ???
340 static ctl_table lasat_root_table[] = {
341         { CTL_LASAT, "lasat", NULL, 0, 0555, lasat_table },
342         { 0 }
343 };
344
345 static int __init lasat_register_sysctl(void)
346 {
347         struct ctl_table_header *lasat_table_header;
348
349         lasat_table_header =
350                 register_sysctl_table(lasat_root_table, 0);
351
352         return 0;
353 }
354
355 __initcall(lasat_register_sysctl);
356 #endif /* CONFIG_SYSCTL */