How to create "dev" files in sysfs?

Arvid Brodin arvid.brodin at enea.com
Wed Jan 19 08:23:41 EST 2011


Hello,

How do I make my platform device a character device that
can be handled by udev?

Background:

I've written a driver for a very simple platform device (a jumper
connected to an input pin on the processor). Today I can read the status
of the pin through a driver attribute file in sysfs at:

/sys/devices/platform/msel/

As a learning experience I want to get udev to detect this device and
create a character device file under /dev for it, that I could read to
get the pin status. If I understand correctly, udev looks for files
named "dev" in sysfs, which contains the major and minor device numbers
which udev use to create the device node.

 From the kernel code it seems that ((struct platform_device *)
my_platform_device)->dev->devt would have to be set to contain the major
& minor device numbers when the device is created for a "dev" file to
appear, which is not possible in this case since the device is
registered in board setup code where it seems wrong to get device
numbers by calling call alloc_chrdev_region() (right?).

I found a few previous questions about this same problem in these
kernelnewbies threads:

http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg08461.html
http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg06807.html

Using that info, and looking at the hpet driver code, I initialised a
struct miscdevice and called misc_register(). This gives me a
/sys/class/misc/msel_chrdev/ folder with the required dev and
uevent files. udev also picks up this and creates the corresponding file
in /dev.


Is this the correct way to do this? One "device" for the hardware and
one or more "devices" for the userland interface(s)? Also, looking at my
desktop system, there's one device under /sys/devices/platform that also
has "dev" files:

/sys/devices/platform/serial8250/tty/ttyS1/dev
/sys/devices/platform/serial8250/tty/ttyS2/dev
/sys/devices/platform/serial8250/tty/ttyS3/dev

I notice that these "dev" files appear as subfolders under the platform
device's own folder, and not under /sys/class/. Perhaps this is more
correct? The source for 8250 is ~223K in 14 different files so I have
not been able to decode how they do this.

(Please CC me directly if you respond!)

Thanks for reading all this,
Arvid Brodin
Enea Services Stockholm AB





More information about the Kernelnewbies mailing list