PCI Driver (How to access info in config space)
Hello all,
I have written the basics of a PCI driver. Currently all the driver does is register the device. It assigns the IRQ and I/O and memeory space. Below is the code.
#include <linux/pci.h> /* PCI specific functions */
#include <linux/kernel.h> /* printk() */
#define PCI_VENDOR_ID_OXFORD 0x1415
#define PCI_DEVICE_ID_OXFORD_FUN0 0x9501 /* Device ID for PCI Function 0 UARTs */
#define PCI_DEVICE_ID_OXFORD_FUN1 0x9511 /* Device ID for PCI Function 1 8-bit bus */
#define PCI_MAJOR_NUM 61 /* Major Number */
static int dev_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
int ret;
u16 venid;
u32 devid;
ret = pci_enable_device(dev); /*Enables the device and assigns the interrupt line*/
if (ret < 0) return ret;
pci_read_config_word(dev, 0x00, &venid); /*Reads the first 16 bits from configuration space*/
printk("Vendor ID= %#0.4x ",venid); /*Outputs the frist 16 bits of the config space (Vendor ID) in hex*/
pci_read_config_dword(dev, 0x00, &devid); /*Reads the first 32 bits from configuration space*/
devid = devid >> 16; /*Shifts the 32bits to the right 16*/
printk("Device ID= %#0.4x\n",devid); /*Outputs the shifted bits (or the last 16 bits of the config space (Device ID)*/
ret = pci_request_regions(dev, "MyPCIDevice");
if (ret < 0)
{
pci_disable_device(dev);
return ret;
}
return 0;
}
static void dev_remove(struct pci_dev *dev)
{
pci_release_regions(dev);
pci_disable_device(dev);
}
/* Table of device ID's the drive is interested in */
static struct pci_device_id ids[] = {
{ PCI_DEVICE(PCI_VENDOR_ID_OXFORD, PCI_DEVICE_ID_OXFORD_FUN0) },
{ PCI_DEVICE(PCI_VENDOR_ID_OXFORD, PCI_DEVICE_ID_OXFORD_FUN1) },
{ 0, }//end of list
};
static struct pci_driver pci_driver = {
.name = "MyPCIDevice",
.id_table = ids,
.probe = dev_probe,
.remove = dev_remove,
};
static int __init pci_init(void)
{
printk("Inserting Module\n");
return pci_register_driver(&pci_driver);
}
static void __exit pci_exit(void)
{
printk("Removing Module\n");
pci_unregister_driver(&pci_driver);
}
module_init(pci_init);
module_exit(pci_exit);
MODULE_AUTHOR("ADDITECH");
MODULE_DESCRIPTION("PCI Oxford Board Driver");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DEVICE_TABLE(pci, ids);
My question is how do I access this information outside of the probe function. I could assign the IRQ number to a global variable. I obviously don't like this approach so I was hoping there is another way. My issue is I don't understand what "dev" is in the probe function or how to access it outside of that function. Any help would be awesome.
|