blob: 7d4aca7948dd1f95a389d4f55498fb132bd7e2e4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * The .../legacy_device_resources file is not used yet.
17 *
18 * The other files are human-readable.
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Bjorn Helgaasdfd2e1b2008-04-28 16:34:42 -060026#include <linux/pnp.h>
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "pnpbios.h"
33
34static struct proc_dir_entry *proc_pnp = NULL;
35static struct proc_dir_entry *proc_pnp_boot = NULL;
36
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080037static int pnpconfig_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct pnp_isa_config_struc pnps;
40
41 if (pnp_bios_isapnp_config(&pnps))
42 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080043 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080050static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct escd_info_struc escd;
66
67 if (pnp_bios_escd_info(&escd))
68 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080069 seq_printf(m, "min_ESCD_write_size %d\n"
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070070 "ESCD_size %d\n"
71 "NVRAM_base 0x%x\n",
72 escd.min_escd_write_size,
73 escd.escd_size, escd.nv_storage_base);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080077static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define MAX_SANE_ESCD_SIZE (32*1024)
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080091static int escd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct escd_info_struc escd;
94 char *tmpbuf;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080095 int escd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (pnp_bios_escd_info(&escd))
98 return -EIO;
99
100 /* sanity check */
101 if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700102 printk(KERN_ERR
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800103 "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EFBIG;
105 }
106
Robert P. J. Daycd861282006-12-13 00:34:52 -0800107 tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700108 if (!tmpbuf)
109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
112 kfree(tmpbuf);
113 return -EIO;
114 }
115
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700116 escd_size =
117 (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* sanity check */
120 if (escd_size > MAX_SANE_ESCD_SIZE) {
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800121 printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
122 " BIOS read_escd call is too great\n", __func__);
Jesper Juhla8b0ac02007-10-16 23:26:56 -0700123 kfree(tmpbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EFBIG;
125 }
126
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800127 seq_write(m, tmpbuf, escd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 kfree(tmpbuf);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800132static int escd_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800134 return single_open(file, escd_proc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800137static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct pnp_bios_node *node;
178 u8 nodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Robert P. J. Daycd861282006-12-13 00:34:52 -0800180 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700181 if (!node)
182 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800184 for (nodenum = 0; nodenum < 0xff;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 u8 thisnodenum = nodenum;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
188 break;
Andy Shevchenkoac2a5092013-04-23 16:37:19 +0300189 seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 node->handle, node->eisa_id,
Andy Shevchenkoac2a5092013-04-23 16:37:19 +0300191 node->type_code, node->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (nodenum <= thisnodenum) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700193 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 }
200 }
201 kfree(node);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800205static int pnp_devices_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800207 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct pnp_bios_node *node;
222 int boot = (long)data >> 8;
223 u8 nodenum = (long)data;
224 int len;
225
Robert P. J. Daycd861282006-12-13 00:34:52 -0800226 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700227 if (!node)
228 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800234 seq_write(m, node->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 kfree(node);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800239static int pnpbios_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Al Virod9dda782013-03-31 18:16:14 -0400241 return single_open(file, pnpbios_proc_show, PDE_DATA(inode));
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800242}
243
244static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
245 size_t count, loff_t *pos)
246{
Al Virod9dda782013-03-31 18:16:14 -0400247 void *data = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct pnp_bios_node *node;
249 int boot = (long)data >> 8;
250 u8 nodenum = (long)data;
251 int ret = count;
252
Robert P. J. Daycd861282006-12-13 00:34:52 -0800253 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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;
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600273out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 kfree(node);
275 return ret;
276}
277
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800278static 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700287int pnpbios_interface_attach_device(struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 char name[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 sprintf(name, "%02x", node->handle);
292
293 if (!proc_pnp)
294 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700295 if (!pnpbios_dont_use_current_config) {
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800296 proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops,
297 (void *)(long)(node->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 if (!proc_pnp_boot)
301 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800302 if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops,
303 (void *)(long)(node->handle + 0x100)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700313int __init pnpbios_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700315 proc_pnp = proc_mkdir("bus/pnp", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!proc_pnp)
317 return -EIO;
318 proc_pnp_boot = proc_mkdir("boot", proc_pnp);
319 if (!proc_pnp_boot)
320 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800321 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700338 for (i = 0; i < 0xff; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 sprintf(name, "%02x", i);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700340 if (!pnpbios_dont_use_current_config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 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);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700350 remove_proc_entry("bus/pnp", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}