sysctl.c 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Thomas Horsten <thh@lasat.com>
 * Copyright (C) 2000 LASAT Networks A/S.
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * Routines specific to the LASAT boards
 */
#include <linux/types.h>
#include <asm/lasat/lasat.h>

#include <linux/module.h>
#include <linux/sysctl.h>
#include <linux/stddef.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/uaccess.h>

34 35
#include <asm/time.h>

T
Thomas Horsten 已提交
36
#ifdef CONFIG_DS1603
37
#include "ds1603.h"
T
Thomas Horsten 已提交
38
#endif
39 40

/* Strategy function to write EEPROM after changing string entry */
A
Alexey Dobriyan 已提交
41
int sysctl_lasatstring(ctl_table *table,
42 43 44 45 46
		void *oldval, size_t *oldlenp,
		void *newval, size_t newlen)
{
	int r;

A
Alexey Dobriyan 已提交
47
	r = sysctl_string(table, oldval, oldlenp, newval, newlen);
T
Thomas Horsten 已提交
48
	if (r < 0)
49
		return r;
T
Thomas Horsten 已提交
50

51 52 53
	if (newval && newlen)
		lasat_write_eeprom_info();

T
Thomas Horsten 已提交
54
	return 0;
55 56 57 58 59 60 61 62 63 64
}


/* And the same for proc */
int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;

	r = proc_dostring(table, write, filp, buffer, lenp, ppos);
T
Thomas Horsten 已提交
65
	if ((!write) || r)
66
		return r;
T
Thomas Horsten 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79
	lasat_write_eeprom_info();

	return 0;
}

/* proc function to write EEPROM after changing int entry */
int proc_dolasatint(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;

	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
T
Thomas Horsten 已提交
80
	if ((!write) || r)
81
		return r;
T
Thomas Horsten 已提交
82

83 84 85 86 87
	lasat_write_eeprom_info();

	return 0;
}

T
Thomas Horsten 已提交
88
#ifdef CONFIG_DS1603
89 90 91 92 93 94
static int rtctmp;

/* proc function to read/write RealTime Clock */
int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
95
	struct timespec ts;
96 97 98
	int r;

	if (!write) {
99 100
		read_persistent_clock(&ts);
		rtctmp = ts.tv_sec;
101 102 103 104 105
		/* check for time < 0 and set to 0 */
		if (rtctmp < 0)
			rtctmp = 0;
	}
	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
T
Thomas Horsten 已提交
106
	if (r)
107
		return r;
T
Thomas Horsten 已提交
108 109 110

	if (write)
		rtc_mips_set_mmss(rtctmp);
111 112 113 114 115 116

	return 0;
}
#endif

/* Sysctl for setting the IP addresses */
A
Alexey Dobriyan 已提交
117
int sysctl_lasat_intvec(ctl_table *table,
118 119 120 121 122
		    void *oldval, size_t *oldlenp,
		    void *newval, size_t newlen)
{
	int r;

A
Alexey Dobriyan 已提交
123
	r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
T
Thomas Horsten 已提交
124
	if (r < 0)
125
		return r;
T
Thomas Horsten 已提交
126

127 128 129
	if (newval && newlen)
		lasat_write_eeprom_info();

T
Thomas Horsten 已提交
130
	return 0;
131 132 133 134
}

#ifdef CONFIG_DS1603
/* Same for RTC */
A
Alexey Dobriyan 已提交
135
int sysctl_lasat_rtc(ctl_table *table,
136 137 138
		    void *oldval, size_t *oldlenp,
		    void *newval, size_t newlen)
{
139
	struct timespec ts;
140 141
	int r;

142 143
	read_persistent_clock(&ts);
	rtctmp = ts.tv_sec;
144 145
	if (rtctmp < 0)
		rtctmp = 0;
A
Alexey Dobriyan 已提交
146
	r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
T
Thomas Horsten 已提交
147
	if (r < 0)
148 149
		return r;
	if (newval && newlen)
150
		rtc_mips_set_mmss(rtctmp);
151

T
Thomas Horsten 已提交
152
	return r;
153 154 155 156 157 158 159 160 161 162
}
#endif

#ifdef CONFIG_INET
int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	unsigned int ip;
	char *p, c;
	int len;
T
Thomas Horsten 已提交
163
	char ipbuf[32];
164 165 166 167 168 169 170 171 172 173 174

	if (!table->data || !table->maxlen || !*lenp ||
	    (*ppos && !write)) {
		*lenp = 0;
		return 0;
	}

	if (write) {
		len = 0;
		p = buffer;
		while (len < *lenp) {
T
Thomas Horsten 已提交
175
			if (get_user(c, p++))
176 177 178 179 180
				return -EFAULT;
			if (c == 0 || c == '\n')
				break;
			len++;
		}
T
Thomas Horsten 已提交
181 182 183
		if (len >= sizeof(ipbuf)-1)
			len = sizeof(ipbuf) - 1;
		if (copy_from_user(ipbuf, buffer, len))
184
			return -EFAULT;
T
Thomas Horsten 已提交
185
		ipbuf[len] = 0;
186 187
		*ppos += *lenp;
		/* Now see if we can convert it to a valid IP */
T
Thomas Horsten 已提交
188
		ip = in_aton(ipbuf);
189 190 191 192
		*(unsigned int *)(table->data) = ip;
		lasat_write_eeprom_info();
	} else {
		ip = *(unsigned int *)(table->data);
T
Thomas Horsten 已提交
193
		sprintf(ipbuf, "%d.%d.%d.%d",
194 195 196 197
			(ip)       & 0xff,
			(ip >>  8) & 0xff,
			(ip >> 16) & 0xff,
			(ip >> 24) & 0xff);
T
Thomas Horsten 已提交
198
		len = strlen(ipbuf);
199 200 201
		if (len > *lenp)
			len = *lenp;
		if (len)
T
Thomas Horsten 已提交
202
			if (copy_to_user(buffer, ipbuf, len))
203 204
				return -EFAULT;
		if (len < *lenp) {
T
Thomas Horsten 已提交
205
			if (put_user('\n', ((char *) buffer) + len))
206 207 208 209 210 211 212 213 214
				return -EFAULT;
			len++;
		}
		*lenp = len;
		*ppos += len;
	}

	return 0;
}
T
Thomas Horsten 已提交
215
#endif
216

