kernel.org linux-2.6.10
[linux-2.6.git] / drivers / pci / pci-sysfs.c
1 /*
2  * drivers/pci/pci-sysfs.c
3  *
4  * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5  * (C) Copyright 2002-2004 IBM Corp.
6  * (C) Copyright 2003 Matthew Wilcox
7  * (C) Copyright 2003 Hewlett-Packard
8  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10  *
11  * File attributes for PCI devices
12  *
13  * Modeled after usb's driverfs.c 
14  *
15  */
16
17
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/stat.h>
22 #include <linux/topology.h>
23
24 #include "pci.h"
25
26 static int sysfs_initialized;   /* = 0 */
27
28 /* show configuration fields */
29 #define pci_config_attr(field, format_string)                           \
30 static ssize_t                                                          \
31 field##_show(struct device *dev, char *buf)                             \
32 {                                                                       \
33         struct pci_dev *pdev;                                           \
34                                                                         \
35         pdev = to_pci_dev (dev);                                        \
36         return sprintf (buf, format_string, pdev->field);               \
37 }
38
39 pci_config_attr(vendor, "0x%04x\n");
40 pci_config_attr(device, "0x%04x\n");
41 pci_config_attr(subsystem_vendor, "0x%04x\n");
42 pci_config_attr(subsystem_device, "0x%04x\n");
43 pci_config_attr(class, "0x%06x\n");
44 pci_config_attr(irq, "%u\n");
45
46 static ssize_t local_cpus_show(struct device *dev, char *buf)
47 {               
48         cpumask_t mask = pcibus_to_cpumask(to_pci_dev(dev)->bus->number);
49         int len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
50         strcat(buf,"\n"); 
51         return 1+len;
52 }
53
54 /* show resources */
55 static ssize_t
56 resource_show(struct device * dev, char * buf)
57 {
58         struct pci_dev * pci_dev = to_pci_dev(dev);
59         char * str = buf;
60         int i;
61         int max = 7;
62
63         if (pci_dev->subordinate)
64                 max = DEVICE_COUNT_RESOURCE;
65
66         for (i = 0; i < max; i++) {
67                 str += sprintf(str,"0x%016lx 0x%016lx 0x%016lx\n",
68                                pci_resource_start(pci_dev,i),
69                                pci_resource_end(pci_dev,i),
70                                pci_resource_flags(pci_dev,i));
71         }
72         return (str - buf);
73 }
74
75 struct device_attribute pci_dev_attrs[] = {
76         __ATTR_RO(resource),
77         __ATTR_RO(vendor),
78         __ATTR_RO(device),
79         __ATTR_RO(subsystem_vendor),
80         __ATTR_RO(subsystem_device),
81         __ATTR_RO(class),
82         __ATTR_RO(irq),
83         __ATTR_RO(local_cpus),
84         __ATTR_NULL,
85 };
86
87 static ssize_t
88 pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
89 {
90         struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
91         unsigned int size = 64;
92         loff_t init_off = off;
93
94         /* Several chips lock up trying to read undefined config space */
95         if (capable(CAP_SYS_ADMIN)) {
96                 size = dev->cfg_size;
97         } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
98                 size = 128;
99         }
100
101         if (off > size)
102                 return 0;
103         if (off + count > size) {
104                 size -= off;
105                 count = size;
106         } else {
107                 size = count;
108         }
109
110         while (off & 3) {
111                 unsigned char val;
112                 pci_read_config_byte(dev, off, &val);
113                 buf[off - init_off] = val;
114                 off++;
115                 if (--size == 0)
116                         break;
117         }
118
119         while (size > 3) {
120                 unsigned int val;
121                 pci_read_config_dword(dev, off, &val);
122                 buf[off - init_off] = val & 0xff;
123                 buf[off - init_off + 1] = (val >> 8) & 0xff;
124                 buf[off - init_off + 2] = (val >> 16) & 0xff;
125                 buf[off - init_off + 3] = (val >> 24) & 0xff;
126                 off += 4;
127                 size -= 4;
128         }
129
130         while (size > 0) {
131                 unsigned char val;
132                 pci_read_config_byte(dev, off, &val);
133                 buf[off - init_off] = val;
134                 off++;
135                 --size;
136         }
137
138         return count;
139 }
140
141 static ssize_t
142 pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
143 {
144         struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
145         unsigned int size = count;
146         loff_t init_off = off;
147
148         if (off > dev->cfg_size)
149                 return 0;
150         if (off + count > dev->cfg_size) {
151                 size = dev->cfg_size - off;
152                 count = size;
153         }
154
155         while (off & 3) {
156                 pci_write_config_byte(dev, off, buf[off - init_off]);
157                 off++;
158                 if (--size == 0)
159                         break;
160         }
161
162         while (size > 3) {
163                 unsigned int val = buf[off - init_off];
164                 val |= (unsigned int) buf[off - init_off + 1] << 8;
165                 val |= (unsigned int) buf[off - init_off + 2] << 16;
166                 val |= (unsigned int) buf[off - init_off + 3] << 24;
167                 pci_write_config_dword(dev, off, val);
168                 off += 4;
169                 size -= 4;
170         }
171
172         while (size > 0) {
173                 pci_write_config_byte(dev, off, buf[off - init_off]);
174                 off++;
175                 --size;
176         }
177
178         return count;
179 }
180
181 /**
182  * pci_write_rom - used to enable access to the PCI ROM display
183  * @kobj: kernel object handle
184  * @buf: user input
185  * @off: file offset
186  * @count: number of byte in input
187  *
188  * writing anything except 0 enables it
189  */
190 static ssize_t
191 pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
192 {
193         struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
194
195         if ((off ==  0) && (*buf == '0') && (count == 2))
196                 pdev->rom_attr_enabled = 0;
197         else
198                 pdev->rom_attr_enabled = 1;
199
200         return count;
201 }
202
203 /**
204  * pci_read_rom - read a PCI ROM
205  * @kobj: kernel object handle
206  * @buf: where to put the data we read from the ROM
207  * @off: file offset
208  * @count: number of bytes to read
209  *
210  * Put @count bytes starting at @off into @buf from the ROM in the PCI
211  * device corresponding to @kobj.
212  */
213 static ssize_t
214 pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
215 {
216         struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
217         void __iomem *rom;
218         size_t size;
219
220         if (!pdev->rom_attr_enabled)
221                 return -EINVAL;
222         
223         rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
224         if (!rom)
225                 return 0;
226                 
227         if (off >= size)
228                 count = 0;
229         else {
230                 if (off + count > size)
231                         count = size - off;
232                 
233                 memcpy_fromio(buf, rom + off, count);
234         }
235         pci_unmap_rom(pdev, rom);
236                 
237         return count;
238 }
239
240 static struct bin_attribute pci_config_attr = {
241         .attr = {
242                 .name = "config",
243                 .mode = S_IRUGO | S_IWUSR,
244                 .owner = THIS_MODULE,
245         },
246         .size = 256,
247         .read = pci_read_config,
248         .write = pci_write_config,
249 };
250
251 static struct bin_attribute pcie_config_attr = {
252         .attr = {
253                 .name = "config",
254                 .mode = S_IRUGO | S_IWUSR,
255                 .owner = THIS_MODULE,
256         },
257         .size = 4096,
258         .read = pci_read_config,
259         .write = pci_write_config,
260 };
261
262 int pci_create_sysfs_dev_files (struct pci_dev *pdev)
263 {
264         if (!sysfs_initialized)
265                 return -EACCES;
266
267         if (pdev->cfg_size < 4096)
268                 sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
269         else
270                 sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
271
272         /* If the device has a ROM, try to expose it in sysfs. */
273         if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
274                 struct bin_attribute *rom_attr;
275                 
276                 rom_attr = kmalloc(sizeof(*rom_attr), GFP_ATOMIC);
277                 if (rom_attr) {
278                         pdev->rom_attr = rom_attr;
279                         rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
280                         rom_attr->attr.name = "rom";
281                         rom_attr->attr.mode = S_IRUSR;
282                         rom_attr->attr.owner = THIS_MODULE;
283                         rom_attr->read = pci_read_rom;
284                         rom_attr->write = pci_write_rom;
285                         sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
286                 }
287         }
288         /* add platform-specific attributes */
289         pcibios_add_platform_entries(pdev);
290         
291         return 0;
292 }
293
294 /**
295  * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
296  * @pdev: device whose entries we should free
297  *
298  * Cleanup when @pdev is removed from sysfs.
299  */
300 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
301 {
302         if (pdev->cfg_size < 4096)
303                 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
304         else
305                 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
306
307         if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
308                 if (pdev->rom_attr) {
309                         sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
310                         kfree(pdev->rom_attr);
311                 }
312         }
313 }
314
315 static int __init pci_sysfs_init(void)
316 {
317         struct pci_dev *pdev = NULL;
318         
319         sysfs_initialized = 1;
320         while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL)
321                 pci_create_sysfs_dev_files(pdev);
322
323         return 0;
324 }
325
326 __initcall(pci_sysfs_init);