Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[sfrench/cifs-2.6.git] / drivers / pnp / isapnp / proc.c
1 /*
2  *  ISA Plug & Play support
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *   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.
18  */
19
20 #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
27 extern struct pnp_protocol isapnp_protocol;
28
29 static struct proc_dir_entry *isapnp_proc_bus_dir = NULL;
30
31 static loff_t isapnp_proc_bus_lseek(struct file *file, loff_t off, int whence)
32 {
33         loff_t new = -1;
34
35         lock_kernel();
36         switch (whence) {
37         case 0:
38                 new = off;
39                 break;
40         case 1:
41                 new = file->f_pos + off;
42                 break;
43         case 2:
44                 new = 256 + off;
45                 break;
46         }
47         if (new < 0 || new > 256) {
48                 unlock_kernel();
49                 return -EINVAL;
50         }
51         unlock_kernel();
52         return (file->f_pos = new);
53 }
54
55 static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,
56                                     size_t nbytes, loff_t * ppos)
57 {
58         struct inode *ino = file->f_path.dentry->d_inode;
59         struct proc_dir_entry *dp = PDE(ino);
60         struct pnp_dev *dev = dp->data;
61         int pos = *ppos;
62         int cnt, size = 256;
63
64         if (pos >= size)
65                 return 0;
66         if (nbytes >= size)
67                 nbytes = size;
68         if (pos + nbytes > size)
69                 nbytes = size - pos;
70         cnt = nbytes;
71
72         if (!access_ok(VERIFY_WRITE, buf, cnt))
73                 return -EINVAL;
74
75         isapnp_cfg_begin(dev->card->number, dev->number);
76         for (; pos < 256 && cnt > 0; pos++, buf++, cnt--) {
77                 unsigned char val;
78                 val = isapnp_read_byte(pos);
79                 __put_user(val, buf);
80         }
81         isapnp_cfg_end();
82
83         *ppos = pos;
84         return nbytes;
85 }
86
87 static const struct file_operations isapnp_proc_bus_file_operations = {
88         .llseek = isapnp_proc_bus_lseek,
89         .read = isapnp_proc_bus_read,
90 };
91
92 static int isapnp_proc_attach_device(struct pnp_dev *dev)
93 {
94         struct pnp_card *bus = dev->card;
95         struct proc_dir_entry *de, *e;
96         char name[16];
97
98         if (!(de = bus->procdir)) {
99                 sprintf(name, "%02x", bus->number);
100                 de = bus->procdir = proc_mkdir(name, isapnp_proc_bus_dir);
101                 if (!de)
102                         return -ENOMEM;
103         }
104         sprintf(name, "%02x", dev->number);
105         e = dev->procent = create_proc_entry(name, S_IFREG | S_IRUGO, de);
106         if (!e)
107                 return -ENOMEM;
108         e->proc_fops = &isapnp_proc_bus_file_operations;
109         e->owner = THIS_MODULE;
110         e->data = dev;
111         e->size = 256;
112         return 0;
113 }
114
115 #ifdef MODULE
116 static int __exit isapnp_proc_detach_device(struct pnp_dev *dev)
117 {
118         struct pnp_card *bus = dev->card;
119         struct proc_dir_entry *de;
120         char name[16];
121
122         if (!(de = bus->procdir))
123                 return -EINVAL;
124         sprintf(name, "%02x", dev->number);
125         remove_proc_entry(name, de);
126         return 0;
127 }
128
129 static int __exit isapnp_proc_detach_bus(struct pnp_card *bus)
130 {
131         struct proc_dir_entry *de;
132         char name[16];
133
134         if (!(de = bus->procdir))
135                 return -EINVAL;
136         sprintf(name, "%02x", bus->number);
137         remove_proc_entry(name, isapnp_proc_bus_dir);
138         return 0;
139 }
140 #endif                          /* MODULE */
141
142 int __init isapnp_proc_init(void)
143 {
144         struct pnp_dev *dev;
145
146         isapnp_proc_bus_dir = proc_mkdir("isapnp", proc_bus);
147         protocol_for_each_dev(&isapnp_protocol, dev) {
148                 isapnp_proc_attach_device(dev);
149         }
150         return 0;
151 }
152
153 #ifdef MODULE
154 int __exit isapnp_proc_done(void)
155 {
156         struct pnp_dev *dev;
157         struct pnp_bus *card;
158
159         isapnp_for_each_dev(dev) {
160                 isapnp_proc_detach_device(dev);
161         }
162         isapnp_for_each_card(card) {
163                 isapnp_proc_detach_bus(card);
164         }
165         if (isapnp_proc_bus_dir)
166                 remove_proc_entry("isapnp", proc_bus);
167         return 0;
168 }
169 #endif                          /* MODULE */