sysctl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  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. #include <linux/module.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/stddef.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/ctype.h>
  28. #include <linux/string.h>
  29. #include <linux/net.h>
  30. #include <linux/inet.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/time.h>
  33. #ifdef CONFIG_DS1603
  34. #include "ds1603.h"
  35. #endif
  36. /* Strategy function to write EEPROM after changing string entry */
  37. int sysctl_lasatstring(ctl_table *table, int *name, int nlen,
  38. void *oldval, size_t *oldlenp,
  39. void *newval, size_t newlen)
  40. {
  41. int r;
  42. r = sysctl_string(table, name,
  43. nlen, oldval, oldlenp, newval, newlen);
  44. if (r < 0)
  45. return r;
  46. if (newval && newlen)
  47. lasat_write_eeprom_info();
  48. return 0;
  49. }
  50. /* And the same for proc */
  51. int proc_dolasatstring(ctl_table *table, int write, struct file *filp,
  52. void *buffer, size_t *lenp, loff_t *ppos)
  53. {
  54. int r;
  55. r = proc_dostring(table, write, filp, buffer, lenp, ppos);
  56. if ((!write) || r)
  57. return r;
  58. lasat_write_eeprom_info();
  59. return 0;
  60. }
  61. /* proc function to write EEPROM after changing int entry */
  62. int proc_dolasatint(ctl_table *table, int write, struct file *filp,
  63. void *buffer, size_t *lenp, loff_t *ppos)
  64. {
  65. int r;
  66. r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  67. if ((!write) || r)
  68. return r;
  69. lasat_write_eeprom_info();
  70. return 0;
  71. }
  72. #ifdef CONFIG_DS1603
  73. static int rtctmp;
  74. /* proc function to read/write RealTime Clock */
  75. int proc_dolasatrtc(ctl_table *table, int write, struct file *filp,
  76. void *buffer, size_t *lenp, loff_t *ppos)
  77. {
  78. int r;
  79. if (!write) {
  80. rtctmp = read_persistent_clock();
  81. /* check for time < 0 and set to 0 */
  82. if (rtctmp < 0)
  83. rtctmp = 0;
  84. }
  85. r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  86. if (r)
  87. return r;
  88. if (write)
  89. rtc_mips_set_mmss(rtctmp);
  90. return 0;
  91. }
  92. #endif
  93. /* Sysctl for setting the IP addresses */
  94. int sysctl_lasat_intvec(ctl_table *table, int *name, int nlen,
  95. void *oldval, size_t *oldlenp,
  96. void *newval, size_t newlen)
  97. {
  98. int r;
  99. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
  100. if (r < 0)
  101. return r;
  102. if (newval && newlen)
  103. lasat_write_eeprom_info();
  104. return 0;
  105. }
  106. #ifdef CONFIG_DS1603
  107. /* Same for RTC */
  108. int sysctl_lasat_rtc(ctl_table *table, int *name, int nlen,
  109. void *oldval, size_t *oldlenp,
  110. void *newval, size_t newlen)
  111. {
  112. int r;
  113. rtctmp = read_persistent_clock();
  114. if (rtctmp < 0)
  115. rtctmp = 0;
  116. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
  117. if (r < 0)
  118. return r;
  119. if (newval && newlen)
  120. rtc_mips_set_mmss(rtctmp);
  121. return r;
  122. }
  123. #endif
  124. #ifdef CONFIG_INET
  125. int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
  126. void *buffer, size_t *lenp, loff_t *ppos)
  127. {
  128. unsigned int ip;
  129. char *p, c;
  130. int len;
  131. char ipbuf[32];
  132. if (!table->data || !table->maxlen || !*lenp ||
  133. (*ppos && !write)) {
  134. *lenp = 0;
  135. return 0;
  136. }
  137. if (write) {
  138. len = 0;
  139. p = buffer;
  140. while (len < *lenp) {
  141. if (get_user(c, p++))
  142. return -EFAULT;
  143. if (c == 0 || c == '\n')
  144. break;
  145. len++;
  146. }
  147. if (len >= sizeof(ipbuf)-1)
  148. len = sizeof(ipbuf) - 1;
  149. if (copy_from_user(ipbuf, buffer, len))
  150. return -EFAULT;
  151. ipbuf[len] = 0;
  152. *ppos += *lenp;
  153. /* Now see if we can convert it to a valid IP */
  154. ip = in_aton(ipbuf);
  155. *(unsigned int *)(table->data) = ip;
  156. lasat_write_eeprom_info();
  157. } else {
  158. ip = *(unsigned int *)(table->data);
  159. sprintf(ipbuf, "%d.%d.%d.%d",
  160. (ip) & 0xff,
  161. (ip >> 8) & 0xff,
  162. (ip >> 16) & 0xff,
  163. (ip >> 24) & 0xff);
  164. len = strlen(ipbuf);
  165. if (len > *lenp)
  166. len = *lenp;
  167. if (len)
  168. if (copy_to_user(buffer, ipbuf, len))
  169. return -EFAULT;
  170. if (len < *lenp) {
  171. if (put_user('\n', ((char *) buffer) + len))
  172. return -EFAULT;
  173. len++;
  174. }
  175. *lenp = len;
  176. *ppos += len;
  177. }
  178. return 0;
  179. }
  180. #endif
  181. static int sysctl_lasat_prid(ctl_table *table, int *name, int nlen,
  182. void *oldval, size_t *oldlenp,
  183. void *newval, size_t newlen)
  184. {
  185. int r;
  186. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen);
  187. if (r < 0)
  188. return r;
  189. if (newval && newlen) {
  190. lasat_board_info.li_eeprom_info.prid = *(int *)newval;
  191. lasat_write_eeprom_info();
  192. lasat_init_board_info();
  193. }
  194. return 0;
  195. }
  196. int proc_lasat_prid(ctl_table *table, int write, struct file *filp,
  197. void *buffer, size_t *lenp, loff_t *ppos)
  198. {
  199. int r;
  200. r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  201. if (r < 0)
  202. return r;
  203. if (write) {
  204. lasat_board_info.li_eeprom_info.prid =
  205. lasat_board_info.li_prid;
  206. lasat_write_eeprom_info();
  207. lasat_init_board_info();
  208. }
  209. return 0;
  210. }
  211. extern int lasat_boot_to_service;
  212. static ctl_table lasat_table[] = {
  213. {
  214. .ctl_name = CTL_UNNUMBERED,
  215. .procname = "cpu-hz",
  216. .data = &lasat_board_info.li_cpu_hz,
  217. .maxlen = sizeof(int),
  218. .mode = 0444,
  219. .proc_handler = &proc_dointvec,
  220. .strategy = &sysctl_intvec
  221. },
  222. {
  223. .ctl_name = CTL_UNNUMBERED,
  224. .procname = "bus-hz",
  225. .data = &lasat_board_info.li_bus_hz,
  226. .maxlen = sizeof(int),
  227. .mode = 0444,
  228. .proc_handler = &proc_dointvec,
  229. .strategy = &sysctl_intvec
  230. },
  231. {
  232. .ctl_name = CTL_UNNUMBERED,
  233. .procname = "bmid",
  234. .data = &lasat_board_info.li_bmid,
  235. .maxlen = sizeof(int),
  236. .mode = 0444,
  237. .proc_handler = &proc_dointvec,
  238. .strategy = &sysctl_intvec
  239. },
  240. {
  241. .ctl_name = CTL_UNNUMBERED,
  242. .procname = "prid",
  243. .data = &lasat_board_info.li_prid,
  244. .maxlen = sizeof(int),
  245. .mode = 0644,
  246. .proc_handler = &proc_lasat_prid,
  247. .strategy = &sysctl_lasat_prid
  248. },
  249. #ifdef CONFIG_INET
  250. {
  251. .ctl_name = CTL_UNNUMBERED,
  252. .procname = "ipaddr",
  253. .data = &lasat_board_info.li_eeprom_info.ipaddr,
  254. .maxlen = sizeof(int),
  255. .mode = 0644,
  256. .proc_handler = &proc_lasat_ip,
  257. .strategy = &sysctl_lasat_intvec
  258. },
  259. {
  260. .ctl_name = CTL_UNNUMBERED,
  261. .procname = "netmask",
  262. .data = &lasat_board_info.li_eeprom_info.netmask,
  263. .maxlen = sizeof(int),
  264. .mode = 0644,
  265. .proc_handler = &proc_lasat_ip,
  266. .strategy = &sysctl_lasat_intvec
  267. },
  268. #endif
  269. {
  270. .ctl_name = CTL_UNNUMBERED,
  271. .procname = "passwd_hash",
  272. .data = &lasat_board_info.li_eeprom_info.passwd_hash,
  273. .maxlen =
  274. sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
  275. .mode = 0600,
  276. .proc_handler = &proc_dolasatstring,
  277. .strategy = &sysctl_lasatstring
  278. },
  279. {
  280. .ctl_name = CTL_UNNUMBERED,
  281. .procname = "boot-service",
  282. .data = &lasat_boot_to_service,
  283. .maxlen = sizeof(int),
  284. .mode = 0644,
  285. .proc_handler = &proc_dointvec,
  286. .strategy = &sysctl_intvec
  287. },
  288. #ifdef CONFIG_DS1603
  289. {
  290. .ctl_name = CTL_UNNUMBERED,
  291. .procname = "rtc",
  292. .data = &rtctmp,
  293. .maxlen = sizeof(int),
  294. .mode = 0644,
  295. .proc_handler = &proc_dolasatrtc,
  296. .strategy = &sysctl_lasat_rtc
  297. },
  298. #endif
  299. {
  300. .ctl_name = CTL_UNNUMBERED,
  301. .procname = "namestr",
  302. .data = &lasat_board_info.li_namestr,
  303. .maxlen = sizeof(lasat_board_info.li_namestr),
  304. .mode = 0444,
  305. .proc_handler = &proc_dostring,
  306. .strategy = &sysctl_string
  307. },
  308. {
  309. .ctl_name = CTL_UNNUMBERED,
  310. .procname = "typestr",
  311. .data = &lasat_board_info.li_typestr,
  312. .maxlen = sizeof(lasat_board_info.li_typestr),
  313. .mode = 0444,
  314. .proc_handler = &proc_dostring,
  315. .strategy = &sysctl_string
  316. },
  317. {}
  318. };
  319. static ctl_table lasat_root_table[] = {
  320. {
  321. .ctl_name = CTL_UNNUMBERED,
  322. .procname = "lasat",
  323. .mode = 0555,
  324. .child = lasat_table
  325. },
  326. {}
  327. };
  328. static int __init lasat_register_sysctl(void)
  329. {
  330. struct ctl_table_header *lasat_table_header;
  331. lasat_table_header =
  332. register_sysctl_table(lasat_root_table);
  333. if (!lasat_table_header) {
  334. printk(KERN_ERR "Unable to register LASAT sysctl\n");
  335. return -ENOMEM;
  336. }
  337. return 0;
  338. }
  339. __initcall(lasat_register_sysctl);