/kern_2.6.32/arch/alpha/kernel/srm_env.c

http://omnia2droid.googlecode.com/ · C · 299 lines · 197 code · 45 blank · 57 comment · 31 complexity · fc4d8799c189190c19e0788b536da6dc MD5 · raw file

  1. /*
  2. * srm_env.c - Access to SRM environment
  3. * variables through linux' procfs
  4. *
  5. * (C) 2001,2002,2006 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
  6. *
  7. * This driver is at all a modified version of Erik Mouw's
  8. * Documentation/DocBook/procfs_example.c, so: thank
  9. * you, Erik! He can be reached via email at
  10. * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
  11. * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They
  12. * included a patch like this as well. Thanks for idea!
  13. *
  14. * This program is free software; you can redistribute
  15. * it and/or modify it under the terms of the GNU General
  16. * Public License version 2 as published by the Free Software
  17. * Foundation.
  18. *
  19. * This program is distributed in the hope that it will be
  20. * useful, but WITHOUT ANY WARRANTY; without even the implied
  21. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  22. * PURPOSE. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public
  26. * License along with this program; if not, write to the
  27. * Free Software Foundation, Inc., 59 Temple Place,
  28. * Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/proc_fs.h>
  35. #include <asm/console.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/machvec.h>
  38. #define BASE_DIR "srm_environment" /* Subdir in /proc/ */
  39. #define NAMED_DIR "named_variables" /* Subdir for known variables */
  40. #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */
  41. #define VERSION "0.0.6" /* Module version */
  42. #define NAME "srm_env" /* Module name */
  43. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  44. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  45. MODULE_LICENSE("GPL");
  46. typedef struct _srm_env {
  47. char *name;
  48. unsigned long id;
  49. struct proc_dir_entry *proc_entry;
  50. } srm_env_t;
  51. static struct proc_dir_entry *base_dir;
  52. static struct proc_dir_entry *named_dir;
  53. static struct proc_dir_entry *numbered_dir;
  54. static char number[256][4];
  55. static srm_env_t srm_named_entries[] = {
  56. { "auto_action", ENV_AUTO_ACTION },
  57. { "boot_dev", ENV_BOOT_DEV },
  58. { "bootdef_dev", ENV_BOOTDEF_DEV },
  59. { "booted_dev", ENV_BOOTED_DEV },
  60. { "boot_file", ENV_BOOT_FILE },
  61. { "booted_file", ENV_BOOTED_FILE },
  62. { "boot_osflags", ENV_BOOT_OSFLAGS },
  63. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  64. { "boot_reset", ENV_BOOT_RESET },
  65. { "dump_dev", ENV_DUMP_DEV },
  66. { "enable_audit", ENV_ENABLE_AUDIT },
  67. { "license", ENV_LICENSE },
  68. { "char_set", ENV_CHAR_SET },
  69. { "language", ENV_LANGUAGE },
  70. { "tty_dev", ENV_TTY_DEV },
  71. { NULL, 0 },
  72. };
  73. static srm_env_t srm_numbered_entries[256];
  74. static int
  75. srm_env_read(char *page, char **start, off_t off, int count, int *eof,
  76. void *data)
  77. {
  78. int nbytes;
  79. unsigned long ret;
  80. srm_env_t *entry;
  81. if (off != 0) {
  82. *eof = 1;
  83. return 0;
  84. }
  85. entry = (srm_env_t *) data;
  86. ret = callback_getenv(entry->id, page, count);
  87. if ((ret >> 61) == 0) {
  88. nbytes = (int) ret;
  89. *eof = 1;
  90. } else
  91. nbytes = -EFAULT;
  92. return nbytes;
  93. }
  94. static int
  95. srm_env_write(struct file *file, const char __user *buffer, unsigned long count,
  96. void *data)
  97. {
  98. int res;
  99. srm_env_t *entry;
  100. char *buf = (char *) __get_free_page(GFP_USER);
  101. unsigned long ret1, ret2;
  102. entry = (srm_env_t *) data;
  103. if (!buf)
  104. return -ENOMEM;
  105. res = -EINVAL;
  106. if (count >= PAGE_SIZE)
  107. goto out;
  108. res = -EFAULT;
  109. if (copy_from_user(buf, buffer, count))
  110. goto out;
  111. buf[count] = '\0';
  112. ret1 = callback_setenv(entry->id, buf, count);
  113. if ((ret1 >> 61) == 0) {
  114. do
  115. ret2 = callback_save_env();
  116. while((ret2 >> 61) == 1);
  117. res = (int) ret1;
  118. }
  119. out:
  120. free_page((unsigned long)buf);
  121. return res;
  122. }
  123. static void
  124. srm_env_cleanup(void)
  125. {
  126. srm_env_t *entry;
  127. unsigned long var_num;
  128. if (base_dir) {
  129. /*
  130. * Remove named entries
  131. */
  132. if (named_dir) {
  133. entry = srm_named_entries;
  134. while (entry->name != NULL && entry->id != 0) {
  135. if (entry->proc_entry) {
  136. remove_proc_entry(entry->name,
  137. named_dir);
  138. entry->proc_entry = NULL;
  139. }
  140. entry++;
  141. }
  142. remove_proc_entry(NAMED_DIR, base_dir);
  143. }
  144. /*
  145. * Remove numbered entries
  146. */
  147. if (numbered_dir) {
  148. for (var_num = 0; var_num <= 255; var_num++) {
  149. entry = &srm_numbered_entries[var_num];
  150. if (entry->proc_entry) {
  151. remove_proc_entry(entry->name,
  152. numbered_dir);
  153. entry->proc_entry = NULL;
  154. entry->name = NULL;
  155. }
  156. }
  157. remove_proc_entry(NUMBERED_DIR, base_dir);
  158. }
  159. remove_proc_entry(BASE_DIR, NULL);
  160. }
  161. return;
  162. }
  163. static int __init
  164. srm_env_init(void)
  165. {
  166. srm_env_t *entry;
  167. unsigned long var_num;
  168. /*
  169. * Check system
  170. */
  171. if (!alpha_using_srm) {
  172. printk(KERN_INFO "%s: This Alpha system doesn't "
  173. "know about SRM (or you've booted "
  174. "SRM->MILO->Linux, which gets "
  175. "misdetected)...\n", __func__);
  176. return -ENODEV;
  177. }
  178. /*
  179. * Init numbers
  180. */
  181. for (var_num = 0; var_num <= 255; var_num++)
  182. sprintf(number[var_num], "%ld", var_num);
  183. /*
  184. * Create base directory
  185. */
  186. base_dir = proc_mkdir(BASE_DIR, NULL);
  187. if (!base_dir) {
  188. printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
  189. BASE_DIR);
  190. goto cleanup;
  191. }
  192. /*
  193. * Create per-name subdirectory
  194. */
  195. named_dir = proc_mkdir(NAMED_DIR, base_dir);
  196. if (!named_dir) {
  197. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  198. BASE_DIR, NAMED_DIR);
  199. goto cleanup;
  200. }
  201. /*
  202. * Create per-number subdirectory
  203. */
  204. numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
  205. if (!numbered_dir) {
  206. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  207. BASE_DIR, NUMBERED_DIR);
  208. goto cleanup;
  209. }
  210. /*
  211. * Create all named nodes
  212. */
  213. entry = srm_named_entries;
  214. while (entry->name && entry->id) {
  215. entry->proc_entry = create_proc_entry(entry->name,
  216. 0644, named_dir);
  217. if (!entry->proc_entry)
  218. goto cleanup;
  219. entry->proc_entry->data = (void *) entry;
  220. entry->proc_entry->read_proc = srm_env_read;
  221. entry->proc_entry->write_proc = srm_env_write;
  222. entry++;
  223. }
  224. /*
  225. * Create all numbered nodes
  226. */
  227. for (var_num = 0; var_num <= 255; var_num++) {
  228. entry = &srm_numbered_entries[var_num];
  229. entry->name = number[var_num];
  230. entry->proc_entry = create_proc_entry(entry->name,
  231. 0644, numbered_dir);
  232. if (!entry->proc_entry)
  233. goto cleanup;
  234. entry->id = var_num;
  235. entry->proc_entry->data = (void *) entry;
  236. entry->proc_entry->read_proc = srm_env_read;
  237. entry->proc_entry->write_proc = srm_env_write;
  238. }
  239. printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
  240. VERSION);
  241. return 0;
  242. cleanup:
  243. srm_env_cleanup();
  244. return -ENOMEM;
  245. }
  246. static void __exit
  247. srm_env_exit(void)
  248. {
  249. srm_env_cleanup();
  250. printk(KERN_INFO "%s: unloaded successfully\n", NAME);
  251. return;
  252. }
  253. module_init(srm_env_init);
  254. module_exit(srm_env_exit);