If I call device_create with a existing class the device lands up in /sys/devices/virtual. Using device_add I could populate the parent of the device and leave the class empty or provide a class and get the device to be created in the parent directory. What exactly is the significance of the" /sys/devices/virtual" directory? Is this meant to carry devices of a specific type say something that is divorced from actual hardware? Secondly a device created under /sys/devices/virtual has a uevent file that is populated like this. -bash-4.2$ cat uevent MAJOR=13 MINOR=63 DEVNAME=input/mice -bash-4.2$ A device in a specific PCI slot has something like this instead.. -bash-4.2$ cat /sys/devices/pci0000:00/0000:00:02.2/0000:03:00.3/net/eth1/uevent INTERFACE=eth1 IFINDEX=3 How is this file populated? Is it done by udev or is this done via a kernel call? Thanks S
On Wed, Feb 26, 2020 at 10:15:43AM -0500, Sadanand Warrier wrote:
If I call device_create with a existing class the device lands up in /sys/devices/virtual.
Yes, think of that as a "class device" (we used to have them as a separate structure, now we don't).
Using device_add I could populate the parent of the device and leave the class empty or provide a class and get the device to be created in the parent directory.
I don't understand what you mean here.
What exactly is the significance of the" /sys/devices/virtual" directory? Is this meant to carry devices of a specific type say something that is divorced from actual hardware?
Yes. These are devices that do not have a "real" device backing them, they are "virtual" devices in that the kernel creates them usually because it needs a way for userspace to interact with the device. A good example of this are tty "devices". They are attached to a "real" device in the system (pci, usb, platform, etc.) but the tty device itself is just there to provide the common interaction with userspace that userspace expects.
Secondly a device created under /sys/devices/virtual has a uevent file that is populated like this.
-bash-4.2$ cat uevent MAJOR=13 MINOR=63 DEVNAME=input/mice
Nice device.
-bash-4.2$
A device in a specific PCI slot has something like this instead.. -bash-4.2$ cat /sys/devices/pci0000:00/0000:00:02.2/0000:03:00.3/net/eth1/uevent INTERFACE=eth1 IFINDEX=3
How is this file populated? Is it done by udev or is this done via a kernel call?
The uevent file is not written to (well, it can, but that does something different), it is read from to show what the attributes that are set when the uevent happens for that device. It is populated by the bus or class for that device by a bus/class specific callback to that driver. Have you read the LDD3 chapter on the driver model for the kernel? It's a bit out of date (well, really out of date), but the basics should still be semi-relevant and might answer some of your questions here. hope this helps, greg k-h
On Wed, 26 Feb 2020 at 12:54, Greg KH <greg@kroah.com> wrote:
Using device_add I could populate the parent of the device and leave the class empty or provide a class and get the device to be created in the parent directory.
I don't understand what you mean here.
Umm sorry was a bit dense there what I meant to say was that if the device kobj was set up to point to the parent it would appear in that parent's directory. Additionally if the link to the parent was there and it also belonged to a class it would appear under the class directory in the parent hierarchy. Like so for an ethernet interface belonging to the class "net" if I am in the sys directory. -bash-4.2$ find . -name "eth*" -print ./class/net/eth1 ./devices/pci0000:00/0000:00:02.2/0000:03:00.3/net/eth1
What exactly is the significance of the" /sys/devices/virtual" directory? Is this meant to carry devices of a specific type say something that is divorced from actual hardware?
Yes. These are devices that do not have a "real" device backing them, they are "virtual" devices in that the kernel creates them usually because it needs a way for userspace to interact with the device.
A good example of this are tty "devices". They are attached to a "real" device in the system (pci, usb, platform, etc.) but the tty device itself is just there to provide the common interaction with userspace that userspace expects.
So would it be considered indecent to create a device under a parent directory with a specific value of devno which we place in dev->devt value and then create a character device with cdev_init/add and the same devno so that the user can access ioctl operations through the entry in /dev. For e.g. I create a device "xx" in /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx cat /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx/dev 243:0 and set up a character device xx like this using cdev_add/init crw-------. 1 root root 243, 0 Jun 22 07:52 /dev/xx In this case there is no device under /sys/devices/virtual but I could open "/dev/xx" from user space and do ioctls etc. Of course I would be limited to accessing sysfs attributes etc. through the real hardware device. Is that frowned upon? Is a virtual device considered necessary for user space access?
The uevent file is not written to (well, it can, but that does something different), it is read from to show what the attributes that are set when the uevent happens for that device. It is populated by the bus or class for that device by a bus/class specific callback to that driver.
Have you read the LDD3 chapter on the driver model for the kernel? It's a bit out of date (well, really out of date), but the basics should still be semi-relevant and might answer some of your questions here.
hope this helps,
Not in detail and I will do so but you have answered my question I think. I'll check the newer kernel code to see how the bus/class specific callback works. Thanks S
On Thu, Feb 27, 2020 at 09:41:21AM -0500, Sadanand Warrier wrote:
On Wed, 26 Feb 2020 at 12:54, Greg KH <greg@kroah.com> wrote:
Using device_add I could populate the parent of the device and leave the class empty or provide a class and get the device to be created in the parent directory.
I don't understand what you mean here.
Umm sorry was a bit dense there what I meant to say was that if the device kobj was set up to point to the parent it would appear in that parent's directory.
If you are using devices, then think about device parents, not kobject parents please.
Additionally if the link to the parent was there and it also belonged to a class it would appear under the class directory in the parent hierarchy. Like so for an ethernet interface belonging to the class "net" if I am in the sys directory.
-bash-4.2$ find . -name "eth*" -print ./class/net/eth1 ./devices/pci0000:00/0000:00:02.2/0000:03:00.3/net/eth1
Yes.
What exactly is the significance of the" /sys/devices/virtual" directory? Is this meant to carry devices of a specific type say something that is divorced from actual hardware?
Yes. These are devices that do not have a "real" device backing them, they are "virtual" devices in that the kernel creates them usually because it needs a way for userspace to interact with the device.
A good example of this are tty "devices". They are attached to a "real" device in the system (pci, usb, platform, etc.) but the tty device itself is just there to provide the common interaction with userspace that userspace expects.
So would it be considered indecent to create a device under a parent directory with a specific value of devno which we place in dev->devt value and then create a character device with cdev_init/add and the same devno so that the user can access ioctl operations through the entry in /dev.
Woah, this is getting way too confusing, it should be very simple to do what "should" be done here.
For e.g. I create a device "xx" in /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx
So this is a child of a pci device, ok.
cat /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx/dev 243:0
Ok, so you created this device with what call, to device_create*() passing in the dev_t value?
and set up a character device xx like this using cdev_add/init crw-------. 1 root root 243, 0 Jun 22 07:52 /dev/xx
Note, cdev_* doesn't do anything in /dev/, that was done by the device_create* call. cdev_* is there to handle things internally (and you should have done that call _before_ the call to device_create*()).
In this case there is no device under /sys/devices/virtual
Possibly.
but I could open "/dev/xx" from user space and do ioctls etc.
Don't get 'struct device' confused with dev_t which is needed by cdev functions please. They are totally different things, but do need to interact in some small ways in order for everything to show up properly in userspace.
Of course I would be limited to accessing sysfs attributes etc. through the real hardware device.
Huh? What attributes? sysfs attributes can not be touched through a device node, that's a totally different interface.
Is that frowned upon? Is a virtual device considered necessary for user space access?
No, but don't go hanging random character devices off of a PCI device for no good reason :) Use the misc device interface if all you need is a simple char device node, that should handle all of the needed logic for you, and put things in the correct location. And please don't try to create new ioctls if at all possible, that's just mean to everyone involved :) thanks, greg k-h
On Fri, 28 Feb 2020 at 07:37, Greg KH <greg@kroah.com> wrote:
If you are using devices, then think about device parents, not kobject parents please. I meant the device parent not kobject.
For e.g. I create a device "xx" in /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx
So this is a child of a pci device, ok.
cat /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/xx/dev 243:0
Ok, so you created this device with what call, to device_create*() passing in the dev_t value?
Actually device_register()
and set up a character device xx like this using cdev_add/init crw-------. 1 root root 243, 0 Jun 22 07:52 /dev/xx
Note, cdev_* doesn't do anything in /dev/, that was done by the device_create* call. cdev_* is there to handle things internally (and you should have done that call _before_ the call to device_create*()).
Again got things mixed up. The cdev_init/add was done before calling device_register. I just used the same devno and put that in dev->devt.
Don't get 'struct device' confused with dev_t which is needed by cdev functions please. They are totally different things, but do need to interact in some small ways in order for everything to show up properly in userspace.
Didn't quite follow what you meant about confusing the two because to me they are distinct but is there something that I should avoid?
Huh? What attributes? sysfs attributes can not be touched through a device node, that's a totally different interface. Yes I wasn't talking about the device node but the actual sysfs entry.
Is that frowned upon? Is a virtual device considered necessary for user space access?
No, but don't go hanging random character devices off of a PCI device for no good reason :)
I agree.
Use the misc device interface if all you need is a simple char device node, that should handle all of the needed logic for you, and put things in the correct location.
And please don't try to create new ioctls if at all possible, that's just mean to everyone involved :)
Oh this particular device is a beast and needs to be manipulated from user space and there are only two options use sysfs attributes or ioctls. The ioctl approach has been voted the most desirable for certain reasons by those involved. :-) Thanks a lot for the help. S
participants (2)
-
Greg KH -
Sadanand Warrier