www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 嵌入式 > 嵌入式軟件
[導讀]在驅(qū)動模塊初始化函數(shù)中實現(xiàn)設(shè)備節(jié)點的自動創(chuàng)建

我們在剛開始寫Linux設(shè)備驅(qū)動程序的時候,很多時候都是利用mknod命令手動創(chuàng)建設(shè)備節(jié)點,實際上Linux內(nèi)核為我們提供了一組函數(shù),可以用來在模塊加載的時候自動在/dev目錄下創(chuàng)建相應設(shè)備節(jié)點,并在卸載模塊時刪除該節(jié)點,當然前提條件是用戶空間移植了udev。

內(nèi)核中定義了STruct class結(jié)構(gòu)體,顧名思義,一個struct class結(jié)構(gòu)體類型變量對應一個類,內(nèi)核同時提供了class_create(…)函數(shù),可以用它來創(chuàng)建一個類,這個類存放于sysfs下面,一旦創(chuàng)建好了這個類,再調(diào)用device_create(…)函數(shù)來在/dev目錄下創(chuàng)建相應的設(shè)備節(jié)點。這樣,加載模塊的時候,用戶空間中的udev會自動響應device_create(…)函數(shù),去/sysfs下尋找對應的類從而創(chuàng)建設(shè)備節(jié)點。

注意,在2.6較早的內(nèi)核版本中,device_create(…)函數(shù)名稱不同,是class_device_create(…),所以在新的內(nèi)核中編譯以前的模塊程序有時會報錯,就是因為函數(shù)名稱不同,而且里面的參數(shù)設(shè)置也有一些變化。

struct class和device_create(…) 以及device_create(…)都定義在/include/linux/device.h中,使用的時候一定要包含這個頭文件,否則編譯器會報錯。

在2.6.26.6內(nèi)核版本中,struct class定義在頭文件include/linux/device.h中:

/*

* device classes

*/

struct class {

coNSt char *name;

struct module *owner;

nbsp;struct kset subsys;

struct list_head devices;

struct list_head interfaces;

struct kset class_dirs;

struct semaphore sem; /* locks children, devices, interfaces */

struct class_attribute *class_attrs;

struct device_attribute *dev_attrs;

int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);

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);

};

class_create(…)在/drivers/base/class.c中實現(xiàn):

/**

* 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.

*

* 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 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);

if (retval)

goto Error;

return cls;

error:

kfree(cls);

return ERR_PTR(retval);

}

第一個參數(shù)指定類的所有者是哪個模塊,第二個參數(shù)指定類名。

在class.c中,還定義了class_destroy(…)函數(shù),用于在模塊卸載時刪除類。

device_create(…)函數(shù)在/drivers/base/core.c中實現(xiàn):

/**

* 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

* @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, const char *fmt, ...)

{

va_list vargs;

struct device *dev;

va_start(vargs, fmt);

dev = device_create_vargs(class, parent, devt, NULL, fmt, vargs);

va_end(vargs);

return dev;

}

第一個參數(shù)指定所要創(chuàng)建的設(shè)備所從屬的類,第二個參數(shù)是這個設(shè)備的父設(shè)備,如果沒有就指定為NULL,第三個參數(shù)是設(shè)備號,第四個參數(shù)是設(shè)備名稱,第五個參數(shù)是從設(shè)備號。

下面以一個簡單字符設(shè)備驅(qū)動來展示如何使用這幾個函數(shù)

#include

#include

#include

#include

#include

#include

MODULE_LICENSE ("GPL");

int hello_major = 555;

int hello_minor = 0;

int number_of_devices = 1;

struct cdev cdev;

dev_t dev = 0;

struct file_operations hello_fops = {

.owner = THIS_MODULE

};

static void char_reg_setup_cdev (void)

{

int error, devno = MKDEV (hello_major, hello_minor);

cdev_init (&cdev, &hello_fops);

cdev.owner = THIS_MODULE;

cdev.ops = &hello_fops;

error = cdev_add (&cdev, devno , 1);

if (error)

printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);

}

struct class *my_class;

static int __init hello_2_init (void)

{

int result;

dev = MKDEV (hello_major, hello_minor);

result = register_chrdev_region (dev, number_of_devices, "hello");

if (result<0) {

printk (KERN_WARNING "hello: can't get major number %dn", hello_major);

return result;

}

char_reg_setup_cdev ();

/* create your own class under /sysfs */

my_class = class_create(THIS_MODULE, "my_class");

if(IS_ERR(my_class))

{

printk("Err: failed in creating class.n");

return -1;

}

/* register your own device in sysfs, and this will cause udev to create corresponding device node */

device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );

printk (KERN_INFO "Registered character drivern");

return 0;

}

static void __exit hello_2_exit (void)

{

dev_t devno = MKDEV (hello_major, hello_minor);

cdev_del (&cdev);

device_destroy(my_class, MKDEV(adc_major, 0)); //delete device node under /dev

class_destroy(my_class); //delete class created by us

unregister_chrdev_region (devno, number_of_devices);

printk (KERN_INFO "char driver cleaned upn");

}

module_init (hello_2_init);

module_exit (hello_2_exit);

這樣,模塊加載后,就能在/dev目錄下找到hello0這個設(shè)備節(jié)點了。

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當下,工業(yè)電機作為核心動力設(shè)備,其驅(qū)動電源的性能直接關(guān)系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅(qū)動電源設(shè)計中至關(guān)重要的兩個環(huán)節(jié),集成化方案的設(shè)計成為提升電機驅(qū)動性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設(shè)備的使用壽命。然而,在實際應用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設(shè)計、生...

關(guān)鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術(shù)之一是電機驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅(qū)動系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動汽車的動力性能和...

關(guān)鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設(shè)計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關(guān)鍵字: LED 驅(qū)動電源 開關(guān)電源

LED驅(qū)動電源是把電源供應轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動電源
關(guān)閉