v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / arch / alpha / kernel / srm_env.c
1 /*
2  * srm_env.c - Access to SRC environment variables through
3  *             the linux procfs
4  *
5  * (C)2001, Jan-Benedict Glaw <jbgaw@lug-owl.de>
6  *
7  * This driver is at all a modified version of Erik Mouw's
8  * ./linux/Documentation/DocBook/procfs_example.c, so: thanky
9  * you, erik! He can be reached via email at
10  * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
11  * provided by DEC^WCompaq's "Jumpstart" CD. They included
12  * a patch like this as well. Thanks for idea!
13  *
14  *
15  * This software has been developed while working on the LART
16  * computing board (http://www.lart.tudelft.nl/). The
17  * development has been sponsored by the Mobile Multi-media
18  * Communications (http://www.mmc.tudelft.nl/) and Ubiquitous
19  * Communications (http://www.ubicom.tudelft.nl/) projects.
20  *
21  * This program is free software; you can redistribute
22  * it and/or modify it under the terms of the GNU General
23  * Public License as published by the Free Software
24  * Foundation version 2.
25  *
26  * This program is distributed in the hope that it will be
27  * useful, but WITHOUT ANY WARRANTY; without even the implied
28  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
29  * PURPOSE.  See the GNU General Public License for more
30  * details.
31  * 
32  * You should have received a copy of the GNU General Public
33  * License along with this program; if not, write to the
34  * Free Software Foundation, Inc., 59 Temple Place,
35  * Suite 330, Boston, MA  02111-1307  USA
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/proc_fs.h>
44 #include <asm/console.h>
45 #include <asm/uaccess.h>
46
47 #define DIRNAME         "srm_environment"       /* Subdir in /proc/     */
48 #define VERSION         "0.0.1"                 /* Module version       */
49 #define NAME            "srm_env"               /* Module name          */
50 #define DEBUG
51
52 MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
53 MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
54 EXPORT_NO_SYMBOLS;
55
56 typedef struct _srm_env {
57         char                    *name;
58         unsigned long           id;
59         struct proc_dir_entry   *proc_entry;
60 } srm_env_t;
61
62 static struct proc_dir_entry    *directory;
63 static srm_env_t        srm_entries[] = {
64         { "auto_action",        ENV_AUTO_ACTION         },
65         { "boot_dev",           ENV_BOOT_DEV            },
66         { "bootdef_dev",        ENV_BOOTDEF_DEV         },
67         { "booted_dev",         ENV_BOOTED_DEV          },
68         { "boot_file",          ENV_BOOT_FILE           },
69         { "booted_file",        ENV_BOOTED_FILE         },
70         { "boot_osflags",       ENV_BOOT_OSFLAGS        },
71         { "booted_osflags",     ENV_BOOTED_OSFLAGS      },
72         { "boot_reset",         ENV_BOOT_RESET          },
73         { "dump_dev",           ENV_DUMP_DEV            },
74         { "enable_audit",       ENV_ENABLE_AUDIT        },
75         { "license",            ENV_LICENSE             },
76         { "char_set",           ENV_CHAR_SET            },
77         { "language",           ENV_LANGUAGE            },
78         { "tty_dev",            ENV_TTY_DEV             },
79         { NULL,                 0                       },
80 };
81
82 static int srm_env_read(char *page, char **start, off_t off, int count,
83                 int *eof, void *data)
84 {
85         int             nbytes;
86         unsigned long   ret;
87         srm_env_t       *entry;
88
89         MOD_INC_USE_COUNT;
90
91         if(off != 0) {
92                 MOD_DEC_USE_COUNT;
93                 return -EFAULT;
94         }
95
96         entry   = (srm_env_t *)data;
97         ret     = callback_getenv(entry->id, page, count);
98
99         if((ret >> 61) == 0)
100                 nbytes = (int)ret;
101         else
102                 nbytes = -EFAULT;
103
104         MOD_DEC_USE_COUNT;
105
106         return nbytes;
107 }
108
109
110 static int srm_env_write(struct file *file, const char *buffer,
111                 unsigned long count, void *data)
112 {
113 #define BUFLEN  512
114         int             nbytes;
115         srm_env_t       *entry;
116         char            buf[BUFLEN];
117         unsigned long   ret1, ret2;
118
119         MOD_INC_USE_COUNT;
120
121         entry = (srm_env_t *) data;
122
123         nbytes = strlen(buffer) + 1;
124         if(nbytes > BUFLEN) {
125                 MOD_DEC_USE_COUNT;
126                 return -ENOMEM;
127         }
128                 
129         //memcpy(aligned_buffer, buffer, nbytes)
130
131         if(copy_from_user(buf, buffer, count)) {
132                 MOD_DEC_USE_COUNT;
133                 return -EFAULT;
134         }
135         buf[count] = 0x00;
136
137         ret1 = callback_setenv(entry->id, buf, count);
138         if((ret1 >> 61) == 0) {
139                 do 
140                         ret2 = callback_save_env();
141                 while((ret2 >> 61) == 1);
142                 nbytes = (int)ret1;
143         } else
144                 nbytes = -EFAULT;
145
146         MOD_DEC_USE_COUNT;
147
148         return nbytes;
149 }
150
151 static void srm_env_cleanup(void)
152 {
153         srm_env_t       *entry;
154
155         if(directory) {
156                 entry = srm_entries;
157                 while(entry->name != NULL && entry->id != 0) {
158                         if(entry->proc_entry) {
159                                 remove_proc_entry(entry->name, directory);
160                                 entry->proc_entry = NULL;
161                         }
162                         entry++;
163                 }
164                 remove_proc_entry(DIRNAME, NULL);
165         }
166
167         return;
168 }
169
170 static int __init srm_env_init(void)
171 {
172         srm_env_t       *entry;
173         
174         if(!alpha_using_srm) {
175                 printk(KERN_INFO "%s: This Alpha system doesn't "
176                                 "know about SRM...\n", __FUNCTION__);
177                 return -ENODEV;
178         }
179
180         directory = proc_mkdir(DIRNAME, NULL);
181         if(directory == NULL)
182                 return -ENOMEM;
183         
184         directory->owner = THIS_MODULE;
185         
186         /* Now create all the nodes... */
187         entry = srm_entries;
188         while(entry->name != NULL && entry->id != 0) {
189                 entry->proc_entry = create_proc_entry(entry->name, 0644,
190                                 directory);
191                 if(entry->proc_entry == NULL)
192                         goto cleanup;
193                 entry->proc_entry->data         = entry;
194                 entry->proc_entry->read_proc    = srm_env_read;
195                 entry->proc_entry->write_proc   = srm_env_write;
196                 entry->proc_entry->owner        = THIS_MODULE;
197                 entry++;
198         }
199         
200         printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
201                         VERSION);
202         return 0;
203
204 cleanup:
205         srm_env_cleanup();
206         return -ENOMEM;
207 }
208
209
210 static void __exit srm_env_exit(void)
211 {
212         srm_env_cleanup();
213         printk(KERN_INFO "%s: unloaded successfully\n", NAME);
214         return;
215 }
216
217 module_init(srm_env_init);
218 module_exit(srm_env_exit);
219