How kernel determines which bus a device in DTS belongs to? - linux

I am new to Linux device driver. During reading books, I learnt that a match between a device and a driver usually happens under a bus driver, providing that the bus driver is registered to the core in the first place. The bus driver maintains two lists, one for devices on the bus, and the other for drivers for the devices on the bus. whenever a device or driver is registered with the bus driver, the bus driver is reponsible to find a match (via compatible string in case of DTS) between the device (or driver) and a driver (or devices).
In the device tree passed to kernel, it seems that there is no place to specify the bus_type of each device. Then how kernel knows to register a device to a proper bus?
Take platform bus for example, I have a pinctrl driver, from the log I know that the pinctrl driver's probe() is invoked, which implies that a match has happened under platform bus...but in DTS there is no place to specify my, say, pinmux device node is on platform bus (although the compatible string matchs).

Related

How can we create a virtio platform device?

I am writing a virtio driver for a device that is seen in the original driver as a platform device, that is the parent of a character device in the device tree.
In the tutorials that I followed, The virtio drivers are always done for "regular" character devices.
I wanted to create my virtio driver for it to expose a fake platform device as a parent of a "regular" character device.
My question is this one: Should I detect my virtual device in the regular way using the probe function and then create my platform device inside of it or is there a recommended way or order to create both (the platform device and the character device (child))?
When we get the virtio device and trigger the associated probe, we can then register a platform driver and then add some platform devices correponding to your needs.
And your device will appear as a platform device instead of a virtio_device in the guest OS.

Create SPIdev devices at runtime

I have a device with SPI bus which is connected via PCIe to a linux machine. I'm developing a simple SPI driver for this device. With spi_register_master I can create a SPI master (it is listed under /sys/class/spi_master/spixxxx).
For accessing the bus I would like to use spidev from the userland, but I have not found a way to register a spidev device at runtime. All ways I have found use the device tree to insert the information into the linux kernel.
Is there some way to create spidevdevicesat runtime?
Edit: I suppose it is the same problem with all protocol drivers and not limited to spidev.

AM335x - i2c slave for linux kernel

I need to have i2c slave Linux kernel driver for TI AM335x.
I googled about and didn't find precise information.
Should I do everything from scratch, or maybe someone has some reference about it? or even a patch
Thanks
Avner
For new device which connected as slave to i2c bus, you should write neither "i2c driver" nor "driver for AM335x" (as far as the processor support already present in kernel).
i2c is a bus and there is kernel infrastructure for the bus, see documentation.
You should figure out what type your device is and then write driver for this type of device using i2c bus primitives.
For example, the driver for DS13xx and compatible IC is rtc driver.
A driver "for" PCF8574 i2c gpio expander can be GPIO driver as well as keypad driver.

How to access USB bus number from Linux device driver?

I have two identical USB devices connected to different USB host controllers. Sometimes devices initialization order changes spontaneously breaking devices enumeration. Is there a way to get USB bus number in device driver (it would be enough for implementing of correct initialization) like it is done in user space with lsusb?
I found the decision. The task was to get USB bus number in kernel module, the similar task was found at this post. It was necessary to get a bus->busnum member of usb_device structure in the driver init function:
struct usb_device *usbdev;
struct usb_bus *mybus;
usbdev=interface_to_usbdev(pusb_intf);
mybus=usbdev->bus;
printk("USB Bus number is %d\n",mybus->busnum);

Process of device driver detection in linux

Wanted to know how a device is detected in Linux? What exactly is the workflow of the device driver in device detection?
It is the Kernel's job to detect devices as it has the lowest level access to the available hardware. When the Kernel scans through all available addresses it maintains a list of Vendor and Device IDs.
To use PCI bus devices as an example, there is a Vendor ID and a Device ID associated with all PCI devices.
Device drivers are written in such a way as to identify to the Kernel what kinds of devices the driver is able to control. Drivers may advertise that they can handle more than one vendor and device type combination.
The Kernel will allocate a driver to each device based on these IDs. A similar process is in place for USB devices. Older technologies like legacy devices (serial ports, parallel, ps2 mice/keyboards) will have explicitly hardcoded methods of associating particular drivers with devices.
You can use the Linux commands lsusb and lspci to see the available devices and IDs on your system.
So in direct answer to your question - the device driver usually does nothing to detect the device, at least in the first instance. Once the driver is associated with a device (by the Kernel) the driver will likely do further interrogation of the device to ensure it contains the right firmware or is the right hardware revision, etc.

Resources