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
Related
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).
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.
Every USB device has to come under some USB device class definition based on the device driver will get use on the Linux system.. that part i understood.
but I can't understand something. For example, mostly all USB modem come under communication device class.. I have one 3G USB modem and one 3G USB dongle (e.g.,tata photon), both come under the same communication class but 3G USB modem uses CDC-ACM driver and 3G dongle uses serial converter driver(USB-Serial). What makes these devices differs?
Can anyone explain this?
The source code of Linux is available so you can look to see how it works. Here is the source code of the cdc-acm USB driver you mentioned:
http://lxr.linux.no/linux+v3.12.2/drivers/usb/class/cdc-acm.c
Look at the acm_ids[] array near line 1516. This is a big array of structs that describe which USB devices the cdc-acm driver will match to. It looks like the array starts with quirky devices and then at the end has more standard, generic devices. See if you can find the line that matches each device you have! This array is passed to some more general USB code in the kernel that takes care of matching USB devices to drivers.
The USB device dictates what type of driver will load. If it presents itself with CDC Class descriptors then it will load a CDC class driver (the same goes for other types of devices such as USBAudio, HID Keyboard/Mouse, etc.).
If the USB device presents itself with vendor specific descriptors then it will need to do matching on the VID and PID to determine which vendor specific driver to load. In the case of USB to serial bridges there are many vendors, so the driver loaded depends on your specific cable. You can find the source for the usbserial based drivers in root/drivers/usb/serial/, and from there you can grep for the VID/PID of your device to find out which driver is being loaded for that interface.
It looks like there was a recent commit for a 3G dongle, maybe this is what you are looking for?
The source code for all Linux drivers is available... you could see for yourself by looking at the source code.
Question
How, given the information udev passes to my kernel module (the block device path maybe), can I send SCSI commands to the block device? (yet have it function normally otherwise, meaning partitions are mounted, no data loss, etc)
What I'm Trying To Do
I have a USB mass storage device that has LEDs which are controlled via SCSI commands.
I would like to write an LED driver to provide /sys/class/leds/* entries for it.
The goal here is to have the device function normally (partitions mounted and functioning, etc) but also allow /sys/class/leds/* interactions that would send SCSI commands to control the LEDs.
It's easy to to send the SCSI commands I need in user space with sg_raw. But I need a kmod to provide /sys/class/leds/...
Essentially what I need is ioctl(). However, I understand it is generally a bad idea to call open(),ioct(),etc from within a kmod.
It can be achieved by some pseudo driver that emulates between USB & SCSI. I think what ever be your problem may achieved while you are accessing the USB device through SCSI.
These low level & mid level scsi drivers are defined.
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