在驅動用加入對udev的支持主要做的就是:在驅動初始化的代碼裡調用class_create(...)為該設備創建一個class,再為每個設備調用device_create(...)( 在2.6較早的內核中用class_device_create)創建對應的設備。
內核中定義的struct class結構體,顧名思義,一個struct class結構體類型變量對應一個類,內核同時提供了class_create(…)函數,可以用它來創建一個類,這個類存放於sysfs下面,一旦創建好了這個類,再調用 device_create(…)函數來在/dev目錄下創建相應的設備節點。這樣,加載模塊的時候,用戶空間中的udev會自動響應 device_create(…)函數,去/sysfs下尋找對應的類從而創建設備節點。
struct class定義在頭文件include/linux/device.h中class_create(…)在/drivers/base/class.c中實現device_create(…)函數在/drivers/base/core.c中實現class_destroy(...),device_destroy(...)也在/drivers/base/core.c中實現調用過程類似如下:
static struct class *spidev_class;
/*-------------------------------------------------------------------------*/
static int spidev_probe(struct spi_device *spi)
{
....
dev = device_create(spidev_class, &spi->dev, spidev->devt,
spidev, "spidev%d.%d",
spi->master->bus_num, spi->chip_select);
...
}
static int spidev_remove(struct spi_device *spi)
{
......
device_destroy(spidev_class, spidev->devt);
.....
return 0;
}
static struct spi_driver spidev_spi = {
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
.probe = spidev_probe,
.remove = __devexit_p(spidev_remove),
};
/*-------------------------------------------------------------------------*/
static int __init spidev_init(void)
{
....
spidev_class = class_create(THIS_MODULE, "spidev");
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
return PTR_ERR(spidev_class);
}
....
}
module_init(spidev_init);
static void __exit spidev_exit(void)
{
......
class_destroy(spidev_class);
......
}
module_exit(spidev_exit);
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
下面www.linuxidc.com 以一個簡單字符設備驅動來展示如何使用這幾個函數
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
int HELLO_MAJOR = 0;
int HELLO_MINOR = 0;
int NUMBER_OF_DEVICES = 2;
struct class *my_class;
struct cdev cdev;
dev_t devno;
struct file_operations hello_fops = {
.owner = THIS_MODULE
};
static int __init hello_init (void)
{
int result;
devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);
if (HELLO_MAJOR)
result = register_chrdev_region(devno, 2, "memdev");
else
{
result = alloc_chrdev_region(&devno, 0, 2, "memdev");
HELLO_MAJOR = MAJOR(devno);
}
printk("MAJOR IS %d\n",HELLO_MAJOR);
my_class = class_create(THIS_MODULE,"hello_char_class"); //類名為hello_char_class
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class,NULL,devno,NULL,"memdev"); //設備名為memdev
if (result<0)
{
printk (KERN_WARNING "hello: can't get major number %d\n", HELLO_MAJOR);
return result;
}
cdev_init(&cdev, &hello_fops);
cdev.owner = THIS_MODULE;
cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
printk (KERN_INFO "Character driver Registered\n");
return 0;
}
static void __exit hello_exit (void)
{
cdev_del (&cdev);
device_destroy(my_class, devno); //delete device node under /dev//必須先刪除設備,再刪除class類
class_destroy(my_class); //delete class created by us
unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
printk (KERN_INFO "char driver cleaned up\n");
}
module_init (hello_init);
module_exit (hello_exit);
MODULE_LICENSE ("GPL");
這樣,模塊加載後,就能在/dev目錄下找到memdev這個設備節點了。