xref: /linux/drivers/pnp/pnpbios/proc.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
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
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 = kcalloc(1, escd.escd_size, GFP_KERNEL);
91 	if (!tmpbuf) return -ENOMEM;
92 
93 	if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
94 		kfree(tmpbuf);
95 		return -EIO;
96 	}
97 
98 	escd_size = (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1])*256;
99 
100 	/* sanity check */
101 	if (escd_size > MAX_SANE_ESCD_SIZE) {
102 		printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS read_escd call is too great\n");
103 		return -EFBIG;
104 	}
105 
106 	escd_left_to_read = escd_size - pos;
107 	if (escd_left_to_read < 0) escd_left_to_read = 0;
108 	if (escd_left_to_read == 0) *eof = 1;
109 	n = min(count,escd_left_to_read);
110 	memcpy(buf, tmpbuf + pos, n);
111 	kfree(tmpbuf);
112 	*start = buf;
113 	return n;
114 }
115 
116 static int proc_read_legacyres(char *buf, char **start, off_t pos,
117                                int count, int *eof, void *data)
118 {
119 	/* Assume that the following won't overflow the buffer */
120 	if (pnp_bios_get_stat_res(buf))
121 		return -EIO;
122 
123 	return count;  // FIXME: Return actual length
124 }
125 
126 static int proc_read_devices(char *buf, char **start, off_t pos,
127                              int count, int *eof, void *data)
128 {
129 	struct pnp_bios_node *node;
130 	u8 nodenum;
131 	char *p = buf;
132 
133 	if (pos >= 0xff)
134 		return 0;
135 
136 	node = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
137 	if (!node) return -ENOMEM;
138 
139 	for (nodenum=pos; nodenum<0xff; ) {
140 		u8 thisnodenum = nodenum;
141 		/* 26 = the number of characters per line sprintf'ed */
142 		if ((p - buf + 26) > count)
143 			break;
144 		if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
145 			break;
146 		p += sprintf(p, "%02x\t%08x\t%02x:%02x:%02x\t%04x\n",
147 			     node->handle, node->eisa_id,
148 			     node->type_code[0], node->type_code[1],
149 			     node->type_code[2], node->flags);
150 		if (nodenum <= thisnodenum) {
151 			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);
152 			*eof = 1;
153 			break;
154 		}
155 	}
156 	kfree(node);
157 	if (nodenum == 0xff)
158 		*eof = 1;
159 	*start = (char *)((off_t)nodenum - pos);
160 	return p - buf;
161 }
162 
163 static int proc_read_node(char *buf, char **start, off_t pos,
164                           int count, int *eof, void *data)
165 {
166 	struct pnp_bios_node *node;
167 	int boot = (long)data >> 8;
168 	u8 nodenum = (long)data;
169 	int len;
170 
171 	node = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
172 	if (!node) return -ENOMEM;
173 	if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
174 		kfree(node);
175 		return -EIO;
176 	}
177 	len = node->size - sizeof(struct pnp_bios_node);
178 	memcpy(buf, node->data, len);
179 	kfree(node);
180 	return len;
181 }
182 
183 static int proc_write_node(struct file *file, const char __user *buf,
184                            unsigned long count, void *data)
185 {
186 	struct pnp_bios_node *node;
187 	int boot = (long)data >> 8;
188 	u8 nodenum = (long)data;
189 	int ret = count;
190 
191 	node = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
192 	if (!node)
193 		return -ENOMEM;
194 	if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
195 		ret = -EIO;
196 		goto out;
197 	}
198 	if (count != node->size - sizeof(struct pnp_bios_node)) {
199 		ret = -EINVAL;
200 		goto out;
201 	}
202 	if (copy_from_user(node->data, buf, count)) {
203 		ret = -EFAULT;
204 		goto out;
205 	}
206 	if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
207 		ret = -EINVAL;
208 		goto out;
209 	}
210 	ret = count;
211 out:
212 	kfree(node);
213 	return ret;
214 }
215 
216 int pnpbios_interface_attach_device(struct pnp_bios_node * node)
217 {
218 	char name[3];
219 	struct proc_dir_entry *ent;
220 
221 	sprintf(name, "%02x", node->handle);
222 
223 	if (!proc_pnp)
224 		return -EIO;
225 	if ( !pnpbios_dont_use_current_config ) {
226 		ent = create_proc_entry(name, 0, proc_pnp);
227 		if (ent) {
228 			ent->read_proc = proc_read_node;
229 			ent->write_proc = proc_write_node;
230 			ent->data = (void *)(long)(node->handle);
231 		}
232 	}
233 
234 	if (!proc_pnp_boot)
235 		return -EIO;
236 	ent = create_proc_entry(name, 0, proc_pnp_boot);
237 	if (ent) {
238 		ent->read_proc = proc_read_node;
239 		ent->write_proc = proc_write_node;
240 		ent->data = (void *)(long)(node->handle+0x100);
241 		return 0;
242 	}
243 
244 	return -EIO;
245 }
246 
247 /*
248  * When this is called, pnpbios functions are assumed to
249  * work and the pnpbios_dont_use_current_config flag
250  * should already have been set to the appropriate value
251  */
252 int __init pnpbios_proc_init( void )
253 {
254 	proc_pnp = proc_mkdir("pnp", proc_bus);
255 	if (!proc_pnp)
256 		return -EIO;
257 	proc_pnp_boot = proc_mkdir("boot", proc_pnp);
258 	if (!proc_pnp_boot)
259 		return -EIO;
260 	create_proc_read_entry("devices", 0, proc_pnp, proc_read_devices, NULL);
261 	create_proc_read_entry("configuration_info", 0, proc_pnp, proc_read_pnpconfig, NULL);
262 	create_proc_read_entry("escd_info", 0, proc_pnp, proc_read_escdinfo, NULL);
263 	create_proc_read_entry("escd", S_IRUSR, proc_pnp, proc_read_escd, NULL);
264 	create_proc_read_entry("legacy_device_resources", 0, proc_pnp, proc_read_legacyres, NULL);
265 
266 	return 0;
267 }
268 
269 void __exit pnpbios_proc_exit(void)
270 {
271 	int i;
272 	char name[3];
273 
274 	if (!proc_pnp)
275 		return;
276 
277 	for (i=0; i<0xff; i++) {
278 		sprintf(name, "%02x", i);
279 		if ( !pnpbios_dont_use_current_config )
280 			remove_proc_entry(name, proc_pnp);
281 		remove_proc_entry(name, proc_pnp_boot);
282 	}
283 	remove_proc_entry("legacy_device_resources", proc_pnp);
284 	remove_proc_entry("escd", proc_pnp);
285 	remove_proc_entry("escd_info", proc_pnp);
286 	remove_proc_entry("configuration_info", proc_pnp);
287 	remove_proc_entry("devices", proc_pnp);
288 	remove_proc_entry("boot", proc_pnp);
289 	remove_proc_entry("pnp", proc_bus);
290 
291 	return;
292 }
293