Re: PCI device driver question



elliotng.ee@xxxxxxxxx wrote:
OK, after doing some research, it appears that I can use the char
device read and write functions in the pci device driver. To summarize
what I have, I have written a pci device driver. In userspace, I want
to be able to access the pci device driver, read from memory, and write
to memory. I have recently found out that I can try to read/write from
using file operations.

(1) You need to map the device memory into kernel virtual address space
with ioremap. Then, in the test_write function, you copy data from
user space into kernel space (possibly to a small temporary holding
area), then use writel to put it into the (mapped) device memory
(adding in the f_pos value).

test_read is simply the inverse, readl to read the device memory space,
then copy it to user space.

copy_to_user and copy_from_user can be used for data transfer between
user space and kernel space. Look for the book "Linux Device Drivers"
(3rd edition) -- which is available in pdf format for free online --
for much detailed info.

(2) You create a device node (e.g. "mknod /dev/mydevice c xxx 0" where
xxx is your device's major number from /proc/devices). Your program
then opens /dev/mydevice, lseek's to the proper offset and does read or
write calls.

Having said all that, if this is just a one-off or occasional
special-purpose administrative operation, you may not even need to
write a driver (on most platforms at least). You can use the lspci
program to find the physical address of the pci device's memory. Then
you open "/dev/mem", mmap the desired area and read / write data
directly to or from it in your user program.

GH

.