Creating devices with mdev (automically)

Michael Abbott michael at araneidae.co.uk
Thu Apr 9 14:18:59 UTC 2009


This is indeed hugely off topic, but as I've just had to work through 
this, I can give you the answer.  If you find a friendly kernel mailing 
list, do let me know (lkml is too high bandwidth, and not newbie 
friendly!)  Maybe kernelnewbies.org?

Apologies for this off topic posting, but this list seems friendly (and 
low traffic) enough to handle this.


On Thu, 9 Apr 2009, Manuel Sahm wrote:
> I use the example init code - I think in my init function of the
> kerneldriver, there´s the problem. Maybe you could analyze the code :
> I think I don´t get the hotplug events ?!

You get the hotplug events (that's easy to see, if you write a test 
program into /proc/sys/kernel/hotplug instead of mdev), but you haven't 
created the /sys nodes need to tell mdev about the device.

Here's what has worked for me (cut down to its absolute minimum, all error 
handling and declarations discarded):

1	static int __init my_init(void)
2	{
3	    allocate_chrdev_region(&my_dev, 0, dev_count, dev_name);
4	    cdev_init(&my_cdev, &my_fops);
5	    cdev_add(&my_cdev, my_dev, dev_count);
6	    my_class = class_create(THIS_MODULE, dev_name);
7	    for (i = 0; i < dev_count; i++)
8	        device_create(my_class, NULL, MKDEV(MAJOR(my_dev), i), names[i]);
9	}

3 - allocates range of device codes.  Think you've already got that.
4 - initialises cdev with file operations
5 - makes device available.  At this point device nodes created with 
`mknod` are probably already active.  You've got this far with your 
driver.

6 - creates /sys/class/ sub-node for your device
7 - creates /sys/devices/virtual/<dev_name>/<names[i]> node with link from 
/sys/class/<dev_name> -- I think this is what's needed by mdev.


P.S.  I've noticed that mdev whinges something along the lines of 
/sys/block not found -- this isn't surprising, as I've removed block 
devices from my kernel, so it can cut that out!  I'll hunt out the 
offending line and make a patch ... or maybe it's gone away in the latest 
release, haven't checked yet.


More information about the busybox mailing list