Old style PCI probing - linux

Starting from Linux kernel 3.0, pci probing is automatic with: pci_register_driver(&pci_driver);
Linux kernel 2.6 and older, programmers had to create a character device, and walk through the PCI list, select appropriate PCI and doing work with it. Can you tell me how the steps of this procedure, why the initialization of a character device is need before working with the PCI driver and why it is unnecessary to register a character driver anymore.

I think you refer to linux 2.4 or older. Current kernel device model with busses, devices and drivers has always been part of the 2.6 series.
What is your question exactly ?
A list of PCI devices is made at boot time. Then when a driver is registered, the pci_driver structure id_table field is used to match
with the devices present on the bus. Then the pci_driver probe function is called with a pointer to the device structure that matched.
pci_driver is registered
for each device present on the bus, id element of the device(product id and vendor id) are compared to id element in the id_table provided by pci_driver
if there is a match, the pci_driver probe function is called, and in this probe function you can register a char device, or a block device etc ..
So it is not very different from 2.4, except all the probing, matching driver and devices, etc... is handled by the "device core" and not by the pci driver.
For a detailed explanation, see this PDF and this page

Related

difference between a pci device driver with "module_pci_driver" and a pci driver with "__init()" but without "module_pci_driver()" in Linux

I have seen in pci device drivers this line
module_pci_driver(cp_driver);
But in other pci device drivers this like
module_init(rtl8139_init_module);
both lines found at the end of driver's .c file in different drivers
What I know is: I can create a pci device driver with __init but I can also create a pci device driver without __init
i.e.
[Realtek Ethernet has two drivers in Linux source
1) 139cp.c (without __init)
2) 8139too.c with __init].
I assume that the main difference between the two is simply that if I have to use a pci device driver right after loading of the driver module with insmod command so I use an implementation of a device driver with __init.
Question
On the contrary, if I just want to load the pci device driver but not use it, Then should I create a pci device driver with module_pci_driver() (So no need to add __init)? And what does it do(module_pci_driver)? How its different from pci driver with __init
I like to know I may have a misconception anyone please clarify. Also does the probe function of both type of drivers will run when I load the driver with insmod command? When? if yes than what's the difference since most configuring of device is done in proble function
To sum up
When initialization happens in driver with module_pci_driver(cp_driver); since they dont have __init implemented. What command used
module_pci_driver() is a helper meant for drivers that don't do anything special in their module init and exit (i.e. modules whose init and exit functions just register/unregister).
It generates the init and exit functions for you, reducing some boilerplate.
In this specific case, the 8139too driver might do something in addition to registering the driver (in this case, logging the driver name), so isn't using module_pci_driver()
The probe function is called for existing or new devices that match the ID table and aren't already owned (see How To Write Linux PCI Drivers for details).
It returns a value indicating if the driver will be taking ownership of the device (either 0 or an error code).

LInux Device Driver Layering Confusion

I have recently been reading about Linux Driver and Device Model. I wanted to understand how the following works in linux in the driver subsystem. So lets say my device tree looks as follows
To be concrete lets assume Bus1 is a PCI bus, Bus2 is ISA and Bus3 is USB. Buses are connected with each other using bridges.
Linux will identify this device tree through the enumeration process and through enumeration and probing mechanism suitable usb driver for terminal device would be identified.
Now lets assume a Tx operation to this terminal device. The terminal device usb driver would end up doing a urb_submit(dev, write_buffer).
My question is in order for the URB to reach the terminal device, theoretically it would have to be enveloped in Bus2 and Bus1 envelopes. So theoretically speaking the outgoing packet has to look something like this
So at some after the urb_submit happens in the driver, does some kernel code walk up the device tree structure and invoke the bus drivers in order (bus 2 driver and then bus 1 driver) to create this envelope structure.
Can anyone point me to the code in the linux kernel where this happens? I tried to follow urb_submit but could not figure this out.
Thanks a bunch!
For device drivers that have memory-mapped registers, the device's private data struct contains the addresses assigned by enumeration for each device and they directly write to the registers.
For device drivers that go through layers, as you describe above, the enumeration tells each driver who its parent is, and the driver saves this in its private data when it is instantiated. Then the read or write call of the driver formats the request properly for the device type and invokes the read or write method of the parent driver. For example, a USB disk read might call the read method in the file_operations block of its parent which would be https://elixir.bootlin.com/linux/v4.19.107/source/drivers/usb/core/devio.c#L2614

sysfs_create_group(): Where to call?

