Scan PCI devices from Linux Kernel

I had a requirement to scan the existing PCI devices connected on a system from Linux kernel. I found the code to iterate the existing PCI devices from the fakephp kernel module. Here it is:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/device.h>
#include <linux/slab.h>

static int __init init_scan(void)
{
	struct pci_dev *pdev = NULL;

	printk("Loading scan\n");

	/* Add existing devices */
	for_each_pci_dev(pdev) {
		printk("vendor:0x%08x dev:0x%08x", pdev->vendor, pdev->device);
		if (pdev->driver && pdev->driver->name)
			printk("     name :%s", pdev->driver->name);
		printk("\n");

	}

	return 0;
}
module_init(init_scan);

static void __exit remove_scan(void)
{
	printk("Unloading scan\n");
}
module_exit(remove_scan);

MODULE_LICENSE("GPL");
Advertisements

2 thoughts on “Scan PCI devices from Linux Kernel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s