[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Device driver Major number
S P wrote:
> Hi,
> I have created a pseudo device usign mknod with major
> number 255. Now when we use mknod we have to specify
> the major number for the device.
> However I want the system to dynamically allocate the
> major number, so am using "register_chrdev". As I
> understand register_chrdev, just registers the char
> device & not create it.
>
> So how do I create the device so that the major number
> is allocated dynamically ?
>
Once you register the character device you can cat the /proc/devices and awk the first and second
columns to get the device name and the majornum which is dynamically assigned by the kernel when you
did a register_chrdev (specifying a 0 for the major number in the register_chrdev call).
You can use the following script (taken from linux device driver book) to do what you need (by
substituting appropriate names for module and device names)
*********SCRIPT BEGIN**********
#!/bin/sh
mode=664
module="YOURMODULEHERE"
device="YOURDEVICEHERE"
# Group: since distributions do it differently, look for wheel or use staff
if grep '^staff:' /etc/group > /dev/null; then
group="staff"
else
group="wheel"
fi
# remove stale nodes
rm -f /dev/${device}
# invoke insmod with all arguments we got and use a pathname
/sbin/insmod -f ./$module.o $* || exit 1
#Get the major numbers of all the device that we care about
major=`cat /proc/devices | awk "\\$2==\"$module\" {print \\$1}"`
#make the node for the Device
mknod /dev/${device} c $major 0
# give appropriate group/permissions
chgrp $group /dev/${device}
chmod $mode /dev/${device}
exit 0
*******SCRIPT END*********
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/