patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pnp / pnpbios / proc.c
1 /*
2  * /proc/bus/pnp interface for Plug and Play devices
3  *
4  * Written by David Hinds, dahinds@users.sourceforge.net
5  * Modified by Thomas Hood, jdthood@mail.com
6  *
7  * The .../devices and .../<node> and .../boot/<node> files are
8  * utilized by the lspnp and setpnp utilities, supplied with the
9  * pcmcia-cs package.
10  *     http://pcmcia-cs.sourceforge.net
11  *
12  * The .../escd file is utilized by the lsescd utility written by
13  * Gunther Mayer.
14  *     http://home.t-online.de/home/gunther.mayer/lsescd
15  *
16  * The .../legacy_device_resources file is not used yet.
17  *
18  * The other files are human-readable.
19  */
20
21 //#include <pcmcia/config.h>
22 //#include <pcmcia/k_compat.h>
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/proc_fs.h>
29 #include <linux/pnpbios.h>
30 #include <linux/init.h>
31
32 #include <asm/uaccess.h>
33
34 #include "pnpbios.h"
35
36 static struct proc_dir_entry *proc_pnp = NULL;
37 static struct proc_dir_entry *proc_pnp_boot = NULL;
38
39 static int proc_read_pnpconfig(char *buf, char **start, off_t pos,
40                                int count, int *eof, void *data)
41 {
42         struct pnp_isa_config_struc pnps;
43
44         if (pnp_bios_isapnp_config(&pnps))
45                 return -EIO;
46         return snprintf(buf, count,
47                 "structure_revision %d\n"
48                 "number_of_CSNs %d\n"
49                 "ISA_read_data_port 0x%x\n",
50                 pnps.revision,
51                 pnps.no_csns,
52                 pnps.isa_rd_data_port
53         );
54 }
55
56 static int proc_read_escdinfo(char *buf, char **start, off_t pos,
57                               int count, int *eof, void *data)
58 {
59         struct escd_info_struc escd;
60
61         if (pnp_bios_escd_info(&escd))
62                 return -EIO;
63         return snprintf(buf, count,
64                 "min_ESCD_write_size %d\n"
65                 "ESCD_size %d\n"
66                 "NVRAM_base 0x%x\n",
67                 escd.min_escd_write_size,
68                 escd.escd_size,
69                 escd.nv_storage_base
70         );
71 }
72
73 #define MAX_SANE_ESCD_SIZE (32*1024)
74 static int proc_read_escd(char *buf, char **start, off_t pos,
75                           int count, int *eof, void *data)
76 {
77         struct escd_info_struc escd;
78         char *tmpbuf;
79         int escd_size, escd_left_to_read, n;
80
81         if (pnp_bios_escd_info(&escd))
82                 return -EIO;
83
84         /* sanity check */
85         if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
86                 printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS escd_info call is too great\n");
87                 return -EFBIG;
88         }
89
90         tmpbuf = pnpbios_kmalloc(escd.escd_size, GFP_KERNEL);
91         if (!tmpbuf) return -ENOMEM;
92
93         if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base))
94                 return -EIO;
95
96         escd_size = (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1])*256;
97
98         /* sanity check */
99         if (escd_size > MAX_SANE_ESCD_SIZE) {
100                 printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS read_escd call is too great\n");
101                 return -EFBIG;
102         }
103
104         escd_left_to_read = escd_size - pos;
105         if (escd_left_to_read < 0) escd_left_to_read = 0;
106         if (escd_left_to_read == 0) *eof = 1;
107         n = min(count,escd_left_to_read);
108         memcpy(buf, tmpbuf + pos, n);
109         kfree(tmpbuf);
110         *start = buf;
111         return n;
112 }
113
114 static int proc_read_legacyres(char *buf, char **start, off_t pos,
115                                int count, int *eof, void *data)
116 {
117         /* Assume that the following won't overflow the buffer */
118         if (pnp_bios_get_stat_res(buf)) 
119                 return -EIO;
120
121         return count;  // FIXME: Return actual length
122 }
123
124 static int proc_read_devices(char *buf, char **start, off_t pos,
125                              int count, int *eof, void *data)
126 {
127         struct pnp_bios_node *node;
128         u8 nodenum;
129         char *p = buf;
130
131         if (pos >= 0xff)
132                 return 0;
133
134         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
135         if (!node) return -ENOMEM;
136
137         for (nodenum=pos; nodenum<0xff; ) {
138                 u8 thisnodenum = nodenum;
139                 /* 26 = the number of characters per line sprintf'ed */
140                 if ((p - buf + 26) > count)
141                         break;
142                 if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
143                         break;
144                 p += sprintf(p, "%02x\t%08x\t%02x:%02x:%02x\t%04x\n",
145                              node->handle, node->eisa_id,
146                              node->type_code[0], node->type_code[1],
147                              node->type_code[2], node->flags);
148                 if (nodenum <= thisnodenum) {
149                         printk(KERN_ERR "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n", "PnPBIOS: proc_read_devices:", (unsigned int)nodenum, (unsigned int)thisnodenum);
150                         *eof = 1;
151                         break;
152                 }
153         }
154         kfree(node);
155         if (nodenum == 0xff)
156                 *eof = 1;
157         *start = (char *)((off_t)nodenum - pos);
158         return p - buf;
159 }
160
161 static int proc_read_node(char *buf, char **start, off_t pos,
162                           int count, int *eof, void *data)
163 {
164         struct pnp_bios_node *node;
165         int boot = (long)data >> 8;
166         u8 nodenum = (long)data;
167         int len;
168
169         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
170         if (!node) return -ENOMEM;
171         if (pnp_bios_get_dev_node(&nodenum, boot, node))
172                 return -EIO;
173         len = node->size - sizeof(struct pnp_bios_node);
174         memcpy(buf, node->data, len);
175         kfree(node);
176         return len;
177 }
178
179 static int proc_write_node(struct file *file, const char __user *buf,
180                            unsigned long count, void *data)
181 {
182         struct pnp_bios_node *node;
183         int boot = (long)data >> 8;
184         u8 nodenum = (long)data;
185         int ret = count;
186
187         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
188         if (!node)
189                 return -ENOMEM;
190         if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
191                 ret = -EIO;
192                 goto out;
193         }
194         if (count != node->size - sizeof(struct pnp_bios_node)) {
195                 ret = -EINVAL;
196                 goto out;
197         }
198         if (copy_from_user(node->data, buf, count)) {
199                 ret = -EFAULT;
200                 goto out;
201         }
202         if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
203                 ret = -EINVAL;
204                 goto out;
205         }
206         ret = count;
207 out:
208         kfree(node);
209         return ret;
210 }
211
212 int pnpbios_interface_attach_device(struct pnp_bios_node * node)
213 {
214         char name[3];
215         struct proc_dir_entry *ent;
216
217         sprintf(name, "%02x", node->handle);
218
219         if (!proc_pnp)
220                 return -EIO;
221         if ( !pnpbios_dont_use_current_config ) {
222                 ent = create_proc_entry(name, 0, proc_pnp);
223                 if (ent) {
224                         ent->read_proc = proc_read_node;
225                         ent->write_proc = proc_write_node;
226                         ent->data = (void *)(long)(node->handle);
227                 }
228         }
229
230         if (!proc_pnp_boot)
231                 return -EIO;
232         ent = create_proc_entry(name, 0, proc_pnp_boot);
233         if (ent) {
234                 ent->read_proc = proc_read_node;
235                 ent->write_proc = proc_write_node;
236                 ent->data = (void *)(long)(node->handle+0x100);
237                 return 0;
238         }
239
240         return -EIO;
241 }
242
243 /*
244  * When this is called, pnpbios functions are assumed to
245  * work and the pnpbios_dont_use_current_config flag
246  * should already have been set to the appropriate value
247  */
248 int __init pnpbios_proc_init( void )
249 {
250         proc_pnp = proc_mkdir("pnp", proc_bus);
251         if (!proc_pnp)
252                 return -EIO;
253         proc_pnp_boot = proc_mkdir("boot", proc_pnp);
254         if (!proc_pnp_boot)
255                 return -EIO;
256         create_proc_read_entry("devices", 0, proc_pnp, proc_read_devices, NULL);
257         create_proc_read_entry("configuration_info", 0, proc_pnp, proc_read_pnpconfig, NULL);
258         create_proc_read_entry("escd_info", 0, proc_pnp, proc_read_escdinfo, NULL);
259         create_proc_read_entry("escd", S_IRUSR, proc_pnp, proc_read_escd, NULL);
260         create_proc_read_entry("legacy_device_resources", 0, proc_pnp, proc_read_legacyres, NULL);
261
262         return 0;
263 }
264
265 void __exit pnpbios_proc_exit(void)
266 {
267         int i;
268         char name[3];
269
270         if (!proc_pnp)
271                 return;
272
273         for (i=0; i<0xff; i++) {
274                 sprintf(name, "%02x", i);
275                 if ( !pnpbios_dont_use_current_config )
276                         remove_proc_entry(name, proc_pnp);
277                 remove_proc_entry(name, proc_pnp_boot);
278         }
279         remove_proc_entry("legacy_device_resources", proc_pnp);
280         remove_proc_entry("escd", proc_pnp);
281         remove_proc_entry("escd_info", proc_pnp);
282         remove_proc_entry("configuration_info", proc_pnp);
283         remove_proc_entry("devices", proc_pnp);
284         remove_proc_entry("boot", proc_pnp);
285         remove_proc_entry("pnp", proc_bus);
286
287         return;
288 }