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/drkaiser.c b/drkaiser.c
index a6eca1c..b94d6dd 100644
--- a/drkaiser.c
+++ b/drkaiser.c
@@ -64,17 +64,20 @@
 int drkaiser_init(void)
+	struct pci_dev *dev = NULL;
 	uint32_t addr;
 	if (rget_io_perms())
 		return 1;
-	/* No need to check for errors, pcidev_init() will not return in case of errors. */
-	addr = pcidev_init(PCI_BASE_ADDRESS_2, drkaiser_pcidev);
+	dev = pcidev_init(drkaiser_pcidev, PCI_BASE_ADDRESS_2);
+	if (!dev)
+		return 1;
+	addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_2);
 	/* Write magic register to enable flash write. */
-	rpci_write_word(pcidev_dev, PCI_MAGIC_DRKAISER_ADDR,
 	/* Map 128kB flash memory window. */
 	drkaiser_bar = physmap("Dr. Kaiser PC-Waechter flash memory",