在Linux內核學習-字符設備驅動學習(一)中編寫字符設備驅動的一種方法,但是需要手動創建設備節點。
有沒有能夠自動的創建設備節點的呢?
有!使用class_create()和device_create()函數可以自動創建節點。
class_create : 創建class
class_destroy : 銷燬class
class_device_create : 創建device
class_device_destroy : 銷燬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
在驅動模塊初始化函數中實現設備節點的自動創建
設備驅動文件char01.c
測試文件char01_test.c
Makefile文件: