sysctl.c 10 KB

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