1. 程式人生 > >device_create ()詳解

device_create ()詳解

    ……
    //建立一個裝置節點,節點名為DEVICE_NAME
    device_create(mydriver_class, NULL, MKDEV(MYDRIVER_Major, 0), NULL, DEVICE_NAME);
    ……
}

從linux核心2.6的某個版本之後,devfs不復存在,udev成為devfs的 替代。相比devfs,udev有很多優勢,在此就不羅嗦了,提醒一點,udev是應用層的東東,不要試圖在核心的配置選項裡找到它;加入對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