sysctl.c 10.2 KB
Newer Older
L
Linus Torvalds 已提交
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
/*
 * 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>
32
#include <linux/mutex.h>
L
Linus Torvalds 已提交
33 34 35 36 37
#include <asm/uaccess.h>

#include "sysctl.h"
#include "ds1603.h"

38
static DEFINE_MUTEX(lasat_info_mutex);
L
Linus Torvalds 已提交
39

40
/* Strategy function to write EEPROM after changing string entry */
L
Linus Torvalds 已提交
41 42
int sysctl_lasatstring(ctl_table *table, int *name, int nlen,
		void *oldval, size_t *oldlenp,
43
		void *newval, size_t newlen)
L
Linus Torvalds 已提交
44 45
{
	int r;
46
	mutex_lock(&lasat_info_mutex);
47
	r = sysctl_string(table, name,
48
			  nlen, oldval, oldlenp, newval, newlen);
L
Linus Torvalds 已提交
49
	if (r < 0) {
50
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
51 52 53 54 55
		return r;
	}
	if (newval && newlen) {
		lasat_write_eeprom_info();
	}
56
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65
	return 1;
}


/* 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;
66
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
67 68
	r = proc_dostring(table, write, filp, buffer, lenp, ppos);
	if ( (!write) || r) {
69
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
70 71 72
		return r;
	}
	lasat_write_eeprom_info();
73
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
74 75 76
	return 0;
}

77
/* proc function to write EEPROM after changing int entry */
L
Linus Torvalds 已提交
78 79 80 81
int proc_dolasatint(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;
82
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
83 84
	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
	if ( (!write) || r) {
85
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
86 87 88
		return r;
	}
	lasat_write_eeprom_info();
89
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
90 91 92 93 94 95
	return 0;
}

static int rtctmp;

#ifdef CONFIG_DS1603
96
/* proc function to read/write RealTime Clock */
L
Linus Torvalds 已提交
97 98 99 100
int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;
101
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109
	if (!write) {
		rtctmp = ds1603_read();
		/* check for time < 0 and set to 0 */
		if (rtctmp < 0)
			rtctmp = 0;
	}
	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
	if ( (!write) || r) {
110
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
111 112 113
		return r;
	}
	ds1603_set(rtctmp);
114
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
115 116 117 118 119 120 121
	return 0;
}
#endif

/* Sysctl for setting the IP addresses */
int sysctl_lasat_intvec(ctl_table *table, int *name, int nlen,
		    void *oldval, size_t *oldlenp,
122
		    void *newval, size_t newlen)
L
Linus Torvalds 已提交
123 124
{
	int r;
125
	mutex_lock(&lasat_info_mutex);
126
	r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
L
Linus Torvalds 已提交
127
	if (r < 0) {
128
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
129 130 131 132 133
		return r;
	}
	if (newval && newlen) {
		lasat_write_eeprom_info();
	}
134
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141
	return 1;
}

#ifdef CONFIG_DS1603
/* Same for RTC */
int sysctl_lasat_rtc(ctl_table *table, int *name, int nlen,
		    void *oldval, size_t *oldlenp,
142
		    void *newval, size_t newlen)
L
Linus Torvalds 已提交
143 144
{
	int r;
145
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
146 147 148
	rtctmp = ds1603_read();
	if (rtctmp < 0)
		rtctmp = 0;
149
	r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
L
Linus Torvalds 已提交
150
	if (r < 0) {
151
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
152 153 154 155 156
		return r;
	}
	if (newval && newlen) {
		ds1603_set(rtctmp);
	}
157
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167
	return 1;
}
#endif

#ifdef CONFIG_INET
static char lasat_bcastaddr[16];

void update_bcastaddr(void)
{
	unsigned int ip;
168 169 170

	ip = (lasat_board_info.li_eeprom_info.ipaddr &
		lasat_board_info.li_eeprom_info.netmask) |
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		~lasat_board_info.li_eeprom_info.netmask;

	sprintf(lasat_bcastaddr, "%d.%d.%d.%d",
			(ip      ) & 0xff,
			(ip >>  8) & 0xff,
			(ip >> 16) & 0xff,
			(ip >> 24) & 0xff);
}

static char proc_lasat_ipbuf[32];
/* Parsing of IP address */
int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int len;
        unsigned int ip;
	char *p, c;

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

