Decouple BAR reading from pci device init, handle errors gracefully

Pcidev_init() now returns struct pci_device * instead of a BAR stored in
PCI config space. This allows for real error checking instead of having
exit(1) everywhere in pcidev.c.
Thanks to Niklas Söderlund for coming up with the original error
handling patch which was slightly modified and folded into this patch.
Move the declaration of struct pci_device in programmer.h before the
first user.

Corresponding to flashrom svn r1644.

Signed-off-by: Carl-Daniel Hailfinger <>
Acked-by: Stefan Tauner <>
diff --git a/ogp_spi.c b/ogp_spi.c
index 6fb1a77..0c09d6a 100644
--- a/ogp_spi.c
+++ b/ogp_spi.c
@@ -105,6 +105,7 @@
 int ogp_spi_init(void)
+	struct pci_dev *dev = NULL;
 	char *type;
 	type = extract_programmer_param("rom");
@@ -131,8 +132,11 @@
 	if (rget_io_perms())
 		return 1;
-	io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, ogp_spi);
+	dev = pcidev_init(ogp_spi, PCI_BASE_ADDRESS_0);
+	if (!dev)
+		return 1;
+	io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
 	ogp_spibar = physmap("OGP registers", io_base_addr, 4096);
 	if (register_shutdown(ogp_spi_shutdown, NULL))