register_chrdev,class_create()

    int register_chrdev(unsigned int major, const char *name,
             struct file_operations *fops);

    其中,major是爲設備驅動程序向系統申請的主設備號,如果爲0則系統爲此驅動程序動態地分配一個主設備號。name是設備名。fops就是前面所說的對各個調用的入口點的說明。此函數返回0表示成功。返回-EINVAL表示申請的主設備號非法,一般來說是主設備號大於系統所允許的最大設備號。返回-EBUSY表示所申請的主設備號正在被其它設備驅動程序使用。如果是動態分配主設備號成功,此函數將返回所分配的主設備號。如果register_chrdev操作成功,設備名就會出現在/proc/devices文件裏。

    在成功的向系統註冊了設備驅動程序後(調用register_chrdev()成功後),就可以用mknod命令來把設備映射爲一個特別文件,其它程序使用這個設備的時候,只要對此特別文件進行操作就行了。

從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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章