sysctl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/config.h>
  23. #include <linux/module.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/stddef.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/ctype.h>
  29. #include <linux/string.h>
  30. #include <linux/net.h>
  31. #include <linux/inet.h>
  32. #include <asm/uaccess.h>
  33. #include "sysctl.h"
  34. #include "ds1603.h"
  35. static DECLARE_MUTEX(lasat_info_sem);
  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, void **context)
  40. {
  41. int r;
  42. down(&lasat_info_sem);
  43. r = sysctl_string(table, name,
  44. nlen, oldval, oldlenp, newval, newlen, context);
  45. if (r < 0) {
  46. up(&lasat_info_sem);
  47. return r;
  48. }
  49. if (newval && newlen) {
  50. lasat_write_eeprom_info();
  51. }
  52. up(&lasat_info_sem);
  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. down(&lasat_info_sem);
  61. r = proc_dostring(table, write, filp, buffer, lenp, ppos);
  62. if ( (!write) || r) {
  63. up(&lasat_info_sem);
  64. return r;
  65. }
  66. lasat_write_eeprom_info();
  67. up(&lasat_info_sem);
  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. down(&lasat_info_sem);
  76. r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  77. if ( (!write) || r) {
  78. up(&lasat_info_sem);
  79. return r;
  80. }
  81. lasat_write_eeprom_info();
  82. up(&lasat_info_sem);
  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. down(&lasat_info_sem);
  93. if (!write) {
  94. rtctmp = ds1603_read();
  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. up(&lasat_info_sem);
  102. return r;
  103. }
  104. ds1603_set(rtctmp);
  105. up(&lasat_info_sem);
  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, void **context)
  113. {
  114. int r;
  115. down(&lasat_info_sem);
  116. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
  117. if (r < 0) {
  118. up(&lasat_info_sem);
  119. return r;
  120. }
  121. if (newval && newlen) {
  122. lasat_write_eeprom_info();
  123. }
  124. up(&lasat_info_sem);
  125. return 1;
  126. }
  127. #ifdef CONFIG_DS1603
  128. /* Same for RTC */
  129. int sysctl_lasat_rtc(ctl_table *table, int *name, int nlen,
  130. void *oldval, size_t *oldlenp,
  131. void *newval, size_t newlen, void **context)
  132. {
  133. int r;
  134. down(&lasat_info_sem);
  135. rtctmp = ds1603_read();
  136. if (rtctmp < 0)
  137. rtctmp = 0;
  138. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
  139. if (r < 0) {
  140. up(&lasat_info_sem);
  141. return r;
  142. }
  143. if (newval && newlen) {
  144. ds1603_set(rtctmp);
  145. }
  146. up(&lasat_info_sem);
  147. return 1;
  148. }
  149. #endif
  150. #ifdef CONFIG_INET
  151. static char lasat_bcastaddr[16];
  152. void update_bcastaddr(void)
  153. {
  154. unsigned int ip;
  155. ip = (lasat_board_info.li_eeprom_info.ipaddr &
  156. lasat_board_info.li_eeprom_info.netmask) |
  157. ~lasat_board_info.li_eeprom_info.netmask;
  158. sprintf(lasat_bcastaddr, "%d.%d.%d.%d",
  159. (ip ) & 0xff,
  160. (ip >> 8) & 0xff,
  161. (ip >> 16) & 0xff,
  162. (ip >> 24) & 0xff);
  163. }
  164. static char proc_lasat_ipbuf[32];
  165. /* Parsing of IP address */
  166. int proc_lasat_ip(ctl_table *table, int write, struct file *filp,
  167. void *buffer, size_t *lenp, loff_t *ppos)
  168. {
  169. int len;
  170. unsigned int ip;
  171. char *p, c;
  172. if (!table->data || !table->maxlen || !*lenp ||
  173. (*ppos && !write)) {
  174. *lenp = 0;
  175. return 0;
  176. }
  177. down(&lasat_info_sem);
  178. if (write) {
  179. len = 0;
  180. p = buffer;
  181. while (len < *lenp) {
  182. if(get_user(c, p++)) {
  183. up(&lasat_info_sem);
  184. return -EFAULT;
  185. }
  186. if (c == 0 || c == '\n')
  187. break;
  188. len++;
  189. }
  190. if (len >= sizeof(proc_lasat_ipbuf)-1)
  191. len = sizeof(proc_lasat_ipbuf) - 1;
  192. if (copy_from_user(proc_lasat_ipbuf, buffer, len))
  193. {
  194. up(&lasat_info_sem);
  195. return -EFAULT;
  196. }
  197. proc_lasat_ipbuf[len] = 0;
  198. *ppos += *lenp;
  199. /* Now see if we can convert it to a valid IP */
  200. ip = in_aton(proc_lasat_ipbuf);
  201. *(unsigned int *)(table->data) = ip;
  202. lasat_write_eeprom_info();
  203. } else {
  204. ip = *(unsigned int *)(table->data);
  205. sprintf(proc_lasat_ipbuf, "%d.%d.%d.%d",
  206. (ip ) & 0xff,
  207. (ip >> 8) & 0xff,
  208. (ip >> 16) & 0xff,
  209. (ip >> 24) & 0xff);
  210. len = strlen(proc_lasat_ipbuf);
  211. if (len > *lenp)
  212. len = *lenp;
  213. if (len)
  214. if(copy_to_user(buffer, proc_lasat_ipbuf, len)) {
  215. up(&lasat_info_sem);
  216. return -EFAULT;
  217. }
  218. if (len < *lenp) {
  219. if(put_user('\n', ((char *) buffer) + len)) {
  220. up(&lasat_info_sem);
  221. return -EFAULT;
  222. }
  223. len++;
  224. }
  225. *lenp = len;
  226. *ppos += len;
  227. }
  228. update_bcastaddr();
  229. up(&lasat_info_sem);
  230. return 0;
  231. }
  232. #endif /* defined(CONFIG_INET) */
  233. static int sysctl_lasat_eeprom_value(ctl_table *table, int *name, int nlen,
  234. void *oldval, size_t *oldlenp,
  235. void *newval, size_t newlen,
  236. void **context)
  237. {
  238. int r;
  239. down(&lasat_info_sem);
  240. r = sysctl_intvec(table, name, nlen, oldval, oldlenp, newval, newlen, context);
  241. if (r < 0) {
  242. up(&lasat_info_sem);
  243. return r;
  244. }
  245. if (newval && newlen)
  246. {
  247. if (name && *name == LASAT_PRID)
  248. lasat_board_info.li_eeprom_info.prid = *(int*)newval;
  249. lasat_write_eeprom_info();
  250. lasat_init_board_info();
  251. }
  252. up(&lasat_info_sem);
  253. return 0;
  254. }
  255. int proc_lasat_eeprom_value(ctl_table *table, int write, struct file *filp,
  256. void *buffer, size_t *lenp, loff_t *ppos)
  257. {
  258. int r;
  259. down(&lasat_info_sem);
  260. r = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  261. if ( (!write) || r) {
  262. up(&lasat_info_sem);
  263. return r;
  264. }
  265. if (filp && filp->f_dentry)
  266. {
  267. if (!strcmp(filp->f_dentry->d_name.name, "prid"))
  268. lasat_board_info.li_eeprom_info.prid = lasat_board_info.li_prid;
  269. if (!strcmp(filp->f_dentry->d_name.name, "debugaccess"))
  270. lasat_board_info.li_eeprom_info.debugaccess = lasat_board_info.li_debugaccess;
  271. }
  272. lasat_write_eeprom_info();
  273. up(&lasat_info_sem);
  274. return 0;
  275. }
  276. extern int lasat_boot_to_service;
  277. #ifdef CONFIG_SYSCTL
  278. static ctl_table lasat_table[] = {
  279. {LASAT_CPU_HZ, "cpu-hz", &lasat_board_info.li_cpu_hz, sizeof(int),
  280. 0444, NULL, &proc_dointvec, &sysctl_intvec},
  281. {LASAT_BUS_HZ, "bus-hz", &lasat_board_info.li_bus_hz, sizeof(int),
  282. 0444, NULL, &proc_dointvec, &sysctl_intvec},
  283. {LASAT_MODEL, "bmid", &lasat_board_info.li_bmid, sizeof(int),
  284. 0444, NULL, &proc_dointvec, &sysctl_intvec},
  285. {LASAT_PRID, "prid", &lasat_board_info.li_prid, sizeof(int),
  286. 0644, NULL, &proc_lasat_eeprom_value, &sysctl_lasat_eeprom_value},
  287. #ifdef CONFIG_INET
  288. {LASAT_IPADDR, "ipaddr", &lasat_board_info.li_eeprom_info.ipaddr, sizeof(int),
  289. 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
  290. {LASAT_NETMASK, "netmask", &lasat_board_info.li_eeprom_info.netmask, sizeof(int),
  291. 0644, NULL, &proc_lasat_ip, &sysctl_lasat_intvec},
  292. {LASAT_BCAST, "bcastaddr", &lasat_bcastaddr,
  293. sizeof(lasat_bcastaddr), 0600, NULL,
  294. &proc_dostring, &sysctl_string},
  295. #endif
  296. {LASAT_PASSWORD, "passwd_hash", &lasat_board_info.li_eeprom_info.passwd_hash, sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
  297. 0600, NULL, &proc_dolasatstring, &sysctl_lasatstring},
  298. {LASAT_SBOOT, "boot-service", &lasat_boot_to_service, sizeof(int),
  299. 0644, NULL, &proc_dointvec, &sysctl_intvec},
  300. #ifdef CONFIG_DS1603
  301. {LASAT_RTC, "rtc", &rtctmp, sizeof(int),
  302. 0644, NULL, &proc_dolasatrtc, &sysctl_lasat_rtc},
  303. #endif
  304. {LASAT_NAMESTR, "namestr", &lasat_board_info.li_namestr, sizeof(lasat_board_info.li_namestr),
  305. 0444, NULL, &proc_dostring, &sysctl_string},
  306. {LASAT_TYPESTR, "typestr", &lasat_board_info.li_typestr, sizeof(lasat_board_info.li_typestr),
  307. 0444, NULL, &proc_dostring, &sysctl_string},
  308. {0}
  309. };
  310. #define CTL_LASAT 1 // CTL_ANY ???
  311. static ctl_table lasat_root_table[] = {
  312. { CTL_LASAT, "lasat", NULL, 0, 0555, lasat_table },
  313. { 0 }
  314. };
  315. static int __init lasat_register_sysctl(void)
  316. {
  317. struct ctl_table_header *lasat_table_header;
  318. lasat_table_header =
  319. register_sysctl_table(lasat_root_table, 0);
  320. return 0;
  321. }
  322. __initcall(lasat_register_sysctl);
  323. #endif /* CONFIG_SYSCTL */