Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * /proc/bus/pnp interface for Plug and Play devices
4 *
5 * Written by David Hinds, dahinds@users.sourceforge.net
6 * Modified by Thomas Hood
7 *
8 * The .../devices and .../<node> and .../boot/<node> files are
9 * utilized by the lspnp and setpnp utilities, supplied with the
10 * pcmcia-cs package.
11 * http://pcmcia-cs.sourceforge.net
12 *
13 * The .../escd file is utilized by the lsescd utility written by
14 * Gunther Mayer.
15 *
16 * The .../legacy_device_resources file is not used yet.
17 *
18 * The other files are human-readable.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25#include <linux/proc_fs.h>
26#include <linux/pnp.h>
27#include <linux/seq_file.h>
28#include <linux/init.h>
29
30#include <linux/uaccess.h>
31
32#include "pnpbios.h"
33
34static struct proc_dir_entry *proc_pnp = NULL;
35static struct proc_dir_entry *proc_pnp_boot = NULL;
36
37static int pnpconfig_proc_show(struct seq_file *m, void *v)
38{
39 struct pnp_isa_config_struc pnps;
40
41 if (pnp_bios_isapnp_config(&pnps))
42 return -EIO;
43 seq_printf(m, "structure_revision %d\n"
44 "number_of_CSNs %d\n"
45 "ISA_read_data_port 0x%x\n",
46 pnps.revision, pnps.no_csns, pnps.isa_rd_data_port);
47 return 0;
48}
49
50static int escd_info_proc_show(struct seq_file *m, void *v)
51{
52 struct escd_info_struc escd;
53
54 if (pnp_bios_escd_info(&escd))
55 return -EIO;
56 seq_printf(m, "min_ESCD_write_size %d\n"
57 "ESCD_size %d\n"
58 "NVRAM_base 0x%x\n",
59 escd.min_escd_write_size,
60 escd.escd_size, escd.nv_storage_base);
61 return 0;
62}
63
64#define MAX_SANE_ESCD_SIZE (32*1024)
65static int escd_proc_show(struct seq_file *m, void *v)
66{
67 struct escd_info_struc escd;
68 char *tmpbuf;
69 int escd_size;
70
71 if (pnp_bios_escd_info(&escd))
72 return -EIO;
73
74 /* sanity check */
75 if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
76 printk(KERN_ERR
77 "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
78 return -EFBIG;
79 }
80
81 tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
82 if (!tmpbuf)
83 return -ENOMEM;
84
85 if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
86 kfree(tmpbuf);
87 return -EIO;
88 }
89
90 escd_size =
91 (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
92
93 /* sanity check */
94 if (escd_size > MAX_SANE_ESCD_SIZE) {
95 printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
96 " BIOS read_escd call is too great\n", __func__);
97 kfree(tmpbuf);
98 return -EFBIG;
99 }
100
101 seq_write(m, tmpbuf, escd_size);
102 kfree(tmpbuf);
103 return 0;
104}
105
106static int pnp_legacyres_proc_show(struct seq_file *m, void *v)
107{
108 void *buf;
109
110 buf = kmalloc(65536, GFP_KERNEL);
111 if (!buf)
112 return -ENOMEM;
113 if (pnp_bios_get_stat_res(buf)) {
114 kfree(buf);
115 return -EIO;
116 }
117
118 seq_write(m, buf, 65536);
119 kfree(buf);
120 return 0;
121}
122
123static int pnp_devices_proc_show(struct seq_file *m, void *v)
124{
125 struct pnp_bios_node *node;
126 u8 nodenum;
127
128 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
129 if (!node)
130 return -ENOMEM;
131
132 for (nodenum = 0; nodenum < 0xff;) {
133 u8 thisnodenum = nodenum;
134
135 if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
136 break;
137 seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
138 node->handle, node->eisa_id,
139 node->type_code, node->flags);
140 if (nodenum <= thisnodenum) {
141 printk(KERN_ERR
142 "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
143 "PnPBIOS: proc_read_devices:",
144 (unsigned int)nodenum,
145 (unsigned int)thisnodenum);
146 break;
147 }
148 }
149 kfree(node);
150 return 0;
151}
152
153static int pnpbios_proc_show(struct seq_file *m, void *v)
154{
155 void *data = m->private;
156 struct pnp_bios_node *node;
157 int boot = (long)data >> 8;
158 u8 nodenum = (long)data;
159 int len;
160
161 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
162 if (!node)
163 return -ENOMEM;
164 if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
165 kfree(node);
166 return -EIO;
167 }
168 len = node->size - sizeof(struct pnp_bios_node);
169 seq_write(m, node->data, len);
170 kfree(node);
171 return 0;
172}
173
174static int pnpbios_proc_open(struct inode *inode, struct file *file)
175{
176 return single_open(file, pnpbios_proc_show, pde_data(inode));
177}
178
179static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
180 size_t count, loff_t *pos)
181{
182 void *data = pde_data(file_inode(file));
183 struct pnp_bios_node *node;
184 int boot = (long)data >> 8;
185 u8 nodenum = (long)data;
186 int ret = count;
187
188 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
189 if (!node)
190 return -ENOMEM;
191 if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
192 ret = -EIO;
193 goto out;
194 }
195 if (count != node->size - sizeof(struct pnp_bios_node)) {
196 ret = -EINVAL;
197 goto out;
198 }
199 if (copy_from_user(node->data, buf, count)) {
200 ret = -EFAULT;
201 goto out;
202 }
203 if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
204 ret = -EINVAL;
205 goto out;
206 }
207 ret = count;
208out:
209 kfree(node);
210 return ret;
211}
212
213static const struct proc_ops pnpbios_proc_ops = {
214 .proc_open = pnpbios_proc_open,
215 .proc_read = seq_read,
216 .proc_lseek = seq_lseek,
217 .proc_release = single_release,
218 .proc_write = pnpbios_proc_write,
219};
220
221int pnpbios_interface_attach_device(struct pnp_bios_node *node)
222{
223 char name[3];
224
225 sprintf(name, "%02x", node->handle);
226
227 if (!proc_pnp)
228 return -EIO;
229 if (!pnpbios_dont_use_current_config) {
230 proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_ops,
231 (void *)(long)(node->handle));
232 }
233
234 if (!proc_pnp_boot)
235 return -EIO;
236 if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_ops,
237 (void *)(long)(node->handle + 0x100)))
238 return 0;
239 return -EIO;
240}
241
242/*
243 * When this is called, pnpbios functions are assumed to
244 * work and the pnpbios_dont_use_current_config flag
245 * should already have been set to the appropriate value
246 */
247int __init pnpbios_proc_init(void)
248{
249 proc_pnp = proc_mkdir("bus/pnp", NULL);
250 if (!proc_pnp)
251 return -EIO;
252 proc_pnp_boot = proc_mkdir("boot", proc_pnp);
253 if (!proc_pnp_boot)
254 return -EIO;
255 proc_create_single("devices", 0, proc_pnp, pnp_devices_proc_show);
256 proc_create_single("configuration_info", 0, proc_pnp,
257 pnpconfig_proc_show);
258 proc_create_single("escd_info", 0, proc_pnp, escd_info_proc_show);
259 proc_create_single("escd", S_IRUSR, proc_pnp, escd_proc_show);
260 proc_create_single("legacy_device_resources", 0, proc_pnp,
261 pnp_legacyres_proc_show);
262 return 0;
263}
264
265void __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("bus/pnp", NULL);
286}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * /proc/bus/pnp interface for Plug and Play devices
4 *
5 * Written by David Hinds, dahinds@users.sourceforge.net
6 * Modified by Thomas Hood
7 *
8 * The .../devices and .../<node> and .../boot/<node> files are
9 * utilized by the lspnp and setpnp utilities, supplied with the
10 * pcmcia-cs package.
11 * http://pcmcia-cs.sourceforge.net
12 *
13 * The .../escd file is utilized by the lsescd utility written by
14 * Gunther Mayer.
15 *
16 * The .../legacy_device_resources file is not used yet.
17 *
18 * The other files are human-readable.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25#include <linux/proc_fs.h>
26#include <linux/pnp.h>
27#include <linux/seq_file.h>
28#include <linux/init.h>
29
30#include <linux/uaccess.h>
31
32#include "pnpbios.h"
33
34static struct proc_dir_entry *proc_pnp = NULL;
35static struct proc_dir_entry *proc_pnp_boot = NULL;
36
37static int pnpconfig_proc_show(struct seq_file *m, void *v)
38{
39 struct pnp_isa_config_struc pnps;
40
41 if (pnp_bios_isapnp_config(&pnps))
42 return -EIO;
43 seq_printf(m, "structure_revision %d\n"
44 "number_of_CSNs %d\n"
45 "ISA_read_data_port 0x%x\n",
46 pnps.revision, pnps.no_csns, pnps.isa_rd_data_port);
47 return 0;
48}
49
50static int pnpconfig_proc_open(struct inode *inode, struct file *file)
51{
52 return single_open(file, pnpconfig_proc_show, NULL);
53}
54
55static const struct file_operations pnpconfig_proc_fops = {
56 .owner = THIS_MODULE,
57 .open = pnpconfig_proc_open,
58 .read = seq_read,
59 .llseek = seq_lseek,
60 .release = single_release,
61};
62
63static int escd_info_proc_show(struct seq_file *m, void *v)
64{
65 struct escd_info_struc escd;
66
67 if (pnp_bios_escd_info(&escd))
68 return -EIO;
69 seq_printf(m, "min_ESCD_write_size %d\n"
70 "ESCD_size %d\n"
71 "NVRAM_base 0x%x\n",
72 escd.min_escd_write_size,
73 escd.escd_size, escd.nv_storage_base);
74 return 0;
75}
76
77static int escd_info_proc_open(struct inode *inode, struct file *file)
78{
79 return single_open(file, escd_info_proc_show, NULL);
80}
81
82static const struct file_operations escd_info_proc_fops = {
83 .owner = THIS_MODULE,
84 .open = escd_info_proc_open,
85 .read = seq_read,
86 .llseek = seq_lseek,
87 .release = single_release,
88};
89
90#define MAX_SANE_ESCD_SIZE (32*1024)
91static int escd_proc_show(struct seq_file *m, void *v)
92{
93 struct escd_info_struc escd;
94 char *tmpbuf;
95 int escd_size;
96
97 if (pnp_bios_escd_info(&escd))
98 return -EIO;
99
100 /* sanity check */
101 if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
102 printk(KERN_ERR
103 "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
104 return -EFBIG;
105 }
106
107 tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
108 if (!tmpbuf)
109 return -ENOMEM;
110
111 if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
112 kfree(tmpbuf);
113 return -EIO;
114 }
115
116 escd_size =
117 (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
118
119 /* sanity check */
120 if (escd_size > MAX_SANE_ESCD_SIZE) {
121 printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
122 " BIOS read_escd call is too great\n", __func__);
123 kfree(tmpbuf);
124 return -EFBIG;
125 }
126
127 seq_write(m, tmpbuf, escd_size);
128 kfree(tmpbuf);
129 return 0;
130}
131
132static int escd_proc_open(struct inode *inode, struct file *file)
133{
134 return single_open(file, escd_proc_show, NULL);
135}
136
137static const struct file_operations escd_proc_fops = {
138 .owner = THIS_MODULE,
139 .open = escd_proc_open,
140 .read = seq_read,
141 .llseek = seq_lseek,
142 .release = single_release,
143};
144
145static int pnp_legacyres_proc_show(struct seq_file *m, void *v)
146{
147 void *buf;
148
149 buf = kmalloc(65536, GFP_KERNEL);
150 if (!buf)
151 return -ENOMEM;
152 if (pnp_bios_get_stat_res(buf)) {
153 kfree(buf);
154 return -EIO;
155 }
156
157 seq_write(m, buf, 65536);
158 kfree(buf);
159 return 0;
160}
161
162static int pnp_legacyres_proc_open(struct inode *inode, struct file *file)
163{
164 return single_open(file, pnp_legacyres_proc_show, NULL);
165}
166
167static const struct file_operations pnp_legacyres_proc_fops = {
168 .owner = THIS_MODULE,
169 .open = pnp_legacyres_proc_open,
170 .read = seq_read,
171 .llseek = seq_lseek,
172 .release = single_release,
173};
174
175static int pnp_devices_proc_show(struct seq_file *m, void *v)
176{
177 struct pnp_bios_node *node;
178 u8 nodenum;
179
180 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
181 if (!node)
182 return -ENOMEM;
183
184 for (nodenum = 0; nodenum < 0xff;) {
185 u8 thisnodenum = nodenum;
186
187 if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
188 break;
189 seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
190 node->handle, node->eisa_id,
191 node->type_code, node->flags);
192 if (nodenum <= thisnodenum) {
193 printk(KERN_ERR
194 "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
195 "PnPBIOS: proc_read_devices:",
196 (unsigned int)nodenum,
197 (unsigned int)thisnodenum);
198 break;
199 }
200 }
201 kfree(node);
202 return 0;
203}
204
205static int pnp_devices_proc_open(struct inode *inode, struct file *file)
206{
207 return single_open(file, pnp_devices_proc_show, NULL);
208}
209
210static const struct file_operations pnp_devices_proc_fops = {
211 .owner = THIS_MODULE,
212 .open = pnp_devices_proc_open,
213 .read = seq_read,
214 .llseek = seq_lseek,
215 .release = single_release,
216};
217
218static int pnpbios_proc_show(struct seq_file *m, void *v)
219{
220 void *data = m->private;
221 struct pnp_bios_node *node;
222 int boot = (long)data >> 8;
223 u8 nodenum = (long)data;
224 int len;
225
226 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
227 if (!node)
228 return -ENOMEM;
229 if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
230 kfree(node);
231 return -EIO;
232 }
233 len = node->size - sizeof(struct pnp_bios_node);
234 seq_write(m, node->data, len);
235 kfree(node);
236 return 0;
237}
238
239static int pnpbios_proc_open(struct inode *inode, struct file *file)
240{
241 return single_open(file, pnpbios_proc_show, PDE_DATA(inode));
242}
243
244static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
245 size_t count, loff_t *pos)
246{
247 void *data = PDE_DATA(file_inode(file));
248 struct pnp_bios_node *node;
249 int boot = (long)data >> 8;
250 u8 nodenum = (long)data;
251 int ret = count;
252
253 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
254 if (!node)
255 return -ENOMEM;
256 if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
257 ret = -EIO;
258 goto out;
259 }
260 if (count != node->size - sizeof(struct pnp_bios_node)) {
261 ret = -EINVAL;
262 goto out;
263 }
264 if (copy_from_user(node->data, buf, count)) {
265 ret = -EFAULT;
266 goto out;
267 }
268 if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
269 ret = -EINVAL;
270 goto out;
271 }
272 ret = count;
273out:
274 kfree(node);
275 return ret;
276}
277
278static const struct file_operations pnpbios_proc_fops = {
279 .owner = THIS_MODULE,
280 .open = pnpbios_proc_open,
281 .read = seq_read,
282 .llseek = seq_lseek,
283 .release = single_release,
284 .write = pnpbios_proc_write,
285};
286
287int pnpbios_interface_attach_device(struct pnp_bios_node *node)
288{
289 char name[3];
290
291 sprintf(name, "%02x", node->handle);
292
293 if (!proc_pnp)
294 return -EIO;
295 if (!pnpbios_dont_use_current_config) {
296 proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops,
297 (void *)(long)(node->handle));
298 }
299
300 if (!proc_pnp_boot)
301 return -EIO;
302 if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops,
303 (void *)(long)(node->handle + 0x100)))
304 return 0;
305 return -EIO;
306}
307
308/*
309 * When this is called, pnpbios functions are assumed to
310 * work and the pnpbios_dont_use_current_config flag
311 * should already have been set to the appropriate value
312 */
313int __init pnpbios_proc_init(void)
314{
315 proc_pnp = proc_mkdir("bus/pnp", NULL);
316 if (!proc_pnp)
317 return -EIO;
318 proc_pnp_boot = proc_mkdir("boot", proc_pnp);
319 if (!proc_pnp_boot)
320 return -EIO;
321 proc_create("devices", 0, proc_pnp, &pnp_devices_proc_fops);
322 proc_create("configuration_info", 0, proc_pnp, &pnpconfig_proc_fops);
323 proc_create("escd_info", 0, proc_pnp, &escd_info_proc_fops);
324 proc_create("escd", S_IRUSR, proc_pnp, &escd_proc_fops);
325 proc_create("legacy_device_resources", 0, proc_pnp, &pnp_legacyres_proc_fops);
326
327 return 0;
328}
329
330void __exit pnpbios_proc_exit(void)
331{
332 int i;
333 char name[3];
334
335 if (!proc_pnp)
336 return;
337
338 for (i = 0; i < 0xff; i++) {
339 sprintf(name, "%02x", i);
340 if (!pnpbios_dont_use_current_config)
341 remove_proc_entry(name, proc_pnp);
342 remove_proc_entry(name, proc_pnp_boot);
343 }
344 remove_proc_entry("legacy_device_resources", proc_pnp);
345 remove_proc_entry("escd", proc_pnp);
346 remove_proc_entry("escd_info", proc_pnp);
347 remove_proc_entry("configuration_info", proc_pnp);
348 remove_proc_entry("devices", proc_pnp);
349 remove_proc_entry("boot", proc_pnp);
350 remove_proc_entry("bus/pnp", NULL);
351}