xref: /linux/arch/alpha/kernel/srm_env.c (revision d9dda78bad879595d8c4220a067fc029d6484a16)
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 	struct proc_dir_entry	*proc_entry;
551da177e4SLinus Torvalds } srm_env_t;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static struct proc_dir_entry	*base_dir;
581da177e4SLinus Torvalds static struct proc_dir_entry	*named_dir;
591da177e4SLinus Torvalds static struct proc_dir_entry	*numbered_dir;
601da177e4SLinus Torvalds static char			number[256][4];
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static srm_env_t	srm_named_entries[] = {
631da177e4SLinus Torvalds 	{ "auto_action",	ENV_AUTO_ACTION		},
641da177e4SLinus Torvalds 	{ "boot_dev",		ENV_BOOT_DEV		},
651da177e4SLinus Torvalds 	{ "bootdef_dev",	ENV_BOOTDEF_DEV		},
661da177e4SLinus Torvalds 	{ "booted_dev",		ENV_BOOTED_DEV		},
671da177e4SLinus Torvalds 	{ "boot_file",		ENV_BOOT_FILE		},
681da177e4SLinus Torvalds 	{ "booted_file",	ENV_BOOTED_FILE		},
691da177e4SLinus Torvalds 	{ "boot_osflags",	ENV_BOOT_OSFLAGS	},
701da177e4SLinus Torvalds 	{ "booted_osflags",	ENV_BOOTED_OSFLAGS	},
711da177e4SLinus Torvalds 	{ "boot_reset",		ENV_BOOT_RESET		},
721da177e4SLinus Torvalds 	{ "dump_dev",		ENV_DUMP_DEV		},
731da177e4SLinus Torvalds 	{ "enable_audit",	ENV_ENABLE_AUDIT	},
741da177e4SLinus Torvalds 	{ "license",		ENV_LICENSE		},
751da177e4SLinus Torvalds 	{ "char_set",		ENV_CHAR_SET		},
761da177e4SLinus Torvalds 	{ "language",		ENV_LANGUAGE		},
771da177e4SLinus Torvalds 	{ "tty_dev",		ENV_TTY_DEV		},
781da177e4SLinus Torvalds 	{ NULL,			0			},
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds static srm_env_t	srm_numbered_entries[256];
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 
830ead0f84SAlexey Dobriyan static int srm_env_proc_show(struct seq_file *m, void *v)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	unsigned long	ret;
861da177e4SLinus Torvalds 	srm_env_t	*entry;
870ead0f84SAlexey Dobriyan 	char		*page;
881da177e4SLinus Torvalds 
89af96f8a3Smatt mooney 	entry = m->private;
900ead0f84SAlexey Dobriyan 	page = (char *)__get_free_page(GFP_USER);
910ead0f84SAlexey Dobriyan 	if (!page)
920ead0f84SAlexey Dobriyan 		return -ENOMEM;
931da177e4SLinus Torvalds 
940ead0f84SAlexey Dobriyan 	ret = callback_getenv(entry->id, page, PAGE_SIZE);
951da177e4SLinus Torvalds 
9616b7f4dcSJan-Benedict Glaw 	if ((ret >> 61) == 0) {
970ead0f84SAlexey Dobriyan 		seq_write(m, page, ret);
980ead0f84SAlexey Dobriyan 		ret = 0;
9916b7f4dcSJan-Benedict Glaw 	} else
1000ead0f84SAlexey Dobriyan 		ret = -EFAULT;
1010ead0f84SAlexey Dobriyan 	free_page((unsigned long)page);
1020ead0f84SAlexey Dobriyan 	return ret;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1050ead0f84SAlexey Dobriyan static int srm_env_proc_open(struct inode *inode, struct file *file)
1060ead0f84SAlexey Dobriyan {
107*d9dda78bSAl Viro 	return single_open(file, srm_env_proc_show, PDE_DATA(inode));
1080ead0f84SAlexey Dobriyan }
1090ead0f84SAlexey Dobriyan 
1100ead0f84SAlexey Dobriyan static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
1110ead0f84SAlexey Dobriyan 				  size_t count, loff_t *pos)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	int res;
114*d9dda78bSAl Viro 	srm_env_t	*entry = PDE_DATA(file_inode(file));
1151da177e4SLinus Torvalds 	char		*buf = (char *) __get_free_page(GFP_USER);
1161da177e4SLinus Torvalds 	unsigned long	ret1, ret2;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (!buf)
1191da177e4SLinus Torvalds 		return -ENOMEM;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	res = -EINVAL;
1221da177e4SLinus Torvalds 	if (count >= PAGE_SIZE)
1231da177e4SLinus Torvalds 		goto out;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	res = -EFAULT;
1261da177e4SLinus Torvalds 	if (copy_from_user(buf, buffer, count))
1271da177e4SLinus Torvalds 		goto out;
1281da177e4SLinus Torvalds 	buf[count] = '\0';
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	ret1 = callback_setenv(entry->id, buf, count);
1311da177e4SLinus Torvalds 	if ((ret1 >> 61) == 0) {
1321da177e4SLinus Torvalds 		do
1331da177e4SLinus Torvalds 			ret2 = callback_save_env();
1341da177e4SLinus Torvalds 		while((ret2 >> 61) == 1);
1351da177e4SLinus Torvalds 		res = (int) ret1;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds  out:
1391da177e4SLinus Torvalds 	free_page((unsigned long)buf);
1401da177e4SLinus Torvalds 	return res;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1430ead0f84SAlexey Dobriyan static const struct file_operations srm_env_proc_fops = {
1440ead0f84SAlexey Dobriyan 	.owner		= THIS_MODULE,
1450ead0f84SAlexey Dobriyan 	.open		= srm_env_proc_open,
1460ead0f84SAlexey Dobriyan 	.read		= seq_read,
1470ead0f84SAlexey Dobriyan 	.llseek		= seq_lseek,
1480ead0f84SAlexey Dobriyan 	.release	= single_release,
1490ead0f84SAlexey Dobriyan 	.write		= srm_env_proc_write,
1500ead0f84SAlexey Dobriyan };
1510ead0f84SAlexey Dobriyan 
1521da177e4SLinus Torvalds static void
1531da177e4SLinus Torvalds srm_env_cleanup(void)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	srm_env_t	*entry;
1561da177e4SLinus Torvalds 	unsigned long	var_num;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (base_dir) {
1591da177e4SLinus Torvalds 		/*
1601da177e4SLinus Torvalds 		 * Remove named entries
1611da177e4SLinus Torvalds 		 */
1621da177e4SLinus Torvalds 		if (named_dir) {
1631da177e4SLinus Torvalds 			entry = srm_named_entries;
1641da177e4SLinus Torvalds 			while (entry->name != NULL && entry->id != 0) {
1651da177e4SLinus Torvalds 				if (entry->proc_entry) {
1661da177e4SLinus Torvalds 					remove_proc_entry(entry->name,
1671da177e4SLinus Torvalds 							named_dir);
1681da177e4SLinus Torvalds 					entry->proc_entry = NULL;
1691da177e4SLinus Torvalds 				}
1701da177e4SLinus Torvalds 				entry++;
1711da177e4SLinus Torvalds 			}
1721da177e4SLinus Torvalds 			remove_proc_entry(NAMED_DIR, base_dir);
1731da177e4SLinus Torvalds 		}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		/*
1761da177e4SLinus Torvalds 		 * Remove numbered entries
1771da177e4SLinus Torvalds 		 */
1781da177e4SLinus Torvalds 		if (numbered_dir) {
1791da177e4SLinus Torvalds 			for (var_num = 0; var_num <= 255; var_num++) {
1801da177e4SLinus Torvalds 				entry =	&srm_numbered_entries[var_num];
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 				if (entry->proc_entry) {
1831da177e4SLinus Torvalds 					remove_proc_entry(entry->name,
1841da177e4SLinus Torvalds 							numbered_dir);
1851da177e4SLinus Torvalds 					entry->proc_entry	= NULL;
1861da177e4SLinus Torvalds 					entry->name		= NULL;
1871da177e4SLinus Torvalds 				}
1881da177e4SLinus Torvalds 			}
1891da177e4SLinus Torvalds 			remove_proc_entry(NUMBERED_DIR, base_dir);
1901da177e4SLinus Torvalds 		}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 		remove_proc_entry(BASE_DIR, NULL);
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	return;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static int __init
1991da177e4SLinus Torvalds srm_env_init(void)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	srm_env_t	*entry;
2021da177e4SLinus Torvalds 	unsigned long	var_num;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/*
2051da177e4SLinus Torvalds 	 * Check system
2061da177e4SLinus Torvalds 	 */
2071da177e4SLinus Torvalds 	if (!alpha_using_srm) {
2081da177e4SLinus Torvalds 		printk(KERN_INFO "%s: This Alpha system doesn't "
2091da177e4SLinus Torvalds 				"know about SRM (or you've booted "
2101da177e4SLinus Torvalds 				"SRM->MILO->Linux, which gets "
211bbb8d343SHarvey Harrison 				"misdetected)...\n", __func__);
2121da177e4SLinus Torvalds 		return -ENODEV;
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/*
2161da177e4SLinus Torvalds 	 * Init numbers
2171da177e4SLinus Torvalds 	 */
2181da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++)
2191da177e4SLinus Torvalds 		sprintf(number[var_num], "%ld", var_num);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/*
2221da177e4SLinus Torvalds 	 * Create base directory
2231da177e4SLinus Torvalds 	 */
2241da177e4SLinus Torvalds 	base_dir = proc_mkdir(BASE_DIR, NULL);
22516b7f4dcSJan-Benedict Glaw 	if (!base_dir) {
2261da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
2271da177e4SLinus Torvalds 				BASE_DIR);
2281da177e4SLinus Torvalds 		goto cleanup;
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/*
2321da177e4SLinus Torvalds 	 * Create per-name subdirectory
2331da177e4SLinus Torvalds 	 */
2341da177e4SLinus Torvalds 	named_dir = proc_mkdir(NAMED_DIR, base_dir);
23516b7f4dcSJan-Benedict Glaw 	if (!named_dir) {
2361da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
2371da177e4SLinus Torvalds 				BASE_DIR, NAMED_DIR);
2381da177e4SLinus Torvalds 		goto cleanup;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/*
2421da177e4SLinus Torvalds 	 * Create per-number subdirectory
2431da177e4SLinus Torvalds 	 */
2441da177e4SLinus Torvalds 	numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
24516b7f4dcSJan-Benedict Glaw 	if (!numbered_dir) {
2461da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
2471da177e4SLinus Torvalds 				BASE_DIR, NUMBERED_DIR);
2481da177e4SLinus Torvalds 		goto cleanup;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/*
2531da177e4SLinus Torvalds 	 * Create all named nodes
2541da177e4SLinus Torvalds 	 */
2551da177e4SLinus Torvalds 	entry = srm_named_entries;
25616b7f4dcSJan-Benedict Glaw 	while (entry->name && entry->id) {
2570ead0f84SAlexey Dobriyan 		entry->proc_entry = proc_create_data(entry->name, 0644, named_dir,
2580ead0f84SAlexey Dobriyan 						     &srm_env_proc_fops, entry);
25916b7f4dcSJan-Benedict Glaw 		if (!entry->proc_entry)
2601da177e4SLinus Torvalds 			goto cleanup;
2611da177e4SLinus Torvalds 		entry++;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/*
2651da177e4SLinus Torvalds 	 * Create all numbered nodes
2661da177e4SLinus Torvalds 	 */
2671da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++) {
2681da177e4SLinus Torvalds 		entry = &srm_numbered_entries[var_num];
2691da177e4SLinus Torvalds 		entry->name = number[var_num];
2701da177e4SLinus Torvalds 
2710ead0f84SAlexey Dobriyan 		entry->proc_entry = proc_create_data(entry->name, 0644, numbered_dir,
2720ead0f84SAlexey Dobriyan 						     &srm_env_proc_fops, entry);
27316b7f4dcSJan-Benedict Glaw 		if (!entry->proc_entry)
2741da177e4SLinus Torvalds 			goto cleanup;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		entry->id			= var_num;
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
2801da177e4SLinus Torvalds 			VERSION);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	return 0;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds cleanup:
2851da177e4SLinus Torvalds 	srm_env_cleanup();
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return -ENOMEM;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void __exit
2911da177e4SLinus Torvalds srm_env_exit(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	srm_env_cleanup();
2941da177e4SLinus Torvalds 	printk(KERN_INFO "%s: unloaded successfully\n", NAME);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	return;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds module_init(srm_env_init);
3001da177e4SLinus Torvalds module_exit(srm_env_exit);
301