Jump to content

D Programming/RTAI/Accessing PCI hardware

From Wikibooks, open books for an open world

With kernel 2.6 all information about devices is hold in the /sys directory. You can find your device under /sys/bus/pci/devices/. Each PCI device directory is named with the a combination of "bus:slot.function". In the device directory you find files named:

vendor
Vendor-ID is a unique number for each vendor. It is assign from the pci-sig and requires a membership.
device
Device-ID is a vendor unique number, chosen by the vendor.
irq
the assigned IRQ IRQ Handling in RTAI
resources
The memory images of the device with their flags
resource0
A virtual file for the config space of the device
resource1-5
Virtual files with the memory images.

Format of .../resource

[edit | edit source]

A file can look like this:

0x00000000feafe000 0x00000000feafefff 0x0000000000000200
0x00000000feaf0000 0x00000000feaf7fff 0x0000000000000200
0x00000000feafd000 0x00000000feafdfff 0x0000000000000200
0x00000000feafc000 0x00000000feafcfff 0x0000000000000200
0x00000000feac0000 0x00000000feadffff 0x0000000000000200
0x00000000fea40000 0x00000000fea7ffff 0x0000000000000200
0x0000000000000000 0x0000000000000000 0x0000000000000000

The lines, are the images 0..6, 0 is config space, 1-5 are device dependent, 6 is an optional ROM. The first column is the start addresses of the images, the second column contains the end address (inclusive). The third column contains the flags for the image. The constants for the flags can be found in /usr/src/linux/include/linux/ioport.h.

IORESOURCE_IO
The image can only be access via IO-ports (outb, inb,.. macros)
IORESOURCE_MEM
The image is accessible via normal memory access
IORESOURCE_IRQ
???
IORESOURCE_DMA
???
IORESOURCE_PREFETCH
access to the image can be cached (no side effects)
IORESOURCE_READONLY
the memory is read only
IORESOURCE_CACHEABLE
memory writes can be cached
IORESOURCE_RANGELENGTH
???
IORESOURCE_SHADOWABLE
???
IORESOURCE_BUS_HAS_VGA
???
IORESOURCE_DISABLED
???
IORESOURCE_UNSET
???
IORESOURCE_AUTO
???
IORESOURCE_BUSY
Driver has marked this resource busy

mmap an image

[edit | edit source]

The image file .../resource1-5 are ready to use. So you have only to do the two steps, open and mmap:

uint aLength = 4096
uint mFileDescriptor = open( "/sys/bus/pci/devices/0000:02:09.0/resource2", O_RDWR );
uint* ptr = cast(uint*) mmap(
       null,
       aLength,
       PROT_READ|PROT_WRITE,
       MAP_SHARED,
       mFileDescriptor,
       0);
// If ptr == -1, than the user was probably not root
assert( cast(uint)ptr != uint.max );
uint[] mMemory = ptr[0 .. aLength/4];

If you are done, don't forget to close the resources:

munmap( mMemory.ptr, mMemory.length );
close( mFileDescriptor );