Single device driver for multiple devices - linux-device-driver

I'm writing a device driver for multiple devices, especially FPGA board.
I want a single driver to drive multiple devices.
In this case, how can I differentiate the devices in the code?
By minor number? PCIe address?
Thanks,

Related

Multiple I2C devices with the same address

Currently working on a project that requires me to use two I2C devices on the Raspberry Pi, but both devices use the same address. Does anyone have an easy fix to changing the address of 1 of the devices? :)
No, most devices don't allow changing the address. Those that do have a separate pin (or pins) that can be used to select the address. Very few devices allow changing the address by software. If you said which device you use, we could tell you which group it belongs.
However, there's a quite easy workaround: The Raspberry Pi has up to 6 I2C busses, so you can just use a second bus for the second device (like the one on GPIO0/1, which is already configured as I2C bus 0 by default and typically unused)

Query OS for hardware characteristics of wireless adapters

I have a raspberry pi with 2 wireless adapters connected to it - one has an antenna.
http://www.modmypi.com/raspberry-pi/accessories/wifi-dongles/wifi-dongle-ultra-long-range-high-gain-w-5dbi-antenna
I am trying to write a script that queries my linux box for wireless interfaces, finds out which one is the one which has the antenna connected and put it into hotspot mode. Tomorrow, it can be any other adapter with an antenna attached.
Are there any tools or commands that can help?
Any entries in /proc or /sys that tell me this is the required device?
Thanks in advance.
EDIT:
Is there a CLI that gives the interface name and the Manufacturer and model?
To know what wifi cards you have plugged in your system, run "iwconfig".
For the antenna, there is no way to know if your dongle has the antenna plugged on, or the antenna has been removed.
I guess, the best workaround could be, query the device for a network scan - then you count how many APs were found: if the antenna has been unplugged, you will not get any APs (or maybe only 1-2...)
I ended up using hwinfo on my pi and doing the text/regex parsing in python to get all the data I was interested in.

iPhone to device via Wifi

I have an embedded hardware device that currently broadcasts data via bluetooth. I don't want to go through the steps of the MFi program, so I'm thinking about using a different chip in my device to broadcast the same data via WiFi/UDP.
It is my understanding that in that case the only thing I need is sockets and bonjour for discovery.
My question is: do I need a third device in the equation (a wireless router where the two devices are connected)? Or can the two devices establish some sort of ad hoc network?
Pointers are greatly appreciated.
Have a look at WiFly from Roving Networks. It might be what you're looking for.

Can two Identical devices be present on the same bus in any PCI Topology

As per the PCI standard, devices are identified on the basis of Vendor Id, Device Id and the bus no. All devices of same type have identical vendor id and device id. If I put two such devices on the same bus say bus 0. How will the PCI Software Subsystem distinguish between the two?
If such a case is not possible in PCI, then can such thing be possible through PCI Express Switch?
Yes, it's perfectly fine. The host distinguishes identical devices by slot.
PCI and PCI Express devices are identified by Bus/Device/Function, which is necessarily unique per device in the system. Vendor and Device ID are just properties of a device found at a certain Bus/Device/Function.
When enumerating board, a driver will typically scan PCI configuration space (iterate through all the installed PCI devices), until it finds one or more devices that match the expected Vendor and Device ID, and possibly also the subsystem IDs. Once it finds a match, it should record the bus/device/function as a "handle" to the open device.
Properly written software should find all vendor/device matches, put them in a table, and let you pick which one you want to use (e.g. /dev/mydevice0, /dev/mydevice1, etc). However, I have seen lazy software that simply stops at the first match.
As I know, each PCI device can be uniquely described by (Bus,Device,Function). Consider your case(2 devices have identical VID and DID installed) and I think they must be located at different PCI bus ! If they must be in the same bus then please make their Device or Function number different

Interfacing socket code with a Linux PCI driver

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!
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
Understanding the Linux Kernel
Linux Device Drivers
Essential Linux Device Drivers