2 * drivers/pci/pci-sysfs.c
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>
11 * File attributes for PCI devices
13 * Modeled after usb's driverfs.c
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>
26 static int sysfs_initialized; /* = 0 */
28 /* show configuration fields */
29 #define pci_config_attr(field, format_string) \
31 field##_show(struct device *dev, char *buf) \
33 struct pci_dev *pdev; \
35 pdev = to_pci_dev (dev); \
36 return sprintf (buf, format_string, pdev->field); \
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");
46 static ssize_t local_cpus_show(struct device *dev, char *buf)
48 cpumask_t mask = pcibus_to_cpumask(to_pci_dev(dev)->bus->number);
49 int len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
56 resource_show(struct device * dev, char * buf)
58 struct pci_dev * pci_dev = to_pci_dev(dev);
63 if (pci_dev->subordinate)
64 max = DEVICE_COUNT_RESOURCE;
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));
75 struct device_attribute pci_dev_attrs[] = {
79 __ATTR_RO(subsystem_vendor),
80 __ATTR_RO(subsystem_device),
83 __ATTR_RO(local_cpus),
88 pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
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;
94 /* Several chips lock up trying to read undefined config space */
95 if (capable(CAP_SYS_ADMIN)) {
97 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
103 if (off + count > size) {
112 pci_read_config_byte(dev, off, &val);
113 buf[off - init_off] = 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;
132 pci_read_config_byte(dev, off, &val);
133 buf[off - init_off] = val;
142 pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
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;
148 if (off > dev->cfg_size)
150 if (off + count > dev->cfg_size) {
151 size = dev->cfg_size - off;
156 pci_write_config_byte(dev, off, buf[off - init_off]);
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);
173 pci_write_config_byte(dev, off, buf[off - init_off]);
182 * pci_write_rom - used to enable access to the PCI ROM display
183 * @kobj: kernel object handle
186 * @count: number of byte in input
188 * writing anything except 0 enables it
191 pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
193 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
195 if ((off == 0) && (*buf == '0') && (count == 2))
196 pdev->rom_attr_enabled = 0;
198 pdev->rom_attr_enabled = 1;
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
208 * @count: number of bytes to read
210 * Put @count bytes starting at @off into @buf from the ROM in the PCI
211 * device corresponding to @kobj.
214 pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
216 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
220 if (!pdev->rom_attr_enabled)
223 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
230 if (off + count > size)
233 memcpy_fromio(buf, rom + off, count);
235 pci_unmap_rom(pdev, rom);
240 static struct bin_attribute pci_config_attr = {
243 .mode = S_IRUGO | S_IWUSR,
244 .owner = THIS_MODULE,
247 .read = pci_read_config,
248 .write = pci_write_config,
251 static struct bin_attribute pcie_config_attr = {
254 .mode = S_IRUGO | S_IWUSR,
255 .owner = THIS_MODULE,
258 .read = pci_read_config,
259 .write = pci_write_config,
262 int pci_create_sysfs_dev_files (struct pci_dev *pdev)
264 if (!sysfs_initialized)
267 if (pdev->cfg_size < 4096)
268 sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
270 sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
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;
276 rom_attr = kmalloc(sizeof(*rom_attr), GFP_ATOMIC);
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);
288 /* add platform-specific attributes */
289 pcibios_add_platform_entries(pdev);
295 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
296 * @pdev: device whose entries we should free
298 * Cleanup when @pdev is removed from sysfs.
300 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
302 if (pdev->cfg_size < 4096)
303 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
305 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
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);
315 static int __init pci_sysfs_init(void)
317 struct pci_dev *pdev = NULL;
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);
326 __initcall(pci_sysfs_init);