blob: ffe996a54fadb3db9e0ed8b10dd0f0f1deb6bea5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * srm_env.c - Access to SRM environment
3 * variables through linux' procfs
4 *
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +01005 * (C) 2001,2002,2006 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Paul Bolle395cf962011-08-15 02:02:26 +02007 * 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 Torvalds1da177e2005-04-16 15:20:36 -07009 * <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 Glaw16b7f4d2006-11-07 23:50:37 +010023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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 Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/proc_fs.h>
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080036#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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 Glaw16b7f4d2006-11-07 23:50:37 +010044#define VERSION "0.0.6" /* Module version */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define NAME "srm_env" /* Module name */
46
47MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
48MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
49MODULE_LICENSE("GPL");
50
51typedef struct _srm_env {
52 char *name;
53 unsigned long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054} srm_env_t;
55
56static struct proc_dir_entry *base_dir;
57static struct proc_dir_entry *named_dir;
58static struct proc_dir_entry *numbered_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static srm_env_t srm_named_entries[] = {
61 { "auto_action", ENV_AUTO_ACTION },
62 { "boot_dev", ENV_BOOT_DEV },
63 { "bootdef_dev", ENV_BOOTDEF_DEV },
64 { "booted_dev", ENV_BOOTED_DEV },
65 { "boot_file", ENV_BOOT_FILE },
66 { "booted_file", ENV_BOOTED_FILE },
67 { "boot_osflags", ENV_BOOT_OSFLAGS },
68 { "booted_osflags", ENV_BOOTED_OSFLAGS },
69 { "boot_reset", ENV_BOOT_RESET },
70 { "dump_dev", ENV_DUMP_DEV },
71 { "enable_audit", ENV_ENABLE_AUDIT },
72 { "license", ENV_LICENSE },
73 { "char_set", ENV_CHAR_SET },
74 { "language", ENV_LANGUAGE },
75 { "tty_dev", ENV_TTY_DEV },
76 { NULL, 0 },
77};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080079static int srm_env_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long ret;
Al Viro1c1ec6c2013-03-31 21:45:09 -040082 unsigned long id = (unsigned long)m->private;
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080083 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080085 page = (char *)__get_free_page(GFP_USER);
86 if (!page)
87 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Al Viro1c1ec6c2013-03-31 21:45:09 -040089 ret = callback_getenv(id, page, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +010091 if ((ret >> 61) == 0) {
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080092 seq_write(m, page, ret);
93 ret = 0;
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +010094 } else
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -080095 ret = -EFAULT;
96 free_page((unsigned long)page);
97 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -0800100static int srm_env_proc_open(struct inode *inode, struct file *file)
101{
Al Virod9dda782013-03-31 18:16:14 -0400102 return single_open(file, srm_env_proc_show, PDE_DATA(inode));
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -0800103}
104
105static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
106 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int res;
Al Viro1c1ec6c2013-03-31 21:45:09 -0400109 unsigned long id = (unsigned long)PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 char *buf = (char *) __get_free_page(GFP_USER);
111 unsigned long ret1, ret2;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!buf)
114 return -ENOMEM;
115
116 res = -EINVAL;
117 if (count >= PAGE_SIZE)
118 goto out;
119
120 res = -EFAULT;
121 if (copy_from_user(buf, buffer, count))
122 goto out;
123 buf[count] = '\0';
124
Al Viro1c1ec6c2013-03-31 21:45:09 -0400125 ret1 = callback_setenv(id, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if ((ret1 >> 61) == 0) {
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100127 do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ret2 = callback_save_env();
129 while((ret2 >> 61) == 1);
130 res = (int) ret1;
131 }
132
133 out:
134 free_page((unsigned long)buf);
135 return res;
136}
137
Alexey Dobriyan0ead0f82009-12-14 18:00:06 -0800138static const struct file_operations srm_env_proc_fops = {
139 .owner = THIS_MODULE,
140 .open = srm_env_proc_open,
141 .read = seq_read,
142 .llseek = seq_lseek,
143 .release = single_release,
144 .write = srm_env_proc_write,
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int __init
148srm_env_init(void)
149{
150 srm_env_t *entry;
151 unsigned long var_num;
152
153 /*
154 * Check system
155 */
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100156 if (!alpha_using_srm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk(KERN_INFO "%s: This Alpha system doesn't "
158 "know about SRM (or you've booted "
159 "SRM->MILO->Linux, which gets "
Harvey Harrisonbbb8d342008-04-28 02:13:46 -0700160 "misdetected)...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -ENODEV;
162 }
163
164 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Create base directory
166 */
167 base_dir = proc_mkdir(BASE_DIR, NULL);
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100168 if (!base_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
170 BASE_DIR);
Al Viroc35f2e42013-03-31 18:29:51 -0400171 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
175 * Create per-name subdirectory
176 */
177 named_dir = proc_mkdir(NAMED_DIR, base_dir);
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100178 if (!named_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
180 BASE_DIR, NAMED_DIR);
181 goto cleanup;
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /*
185 * Create per-number subdirectory
186 */
187 numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100188 if (!numbered_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
190 BASE_DIR, NUMBERED_DIR);
191 goto cleanup;
192
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
196 * Create all named nodes
197 */
198 entry = srm_named_entries;
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100199 while (entry->name && entry->id) {
Al Viroc35f2e42013-03-31 18:29:51 -0400200 if (!proc_create_data(entry->name, 0644, named_dir,
Al Viro1c1ec6c2013-03-31 21:45:09 -0400201 &srm_env_proc_fops, (void *)entry->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 entry++;
204 }
205
206 /*
207 * Create all numbered nodes
208 */
Jan-Benedict Glaw16b7f4d2006-11-07 23:50:37 +0100209 for (var_num = 0; var_num <= 255; var_num++) {
Al Viro1c1ec6c2013-03-31 21:45:09 -0400210 char name[4];
211 sprintf(name, "%ld", var_num);
212 if (!proc_create_data(name, 0644, numbered_dir,
213 &srm_env_proc_fops, (void *)var_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
218 VERSION);
219
220 return 0;
221
222cleanup:
Al Viroc35f2e42013-03-31 18:29:51 -0400223 remove_proc_subtree(BASE_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -ENOMEM;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void __exit
228srm_env_exit(void)
229{
Al Viroc35f2e42013-03-31 18:29:51 -0400230 remove_proc_subtree(BASE_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 printk(KERN_INFO "%s: unloaded successfully\n", NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234module_init(srm_env_init);
235module_exit(srm_env_exit);