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