Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
| 16 | * The .../legacy_device_resources file is not used yet. |
| 17 | * |
| 18 | * The other files are human-readable. |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Bjorn Helgaas | dfd2e1b | 2008-04-28 16:34:42 -0600 | [diff] [blame] | 26 | #include <linux/pnp.h> |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/init.h> |
| 29 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include "pnpbios.h" |
| 33 | |
| 34 | static struct proc_dir_entry *proc_pnp = NULL; |
| 35 | static struct proc_dir_entry *proc_pnp_boot = NULL; |
| 36 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 37 | static int pnpconfig_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | struct pnp_isa_config_struc pnps; |
| 40 | |
| 41 | if (pnp_bios_isapnp_config(&pnps)) |
| 42 | return -EIO; |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 50 | static int escd_info_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | struct escd_info_struc escd; |
| 53 | |
| 54 | if (pnp_bios_escd_info(&escd)) |
| 55 | return -EIO; |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 56 | seq_printf(m, "min_ESCD_write_size %d\n" |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 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); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 61 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #define MAX_SANE_ESCD_SIZE (32*1024) |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 65 | static int escd_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | struct escd_info_struc escd; |
| 68 | char *tmpbuf; |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 69 | int escd_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | if (pnp_bios_escd_info(&escd)) |
| 72 | return -EIO; |
| 73 | |
| 74 | /* sanity check */ |
| 75 | if (escd.escd_size > MAX_SANE_ESCD_SIZE) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 76 | printk(KERN_ERR |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 77 | "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return -EFBIG; |
| 79 | } |
| 80 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 81 | tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 82 | if (!tmpbuf) |
| 83 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) { |
| 86 | kfree(tmpbuf); |
| 87 | return -EIO; |
| 88 | } |
| 89 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 90 | escd_size = |
| 91 | (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | /* sanity check */ |
| 94 | if (escd_size > MAX_SANE_ESCD_SIZE) { |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 95 | printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by" |
| 96 | " BIOS read_escd call is too great\n", __func__); |
Jesper Juhl | a8b0ac0 | 2007-10-16 23:26:56 -0700 | [diff] [blame] | 97 | kfree(tmpbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return -EFBIG; |
| 99 | } |
| 100 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 101 | seq_write(m, tmpbuf, escd_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | kfree(tmpbuf); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 103 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 106 | static 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 Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 123 | static int pnp_devices_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | struct pnp_bios_node *node; |
| 126 | u8 nodenum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 128 | node = kzalloc(node_info.max_node_size, GFP_KERNEL); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 129 | if (!node) |
| 130 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 132 | for (nodenum = 0; nodenum < 0xff;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | u8 thisnodenum = nodenum; |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node)) |
| 136 | break; |
Andy Shevchenko | ac2a509 | 2013-04-23 16:37:19 +0300 | [diff] [blame] | 137 | seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | node->handle, node->eisa_id, |
Andy Shevchenko | ac2a509 | 2013-04-23 16:37:19 +0300 | [diff] [blame] | 139 | node->type_code, node->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (nodenum <= thisnodenum) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | } |
| 149 | kfree(node); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 153 | static int pnpbios_proc_show(struct seq_file *m, void *v) |
| 154 | { |
| 155 | void *data = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct pnp_bios_node *node; |
| 157 | int boot = (long)data >> 8; |
| 158 | u8 nodenum = (long)data; |
| 159 | int len; |
| 160 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 161 | node = kzalloc(node_info.max_node_size, GFP_KERNEL); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 162 | if (!node) |
| 163 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 169 | seq_write(m, node->data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | kfree(node); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 174 | static int pnpbios_proc_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 176 | return single_open(file, pnpbios_proc_show, PDE_DATA(inode)); |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf, |
| 180 | size_t count, loff_t *pos) |
| 181 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 182 | void *data = PDE_DATA(file_inode(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct pnp_bios_node *node; |
| 184 | int boot = (long)data >> 8; |
| 185 | u8 nodenum = (long)data; |
| 186 | int ret = count; |
| 187 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 188 | node = kzalloc(node_info.max_node_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 208 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | kfree(node); |
| 210 | return ret; |
| 211 | } |
| 212 | |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 213 | static 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 Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 222 | int pnpbios_interface_attach_device(struct pnp_bios_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | char name[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | sprintf(name, "%02x", node->handle); |
| 227 | |
| 228 | if (!proc_pnp) |
| 229 | return -EIO; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 230 | if (!pnpbios_dont_use_current_config) { |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 231 | proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops, |
| 232 | (void *)(long)(node->handle)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (!proc_pnp_boot) |
| 236 | return -EIO; |
Alexey Dobriyan | 5116fa2b | 2009-12-15 16:46:47 -0800 | [diff] [blame] | 237 | if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops, |
| 238 | (void *)(long)(node->handle + 0x100))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 248 | int __init pnpbios_proc_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Alexey Dobriyan | 9c37066 | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 250 | proc_pnp = proc_mkdir("bus/pnp", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (!proc_pnp) |
| 252 | return -EIO; |
| 253 | proc_pnp_boot = proc_mkdir("boot", proc_pnp); |
| 254 | if (!proc_pnp_boot) |
| 255 | return -EIO; |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 256 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | void __exit pnpbios_proc_exit(void) |
| 267 | { |
| 268 | int i; |
| 269 | char name[3]; |
| 270 | |
| 271 | if (!proc_pnp) |
| 272 | return; |
| 273 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 274 | for (i = 0; i < 0xff; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | sprintf(name, "%02x", i); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 276 | if (!pnpbios_dont_use_current_config) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 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 Dobriyan | 9c37066 | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 286 | remove_proc_entry("bus/pnp", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |