blob: e73ebefdf3e0930ba979b3a3a9b348dfe181d730 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ISA Plug & Play support
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/isapnp.h>
22#include <linux/proc_fs.h>
23#include <linux/init.h>
24#include <linux/smp_lock.h>
25#include <asm/uaccess.h>
26
27extern struct pnp_protocol isapnp_protocol;
28
29static struct proc_dir_entry *isapnp_proc_bus_dir = NULL;
30
31static loff_t isapnp_proc_bus_lseek(struct file *file, loff_t off, int whence)
32{
33 loff_t new = -1;
Arnd Bergmann6117d212010-06-05 13:28:09 +020034 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Arnd Bergmann6117d212010-06-05 13:28:09 +020036 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 switch (whence) {
38 case 0:
39 new = off;
40 break;
41 case 1:
42 new = file->f_pos + off;
43 break;
44 case 2:
45 new = 256 + off;
46 break;
47 }
Arnd Bergmann6117d212010-06-05 13:28:09 +020048 if (new < 0 || new > 256)
49 new = -EINVAL;
50 else
51 file->f_pos = new;
52 mutex_unlock(&inode->i_mutex);
53 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070056static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,
57 size_t nbytes, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Josef Sipekb23463b2006-12-08 02:37:13 -080059 struct inode *ino = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct proc_dir_entry *dp = PDE(ino);
61 struct pnp_dev *dev = dp->data;
62 int pos = *ppos;
63 int cnt, size = 256;
64
65 if (pos >= size)
66 return 0;
67 if (nbytes >= size)
68 nbytes = size;
69 if (pos + nbytes > size)
70 nbytes = size - pos;
71 cnt = nbytes;
72
73 if (!access_ok(VERIFY_WRITE, buf, cnt))
74 return -EINVAL;
75
76 isapnp_cfg_begin(dev->card->number, dev->number);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070077 for (; pos < 256 && cnt > 0; pos++, buf++, cnt--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned char val;
79 val = isapnp_read_byte(pos);
80 __put_user(val, buf);
81 }
82 isapnp_cfg_end();
83
84 *ppos = pos;
85 return nbytes;
86}
87
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070088static const struct file_operations isapnp_proc_bus_file_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -070089 .owner = THIS_MODULE,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070090 .llseek = isapnp_proc_bus_lseek,
91 .read = isapnp_proc_bus_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94static int isapnp_proc_attach_device(struct pnp_dev *dev)
95{
96 struct pnp_card *bus = dev->card;
97 struct proc_dir_entry *de, *e;
98 char name[16];
99
100 if (!(de = bus->procdir)) {
101 sprintf(name, "%02x", bus->number);
102 de = bus->procdir = proc_mkdir(name, isapnp_proc_bus_dir);
103 if (!de)
104 return -ENOMEM;
105 }
106 sprintf(name, "%02x", dev->number);
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700107 e = dev->procent = proc_create_data(name, S_IFREG | S_IRUGO, de,
108 &isapnp_proc_bus_file_operations, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!e)
110 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 e->size = 256;
112 return 0;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115int __init isapnp_proc_init(void)
116{
117 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700118
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700119 isapnp_proc_bus_dir = proc_mkdir("bus/isapnp", NULL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700120 protocol_for_each_dev(&isapnp_protocol, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 isapnp_proc_attach_device(dev);
122 }
123 return 0;
124}