views:

16

answers:

1

I have two devices that are interfaced with PCI. I also have code for both devices that uses generic socket code. (The devices were originally connected by MII/Ethernet.)

Now, I need to write a PCI device driver to transport packets back and forth between the two devices.

How do I access the file descriptors opened by the socket code? Is this the same as accessing a character device file?

The PCI driver has to somehow capture packets from read() and write() in the code.

Thanks!

A: 

The answers to your questions are: (1) You don't, and (2) no.

File descriptors are a user-space concept, and kernel drivers don't interact with user-space concepts. (Yes, they're implemented by the kernel, but other device drivers don't get to play with them directly, and shouldn't play with them even indirectly.)

What you do is implement methods that will receive data that is buffered in a kernel-accessible memory space, and send that to your hardware, and then receive data from your hardware and write it (when asked) to a buffer in kernel-accessible memory.

You'll do this by implementing the character device driver APIs as well as the PCI device driver APIs and then registering your driver as a PCI device, and then a character device. While some of these methods may refer to file structures, they will not be the user-land structures that you know and love.

For devices that implement the Ethernet protocols, life is easier because you implement the Net Device Interface instead. This way all you have to write is the parts necessary to get data to and from your hardware.

What you'll need is specifications for the device hardware, how you control the hardware using PCI registers and regions.

The good news is, you don't have to do this alone -- there's a large community of kernel developers, and several good (and current) books on developing for the Linux kernel (see below).

References

Craig Trader
Thanks for the quick response! Let's see if I understand what you've written..I'll have to write a PCI driver for the target device. The driver will also allocate an Ethernet interface with all the TX/RX functions.Now I open a socket and this is where I'm confused. Do you have information on "linking" / "binding" a created socket to a specific ethernet PCI device? There's another PCI ethernet device that I don't want to transmit data through.
StuffedB
In order for your device to appear as a possible network interface, you'll have to load the driver into memory. The driver will register the PCI IDs that match your device. After that, you'll need to configure your driver with a network address (similar to mapping an IP address to eth0). At that point it is, for all intents and purposes, a network card, and is treated as such by the OS.
Craig Trader