在宋寶華《Linux設備驅動開發詳解》中我們能找到如下描述:
devfs與udev的另一個顯著區別在於:採用devfs,當一個並不存在的/dev節點被打開的時候,devfs能自動加載對應的驅動,而udev則不能。這是因爲 udev的設計者認爲Linux應該在設備被發現的時候加載驅動模塊,而不是當它被訪問的時候。
udev 完全在用戶態工作,利用設備加入或移除時內核所發送的熱插拔事件(hotplug event)來工作。在熱插拔時,設備的詳細信息會由內核輸出到位於/sys的sysfs文件系統。udev的設備命名策略、權限控制和事件處理都是在用戶態下完成的,它利用sysfs中的信息來進行創建設備文件節點等工作。
Linux內核爲我們提供了一組函數,可以用來在模塊加載的時候自動在/dev目錄下創建相應設備節點,並在卸載模塊時刪除該節點,當然前提條件是用戶空間移植了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和device_create(…) 都定義在/include/linux/device.h中,使用的時候一定要包含這個頭文件,否則編譯器會報錯。
在Linux2.6.32.2內核頭文件include/linux/device.h中:
/*
* device classes
*/
struct class {
const char
*name;
struct module
*owner;
struct class_attribute*class_attrs;
struct device_attribute*dev_attrs;
struct kobject*dev_kobj;
int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
char *(*devnode)(struct device *dev, mode_t *mode);
void (*class_release)(struct class *class);
void (*dev_release)(struct device *dev);
int (*suspend)(struct device *dev, pm_message_t state);
int (*resume)(struct device *dev);
const struct dev_pm_ops *pm;
struct class_private *p;
};
#define class_create(owner, name) \
({ \
static struct lock_class_key __key;\
__class_create(owner, name, &__key);\
})
/**
* class_create - create a struct class structure
* @owner: pointer to the module that is to "own" this struct class
* @name: pointer to a string for the name of this class.
* @key: the lock_class_key for this class; used by mutex lock debugging
*
* This is used to create a struct class pointer that can then be used
* in calls to device_create().
*
* Note, the pointer created here is to be destroyed when finished by
* making a call to class_destroy().
*/
struct class *__class_create(struct module *owner, const char *name,
struct lock_class_key *key)
{
struct class *cls;
int retval;
cls = kzalloc(sizeof(*cls), GFP_KERNEL);
if (!cls) {
retval = -ENOMEM;
goto error;
}
cls->name = name;
cls->owner = owner;
cls->class_release = class_create_release;
retval = __class_register(cls, key);
if (retval)
goto error;
return cls;
error:
kfree(cls);
return ERR_PTR(retval);
}
第一個參數指定類的所有者是哪個模塊,第二個參數指定類名。
class_destroy(struct class *cls)函數,用於在模塊卸載時刪除類。
/**
* device_create - creates a device and registers it with sysfs
* @class: pointer to the struct class that this device should be registered to
* @parent: pointer to the parent struct device of this new device, if any
* @devt: the dev_t for the char device to be added
* @drvdata: the data to be added to the device for callbacks
* @fmt: string for the device's name
*
* This function can be used by char device classes. A struct device
* will be created in sysfs, registered to the specified class.
*
* A "dev" file will be created, showing the dev_t for the device, if
* the dev_t is not 0,0.
* If a pointer to a parent struct device is passed in, the newly created
* struct device will be a child of that device in sysfs.
* The pointer to the struct device will be returned from the call.
* Any further sysfs files that might be required can be created using this
* pointer.
*
* Note: the struct class passed to this function must have previously
* been created with a call to class_create().
*/
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
{
va_list vargs;
struct device *dev;
va_start(vargs, fmt);
dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
va_end(vargs);
return dev;
}
第一個參數指定所要創建的設備所從屬的類,第二個參數是這個設備的父設備,如果沒有就指定爲NULL,第三個參數是設備號,第四個參數是void類型的指針,代表回調函數的輸入參數,第五個參數是設備名字符串
函數 device_unregister( struct device *dev);用於在模塊卸載是刪除設備,在卸載函數中要先刪除設備,再刪除類,順序不能顛倒
例:
#define DEVICE_NAME "fpga_dma"
static int dev_major = 0;
static struct class *fpga_class;
static struct cdev fpgaDevice;
static int dev_init(void)
{
int result;
int err;
dev_t dev = MKDEV(dev_major, 0);
if (dev_major)
result = register_chrdev_region(dev, 1, DEVICE_NAME);
else {
result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
dev_major = MAJOR(dev);
}
if (result < 0)
{
printk("unable to get major %d\n", dev_major);
return result;
}
printk("get major is %d\n", dev_major);
if (dev_major == 0)
dev_major = result;
cdev_init(&fpgaDevice, &fpga_dma_fops);
fpgaDevice.owner = THIS_MODULE;
fpgaDevice.ops = &fpga_dma_fops;
err = cdev_add(&fpgaDevice, dev, 1);
if (err)
printk("error %d add fpga ", err);
fpga_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(fpga_class))
{
printk("Err:failed in creating class.\n");
return -1;
}
class_device_create(fpga_class, NULL, MKDEV(dev_major, 0), NULL, "%s", DEVICE_NAME);
printk("MPC8377core FPGA_GPIO_driver installed OK\n");
return 0;
}
這樣,模塊加載後,就能在/dev目錄下找到設備節點了。