sysctl.c 8.1 KB

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