blob: 12878359f2c883673950f0e90ec2da01732c543d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/sysctl.h>
25#include <linux/stddef.h>
26#include <linux/init.h>
27#include <linux/fs.h>
28#include <linux/ctype.h>
29#include <linux/string.h>
30#include <linux/net.h>
31#include <linux/inet.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/uaccess.h>
34
35#include "sysctl.h"
36#include "ds1603.h"
37
Ingo Molnar14cc3e22006-03-26 01:37:14 -080038static DEFINE_MUTEX(lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070040/* Strategy function to write EEPROM after changing string entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041int sysctl_lasatstring(ctl_table *table, int *name, int nlen,
42 void *oldval, size_t *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -080043 void *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080046 mutex_lock(&lasat_info_mutex);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070047 r = sysctl_string(table, name,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -080048 nlen, oldval, oldlenp, newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080050 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return r;
52 }
53 if (newval && newlen) {
54 lasat_write_eeprom_info();
55 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -080056 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return 1;
58}
59
60
61/* And the same for proc */
62int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
63 void *buffer, size_t *lenp, loff_t *ppos)
64{
65 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080066 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 r = proc_dostring(table, write, filp, buffer, lenp, ppos);
68 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080069 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return r;
71 }
72 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -080073 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75}
76
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070077/* proc function to write EEPROM after changing int entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int proc_dolasatint(ctl_table *table, int write, struct file *filp,
79 void *buffer, size_t *lenp, loff_t *ppos)
80{
81 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080082 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
84 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080085 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return r;
87 }
88 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -080089 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
93static int rtctmp;
94
95#ifdef CONFIG_DS1603
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070096/* proc function to read/write RealTime Clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
98 void *buffer, size_t *lenp, loff_t *ppos)
99{
100 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800101 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!write) {
103 rtctmp = ds1603_read();
104 /* check for time < 0 and set to 0 */
105 if (rtctmp < 0)
106 rtctmp = 0;
107 }
108 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
109 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800110 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return r;
112 }
113 ds1603_set(rtctmp);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800114 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117#endif
118
119/* Sysctl for setting the IP addresses */
120int sysctl_lasat_intvec(ctl_table *table, int *name, int nlen,
121 void *oldval, size_t *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800122 void *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800125 mutex_lock(&lasat_info_mutex);
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800126 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800128 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return r;
130 }
131 if (newval && newlen) {
132 lasat_write_eeprom_info();
133 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800134 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 1;
136}
137
138#ifdef CONFIG_DS1603
139/* Same for RTC */
140int sysctl_lasat_rtc(ctl_table *table, int *name, int nlen,
141 void *oldval, size_t *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800142 void *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800145 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 rtctmp = ds1603_read();
147 if (rtctmp < 0)
148 rtctmp = 0;
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800149 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800151 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return r;
153 }
154 if (newval && newlen) {
155 ds1603_set(rtctmp);
156 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800157 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 1;
159}
160#endif
161
162#ifdef CONFIG_INET
163static char lasat_bcastaddr[16];
164
165void update_bcastaddr(void)
166{
167 unsigned int ip;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700168
169 ip = (lasat_board_info.li_eeprom_info.ipaddr &
170 lasat_board_info.li_eeprom_info.netmask) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ~lasat_board_info.li_eeprom_info.netmask;
172
173 sprintf(lasat_bcastaddr, "%d.%d.%d.%d",
174 (ip ) & 0xff,
175 (ip >> 8) & 0xff,
176 (ip >> 16) & 0xff,
177 (ip >> 24) & 0xff);
178}
179
180static char proc_lasat_ipbuf[32];
181/* Parsing of IP address */
182int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
183 void *buffer, size_t *lenp, loff_t *ppos)
184{
185 int len;
186 unsigned int ip;
187 char *p, c;
188
189 if (!table->data || !table->maxlen || !*lenp ||
190 (*ppos && !write)) {
191 *lenp = 0;
192 return 0;
193 }
194
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800195 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (write) {
197 len = 0;
198 p = buffer;
199 while (len < *lenp) {
200 if(get_user(c, p++)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800201 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EFAULT;
203 }
204 if (c == 0 || c == '\n')
205 break;
206 len++;
207 }
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700208 if (len >= sizeof(proc_lasat_ipbuf)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 len = sizeof(proc_lasat_ipbuf) - 1;
210 if (copy_from_user(proc_lasat_ipbuf, buffer, len))
211 {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800212 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -EFAULT;
214 }
215 proc_lasat_ipbuf[len] = 0;
216 *ppos += *lenp;
217 /* Now see if we can convert it to a valid IP */
218 ip = in_aton(proc_lasat_ipbuf);
219 *(unsigned int *)(table->data) = ip;
220 lasat_write_eeprom_info();
221 } else {
222 ip = *(unsigned int *)(table->data);
223 sprintf(proc_lasat_ipbuf, "%d.%d.%d.%d",
224 (ip ) & 0xff,
225 (ip >> 8) & 0xff,
226 (ip >> 16) & 0xff,
227 (ip >> 24) & 0xff);
228 len = strlen(proc_lasat_ipbuf);
229 if (len > *lenp)
230 len = *lenp;
231 if (len)
232 if(copy_to_user(buffer, proc_lasat_ipbuf, len)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800233 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EFAULT;
235 }
236 if (len < *lenp) {
237 if(put_user('\n', ((char *) buffer) + len)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800238 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EFAULT;
240 }
241 len++;
242 }
243 *lenp = len;
244 *ppos += len;
245 }
246 update_bcastaddr();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800247 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250#endif /* defined(CONFIG_INET) */
251
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700252static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
253 void *oldval, size_t *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800254 void *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int r;
257
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800258 mutex_lock(&lasat_info_mutex);
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800259 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800261 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return r;
263 }
264
265 if (newval && newlen)
266 {
267 if (name && *name == LASAT_PRID)
268 lasat_board_info.li_eeprom_info.prid = *(int*)newval;
269
270 lasat_write_eeprom_info();
271 lasat_init_board_info();
272 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800273 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 return 0;
276}
277
278int proc_lasat_eeprom_value(ctl_table *table, int write, struct file *filp,
279 void *buffer, size_t *lenp, loff_t *ppos)
280{
281 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800282 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
284 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800285 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return r;
287 }
Josef Sipek1b04fe92006-12-08 02:37:20 -0800288 if (filp && filp->f_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 {
Josef Sipek1b04fe92006-12-08 02:37:20 -0800290 if (!strcmp(filp->f_path.dentry->d_name.name, "prid"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 lasat_board_info.li_eeprom_info.prid = lasat_board_info.li_prid;
Josef Sipek1b04fe92006-12-08 02:37:20 -0800292 if (!strcmp(filp->f_path.dentry->d_name.name, "debugaccess"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 lasat_board_info.li_eeprom_info.debugaccess = lasat_board_info.li_debugaccess;
294 }
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700295 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800296 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298}
299
300extern int lasat_boot_to_service;
301
302#ifdef CONFIG_SYSCTL
303
304static ctl_table lasat_table[] = {
305 {LASAT_CPU_HZ, "cpu-hz", &lasat_board_info.li_cpu_hz, sizeof(int),
306 0444, NULL, &proc_dointvec, &sysctl_intvec},
307 {LASAT_BUS_HZ, "bus-hz", &lasat_board_info.li_bus_hz, sizeof(int),
308 0444, NULL, &proc_dointvec, &sysctl_intvec},
309 {LASAT_MODEL, "bmid", &lasat_board_info.li_bmid, sizeof(int),
310 0444, NULL, &proc_dointvec, &sysctl_intvec},
311 {LASAT_PRID, "prid", &lasat_board_info.li_prid, sizeof(int),
312 0644, NULL, &proc_lasat_eeprom_value, &sysctl_lasat_eeprom_value},
313#ifdef CONFIG_INET
314 {LASAT_IPADDR, "ipaddr", &lasat_board_info.li_eeprom_info.ipaddr, sizeof(int),
315 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
316 {LASAT_NETMASK, "netmask", &lasat_board_info.li_eeprom_info.netmask, sizeof(int),
317 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700318 {LASAT_BCAST, "bcastaddr", &lasat_bcastaddr,
319 sizeof(lasat_bcastaddr), 0600, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 &proc_dostring, &sysctl_string},
321#endif
322 {LASAT_PASSWORD, "passwd_hash", &lasat_board_info.li_eeprom_info.passwd_hash, sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
323 0600, NULL, &proc_dolasatstring, &sysctl_lasatstring},
324 {LASAT_SBOOT, "boot-service", &lasat_boot_to_service, sizeof(int),
325 0644, NULL, &proc_dointvec, &sysctl_intvec},
326#ifdef CONFIG_DS1603
327 {LASAT_RTC, "rtc", &rtctmp, sizeof(int),
328 0644, NULL, &proc_dolasatrtc, &sysctl_lasat_rtc},
329#endif
330 {LASAT_NAMESTR, "namestr", &lasat_board_info.li_namestr, sizeof(lasat_board_info.li_namestr),
331 0444, NULL, &proc_dostring, &sysctl_string},
332 {LASAT_TYPESTR, "typestr", &lasat_board_info.li_typestr, sizeof(lasat_board_info.li_typestr),
333 0444, NULL, &proc_dostring, &sysctl_string},
334 {0}
335};
336
337#define CTL_LASAT 1 // CTL_ANY ???
338static ctl_table lasat_root_table[] = {
339 { CTL_LASAT, "lasat", NULL, 0, 0555, lasat_table },
340 { 0 }
341};
342
343static int __init lasat_register_sysctl(void)
344{
345 struct ctl_table_header *lasat_table_header;
346
347 lasat_table_header =
348 register_sysctl_table(lasat_root_table, 0);
349
350 return 0;
351}
352
353__initcall(lasat_register_sysctl);
354#endif /* CONFIG_SYSCTL */