A
Alexey Dobriyan 已提交
217
static int sysctl_lasat_prid(ctl_table *table,
218 219 220 221 222
				     void *oldval, size_t *oldlenp,
				     void *newval, size_t newlen)
{
	int r;

A
Alexey Dobriyan 已提交
223
	r = sysctl_intvec(table, oldval, oldlenp, newval, newlen);
T
Thomas Horsten 已提交
224
	if (r < 0)
225 226
		return r;
	if (newval && newlen) {
T
Thomas Horsten 已提交
227
		lasat_board_info.li_eeprom_info.prid = *(int *)newval;
228 229 230 231 232 233
		lasat_write_eeprom_info();
		lasat_init_board_info();
	}
	return 0;
}

T
Thomas Horsten 已提交
234
int proc_lasat_prid(ctl_table *table, int write, struct file *filp,
235 236 237 238 239
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;

	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
T
Thomas Horsten 已提交
240
	if (r < 0)
241
		return r;
T
Thomas Horsten 已提交
242 243 244 245 246
	if (write) {
		lasat_board_info.li_eeprom_info.prid =
			lasat_board_info.li_prid;
		lasat_write_eeprom_info();
		lasat_init_board_info();
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	}
	return 0;
}

extern int lasat_boot_to_service;

static ctl_table lasat_table[] = {
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "cpu-hz",
		.data		= &lasat_board_info.li_cpu_hz,
		.maxlen		= sizeof(int),
		.mode		= 0444,
		.proc_handler	= &proc_dointvec,
		.strategy	= &sysctl_intvec
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "bus-hz",
		.data		= &lasat_board_info.li_bus_hz,
		.maxlen		= sizeof(int),
		.mode		= 0444,
		.proc_handler	= &proc_dointvec,
		.strategy	= &sysctl_intvec
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "bmid",
		.data		= &lasat_board_info.li_bmid,
		.maxlen		= sizeof(int),
		.mode		= 0444,
		.proc_handler	= &proc_dointvec,
		.strategy	= &sysctl_intvec
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "prid",
		.data		= &lasat_board_info.li_prid,
		.maxlen		= sizeof(int),
		.mode		= 0644,
T
Thomas Horsten 已提交
287 288
		.proc_handler	= &proc_lasat_prid,
		.strategy	= &sysctl_lasat_prid
289 290 291 292 293 294 295 296 297 298 299 300
	},
#ifdef CONFIG_INET
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "ipaddr",
		.data		= &lasat_board_info.li_eeprom_info.ipaddr,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_lasat_ip,
		.strategy	= &sysctl_lasat_intvec
	},
	{
T
Thomas Horsten 已提交
301
		.ctl_name	= CTL_UNNUMBERED,
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
		.procname	= "netmask",
		.data		= &lasat_board_info.li_eeprom_info.netmask,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_lasat_ip,
		.strategy	= &sysctl_lasat_intvec
	},
#endif
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "passwd_hash",
		.data		= &lasat_board_info.li_eeprom_info.passwd_hash,
		.maxlen		=
			sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
		.mode		= 0600,
		.proc_handler	= &proc_dolasatstring,
		.strategy	= &sysctl_lasatstring
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "boot-service",
		.data		= &lasat_boot_to_service,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
		.strategy	= &sysctl_intvec
	},
#ifdef CONFIG_DS1603
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "rtc",
		.data		= &rtctmp,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dolasatrtc,
		.strategy	= &sysctl_lasat_rtc
	},
#endif
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "namestr",
		.data		= &lasat_board_info.li_namestr,
		.maxlen		= sizeof(lasat_board_info.li_namestr),
		.mode		= 0444,
T
Thomas Horsten 已提交
346
		.proc_handler	= &proc_dostring,
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		.strategy	= &sysctl_string
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "typestr",
		.data		= &lasat_board_info.li_typestr,
		.maxlen		= sizeof(lasat_board_info.li_typestr),
		.mode		= 0444,
		.proc_handler	= &proc_dostring,
		.strategy	= &sysctl_string
	},
	{}
};

static ctl_table lasat_root_table[] = {
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "lasat",
		.mode		=  0555,
		.child		= lasat_table
	},
	{}
};

static int __init lasat_register_sysctl(void)
{
	struct ctl_table_header *lasat_table_header;

	lasat_table_header =
		register_sysctl_table(lasat_root_table);
T
Thomas Horsten 已提交
377 378 379 380
	if (!lasat_table_header) {
		printk(KERN_ERR "Unable to register LASAT sysctl\n");
		return -ENOMEM;
	}
381 382 383 384 385

	return 0;
}

__initcall(lasat_register_sysctl);