Skip to content

Commit 527139d

Browse files
kwilczynskibjorn-helgaas
authored andcommitted
PCI/sysfs: Convert "rom" to static attribute
The "rom" sysfs attribute allows access to the PCI Option ROM. Previously it was dynamically created either by pci_bus_add_device() or the pci_sysfs_init() initcall, but since it doesn't need to be created or removed dynamically, we can use a static attribute so the device model takes care of addition and removal automatically. Convert "rom" to a static attribute and use the .is_bin_visible() callback to set the correct object size based on the ROM size. Remove "rom_attr" from the struct pci_dev since it is no longer needed. This attribute was added in the pre-git era by https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/pci/pci-sysfs.c?id=f6d553444da2 [bhelgaas: commit log] Suggested-by: Oliver O'Halloran <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Krzysztof Wilczyński <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]>
1 parent e1d3f32 commit 527139d

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

drivers/pci/pci-sysfs.c

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,33 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
13061306

13071307
return count;
13081308
}
1309+
static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
1310+
1311+
static struct bin_attribute *pci_dev_rom_attrs[] = {
1312+
&bin_attr_rom,
1313+
NULL,
1314+
};
1315+
1316+
static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
1317+
struct bin_attribute *a, int n)
1318+
{
1319+
struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1320+
size_t rom_size;
1321+
1322+
/* If the device has a ROM, try to expose it in sysfs. */
1323+
rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1324+
if (!rom_size)
1325+
return 0;
1326+
1327+
a->size = rom_size;
1328+
1329+
return a->attr.mode;
1330+
}
1331+
1332+
static const struct attribute_group pci_dev_rom_attr_group = {
1333+
.bin_attrs = pci_dev_rom_attrs,
1334+
.is_bin_visible = pci_dev_rom_attr_is_visible,
1335+
};
13091336

13101337
static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
13111338
const char *buf, size_t count)
@@ -1352,8 +1379,6 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev)
13521379
int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
13531380
{
13541381
int retval;
1355-
int rom_size;
1356-
struct bin_attribute *attr;
13571382

13581383
if (!sysfs_initialized)
13591384
return -EACCES;
@@ -1362,43 +1387,15 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
13621387
if (retval)
13631388
goto err;
13641389

1365-
/* If the device has a ROM, try to expose it in sysfs. */
1366-
rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1367-
if (rom_size) {
1368-
attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1369-
if (!attr) {
1370-
retval = -ENOMEM;
1371-
goto err_resource_files;
1372-
}
1373-
sysfs_bin_attr_init(attr);
1374-
attr->size = rom_size;
1375-
attr->attr.name = "rom";
1376-
attr->attr.mode = 0600;
1377-
attr->read = pci_read_rom;
1378-
attr->write = pci_write_rom;
1379-
retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1380-
if (retval) {
1381-
kfree(attr);
1382-
goto err_resource_files;
1383-
}
1384-
pdev->rom_attr = attr;
1385-
}
1386-
13871390
/* add sysfs entries for various capabilities */
13881391
retval = pci_create_capabilities_sysfs(pdev);
13891392
if (retval)
1390-
goto err_rom_file;
1393+
goto err_resource_files;
13911394

13921395
pci_create_firmware_label_files(pdev);
13931396

13941397
return 0;
13951398

1396-
err_rom_file:
1397-
if (pdev->rom_attr) {
1398-
sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1399-
kfree(pdev->rom_attr);
1400-
pdev->rom_attr = NULL;
1401-
}
14021399
err_resource_files:
14031400
pci_remove_resource_files(pdev);
14041401
err:
@@ -1427,13 +1424,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
14271424

14281425
pci_remove_capabilities_sysfs(pdev);
14291426
pci_remove_resource_files(pdev);
1430-
1431-
if (pdev->rom_attr) {
1432-
sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1433-
kfree(pdev->rom_attr);
1434-
pdev->rom_attr = NULL;
1435-
}
1436-
14371427
pci_remove_firmware_label_files(pdev);
14381428
}
14391429

@@ -1526,6 +1516,7 @@ static const struct attribute_group pci_dev_group = {
15261516
const struct attribute_group *pci_dev_groups[] = {
15271517
&pci_dev_group,
15281518
&pci_dev_config_attr_group,
1519+
&pci_dev_rom_attr_group,
15291520
NULL,
15301521
};
15311522

include/linux/pci.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ struct pci_dev {
458458

459459
u32 saved_config_space[16]; /* Config space saved at suspend time */
460460
struct hlist_head saved_cap_space;
461-
struct bin_attribute *rom_attr; /* Attribute descriptor for sysfs ROM entry */
462461
int rom_attr_enabled; /* Display of ROM attribute enabled? */
463462
struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
464463
struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */

0 commit comments

Comments
 (0)