sysctl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/sysctl.h>
  23. #include <linux/stddef.h>
  24. #include <linux/init.h>
  25. #include <linux/fs.h>
  26. #include <linux/ctype.h>
  27. #include <linux/string.h>
  28. #include <linux/net.h>
  29. #include <linux/inet.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/time.h>
  32. #ifdef CONFIG_DS1603
  33. #include "ds1603.h"
  34. #endif
  35. /* And the same for proc */
  36. int proc_dolasatstring(struct ctl_table *table, int write,
  37. void *buffer, size_t *lenp, loff_t *ppos)
  38. {
  39. int r;
  40. r = proc_dostring(table, write, buffer, lenp, ppos);
  41. if ((!write) || r)
  42. return r;
  43. lasat_write_eeprom_info();
  44. return 0;
  45. }
  46. #ifdef CONFIG_DS1603
  47. static int rtctmp;
  48. /* proc function to read/write RealTime Clock */
  49. int proc_dolasatrtc(struct ctl_table *table, int write,
  50. void *buffer, size_t *lenp, loff_t *ppos)
  51. {
  52. struct timespec64 ts;
  53. int r;
  54. if (!write) {
  55. read_persistent_clock64(&ts);
  56. rtctmp = ts.tv_sec;
  57. /* check for time < 0 and set to 0 */
  58. if (rtctmp < 0)
  59. rtctmp = 0;
  60. }
  61. r = proc_dointvec(table, write, buffer, lenp, ppos);
  62. if (r)
  63. return r;
  64. if (write) {
  65. /*
  66. * Due to the RTC hardware limitation, we can not actually
  67. * use the full 64-bit range here.
  68. */
  69. ts.tv_sec = rtctmp;
  70. ts.tv_nsec = 0;
  71. update_persistent_clock64(ts);
  72. }
  73. return 0;
  74. }
  75. #endif
  76. #ifdef CONFIG_INET
  77. int proc_lasat_ip(struct ctl_table *table, int write,
  78. void *buffer, size_t *lenp, loff_t *ppos)
  79. {
  80. unsigned int ip;
  81. char *p, c;
  82. int len;
  83. char ipbuf[32];
  84. if (!table->data || !table->maxlen || !*lenp ||
  85. (*ppos && !write)) {
  86. *lenp = 0;
  87. return 0;
  88. }
  89. if (write) {
  90. len = 0;
  91. p = buffer;
  92. while (len < *lenp) {
  93. if (get_user(c, p++))
  94. return -EFAULT;
  95. if (c == 0 || c == '\n')
  96. break;
  97. len++;
  98. }
  99. if (len >= sizeof(ipbuf)-1)
  100. len = sizeof(ipbuf) - 1;
  101. if (copy_from_user(ipbuf, buffer, len))
  102. return -EFAULT;
  103. ipbuf[len] = 0;
  104. *ppos += *lenp;
  105. /* Now see if we can convert it to a valid IP */
  106. ip = in_aton(ipbuf);
  107. *(unsigned int *)(table->data) = ip;
  108. lasat_write_eeprom_info();
  109. } else {
  110. ip = *(unsigned int *)(table->data);
  111. sprintf(ipbuf, "%d.%d.%d.%d",
  112. (ip) & 0xff,
  113. (ip >> 8) & 0xff,
  114. (ip >> 16) & 0xff,
  115. (ip >> 24) & 0xff);
  116. len = strlen(ipbuf);
  117. if (len > *lenp)
  118. len = *lenp;
  119. if (len)
  120. if (copy_to_user(buffer, ipbuf, len))
  121. return -EFAULT;
  122. if (len < *lenp) {
  123. if (put_user('\n', ((char *) buffer) + len))
  124. return -EFAULT;
  125. len++;
  126. }
  127. *lenp = len;
  128. *ppos += len;
  129. }
  130. return 0;
  131. }
  132. #endif
  133. int proc_lasat_prid(struct ctl_table *table, int write,
  134. void *buffer, size_t *lenp, loff_t *ppos)
  135. {
  136. int r;
  137. r = proc_dointvec(table, write, buffer, lenp, ppos);
  138. if (r < 0)
  139. return r;
  140. if (write) {
  141. lasat_board_info.li_eeprom_info.prid =
  142. lasat_board_info.li_prid;
  143. lasat_write_eeprom_info();
  144. lasat_init_board_info();
  145. }
  146. return 0;
  147. }
  148. extern int lasat_boot_to_service;
  149. static struct ctl_table lasat_table[] = {
  150. {
  151. .procname = "cpu-hz",
  152. .data = &lasat_board_info.li_cpu_hz,
  153. .maxlen = sizeof(int),
  154. .mode = 0444,
  155. .proc_handler = proc_dointvec,
  156. },
  157. {
  158. .procname = "bus-hz",
  159. .data = &lasat_board_info.li_bus_hz,
  160. .maxlen = sizeof(int),
  161. .mode = 0444,
  162. .proc_handler = proc_dointvec,
  163. },
  164. {
  165. .procname = "bmid",
  166. .data = &lasat_board_info.li_bmid,
  167. .maxlen = sizeof(int),
  168. .mode = 0444,
  169. .proc_handler = proc_dointvec,
  170. },
  171. {
  172. .procname = "prid",
  173. .data = &lasat_board_info.li_prid,
  174. .maxlen = sizeof(int),
  175. .mode = 0644,
  176. .proc_handler = proc_lasat_prid,
  177. },
  178. #ifdef CONFIG_INET
  179. {
  180. .procname = "ipaddr",
  181. .data = &lasat_board_info.li_eeprom_info.ipaddr,
  182. .maxlen = sizeof(int),
  183. .mode = 0644,
  184. .proc_handler = proc_lasat_ip,
  185. },
  186. {
  187. .procname = "netmask",
  188. .data = &lasat_board_info.li_eeprom_info.netmask,
  189. .maxlen = sizeof(int),
  190. .mode = 0644,
  191. .proc_handler = proc_lasat_ip,
  192. },
  193. #endif
  194. {
  195. .procname = "passwd_hash",
  196. .data = &lasat_board_info.li_eeprom_info.passwd_hash,
  197. .maxlen =
  198. sizeof(lasat_board_info.li_eeprom_info.passwd_hash),
  199. .mode = 0600,
  200. .proc_handler = proc_dolasatstring,
  201. },
  202. {
  203. .procname = "boot-service",
  204. .data = &lasat_boot_to_service,
  205. .maxlen = sizeof(int),
  206. .mode = 0644,
  207. .proc_handler = proc_dointvec,
  208. },
  209. #ifdef CONFIG_DS1603
  210. {
  211. .procname = "rtc",
  212. .data = &rtctmp,
  213. .maxlen = sizeof(int),
  214. .mode = 0644,
  215. .proc_handler = proc_dolasatrtc,
  216. },
  217. #endif
  218. {
  219. .procname = "namestr",
  220. .data = &lasat_board_info.li_namestr,
  221. .maxlen = sizeof(lasat_board_info.li_namestr),
  222. .mode = 0444,
  223. .proc_handler = proc_dostring,
  224. },
  225. {
  226. .procname = "typestr",
  227. .data = &lasat_board_info.li_typestr,
  228. .maxlen = sizeof(lasat_board_info.li_typestr),
  229. .mode = 0444,
  230. .proc_handler = proc_dostring,
  231. },
  232. {}
  233. };
  234. static struct ctl_table lasat_root_table[] = {
  235. {
  236. .procname = "lasat",
  237. .mode = 0555,
  238. .child = lasat_table
  239. },
  240. {}
  241. };
  242. static int __init lasat_register_sysctl(void)
  243. {
  244. struct ctl_table_header *lasat_table_header;
  245. lasat_table_header =
  246. register_sysctl_table(lasat_root_table);
  247. if (!lasat_table_header) {
  248. printk(KERN_ERR "Unable to register LASAT sysctl\n");
  249. return -ENOMEM;
  250. }
  251. return 0;
  252. }
  253. arch_initcall(lasat_register_sysctl);