195
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
196 197 198 199 200
	if (write) {
		len = 0;
		p = buffer;
		while (len < *lenp) {
			if(get_user(c, p++)) {
201
				mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
202 203 204 205 206 207
				return -EFAULT;
			}
			if (c == 0 || c == '\n')
				break;
			len++;
		}
208
		if (len >= sizeof(proc_lasat_ipbuf)-1)
L
Linus Torvalds 已提交
209 210 211
			len = sizeof(proc_lasat_ipbuf) - 1;
		if (copy_from_user(proc_lasat_ipbuf, buffer, len))
		{
212
			mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
			return -EFAULT;
		}
		proc_lasat_ipbuf[len] = 0;
		*ppos += *lenp;
		/* Now see if we can convert it to a valid IP */
		ip = in_aton(proc_lasat_ipbuf);
		*(unsigned int *)(table->data) = ip;
		lasat_write_eeprom_info();
	} else {
		ip = *(unsigned int *)(table->data);
		sprintf(proc_lasat_ipbuf, "%d.%d.%d.%d",
			(ip      ) & 0xff,
			(ip >>  8) & 0xff,
			(ip >> 16) & 0xff,
			(ip >> 24) & 0xff);
		len = strlen(proc_lasat_ipbuf);
		if (len > *lenp)
			len = *lenp;
		if (len)
			if(copy_to_user(buffer, proc_lasat_ipbuf, len)) {
233
				mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
234 235 236 237
				return -EFAULT;
			}
		if (len < *lenp) {
			if(put_user('\n', ((char *) buffer) + len)) {
238
				mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246
				return -EFAULT;
			}
			len++;
		}
		*lenp = len;
		*ppos += len;
	}
	update_bcastaddr();
247
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
248 249 250 251
	return 0;
}
#endif /* defined(CONFIG_INET) */

252 253
static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
				     void *oldval, size_t *oldlenp,
254
				     void *newval, size_t newlen)
L
Linus Torvalds 已提交
255 256 257
{
	int r;

258
	mutex_lock(&lasat_info_mutex);
259
	r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
L
Linus Torvalds 已提交
260
	if (r < 0) {
261
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272
		return r;
	}

	if (newval && newlen)
	{
		if (name && *name == LASAT_PRID)
			lasat_board_info.li_eeprom_info.prid = *(int*)newval;

		lasat_write_eeprom_info();
		lasat_init_board_info();
	}
273
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281

	return 0;
}

int proc_lasat_eeprom_value(ctl_table *table, int write, struct file *filp,
		       void *buffer, size_t *lenp, loff_t *ppos)
{
	int r;
282
	mutex_lock(&lasat_info_mutex);
L
Linus Torvalds 已提交
283 284
	r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
	if ( (!write) || r) {
285
		mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
286 287
		return r;
	}
J
Josef Sipek 已提交
288
	if (filp && filp->f_path.dentry)
L
Linus Torvalds 已提交
289
	{
J
Josef Sipek 已提交
290
		if (!strcmp(filp->f_path.dentry->d_name.name, "prid"))
L
Linus Torvalds 已提交
291
			lasat_board_info.li_eeprom_info.prid = lasat_board_info.li_prid;
J
Josef Sipek 已提交
292
		if (!strcmp(filp->f_path.dentry->d_name.name, "debugaccess"))
L
Linus Torvalds 已提交
293 294
			lasat_board_info.li_eeprom_info.debugaccess = lasat_board_info.li_debugaccess;
	}
295
	lasat_write_eeprom_info();
296
	mutex_unlock(&lasat_info_mutex);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304
	return 0;
}

extern int lasat_boot_to_service;

#ifdef CONFIG_SYSCTL

static ctl_table lasat_table[] = {
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
	{
		.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,
		.proc_handler	= &proc_lasat_eeprom_value,
		.strategy	= &sysctl_lasat_eeprom_value
	},
L
Linus Torvalds 已提交
341
#ifdef CONFIG_INET
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	{
		.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
	},
	{
		.ctl_name	= LASAT_NETMASK,
		.procname	= "netmask",
		.data		= &lasat_board_info.li_eeprom_info.netmask,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_lasat_ip,
		.strategy	= &sysctl_lasat_intvec
	},
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "bcastaddr",
		.data		= &lasat_bcastaddr,
		.maxlen		= sizeof(lasat_bcastaddr),
		.mode		= 0600,
		.proc_handler	= &proc_dostring,
		.strategy	= &sysctl_string
	},
L
Linus Torvalds 已提交
369
#endif
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	{
		.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
	},
L
Linus Torvalds 已提交
388
#ifdef CONFIG_DS1603
389 390 391 392 393 394 395 396 397
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "rtc",
		.data		= &rtctmp,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dolasatrtc,
		.strategy	= &sysctl_lasat_rtc
	},
L
Linus Torvalds 已提交
398
#endif
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "namestr",
		.data		= &lasat_board_info.li_namestr,
		.maxlen		= sizeof(lasat_board_info.li_namestr),
		.mode		= 0444,
		.proc_handler	=  &proc_dostring,
		.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
	},
	{}
L
Linus Torvalds 已提交
418 419 420
};

static ctl_table lasat_root_table[] = {
421 422 423 424 425 426 427
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "lasat",
		.mode		=  0555,
		.child		= lasat_table
	},
	{}
L
Linus Torvalds 已提交
428 429 430 431 432 433 434
};

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

	lasat_table_header =
435
		register_sysctl_table(lasat_root_table);
L
Linus Torvalds 已提交
436 437 438 439 440 441

	return 0;
}

__initcall(lasat_register_sysctl);
#endif /* CONFIG_SYSCTL */