syfs question

Greg KH greg at
Fri Feb 28 07:37:55 EST 2020

On Thu, Feb 27, 2020 at 09:41:21AM -0500, Sadanand Warrier wrote:
> On Wed, 26 Feb 2020 at 12:54, Greg KH <greg at> 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


> > > 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


> 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 :)


greg k-h

More information about the Kernelnewbies mailing list