sysctl.c 9.3 KB

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