blob: e3d5aaa90f0db260ee78026df04ff603f4690c8c [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
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>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080033#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35
36#include "sysctl.h"
37#include "ds1603.h"
38
Ingo Molnar14cc3e22006-03-26 01:37:14 -080039static DEFINE_MUTEX(lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070041/* Strategy function to write EEPROM after changing string entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042int 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;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080047 mutex_lock(&lasat_info_mutex);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070048 r = sysctl_string(table, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 nlen, oldval, oldlenp, newval, newlen, context);
50 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080051 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return r;
53 }
54 if (newval && newlen) {
55 lasat_write_eeprom_info();
56 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -080057 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return 1;
59}
60
61
62/* And the same for proc */
63int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
64 void *buffer, size_t *lenp, loff_t *ppos)
65{
66 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080067 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 r = proc_dostring(table, write, filp, buffer, lenp, ppos);
69 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080070 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return r;
72 }
73 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -080074 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 0;
76}
77
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070078/* proc function to write EEPROM after changing int entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079int proc_dolasatint(ctl_table *table, int write, struct file *filp,
80 void *buffer, size_t *lenp, loff_t *ppos)
81{
82 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080083 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
85 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080086 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return r;
88 }
89 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -080090 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
94static int rtctmp;
95
96#ifdef CONFIG_DS1603
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070097/* proc function to read/write RealTime Clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
99 void *buffer, size_t *lenp, loff_t *ppos)
100{
101 int r;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800102 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 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) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800111 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return r;
113 }
114 ds1603_set(rtctmp);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800115 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118#endif
119
120/* Sysctl for setting the IP addresses */
121int 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;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800126 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
128 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800129 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return r;
131 }
132 if (newval && newlen) {
133 lasat_write_eeprom_info();
134 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800135 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 1;
137}
138
139#ifdef CONFIG_DS1603
140/* Same for RTC */
141int 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;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800146 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 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) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800152 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return r;
154 }
155 if (newval && newlen) {
156 ds1603_set(rtctmp);
157 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800158 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 1;
160}
161#endif
162
163#ifdef CONFIG_INET
164static char lasat_bcastaddr[16];
165
166void update_bcastaddr(void)
167{
168 unsigned int ip;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700169
170 ip = (lasat_board_info.li_eeprom_info.ipaddr &
171 lasat_board_info.li_eeprom_info.netmask) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ~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
181static char proc_lasat_ipbuf[32];
182/* Parsing of IP address */
183int 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
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800196 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (write) {
198 len = 0;
199 p = buffer;
200 while (len < *lenp) {
201 if(get_user(c, p++)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800202 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -EFAULT;
204 }
205 if (c == 0 || c == '\n')
206 break;
207 len++;
208 }
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700209 if (len >= sizeof(proc_lasat_ipbuf)-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 len = sizeof(proc_lasat_ipbuf) - 1;
211 if (copy_from_user(proc_lasat_ipbuf, buffer, len))
212 {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800213 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800234 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EFAULT;
236 }
237 if (len < *lenp) {
238 if(put_user('\n', ((char *) buffer) + len)) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800239 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EFAULT;
241 }
242 len++;
243 }
244 *lenp = len;
245 *ppos += len;
246 }
247 update_bcastaddr();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800248 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251#endif /* defined(CONFIG_INET) */
252
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700253static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
254 void *oldval, size_t *oldlenp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 void *newval, size_t newlen,
256 void **context)
257{
258 int r;
259
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800260 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
262 if (r < 0) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800263 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 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 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800275 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return 0;
278}
279
280int 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;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800284 mutex_lock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
286 if ( (!write) || r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800287 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 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 }
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700297 lasat_write_eeprom_info();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800298 mutex_unlock(&lasat_info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
301
302extern int lasat_boot_to_service;
303
304#ifdef CONFIG_SYSCTL
305
306static 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},
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700320 {LASAT_BCAST, "bcastaddr", &lasat_bcastaddr,
321 sizeof(lasat_bcastaddr), 0600, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 &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 ???
340static ctl_table lasat_root_table[] = {
341 { CTL_LASAT, "lasat", NULL, 0, 0555, lasat_table },
342 { 0 }
343};
344
345static 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 */