1 /*
2 * sysctl.c
3 *
4 * Thomas Horsten <thh@lasat.com>
5 * Copyright (C) 2000 LASAT Networks A/S.
6 *
7 * ########################################################################
8 *
9 * This program is free software; you can distribute it and/or modify it
10 * under the terms of the GNU General Public License (Version 2) as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
21 *
22 * ########################################################################
23 *
24 * Routines specific to the LASAT boards
25 */
26 #include <linux/types.h>
27 #include <asm/lasat/lasat.h>
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/sysctl.h>
32 #include <linux/stddef.h>
33 #include <linux/init.h>
34 #include <linux/ctype.h>
35 #include <linux/string.h>
36 #include <linux/net.h>
37 #include <linux/inet.h>
38 #include <asm/uaccess.h>
39
40 #include "sysctl.h"
41 #include "ds1603.h"
42
43 static DECLARE_MUTEX(lasat_info_sem);
44
45 /* Strategy function to write EEPROM after changing string entry */
sysctl_lasatstring(ctl_table * table,int * name,int nlen,void * oldval,size_t * oldlenp,void * newval,size_t newlen,void ** context)46 int sysctl_lasatstring(ctl_table *table, int *name, int nlen,
47 void *oldval, size_t *oldlenp,
48 void *newval, size_t newlen, void **context)
49 {
50 int r;
51 down(&lasat_info_sem);
52 r = sysctl_string(table, name,
53 nlen, oldval, oldlenp, newval, newlen, context);
54 if (r < 0) {
55 up(&lasat_info_sem);
56 return r;
57 }
58 if (newval && newlen) {
59 lasat_write_eeprom_info();
60 }
61 up(&lasat_info_sem);
62 return 1;
63 }
64
65
66 /* And the same for proc */
proc_dolasatstring(ctl_table * table,int write,struct file * filp,void * buffer,size_t * lenp)67 int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
68 void *buffer, size_t *lenp)
69 {
70 int r;
71 down(&lasat_info_sem);
72 r = proc_dostring(table, write, filp, buffer, lenp);
73 if ( (!write) || r) {
74 up(&lasat_info_sem);
75 return r;
76 }
77 lasat_write_eeprom_info();
78 up(&lasat_info_sem);
79 return 0;
80 }
81
82 /* proc function to write EEPROM after changing int entry */
proc_dolasatint(ctl_table * table,int write,struct file * filp,void * buffer,size_t * lenp)83 int proc_dolasatint(ctl_table *table, int write, struct file *filp,
84 void *buffer, size_t *lenp)
85 {
86 int r;
87 down(&lasat_info_sem);
88 r = proc_dointvec(table, write, filp, buffer, lenp);
89 if ( (!write) || r) {
90 up(&lasat_info_sem);
91 return r;
92 }
93 lasat_write_eeprom_info();
94 up(&lasat_info_sem);
95 return 0;
96 }
97
98 /* Sysctl for setting the IP addresses */
sysctl_lasat_intvec(ctl_table * table,int * name,int nlen,void * oldval,size_t * oldlenp,void * newval,size_t newlen,void ** context)99 int sysctl_lasat_intvec(ctl_table *table, int *name, int nlen,
100 void *oldval, size_t *oldlenp,
101 void *newval, size_t newlen, void **context)
102 {
103 int r;
104 down(&lasat_info_sem);
105 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
106 if (r < 0) {
107 up(&lasat_info_sem);
108 return r;
109 }
110 if (newval && newlen) {
111 lasat_write_eeprom_info();
112 }
113 up(&lasat_info_sem);
114 return 1;
115 }
116
117 #ifdef CONFIG_INET
118 static char lasat_bcastaddr[16];
119
update_bcastaddr(void)120 void update_bcastaddr(void)
121 {
122 unsigned int ip;
123
124 ip = lasat_board_info.li_eeprom_info.ipaddr |
125 ~lasat_board_info.li_eeprom_info.netmask;
126
127 sprintf(lasat_bcastaddr, "%d.%d.%d.%d",
128 (ip ) & 0xff,
129 (ip >> 8) & 0xff,
130 (ip >> 16) & 0xff,
131 (ip >> 24) & 0xff);
132 }
133
134 static char proc_lasat_ipbuf[32];
135 /* Parsing of IP address */
proc_lasat_ip(ctl_table * table,int write,struct file * filp,void * buffer,size_t * lenp)136 int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
137 void *buffer, size_t *lenp)
138 {
139 int len;
140 unsigned int ip;
141 char *p, c;
142
143 if (!table->data || !table->maxlen || !*lenp ||
144 (filp->f_pos && !write)) {
145 *lenp = 0;
146 return 0;
147 }
148
149 down(&lasat_info_sem);
150 if (write) {
151 len = 0;
152 p = buffer;
153 while (len < *lenp) {
154 if(get_user(c, p++)) {
155 up(&lasat_info_sem);
156 return -EFAULT;
157 }
158 if (c == 0 || c == '\n')
159 break;
160 len++;
161 }
162 if (len >= sizeof(proc_lasat_ipbuf)-1)
163 len = sizeof(proc_lasat_ipbuf) - 1;
164 if (copy_from_user(proc_lasat_ipbuf, buffer, len))
165 {
166 up(&lasat_info_sem);
167 return -EFAULT;
168 }
169 proc_lasat_ipbuf[len] = 0;
170 filp->f_pos += *lenp;
171 /* Now see if we can convert it to a valid IP */
172 ip = in_aton(proc_lasat_ipbuf);
173 *(unsigned int *)(table->data) = ip;
174 lasat_write_eeprom_info();
175 } else {
176 ip = *(unsigned int *)(table->data);
177 sprintf(proc_lasat_ipbuf, "%d.%d.%d.%d",
178 (ip ) & 0xff,
179 (ip >> 8) & 0xff,
180 (ip >> 16) & 0xff,
181 (ip >> 24) & 0xff);
182 len = strlen(proc_lasat_ipbuf);
183 if (len > *lenp)
184 len = *lenp;
185 if (len)
186 if(copy_to_user(buffer, proc_lasat_ipbuf, len)) {
187 up(&lasat_info_sem);
188 return -EFAULT;
189 }
190 if (len < *lenp) {
191 if(put_user('\n', ((char *) buffer) + len)) {
192 up(&lasat_info_sem);
193 return -EFAULT;
194 }
195 len++;
196 }
197 *lenp = len;
198 filp->f_pos += len;
199 }
200 update_bcastaddr();
201 up(&lasat_info_sem);
202 return 0;
203 }
204 #endif /* defined(CONFIG_INET) */
205
sysctl_lasat_eeprom_value(ctl_table * table,int * name,int nlen,void * oldval,size_t * oldlenp,void * newval,size_t newlen,void ** context)206 static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
207 void *oldval, size_t *oldlenp,
208 void *newval, size_t newlen,
209 void **context)
210 {
211 int r;
212
213 down(&lasat_info_sem);
214 r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
215 if (r < 0) {
216 up(&lasat_info_sem);
217 return r;
218 }
219
220 if (newval && newlen)
221 {
222 if (name && *name == LASAT_PRID)
223 lasat_board_info.li_eeprom_info.prid = *(int*)newval;
224
225 lasat_write_eeprom_info();
226 lasat_init_board_info();
227 }
228 up(&lasat_info_sem);
229
230 return 0;
231 }
232
proc_lasat_eeprom_value(ctl_table * table,int write,struct file * filp,void * buffer,size_t * lenp)233 int proc_lasat_eeprom_value(ctl_table *table, int write, struct file *filp,
234 void *buffer, size_t *lenp)
235 {
236 int r;
237 down(&lasat_info_sem);
238 r = proc_dointvec(table, write, filp, buffer, lenp);
239 if ( (!write) || r) {
240 up(&lasat_info_sem);
241 return r;
242 }
243 if (filp && filp->f_dentry)
244 {
245 if (!strcmp(filp->f_dentry->d_name.name, "prid"))
246 lasat_board_info.li_eeprom_info.prid = lasat_board_info.li_prid;
247 if (!strcmp(filp->f_dentry->d_name.name, "debugaccess"))
248 lasat_board_info.li_eeprom_info.debugaccess = lasat_board_info.li_debugaccess;
249 }
250 lasat_write_eeprom_info();
251 up(&lasat_info_sem);
252 return 0;
253 }
254
255 extern int lasat_boot_to_service;
256
257 #ifdef CONFIG_SYSCTL
258
259 static ctl_table lasat_table[] = {
260 {LASAT_CPU_HZ, "cpu-hz", &lasat_board_info.li_cpu_hz, sizeof(int),
261 0444, NULL, &proc_dointvec, &sysctl_intvec},
262 {LASAT_BUS_HZ, "bus-hz", &lasat_board_info.li_bus_hz, sizeof(int),
263 0444, NULL, &proc_dointvec, &sysctl_intvec},
264 {LASAT_MODEL, "bmid", &lasat_board_info.li_bmid, sizeof(int),
265 0444, NULL, &proc_dointvec, &sysctl_intvec},
266 {LASAT_PRID, "prid", &lasat_board_info.li_prid, sizeof(int),
267 0644, NULL, &proc_lasat_eeprom_value, &sysctl_lasat_eeprom_value},
268 #ifdef CONFIG_INET
269 {LASAT_IPADDR, "ipaddr", &lasat_board_info.li_eeprom_info.ipaddr, sizeof(int),
270 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
271 {LASAT_NETMASK, "netmask", &lasat_board_info.li_eeprom_info.netmask, sizeof(int),
272 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
273 {LASAT_BCAST, "bcastaddr", &lasat_bcastaddr,
274 sizeof(lasat_bcastaddr), 0600, NULL,
275 &proc_dostring, &sysctl_string},
276 #endif
277 {LASAT_PASSWORD, "passwd_hash", &lasat_board_info.li_eeprom_info.passwd_hash, sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
278 0600, NULL, &proc_dolasatstring, &sysctl_lasatstring},
279 {LASAT_SBOOT, "boot-service", &lasat_boot_to_service, sizeof(int),
280 0644, NULL, &proc_dointvec, &sysctl_intvec},
281 {LASAT_NAMESTR, "namestr", &lasat_board_info.li_namestr, sizeof(lasat_board_info.li_namestr),
282 0444, NULL, &proc_dostring, &sysctl_string},
283 {LASAT_TYPESTR, "typestr", &lasat_board_info.li_typestr, sizeof(lasat_board_info.li_typestr),
284 0444, NULL, &proc_dostring, &sysctl_string},
285 {0}
286 };
287
288 #define CTL_LASAT 1 // CTL_ANY ???
289 static ctl_table lasat_root_table[] = {
290 { CTL_LASAT, "lasat", NULL, 0, 0555, lasat_table },
291 { 0 }
292 };
293
lasat_register_sysctl(void)294 static int __init lasat_register_sysctl(void)
295 {
296 struct ctl_table_header *lasat_table_header;
297
298 lasat_table_header =
299 register_sysctl_table(lasat_root_table, 0);
300
301 return 0;
302 }
303
304 __initcall(lasat_register_sysctl);
305 #endif /* CONFIG_SYSCTL */
306