currently i write an driver module which offers some entries in the sysfs. I read a lot through the driver source tree and the internet. I found two approches where the sysfs_create_group() is called:
a) most commonly: In the probe() function of the Driver. Like adviced here
How to attach file operations to sysfs attribute in platform driver?
Random Thing i looked at:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/rtc/rtc-ds1307.c#n1580
b) In the driver struct.
http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/
I know, Greg KH is a very well known developer. So i tried to follow his advice. In the bla_show()/bla_store() functions i tried to get my Driver private data, but my printk()'s showed much different addresses than i printed in the probe() function. My private data is (null). Which is ofc wrong.
When i use approch a) it works as expected, but as Greg KH suggests it is wrong too. I see it a lot in the stable tree in different drivers. Greg writes, the userspace already got the notification that there is a new device, but the LDD3 book states, that the probe function is there to determine if the device is present.
To sum my question up:
Why get the userspace notified about it, even when the kernel doesnt know if it can handle it?
Where is the right place to call sysfs_create_group()? Is it a) or b) ?
LDD3: https://static.lwn.net/images/pdf/LDD3/ch14.pdf
PDF page 24
probe is a function called to query the existence of a specific device
(and whether this driver can work with it), remove is called when the
device is removed from the system,and shutdown is called at shutdown
time to quiesce the device.
I am more confused than before .....
Best Regards
Georg
A device driver is a program that controls a particular type of device that is attached to your computer.
Platform devices are inherently not discoverable, i.e. the hardware cannot say "Hey! I'm present!" to the software. So for these kind of device we need a driver which call as Platform drivers. Drivers provide probe() and remove() methods.
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
.
.
struct device_driver driver;// this file has 2 parameter name or owner.
};
probe() should in general verify that the specified device hardware
actually exists. First we register our driver. Once it found device then it'll call driver probe. It is using name for searching a device.
Ans : your device is available then you need sysfs entry for communication (To the user space). so conceptually you need to define you sysfs entry in probe.
sys_notify function on your attribute and it will cause your userspace code to wake up. It will trigger when sysfs is available for userspace. It just avoiding a blocking call. When kernel does not have sysfs then it'll not notify userspace.
sysfs is a virtual file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files. When your device is available then you need this entry to export your information.

Linux, ARM: Why is gpiochip<num> only created if I2C GPIO Expander is present at boot?

Using Linux 3.14.52 on a imx6sx hardware platform (NXP embedded ARM).
The problem is that PCF8575 I2C GPIO expanders specified in the device tree are not instantiated as devices in the /sys/class/gpio structure unless they are present during kernel boot. The devices are listed in the /sys/bus/i2c/devices/i2c-1 (i2c bus 1) structures but are not given gpiochip 's in the /sys/class/gpio structure.
Is there a way to have these devices assigned as gpiochip after boot once they are added to the system?
On previous (PowerPC) platform, all devices listed in the device tree were assigned gpiochip's regardless of whether they were on during kernel boot. But with our ARM platform, the devices must be available during kernel boot. I have tried changing the kernel i2c/gpio options (via .config) as close to the previous platform as possible but this seems to have no effect.
For sure the kernel worked differently regarding sysfs in the 2.6 kernel branch. I have experienced similar issues as well. It has to do with handling of the device tree. The device tree will get unflattened, but this only kicks off the actual discovery of devices. If the devices do not actual exist, they will not be probed and will not have entries created in sysfs.
Device Tree Usage
Linux board support code calls of_platform_populate(NULL, NULL, NULL, NULL)
to kick off discovery of devices at the root of the tree. The
parameters are all NULL because when starting from the root of the
tree, there is no need to provide a starting node (the first NULL), a
parent struct device (the last NULL), and we're not using a match
table (yet). For a board that only needs to register devices,
.init_machine() can be completely empty except for the
of_platform_populate() call.
So the device tree will only tell the kernel what to discover, it will not actually add anything if it is not found.
I can confirm the gpiochip is ONLY added at probe of your device:
gpio-pcf857x.c
Notice the call on line 397 to gpiochip_add
What I would recommend is to try compiling your kernel with the gpio expander set as a module and then insmod it after it has actually been attached.

Linux Device Driver Registration procedure

I am a linux newbie, trying to understand Linux Device Model. I had been going through Linux 3.1.6 code base, particularly the driver part and found that
some of the drivers were using (for example i2c-bus device : linux-3.1.6/drivers/i2c/i2c-dev.c) *register_chrdev()* and
few others (for example pci bus : linux-3.1.6/drivers/pci/bus.c) were using *device_register()*.
My question is when to use register_chrdev (yes, I know its for a character device, but why not use device_register) and device_register ?
Does that depend on where does the driver developer wants his device/driver to be listed down, like devfs vs sysfs ? Or the interface exposed to the user space to access the device ?
One function registers a character device association (hooking up major:minors to your function), the other just creates an abstract device object (only), so to speak. The two are complementary. The device object is used for the generation of an event so that udev can, if there is also a cdev association registered, create a node in /dev. (Compare with, for example, drivers/char/misc.c.)
See when you register a device as a character device specifically then following thing happens:
Major Number is given in accordance. If you use any device depending on functionality whose registration is based on character device (like tty, input etc), then those will have their respective major number. Thats why its said that dont assign major number statically if not sure.
And
There are certain file operations which correspond to operations that could be performed on char devices only.
Do ask if any query.

Resources