blob: 8f88886feb12e147167fa7bcf61a5961bd6430cf [file] [log] [blame]
Brian Murphy1f21d2b2007-08-21 22:34:16 +02001/*
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/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>
Brian Murphy1f21d2b2007-08-21 22:34:16 +020032#include <linux/uaccess.h>
33
Ralf Baechle4b550482007-10-11 23:46:08 +010034#include <asm/time.h>
35
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010036#ifdef CONFIG_DS1603
Brian Murphy1f21d2b2007-08-21 22:34:16 +020037#include "ds1603.h"
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010038#endif
Brian Murphy1f21d2b2007-08-21 22:34:16 +020039
40/* Strategy function to write EEPROM after changing string entry */
Alexey Dobriyanf221e722008-10-15 22:04:23 -070041int sysctl_lasatstring(ctl_table *table,
Brian Murphy1f21d2b2007-08-21 22:34:16 +020042 void *oldval, size_t *oldlenp,
43 void *newval, size_t newlen)
44{
45 int r;
46
Alexey Dobriyanf221e722008-10-15 22:04:23 -070047 r = sysctl_string(table, oldval, oldlenp, newval, newlen);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010048 if (r < 0)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020049 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010050
Brian Murphy1f21d2b2007-08-21 22:34:16 +020051 if (newval && newlen)
52 lasat_write_eeprom_info();
Brian Murphy1f21d2b2007-08-21 22:34:16 +020053
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010054 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020055}
56
57
58/* And the same for proc */
59int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
60 void *buffer, size_t *lenp, loff_t *ppos)
61{
62 int r;
63
Brian Murphy1f21d2b2007-08-21 22:34:16 +020064 r = proc_dostring(table, write, filp, buffer, lenp, ppos);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010065 if ((!write) || r)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020066 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010067
Brian Murphy1f21d2b2007-08-21 22:34:16 +020068 lasat_write_eeprom_info();
Brian Murphy1f21d2b2007-08-21 22:34:16 +020069
70 return 0;
71}
72
73/* proc function to write EEPROM after changing int entry */
74int proc_dolasatint(ctl_table *table, int write, struct file *filp,
75 void *buffer, size_t *lenp, loff_t *ppos)
76{
77 int r;
78
Brian Murphy1f21d2b2007-08-21 22:34:16 +020079 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010080 if ((!write) || r)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020081 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010082
Brian Murphy1f21d2b2007-08-21 22:34:16 +020083 lasat_write_eeprom_info();
Brian Murphy1f21d2b2007-08-21 22:34:16 +020084
85 return 0;
86}
87
Thomas Horsten1f34f2e2008-06-15 02:17:11 +010088#ifdef CONFIG_DS1603
Brian Murphy1f21d2b2007-08-21 22:34:16 +020089static int rtctmp;
90
Brian Murphy1f21d2b2007-08-21 22:34:16 +020091/* proc function to read/write RealTime Clock */
92int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
93 void *buffer, size_t *lenp, loff_t *ppos)
94{
95 int r;
96
Brian Murphy1f21d2b2007-08-21 22:34:16 +020097 if (!write) {
Ralf Baechle4b550482007-10-11 23:46:08 +010098 rtctmp = read_persistent_clock();
Brian Murphy1f21d2b2007-08-21 22:34:16 +020099 /* check for time < 0 and set to 0 */
100 if (rtctmp < 0)
101 rtctmp = 0;
102 }
103 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100104 if (r)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200105 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100106
107 if (write)
108 rtc_mips_set_mmss(rtctmp);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200109
110 return 0;
111}
112#endif
113
114/* Sysctl for setting the IP addresses */
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700115int sysctl_lasat_intvec(ctl_table *table,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200116 void *oldval, size_t *oldlenp,
117 void *newval, size_t newlen)
118{
119 int r;
120
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700121 r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100122 if (r < 0)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200123 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100124
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200125 if (newval && newlen)
126 lasat_write_eeprom_info();
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200127
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100128 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200129}
130
131#ifdef CONFIG_DS1603
132/* Same for RTC */
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700133int sysctl_lasat_rtc(ctl_table *table,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200134 void *oldval, size_t *oldlenp,
135 void *newval, size_t newlen)
136{
137 int r;
138
Ralf Baechle4b550482007-10-11 23:46:08 +0100139 rtctmp = read_persistent_clock();
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200140 if (rtctmp < 0)
141 rtctmp = 0;
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700142 r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100143 if (r < 0)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200144 return r;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200145 if (newval && newlen)
Ralf Baechle4b550482007-10-11 23:46:08 +0100146 rtc_mips_set_mmss(rtctmp);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200147
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100148 return r;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200149}
150#endif
151
152#ifdef CONFIG_INET
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200153int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
154 void *buffer, size_t *lenp, loff_t *ppos)
155{
156 unsigned int ip;
157 char *p, c;
158 int len;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100159 char ipbuf[32];
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200160
161 if (!table->data || !table->maxlen || !*lenp ||
162 (*ppos && !write)) {
163 *lenp = 0;
164 return 0;
165 }
166
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200167 if (write) {
168 len = 0;
169 p = buffer;
170 while (len < *lenp) {
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100171 if (get_user(c, p++))
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200172 return -EFAULT;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200173 if (c == 0 || c == '\n')
174 break;
175 len++;
176 }
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100177 if (len >= sizeof(ipbuf)-1)
178 len = sizeof(ipbuf) - 1;
179 if (copy_from_user(ipbuf, buffer, len))
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200180 return -EFAULT;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100181 ipbuf[len] = 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200182 *ppos += *lenp;
183 /* Now see if we can convert it to a valid IP */
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100184 ip = in_aton(ipbuf);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200185 *(unsigned int *)(table->data) = ip;
186 lasat_write_eeprom_info();
187 } else {
188 ip = *(unsigned int *)(table->data);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100189 sprintf(ipbuf, "%d.%d.%d.%d",
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200190 (ip) & 0xff,
191 (ip >> 8) & 0xff,
192 (ip >> 16) & 0xff,
193 (ip >> 24) & 0xff);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100194 len = strlen(ipbuf);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200195 if (len > *lenp)
196 len = *lenp;
197 if (len)
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100198 if (copy_to_user(buffer, ipbuf, len))
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200199 return -EFAULT;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200200 if (len < *lenp) {
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100201 if (put_user('\n', ((char *) buffer) + len))
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200202 return -EFAULT;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200203 len++;
204 }
205 *lenp = len;
206 *ppos += len;
207 }
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200208
209 return 0;
210}
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100211#endif
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200212
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700213static int sysctl_lasat_prid(ctl_table *table,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200214 void *oldval, size_t *oldlenp,
215 void *newval, size_t newlen)
216{
217 int r;
218
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700219 r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100220 if (r < 0)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200221 return r;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200222 if (newval && newlen) {
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100223 lasat_board_info.li_eeprom_info.prid = *(int *)newval;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200224 lasat_write_eeprom_info();
225 lasat_init_board_info();
226 }
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200227 return 0;
228}
229
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100230int proc_lasat_prid(ctl_table *table, int write, struct file *filp,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200231 void *buffer, size_t *lenp, loff_t *ppos)
232{
233 int r;
234
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200235 r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100236 if (r < 0)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200237 return r;
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100238 if (write) {
239 lasat_board_info.li_eeprom_info.prid =
240 lasat_board_info.li_prid;
241 lasat_write_eeprom_info();
242 lasat_init_board_info();
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200243 }
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200244 return 0;
245}
246
247extern int lasat_boot_to_service;
248
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200249static ctl_table lasat_table[] = {
250 {
251 .ctl_name = CTL_UNNUMBERED,
252 .procname = "cpu-hz",
253 .data = &lasat_board_info.li_cpu_hz,
254 .maxlen = sizeof(int),
255 .mode = 0444,
256 .proc_handler = &proc_dointvec,
257 .strategy = &sysctl_intvec
258 },
259 {
260 .ctl_name = CTL_UNNUMBERED,
261 .procname = "bus-hz",
262 .data = &lasat_board_info.li_bus_hz,
263 .maxlen = sizeof(int),
264 .mode = 0444,
265 .proc_handler = &proc_dointvec,
266 .strategy = &sysctl_intvec
267 },
268 {
269 .ctl_name = CTL_UNNUMBERED,
270 .procname = "bmid",
271 .data = &lasat_board_info.li_bmid,
272 .maxlen = sizeof(int),
273 .mode = 0444,
274 .proc_handler = &proc_dointvec,
275 .strategy = &sysctl_intvec
276 },
277 {
278 .ctl_name = CTL_UNNUMBERED,
279 .procname = "prid",
280 .data = &lasat_board_info.li_prid,
281 .maxlen = sizeof(int),
282 .mode = 0644,
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100283 .proc_handler = &proc_lasat_prid,
284 .strategy = &sysctl_lasat_prid
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200285 },
286#ifdef CONFIG_INET
287 {
288 .ctl_name = CTL_UNNUMBERED,
289 .procname = "ipaddr",
290 .data = &lasat_board_info.li_eeprom_info.ipaddr,
291 .maxlen = sizeof(int),
292 .mode = 0644,
293 .proc_handler = &proc_lasat_ip,
294 .strategy = &sysctl_lasat_intvec
295 },
296 {
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100297 .ctl_name = CTL_UNNUMBERED,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200298 .procname = "netmask",
299 .data = &lasat_board_info.li_eeprom_info.netmask,
300 .maxlen = sizeof(int),
301 .mode = 0644,
302 .proc_handler = &proc_lasat_ip,
303 .strategy = &sysctl_lasat_intvec
304 },
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200305#endif
306 {
307 .ctl_name = CTL_UNNUMBERED,
308 .procname = "passwd_hash",
309 .data = &lasat_board_info.li_eeprom_info.passwd_hash,
310 .maxlen =
311 sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
312 .mode = 0600,
313 .proc_handler = &proc_dolasatstring,
314 .strategy = &sysctl_lasatstring
315 },
316 {
317 .ctl_name = CTL_UNNUMBERED,
318 .procname = "boot-service",
319 .data = &lasat_boot_to_service,
320 .maxlen = sizeof(int),
321 .mode = 0644,
322 .proc_handler = &proc_dointvec,
323 .strategy = &sysctl_intvec
324 },
325#ifdef CONFIG_DS1603
326 {
327 .ctl_name = CTL_UNNUMBERED,
328 .procname = "rtc",
329 .data = &rtctmp,
330 .maxlen = sizeof(int),
331 .mode = 0644,
332 .proc_handler = &proc_dolasatrtc,
333 .strategy = &sysctl_lasat_rtc
334 },
335#endif
336 {
337 .ctl_name = CTL_UNNUMBERED,
338 .procname = "namestr",
339 .data = &lasat_board_info.li_namestr,
340 .maxlen = sizeof(lasat_board_info.li_namestr),
341 .mode = 0444,
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100342 .proc_handler = &proc_dostring,
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200343 .strategy = &sysctl_string
344 },
345 {
346 .ctl_name = CTL_UNNUMBERED,
347 .procname = "typestr",
348 .data = &lasat_board_info.li_typestr,
349 .maxlen = sizeof(lasat_board_info.li_typestr),
350 .mode = 0444,
351 .proc_handler = &proc_dostring,
352 .strategy = &sysctl_string
353 },
354 {}
355};
356
357static ctl_table lasat_root_table[] = {
358 {
359 .ctl_name = CTL_UNNUMBERED,
360 .procname = "lasat",
361 .mode = 0555,
362 .child = lasat_table
363 },
364 {}
365};
366
367static int __init lasat_register_sysctl(void)
368{
369 struct ctl_table_header *lasat_table_header;
370
371 lasat_table_header =
372 register_sysctl_table(lasat_root_table);
Thomas Horsten1f34f2e2008-06-15 02:17:11 +0100373 if (!lasat_table_header) {
374 printk(KERN_ERR "Unable to register LASAT sysctl\n");
375 return -ENOMEM;
376 }
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200377
378 return 0;
379}
380
381__initcall(lasat_register_sysctl);