blob: fe1c8f5d9af0c6e8ff55fb2d343d54708a99b24b [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 escd_info_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 struct escd_info_struc escd;
53
54 if (pnp_bios_escd_info(&escd))
55 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080056 seq_printf(m, "min_ESCD_write_size %d\n"
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070057 "ESCD_size %d\n"
58 "NVRAM_base 0x%x\n",
59 escd.min_escd_write_size,
60 escd.escd_size, escd.nv_storage_base);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64#define MAX_SANE_ESCD_SIZE (32*1024)
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080065static int escd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct escd_info_struc escd;
68 char *tmpbuf;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080069 int escd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (pnp_bios_escd_info(&escd))
72 return -EIO;
73
74 /* sanity check */
75 if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070076 printk(KERN_ERR
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080077 "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EFBIG;
79 }
80
Robert P. J. Daycd861282006-12-13 00:34:52 -080081 tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070082 if (!tmpbuf)
83 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
86 kfree(tmpbuf);
87 return -EIO;
88 }
89
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070090 escd_size =
91 (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* sanity check */
94 if (escd_size > MAX_SANE_ESCD_SIZE) {
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -080095 printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
96 " BIOS read_escd call is too great\n", __func__);
Jesper Juhla8b0ac02007-10-16 23:26:56 -070097 kfree(tmpbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EFBIG;
99 }
100
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800101 seq_write(m, tmpbuf, escd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 kfree(tmpbuf);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800106static 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
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800123static int pnp_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct pnp_bios_node *node;
126 u8 nodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Robert P. J. Daycd861282006-12-13 00:34:52 -0800128 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700129 if (!node)
130 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800132 for (nodenum = 0; nodenum < 0xff;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 u8 thisnodenum = nodenum;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
136 break;
Andy Shevchenkoac2a5092013-04-23 16:37:19 +0300137 seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 node->handle, node->eisa_id,
Andy Shevchenkoac2a5092013-04-23 16:37:19 +0300139 node->type_code, node->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (nodenum <= thisnodenum) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700141 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 break;
147 }
148 }
149 kfree(node);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800153static int pnpbios_proc_show(struct seq_file *m, void *v)
154{
155 void *data = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct pnp_bios_node *node;
157 int boot = (long)data >> 8;
158 u8 nodenum = (long)data;
159 int len;
160
Robert P. J. Daycd861282006-12-13 00:34:52 -0800161 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700162 if (!node)
163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800169 seq_write(m, node->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kfree(node);
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800174static int pnpbios_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Al Virod9dda782013-03-31 18:16:14 -0400176 return single_open(file, pnpbios_proc_show, PDE_DATA(inode));
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800177}
178
179static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
180 size_t count, loff_t *pos)
181{
Al Virod9dda782013-03-31 18:16:14 -0400182 void *data = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct pnp_bios_node *node;
184 int boot = (long)data >> 8;
185 u8 nodenum = (long)data;
186 int ret = count;
187
Robert P. J. Daycd861282006-12-13 00:34:52 -0800188 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 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;
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600208out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 kfree(node);
210 return ret;
211}
212
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800213static const struct file_operations pnpbios_proc_fops = {
214 .owner = THIS_MODULE,
215 .open = pnpbios_proc_open,
216 .read = seq_read,
217 .llseek = seq_lseek,
218 .release = single_release,
219 .write = pnpbios_proc_write,
220};
221
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700222int pnpbios_interface_attach_device(struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 char name[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 sprintf(name, "%02x", node->handle);
227
228 if (!proc_pnp)
229 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700230 if (!pnpbios_dont_use_current_config) {
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800231 proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops,
232 (void *)(long)(node->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 if (!proc_pnp_boot)
236 return -EIO;
Alexey Dobriyan5116fa2b2009-12-15 16:46:47 -0800237 if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops,
238 (void *)(long)(node->handle + 0x100)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700248int __init pnpbios_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700250 proc_pnp = proc_mkdir("bus/pnp", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!proc_pnp)
252 return -EIO;
253 proc_pnp_boot = proc_mkdir("boot", proc_pnp);
254 if (!proc_pnp_boot)
255 return -EIO;
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200256 proc_create_single("devices", 0, proc_pnp, pnp_devices_proc_show);
257 proc_create_single("configuration_info", 0, proc_pnp,
258 pnpconfig_proc_show);
259 proc_create_single("escd_info", 0, proc_pnp, escd_info_proc_show);
260 proc_create_single("escd", S_IRUSR, proc_pnp, escd_proc_show);
261 proc_create_single("legacy_device_resources", 0, proc_pnp,
262 pnp_legacyres_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
266void __exit pnpbios_proc_exit(void)
267{
268 int i;
269 char name[3];
270
271 if (!proc_pnp)
272 return;
273
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700274 for (i = 0; i < 0xff; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 sprintf(name, "%02x", i);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700276 if (!pnpbios_dont_use_current_config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 remove_proc_entry(name, proc_pnp);
278 remove_proc_entry(name, proc_pnp_boot);
279 }
280 remove_proc_entry("legacy_device_resources", proc_pnp);
281 remove_proc_entry("escd", proc_pnp);
282 remove_proc_entry("escd_info", proc_pnp);
283 remove_proc_entry("configuration_info", proc_pnp);
284 remove_proc_entry("devices", proc_pnp);
285 remove_proc_entry("boot", proc_pnp);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700286 remove_proc_entry("bus/pnp", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}