Import 2.1.118
[davej-history.git] / drivers / pci / proc.c
blobd1a78fe53feb9364c6e395749b078daf8e33b746
1 /*
2 * $Id: proc.c,v 1.13 1998/05/12 07:36:07 mj Exp $
4 * Procfs interface for the PCI bus.
6 * Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 */
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/proc_fs.h>
14 #include <linux/init.h>
15 #include <asm/uaccess.h>
16 #include <asm/byteorder.h>
18 #define PCI_CFG_SPACE_SIZE 256
20 static loff_t
21 proc_bus_pci_lseek(struct file *file, loff_t off, int whence)
23 loff_t new;
25 switch (whence) {
26 case 0:
27 new = off;
28 break;
29 case 1:
30 new = file->f_pos + off;
31 break;
32 case 2:
33 new = PCI_CFG_SPACE_SIZE + off;
34 break;
35 default:
36 return -EINVAL;
38 if (new < 0 || new > PCI_CFG_SPACE_SIZE)
39 return -EINVAL;
40 return (file->f_pos = new);
43 static ssize_t
44 proc_bus_pci_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
46 struct inode *ino = file->f_dentry->d_inode;
47 struct proc_dir_entry *dp = ino->u.generic_ip;
48 struct pci_dev *dev = dp->data;
49 int pos = *ppos;
50 unsigned char bus = dev->bus->number;
51 unsigned char dfn = dev->devfn;
52 int cnt, size;
55 * Normal users can read only the standardized portion of the
56 * configuration space as several chips lock up when trying to read
57 * undefined locations (think of Intel PIIX4 as a typical example).
60 if (capable(CAP_SYS_ADMIN))
61 size = PCI_CFG_SPACE_SIZE;
62 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
63 size = 128;
64 else
65 size = 64;
67 if (pos >= size)
68 return 0;
69 if (nbytes >= size)
70 nbytes = size;
71 if (pos + nbytes > size)
72 nbytes = size - pos;
73 cnt = nbytes;
75 if (!access_ok(VERIFY_WRITE, buf, cnt))
76 return -EINVAL;
78 if ((pos & 1) && cnt) {
79 unsigned char val;
80 pcibios_read_config_byte(bus, dfn, pos, &val);
81 __put_user(val, buf);
82 buf++;
83 pos++;
84 cnt--;
87 if ((pos & 3) && cnt > 2) {
88 unsigned short val;
89 pcibios_read_config_word(bus, dfn, pos, &val);
90 __put_user(cpu_to_le16(val), (unsigned short *) buf);
91 buf += 2;
92 pos += 2;
93 cnt -= 2;
96 while (cnt >= 4) {
97 unsigned int val;
98 pcibios_read_config_dword(bus, dfn, pos, &val);
99 __put_user(cpu_to_le32(val), (unsigned int *) buf);
100 buf += 4;
101 pos += 4;
102 cnt -= 4;
105 if (cnt >= 2) {
106 unsigned short val;
107 pcibios_read_config_word(bus, dfn, pos, &val);
108 __put_user(cpu_to_le16(val), (unsigned short *) buf);
109 buf += 2;
110 pos += 2;
111 cnt -= 2;
114 if (cnt) {
115 unsigned char val;
116 pcibios_read_config_byte(bus, dfn, pos, &val);
117 __put_user(val, buf);
118 buf++;
119 pos++;
120 cnt--;
123 *ppos = pos;
124 return nbytes;
127 static ssize_t
128 proc_bus_pci_write(struct file *file, const char *buf, size_t nbytes, loff_t *ppos)
130 struct inode *ino = file->f_dentry->d_inode;
131 struct proc_dir_entry *dp = ino->u.generic_ip;
132 struct pci_dev *dev = dp->data;
133 int pos = *ppos;
134 unsigned char bus = dev->bus->number;
135 unsigned char dfn = dev->devfn;
136 int cnt;
138 if (pos >= PCI_CFG_SPACE_SIZE)
139 return 0;
140 if (nbytes >= PCI_CFG_SPACE_SIZE)
141 nbytes = PCI_CFG_SPACE_SIZE;
142 if (pos + nbytes > PCI_CFG_SPACE_SIZE)
143 nbytes = PCI_CFG_SPACE_SIZE - pos;
144 cnt = nbytes;
146 if (!access_ok(VERIFY_READ, buf, cnt))
147 return -EINVAL;
149 if ((pos & 1) && cnt) {
150 unsigned char val;
151 __get_user(val, buf);
152 pcibios_write_config_byte(bus, dfn, pos, val);
153 buf++;
154 pos++;
155 cnt--;
158 if ((pos & 3) && cnt > 2) {
159 unsigned short val;
160 __get_user(val, (unsigned short *) buf);
161 pcibios_write_config_word(bus, dfn, pos, le16_to_cpu(val));
162 buf += 2;
163 pos += 2;
164 cnt -= 2;
167 while (cnt >= 4) {
168 unsigned int val;
169 __get_user(val, (unsigned int *) buf);
170 pcibios_write_config_dword(bus, dfn, pos, le32_to_cpu(val));
171 buf += 4;
172 pos += 4;
173 cnt -= 4;
176 if (cnt >= 2) {
177 unsigned short val;
178 __get_user(val, (unsigned short *) buf);
179 pcibios_write_config_word(bus, dfn, pos, le16_to_cpu(val));
180 buf += 2;
181 pos += 2;
182 cnt -= 2;
185 if (cnt) {
186 unsigned char val;
187 __get_user(val, buf);
188 pcibios_write_config_byte(bus, dfn, pos, val);
189 buf++;
190 pos++;
191 cnt--;
194 *ppos = pos;
195 return nbytes;
198 static struct file_operations proc_bus_pci_operations = {
199 proc_bus_pci_lseek,
200 proc_bus_pci_read,
201 proc_bus_pci_write,
202 NULL, /* readdir */
203 NULL, /* poll */
204 NULL, /* ioctl */
205 NULL, /* mmap */
206 NULL, /* no special open code */
207 NULL, /* flush */
208 NULL, /* no special release code */
209 NULL /* can't fsync */
212 static struct inode_operations proc_bus_pci_inode_operations = {
213 &proc_bus_pci_operations, /* default base directory file-ops */
214 NULL, /* create */
215 NULL, /* lookup */
216 NULL, /* link */
217 NULL, /* unlink */
218 NULL, /* symlink */
219 NULL, /* mkdir */
220 NULL, /* rmdir */
221 NULL, /* mknod */
222 NULL, /* rename */
223 NULL, /* readlink */
224 NULL, /* follow_link */
225 NULL, /* readpage */
226 NULL, /* writepage */
227 NULL, /* bmap */
228 NULL, /* truncate */
229 NULL /* permission */
233 get_pci_dev_info(char *buf, char **start, off_t pos, int count, int wr)
235 struct pci_dev *dev = pci_devices;
236 off_t at = 0;
237 int len, i, cnt;
239 cnt = 0;
240 while (dev && count > cnt) {
241 len = sprintf(buf, "%02x%02x\t%04x%04x\t%x",
242 dev->bus->number,
243 dev->devfn,
244 dev->vendor,
245 dev->device,
246 dev->irq);
247 for(i=0; i<6; i++)
248 len += sprintf(buf+len,
249 #if BITS_PER_LONG == 32
250 "\t%08lx",
251 #else
252 "\t%016lx",
253 #endif
254 dev->base_address[i]);
255 len += sprintf(buf+len,
256 #if BITS_PER_LONG == 32
257 "\t%08lx",
258 #else
259 "\t%016lx",
260 #endif
261 dev->rom_address);
262 buf[len++] = '\n';
263 at += len;
264 if (at >= pos) {
265 if (!*start) {
266 *start = buf + (pos - (at - len));
267 cnt = at - pos;
268 } else
269 cnt += len;
270 buf += len;
272 dev = dev->next;
274 return (count > cnt) ? cnt : count;
277 static struct proc_dir_entry proc_pci_devices = {
278 PROC_BUS_PCI_DEVICES, 7, "devices",
279 S_IFREG | S_IRUGO, 1, 0, 0,
280 0, &proc_array_inode_operations,
281 get_pci_dev_info
284 static struct proc_dir_entry *proc_bus_pci_dir;
286 int pci_proc_attach_device(struct pci_dev *dev)
288 struct pci_bus *bus = dev->bus;
289 struct proc_dir_entry *de, *e;
290 char name[16];
292 if (!(de = bus->procdir)) {
293 sprintf(name, "%02x", bus->number);
294 de = bus->procdir = create_proc_entry(name, S_IFDIR, proc_bus_pci_dir);
295 if (!de)
296 return -ENOMEM;
298 sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
299 e = dev->procent = create_proc_entry(name, S_IFREG | S_IRUGO | S_IWUSR, de);
300 if (!e)
301 return -ENOMEM;
302 e->ops = &proc_bus_pci_inode_operations;
303 e->data = dev;
304 e->size = PCI_CFG_SPACE_SIZE;
305 return 0;
308 int pci_proc_detach_device(struct pci_dev *dev)
310 struct proc_dir_entry *e;
312 if ((e = dev->procent)) {
313 if (e->count)
314 return -EBUSY;
315 remove_proc_entry(e->name, dev->bus->procdir);
316 dev->procent = NULL;
318 return 0;
321 __initfunc(void proc_bus_pci_add(struct pci_bus *bus))
323 while (bus) {
324 struct pci_dev *dev;
326 for(dev = bus->devices; dev; dev = dev->sibling)
327 pci_proc_attach_device(dev);
328 if (bus->children)
329 proc_bus_pci_add(bus->children);
330 bus = bus->next;
334 __initfunc(void pci_proc_init(void))
336 if (!pci_present())
337 return;
338 proc_bus_pci_dir = create_proc_entry("pci", S_IFDIR, proc_bus);
339 proc_register(proc_bus_pci_dir, &proc_pci_devices);
340 proc_bus_pci_add(&pci_root);
342 #ifdef CONFIG_PCI_OLD_PROC
343 proc_old_pci_init();
344 #endif