blob: 55ae72a2818bbfa79230bea808dfd86c4bdfb2e1 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ISA Plug & Play support
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/isapnp.h>
9#include <linux/proc_fs.h>
10#include <linux/init.h>
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070011#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13extern struct pnp_protocol isapnp_protocol;
14
15static struct proc_dir_entry *isapnp_proc_bus_dir = NULL;
16
17static loff_t isapnp_proc_bus_lseek(struct file *file, loff_t off, int whence)
18{
Al Viroc09ed2a2013-06-23 12:09:11 +040019 return fixed_size_llseek(file, off, whence, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020}
21
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070022static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,
23 size_t nbytes, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Muchun Song359745d2022-01-21 22:14:23 -080025 struct pnp_dev *dev = pde_data(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int pos = *ppos;
27 int cnt, size = 256;
28
29 if (pos >= size)
30 return 0;
31 if (nbytes >= size)
32 nbytes = size;
33 if (pos + nbytes > size)
34 nbytes = size - pos;
35 cnt = nbytes;
36
Linus Torvalds96d4f262019-01-03 18:57:57 -080037 if (!access_ok(buf, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return -EINVAL;
39
40 isapnp_cfg_begin(dev->card->number, dev->number);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070041 for (; pos < 256 && cnt > 0; pos++, buf++, cnt--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 unsigned char val;
43 val = isapnp_read_byte(pos);
44 __put_user(val, buf);
45 }
46 isapnp_cfg_end();
47
48 *ppos = pos;
49 return nbytes;
50}
51
Alexey Dobriyan97a32532020-02-03 17:37:17 -080052static const struct proc_ops isapnp_proc_bus_proc_ops = {
53 .proc_lseek = isapnp_proc_bus_lseek,
54 .proc_read = isapnp_proc_bus_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57static int isapnp_proc_attach_device(struct pnp_dev *dev)
58{
59 struct pnp_card *bus = dev->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 char name[16];
61
Anupama K Patildaadabf2021-04-29 01:09:01 +053062 if (!bus->procdir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 sprintf(name, "%02x", bus->number);
Anupama K Patildaadabf2021-04-29 01:09:01 +053064 bus->procdir = proc_mkdir(name, isapnp_proc_bus_dir);
65 if (!bus->procdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -ENOMEM;
67 }
68 sprintf(name, "%02x", dev->number);
Anupama K Patildaadabf2021-04-29 01:09:01 +053069 dev->procent = proc_create_data(name, S_IFREG | S_IRUGO, bus->procdir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -080070 &isapnp_proc_bus_proc_ops, dev);
Anupama K Patildaadabf2021-04-29 01:09:01 +053071 if (!dev->procent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -ENOMEM;
Anupama K Patildaadabf2021-04-29 01:09:01 +053073 proc_set_size(dev->procent, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077int __init isapnp_proc_init(void)
78{
79 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070080
Alexey Dobriyan9c370662008-04-29 01:01:41 -070081 isapnp_proc_bus_dir = proc_mkdir("bus/isapnp", NULL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070082 protocol_for_each_dev(&isapnp_protocol, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 isapnp_proc_attach_device(dev);
84 }
85 return 0;
86}