LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2007, 11:26 AM   #1
bramsey123
LQ Newbie
 
Registered: Aug 2007
Posts: 7

Rep: Reputation: 0
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.
 
Old 09-29-2007, 03:36 PM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You probably don't need to probe a PCI device; that is more for old ISA things. Look into pci_find_device().

To access the information outside, you could (for instance) printk it to the log file, or you could set up an entry in /proc or /sys for it. Look into create_proc_entry().

I usually will use a global variable for a global piece of information, such as the IRQ number. In fact, if my hardware permits interrupts to be disabled, then I will design my driver to enable/disable interrupts. In this case, I use the IRQ number value itself to determine whether interrupts are enabled; NULL if disabled, and containing a value if enabled. Using a global for this is not a problem; the IRQ is associated with the specific hardware and won't change.

For multiple instances of the hardware, replace the IRQ number with a pointer to a hardware configuration structure, then build a linked list to provide an instance of the structure for each piece of hardware that is present.

Last edited by jiml8; 09-29-2007 at 03:43 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom PCI driver problems (acess from user-space) - Ubuntu 6.06 / Linux 2.6 kalen Linux - Kernel 0 11-09-2006 06:11 AM
User-Space Routing Info cophy Linux - Networking 1 07-23-2005 10:35 AM
where is info on free disk space stored (/proc?) eigenyeugen Linux - General 3 01-30-2005 09:45 AM
Gpio Twiddling In Embedded Linux? or How to access PCI memory space regs? jds-linux Programming 1 07-11-2004 02:17 AM
Accessing PCI memory space HTS Programming 7 04-30-2004 10:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration