sysctl.c 8.1 KB

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