歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux自動生成設備名的方法

Linux自動生成設備名的方法

日期:2017/2/28 16:16:35   编辑:Linux教程

從Linux內核2.6的某個版本之後,devfs不復存在,udev成為devfs的替代。相比devfs,udev有很多優勢,在此就不羅嗦了,提醒一點,udev是應用層的東東,不要試圖在內核的配置選項裡找到它,而在Android系統中沒有udev,但是udev的功能在androidsrc/system/core/init的代碼中實現的;加入對udev的支持很簡單,以作者所寫的一個字符設備驅動為例,在驅動初始化的代碼裡調用class_create為該設備創建一個class,再為每個設備調用 class_device_create創建對應的設備。

大致用法如下:
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);


這樣的module被加載時,udev daemon就會自動在/dev下創建my_device設備文件 。

class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
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.
在/sys/class/下創建類目錄

class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device * class_device_create (struct class *cls,
struct class_device *parent,
dev_t devt,
struct device *device,
const char *fmt, ...)

class_device_create - creates a class device and registers it with sysfs
@cls: pointer to the struct class that this device should be registered to.
@parent: pointer to the parent struct class_device of this new device, if any.
@devt: the dev_t for the char device to be added.
@device: a pointer to a struct device that is assiociated with this class device.
@fmt: string for the class device's name

但是在 2.6.27中: class_device_create()和 class_device_destroy()兩個函數已變成了 device_create() 和 device_destroy() ,使用這個系統調用時請注意使用的kernel的版本。

Copyright © Linux教程網 All Rights Reserved