xref: /linux/arch/alpha/kernel/srm_env.c (revision 1c1ec6c6255cce0fa48391e36b419cf951800b50)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * srm_env.c - Access to SRM environment
31da177e4SLinus Torvalds  *             variables through linux' procfs
41da177e4SLinus Torvalds  *
516b7f4dcSJan-Benedict Glaw  * (C) 2001,2002,2006 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
61da177e4SLinus Torvalds  *
7395cf969SPaul Bolle  * This driver is a modified version of Erik Mouw's example proc
8395cf969SPaul Bolle  * interface, so: thank you, Erik! He can be reached via email at
91da177e4SLinus Torvalds  * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
101da177e4SLinus Torvalds  * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They
111da177e4SLinus Torvalds  * included a patch like this as well. Thanks for idea!
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute
141da177e4SLinus Torvalds  * it and/or modify it under the terms of the GNU General
151da177e4SLinus Torvalds  * Public License version 2 as published by the Free Software
161da177e4SLinus Torvalds  * Foundation.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * This program is distributed in the hope that it will be
191da177e4SLinus Torvalds  * useful, but WITHOUT ANY WARRANTY; without even the implied
201da177e4SLinus Torvalds  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
211da177e4SLinus Torvalds  * PURPOSE.  See the GNU General Public License for more
221da177e4SLinus Torvalds  * details.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public
251da177e4SLinus Torvalds  * License along with this program; if not, write to the
261da177e4SLinus Torvalds  * Free Software Foundation, Inc., 59 Temple Place,
271da177e4SLinus Torvalds  * Suite 330, Boston, MA  02111-1307  USA
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/kernel.h>
325a0e3ad6STejun Heo #include <linux/gfp.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
360ead0f84SAlexey Dobriyan #include <linux/seq_file.h>
371da177e4SLinus Torvalds #include <asm/console.h>
381da177e4SLinus Torvalds #include <asm/uaccess.h>
391da177e4SLinus Torvalds #include <asm/machvec.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define BASE_DIR	"srm_environment"	/* Subdir in /proc/		*/
421da177e4SLinus Torvalds #define NAMED_DIR	"named_variables"	/* Subdir for known variables	*/
431da177e4SLinus Torvalds #define NUMBERED_DIR	"numbered_variables"	/* Subdir for all variables	*/
4416b7f4dcSJan-Benedict Glaw #define VERSION		"0.0.6"			/* Module version		*/
451da177e4SLinus Torvalds #define NAME		"srm_env"		/* Module name			*/
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
481da177e4SLinus Torvalds MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
491da177e4SLinus Torvalds MODULE_LICENSE("GPL");
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds typedef struct _srm_env {
521da177e4SLinus Torvalds 	char			*name;
531da177e4SLinus Torvalds 	unsigned long		id;
541da177e4SLinus Torvalds } srm_env_t;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static struct proc_dir_entry	*base_dir;
571da177e4SLinus Torvalds static struct proc_dir_entry	*named_dir;
581da177e4SLinus Torvalds static struct proc_dir_entry	*numbered_dir;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static srm_env_t	srm_named_entries[] = {
611da177e4SLinus Torvalds 	{ "auto_action",	ENV_AUTO_ACTION		},
621da177e4SLinus Torvalds 	{ "boot_dev",		ENV_BOOT_DEV		},
631da177e4SLinus Torvalds 	{ "bootdef_dev",	ENV_BOOTDEF_DEV		},
641da177e4SLinus Torvalds 	{ "booted_dev",		ENV_BOOTED_DEV		},
651da177e4SLinus Torvalds 	{ "boot_file",		ENV_BOOT_FILE		},
661da177e4SLinus Torvalds 	{ "booted_file",	ENV_BOOTED_FILE		},
671da177e4SLinus Torvalds 	{ "boot_osflags",	ENV_BOOT_OSFLAGS	},
681da177e4SLinus Torvalds 	{ "booted_osflags",	ENV_BOOTED_OSFLAGS	},
691da177e4SLinus Torvalds 	{ "boot_reset",		ENV_BOOT_RESET		},
701da177e4SLinus Torvalds 	{ "dump_dev",		ENV_DUMP_DEV		},
711da177e4SLinus Torvalds 	{ "enable_audit",	ENV_ENABLE_AUDIT	},
721da177e4SLinus Torvalds 	{ "license",		ENV_LICENSE		},
731da177e4SLinus Torvalds 	{ "char_set",		ENV_CHAR_SET		},
741da177e4SLinus Torvalds 	{ "language",		ENV_LANGUAGE		},
751da177e4SLinus Torvalds 	{ "tty_dev",		ENV_TTY_DEV		},
761da177e4SLinus Torvalds 	{ NULL,			0			},
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
790ead0f84SAlexey Dobriyan static int srm_env_proc_show(struct seq_file *m, void *v)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	unsigned long	ret;
82*1c1ec6c6SAl Viro 	unsigned long	id = (unsigned long)m->private;
830ead0f84SAlexey Dobriyan 	char		*page;
841da177e4SLinus Torvalds 
850ead0f84SAlexey Dobriyan 	page = (char *)__get_free_page(GFP_USER);
860ead0f84SAlexey Dobriyan 	if (!page)
870ead0f84SAlexey Dobriyan 		return -ENOMEM;
881da177e4SLinus Torvalds 
89*1c1ec6c6SAl Viro 	ret = callback_getenv(id, page, PAGE_SIZE);
901da177e4SLinus Torvalds 
9116b7f4dcSJan-Benedict Glaw 	if ((ret >> 61) == 0) {
920ead0f84SAlexey Dobriyan 		seq_write(m, page, ret);
930ead0f84SAlexey Dobriyan 		ret = 0;
9416b7f4dcSJan-Benedict Glaw 	} else
950ead0f84SAlexey Dobriyan 		ret = -EFAULT;
960ead0f84SAlexey Dobriyan 	free_page((unsigned long)page);
970ead0f84SAlexey Dobriyan 	return ret;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1000ead0f84SAlexey Dobriyan static int srm_env_proc_open(struct inode *inode, struct file *file)
1010ead0f84SAlexey Dobriyan {
102d9dda78bSAl Viro 	return single_open(file, srm_env_proc_show, PDE_DATA(inode));
1030ead0f84SAlexey Dobriyan }
1040ead0f84SAlexey Dobriyan 
1050ead0f84SAlexey Dobriyan static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
1060ead0f84SAlexey Dobriyan 				  size_t count, loff_t *pos)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	int res;
109*1c1ec6c6SAl Viro 	unsigned long	id = (unsigned long)PDE_DATA(file_inode(file));
1101da177e4SLinus Torvalds 	char		*buf = (char *) __get_free_page(GFP_USER);
1111da177e4SLinus Torvalds 	unsigned long	ret1, ret2;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (!buf)
1141da177e4SLinus Torvalds 		return -ENOMEM;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	res = -EINVAL;
1171da177e4SLinus Torvalds 	if (count >= PAGE_SIZE)
1181da177e4SLinus Torvalds 		goto out;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	res = -EFAULT;
1211da177e4SLinus Torvalds 	if (copy_from_user(buf, buffer, count))
1221da177e4SLinus Torvalds 		goto out;
1231da177e4SLinus Torvalds 	buf[count] = '\0';
1241da177e4SLinus Torvalds 
125*1c1ec6c6SAl Viro 	ret1 = callback_setenv(id, buf, count);
1261da177e4SLinus Torvalds 	if ((ret1 >> 61) == 0) {
1271da177e4SLinus Torvalds 		do
1281da177e4SLinus Torvalds 			ret2 = callback_save_env();
1291da177e4SLinus Torvalds 		while((ret2 >> 61) == 1);
1301da177e4SLinus Torvalds 		res = (int) ret1;
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds  out:
1341da177e4SLinus Torvalds 	free_page((unsigned long)buf);
1351da177e4SLinus Torvalds 	return res;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1380ead0f84SAlexey Dobriyan static const struct file_operations srm_env_proc_fops = {
1390ead0f84SAlexey Dobriyan 	.owner		= THIS_MODULE,
1400ead0f84SAlexey Dobriyan 	.open		= srm_env_proc_open,
1410ead0f84SAlexey Dobriyan 	.read		= seq_read,
1420ead0f84SAlexey Dobriyan 	.llseek		= seq_lseek,
1430ead0f84SAlexey Dobriyan 	.release	= single_release,
1440ead0f84SAlexey Dobriyan 	.write		= srm_env_proc_write,
1450ead0f84SAlexey Dobriyan };
1460ead0f84SAlexey Dobriyan 
1471da177e4SLinus Torvalds static int __init
1481da177e4SLinus Torvalds srm_env_init(void)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	srm_env_t	*entry;
1511da177e4SLinus Torvalds 	unsigned long	var_num;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	/*
1541da177e4SLinus Torvalds 	 * Check system
1551da177e4SLinus Torvalds 	 */
1561da177e4SLinus Torvalds 	if (!alpha_using_srm) {
1571da177e4SLinus Torvalds 		printk(KERN_INFO "%s: This Alpha system doesn't "
1581da177e4SLinus Torvalds 				"know about SRM (or you've booted "
1591da177e4SLinus Torvalds 				"SRM->MILO->Linux, which gets "
160bbb8d343SHarvey Harrison 				"misdetected)...\n", __func__);
1611da177e4SLinus Torvalds 		return -ENODEV;
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/*
1651da177e4SLinus Torvalds 	 * Create base directory
1661da177e4SLinus Torvalds 	 */
1671da177e4SLinus Torvalds 	base_dir = proc_mkdir(BASE_DIR, NULL);
16816b7f4dcSJan-Benedict Glaw 	if (!base_dir) {
1691da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
1701da177e4SLinus Torvalds 				BASE_DIR);
171c35f2e49SAl Viro 		return -ENOMEM;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/*
1751da177e4SLinus Torvalds 	 * Create per-name subdirectory
1761da177e4SLinus Torvalds 	 */
1771da177e4SLinus Torvalds 	named_dir = proc_mkdir(NAMED_DIR, base_dir);
17816b7f4dcSJan-Benedict Glaw 	if (!named_dir) {
1791da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
1801da177e4SLinus Torvalds 				BASE_DIR, NAMED_DIR);
1811da177e4SLinus Torvalds 		goto cleanup;
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/*
1851da177e4SLinus Torvalds 	 * Create per-number subdirectory
1861da177e4SLinus Torvalds 	 */
1871da177e4SLinus Torvalds 	numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
18816b7f4dcSJan-Benedict Glaw 	if (!numbered_dir) {
1891da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
1901da177e4SLinus Torvalds 				BASE_DIR, NUMBERED_DIR);
1911da177e4SLinus Torvalds 		goto cleanup;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/*
1961da177e4SLinus Torvalds 	 * Create all named nodes
1971da177e4SLinus Torvalds 	 */
1981da177e4SLinus Torvalds 	entry = srm_named_entries;
19916b7f4dcSJan-Benedict Glaw 	while (entry->name && entry->id) {
200c35f2e49SAl Viro 		if (!proc_create_data(entry->name, 0644, named_dir,
201*1c1ec6c6SAl Viro 			     &srm_env_proc_fops, (void *)entry->id))
2021da177e4SLinus Torvalds 			goto cleanup;
2031da177e4SLinus Torvalds 		entry++;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/*
2071da177e4SLinus Torvalds 	 * Create all numbered nodes
2081da177e4SLinus Torvalds 	 */
2091da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++) {
210*1c1ec6c6SAl Viro 		char name[4];
211*1c1ec6c6SAl Viro 		sprintf(name, "%ld", var_num);
212*1c1ec6c6SAl Viro 		if (!proc_create_data(name, 0644, numbered_dir,
213*1c1ec6c6SAl Viro 			     &srm_env_proc_fops, (void *)var_num))
2141da177e4SLinus Torvalds 			goto cleanup;
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
2181da177e4SLinus Torvalds 			VERSION);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	return 0;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds cleanup:
223c35f2e49SAl Viro 	remove_proc_subtree(BASE_DIR, NULL);
2241da177e4SLinus Torvalds 	return -ENOMEM;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static void __exit
2281da177e4SLinus Torvalds srm_env_exit(void)
2291da177e4SLinus Torvalds {
230c35f2e49SAl Viro 	remove_proc_subtree(BASE_DIR, NULL);
2311da177e4SLinus Torvalds 	printk(KERN_INFO "%s: unloaded successfully\n", NAME);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds module_init(srm_env_init);
2351da177e4SLinus Torvalds module_exit(srm_env_exit);
236