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 * 71da177e4SLinus Torvalds * This driver is at all a modified version of Erik Mouw's 81da177e4SLinus Torvalds * Documentation/DocBook/procfs_example.c, so: thank 91da177e4SLinus Torvalds * you, Erik! He can be reached via email at 101da177e4SLinus Torvalds * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea 111da177e4SLinus Torvalds * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They 121da177e4SLinus Torvalds * included a patch like this as well. Thanks for idea! 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This program is free software; you can redistribute 151da177e4SLinus Torvalds * it and/or modify it under the terms of the GNU General 161da177e4SLinus Torvalds * Public License version 2 as published by the Free Software 171da177e4SLinus Torvalds * Foundation. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * This program is distributed in the hope that it will be 201da177e4SLinus Torvalds * useful, but WITHOUT ANY WARRANTY; without even the implied 211da177e4SLinus Torvalds * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 221da177e4SLinus Torvalds * PURPOSE. See the GNU General Public License for more 231da177e4SLinus Torvalds * details. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * You should have received a copy of the GNU General Public 261da177e4SLinus Torvalds * License along with this program; if not, write to the 271da177e4SLinus Torvalds * Free Software Foundation, Inc., 59 Temple Place, 281da177e4SLinus Torvalds * Suite 330, Boston, MA 02111-1307 USA 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <linux/kernel.h> 335a0e3ad6STejun Heo #include <linux/gfp.h> 341da177e4SLinus Torvalds #include <linux/module.h> 351da177e4SLinus Torvalds #include <linux/init.h> 361da177e4SLinus Torvalds #include <linux/proc_fs.h> 370ead0f84SAlexey Dobriyan #include <linux/seq_file.h> 381da177e4SLinus Torvalds #include <asm/console.h> 391da177e4SLinus Torvalds #include <asm/uaccess.h> 401da177e4SLinus Torvalds #include <asm/machvec.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define BASE_DIR "srm_environment" /* Subdir in /proc/ */ 431da177e4SLinus Torvalds #define NAMED_DIR "named_variables" /* Subdir for known variables */ 441da177e4SLinus Torvalds #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */ 4516b7f4dcSJan-Benedict Glaw #define VERSION "0.0.6" /* Module version */ 461da177e4SLinus Torvalds #define NAME "srm_env" /* Module name */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>"); 491da177e4SLinus Torvalds MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface"); 501da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds typedef struct _srm_env { 531da177e4SLinus Torvalds char *name; 541da177e4SLinus Torvalds unsigned long id; 551da177e4SLinus Torvalds struct proc_dir_entry *proc_entry; 561da177e4SLinus Torvalds } srm_env_t; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static struct proc_dir_entry *base_dir; 591da177e4SLinus Torvalds static struct proc_dir_entry *named_dir; 601da177e4SLinus Torvalds static struct proc_dir_entry *numbered_dir; 611da177e4SLinus Torvalds static char number[256][4]; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds static srm_env_t srm_named_entries[] = { 641da177e4SLinus Torvalds { "auto_action", ENV_AUTO_ACTION }, 651da177e4SLinus Torvalds { "boot_dev", ENV_BOOT_DEV }, 661da177e4SLinus Torvalds { "bootdef_dev", ENV_BOOTDEF_DEV }, 671da177e4SLinus Torvalds { "booted_dev", ENV_BOOTED_DEV }, 681da177e4SLinus Torvalds { "boot_file", ENV_BOOT_FILE }, 691da177e4SLinus Torvalds { "booted_file", ENV_BOOTED_FILE }, 701da177e4SLinus Torvalds { "boot_osflags", ENV_BOOT_OSFLAGS }, 711da177e4SLinus Torvalds { "booted_osflags", ENV_BOOTED_OSFLAGS }, 721da177e4SLinus Torvalds { "boot_reset", ENV_BOOT_RESET }, 731da177e4SLinus Torvalds { "dump_dev", ENV_DUMP_DEV }, 741da177e4SLinus Torvalds { "enable_audit", ENV_ENABLE_AUDIT }, 751da177e4SLinus Torvalds { "license", ENV_LICENSE }, 761da177e4SLinus Torvalds { "char_set", ENV_CHAR_SET }, 771da177e4SLinus Torvalds { "language", ENV_LANGUAGE }, 781da177e4SLinus Torvalds { "tty_dev", ENV_TTY_DEV }, 791da177e4SLinus Torvalds { NULL, 0 }, 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds static srm_env_t srm_numbered_entries[256]; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds 840ead0f84SAlexey Dobriyan static int srm_env_proc_show(struct seq_file *m, void *v) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds unsigned long ret; 871da177e4SLinus Torvalds srm_env_t *entry; 880ead0f84SAlexey Dobriyan char *page; 891da177e4SLinus Torvalds 90*af96f8a3Smatt mooney entry = m->private; 910ead0f84SAlexey Dobriyan page = (char *)__get_free_page(GFP_USER); 920ead0f84SAlexey Dobriyan if (!page) 930ead0f84SAlexey Dobriyan return -ENOMEM; 941da177e4SLinus Torvalds 950ead0f84SAlexey Dobriyan ret = callback_getenv(entry->id, page, PAGE_SIZE); 961da177e4SLinus Torvalds 9716b7f4dcSJan-Benedict Glaw if ((ret >> 61) == 0) { 980ead0f84SAlexey Dobriyan seq_write(m, page, ret); 990ead0f84SAlexey Dobriyan ret = 0; 10016b7f4dcSJan-Benedict Glaw } else 1010ead0f84SAlexey Dobriyan ret = -EFAULT; 1020ead0f84SAlexey Dobriyan free_page((unsigned long)page); 1030ead0f84SAlexey Dobriyan return ret; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1060ead0f84SAlexey Dobriyan static int srm_env_proc_open(struct inode *inode, struct file *file) 1070ead0f84SAlexey Dobriyan { 1080ead0f84SAlexey Dobriyan return single_open(file, srm_env_proc_show, PDE(inode)->data); 1090ead0f84SAlexey Dobriyan } 1100ead0f84SAlexey Dobriyan 1110ead0f84SAlexey Dobriyan static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer, 1120ead0f84SAlexey Dobriyan size_t count, loff_t *pos) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds int res; 1150ead0f84SAlexey Dobriyan srm_env_t *entry = PDE(file->f_path.dentry->d_inode)->data; 1161da177e4SLinus Torvalds char *buf = (char *) __get_free_page(GFP_USER); 1171da177e4SLinus Torvalds unsigned long ret1, ret2; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds if (!buf) 1201da177e4SLinus Torvalds return -ENOMEM; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds res = -EINVAL; 1231da177e4SLinus Torvalds if (count >= PAGE_SIZE) 1241da177e4SLinus Torvalds goto out; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds res = -EFAULT; 1271da177e4SLinus Torvalds if (copy_from_user(buf, buffer, count)) 1281da177e4SLinus Torvalds goto out; 1291da177e4SLinus Torvalds buf[count] = '\0'; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds ret1 = callback_setenv(entry->id, buf, count); 1321da177e4SLinus Torvalds if ((ret1 >> 61) == 0) { 1331da177e4SLinus Torvalds do 1341da177e4SLinus Torvalds ret2 = callback_save_env(); 1351da177e4SLinus Torvalds while((ret2 >> 61) == 1); 1361da177e4SLinus Torvalds res = (int) ret1; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds out: 1401da177e4SLinus Torvalds free_page((unsigned long)buf); 1411da177e4SLinus Torvalds return res; 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1440ead0f84SAlexey Dobriyan static const struct file_operations srm_env_proc_fops = { 1450ead0f84SAlexey Dobriyan .owner = THIS_MODULE, 1460ead0f84SAlexey Dobriyan .open = srm_env_proc_open, 1470ead0f84SAlexey Dobriyan .read = seq_read, 1480ead0f84SAlexey Dobriyan .llseek = seq_lseek, 1490ead0f84SAlexey Dobriyan .release = single_release, 1500ead0f84SAlexey Dobriyan .write = srm_env_proc_write, 1510ead0f84SAlexey Dobriyan }; 1520ead0f84SAlexey Dobriyan 1531da177e4SLinus Torvalds static void 1541da177e4SLinus Torvalds srm_env_cleanup(void) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds srm_env_t *entry; 1571da177e4SLinus Torvalds unsigned long var_num; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds if (base_dir) { 1601da177e4SLinus Torvalds /* 1611da177e4SLinus Torvalds * Remove named entries 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds if (named_dir) { 1641da177e4SLinus Torvalds entry = srm_named_entries; 1651da177e4SLinus Torvalds while (entry->name != NULL && entry->id != 0) { 1661da177e4SLinus Torvalds if (entry->proc_entry) { 1671da177e4SLinus Torvalds remove_proc_entry(entry->name, 1681da177e4SLinus Torvalds named_dir); 1691da177e4SLinus Torvalds entry->proc_entry = NULL; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds entry++; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds remove_proc_entry(NAMED_DIR, base_dir); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* 1771da177e4SLinus Torvalds * Remove numbered entries 1781da177e4SLinus Torvalds */ 1791da177e4SLinus Torvalds if (numbered_dir) { 1801da177e4SLinus Torvalds for (var_num = 0; var_num <= 255; var_num++) { 1811da177e4SLinus Torvalds entry = &srm_numbered_entries[var_num]; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds if (entry->proc_entry) { 1841da177e4SLinus Torvalds remove_proc_entry(entry->name, 1851da177e4SLinus Torvalds numbered_dir); 1861da177e4SLinus Torvalds entry->proc_entry = NULL; 1871da177e4SLinus Torvalds entry->name = NULL; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds remove_proc_entry(NUMBERED_DIR, base_dir); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds remove_proc_entry(BASE_DIR, NULL); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds return; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static int __init 2001da177e4SLinus Torvalds srm_env_init(void) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds srm_env_t *entry; 2031da177e4SLinus Torvalds unsigned long var_num; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* 2061da177e4SLinus Torvalds * Check system 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds if (!alpha_using_srm) { 2091da177e4SLinus Torvalds printk(KERN_INFO "%s: This Alpha system doesn't " 2101da177e4SLinus Torvalds "know about SRM (or you've booted " 2111da177e4SLinus Torvalds "SRM->MILO->Linux, which gets " 212bbb8d343SHarvey Harrison "misdetected)...\n", __func__); 2131da177e4SLinus Torvalds return -ENODEV; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* 2171da177e4SLinus Torvalds * Init numbers 2181da177e4SLinus Torvalds */ 2191da177e4SLinus Torvalds for (var_num = 0; var_num <= 255; var_num++) 2201da177e4SLinus Torvalds sprintf(number[var_num], "%ld", var_num); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * Create base directory 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds base_dir = proc_mkdir(BASE_DIR, NULL); 22616b7f4dcSJan-Benedict Glaw if (!base_dir) { 2271da177e4SLinus Torvalds printk(KERN_ERR "Couldn't create base dir /proc/%s\n", 2281da177e4SLinus Torvalds BASE_DIR); 2291da177e4SLinus Torvalds goto cleanup; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * Create per-name subdirectory 2341da177e4SLinus Torvalds */ 2351da177e4SLinus Torvalds named_dir = proc_mkdir(NAMED_DIR, base_dir); 23616b7f4dcSJan-Benedict Glaw if (!named_dir) { 2371da177e4SLinus Torvalds printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n", 2381da177e4SLinus Torvalds BASE_DIR, NAMED_DIR); 2391da177e4SLinus Torvalds goto cleanup; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* 2431da177e4SLinus Torvalds * Create per-number subdirectory 2441da177e4SLinus Torvalds */ 2451da177e4SLinus Torvalds numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir); 24616b7f4dcSJan-Benedict Glaw if (!numbered_dir) { 2471da177e4SLinus Torvalds printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n", 2481da177e4SLinus Torvalds BASE_DIR, NUMBERED_DIR); 2491da177e4SLinus Torvalds goto cleanup; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * Create all named nodes 2551da177e4SLinus Torvalds */ 2561da177e4SLinus Torvalds entry = srm_named_entries; 25716b7f4dcSJan-Benedict Glaw while (entry->name && entry->id) { 2580ead0f84SAlexey Dobriyan entry->proc_entry = proc_create_data(entry->name, 0644, named_dir, 2590ead0f84SAlexey Dobriyan &srm_env_proc_fops, entry); 26016b7f4dcSJan-Benedict Glaw if (!entry->proc_entry) 2611da177e4SLinus Torvalds goto cleanup; 2621da177e4SLinus Torvalds entry++; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* 2661da177e4SLinus Torvalds * Create all numbered nodes 2671da177e4SLinus Torvalds */ 2681da177e4SLinus Torvalds for (var_num = 0; var_num <= 255; var_num++) { 2691da177e4SLinus Torvalds entry = &srm_numbered_entries[var_num]; 2701da177e4SLinus Torvalds entry->name = number[var_num]; 2711da177e4SLinus Torvalds 2720ead0f84SAlexey Dobriyan entry->proc_entry = proc_create_data(entry->name, 0644, numbered_dir, 2730ead0f84SAlexey Dobriyan &srm_env_proc_fops, entry); 27416b7f4dcSJan-Benedict Glaw if (!entry->proc_entry) 2751da177e4SLinus Torvalds goto cleanup; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds entry->id = var_num; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds printk(KERN_INFO "%s: version %s loaded successfully\n", NAME, 2811da177e4SLinus Torvalds VERSION); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds return 0; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds cleanup: 2861da177e4SLinus Torvalds srm_env_cleanup(); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds return -ENOMEM; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds static void __exit 2921da177e4SLinus Torvalds srm_env_exit(void) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds srm_env_cleanup(); 2951da177e4SLinus Torvalds printk(KERN_INFO "%s: unloaded successfully\n", NAME); 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds return; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds module_init(srm_env_init); 3011da177e4SLinus Torvalds module_exit(srm_env_exit); 302