動態申請字元類裝置號

2021-09-05 08:41:44 字數 1333 閱讀 9346

字元裝置函式在檔案 include/linux/fs.h 中

alloc_chrdev_region() 是動態分配主次裝置號

巨集定義major提取dev_t資料中的主裝置號

編寫,編譯

測試原始碼:

#include /*包含初始化巨集定義的標頭檔案,**中的module_init和module_exit在此檔案中*/

#include /*包含初始化載入模組的標頭檔案,**中的module_license在此標頭檔案中*/

/*定義module_param module_param_array的標頭檔案*/

#include /*定義module_param module_param_array中perm的標頭檔案*/

#include /*三個字元裝置函式*/

#include /*mkdev轉換裝置號資料型別的巨集定義*/

#include /*定義字元裝置的結構體*/

#include #define device_name "ascdev"

#define device_minor_num 2

#define dev_major 0

#define dev_minor 0

module_license("dual bsd/gpl");

/*宣告是開源的,沒有核心版本限制*/

module_author("skyfall");

/*宣告作者*/

int numdev_major = dev_major;

int numdev_minor = dev_minor;

/*輸入主裝置號*/

module_param(numdev_major,int,s_irusr);

/*輸入次裝置號*/

module_param(numdev_minor,int,s_irusr);

static int scdev_init(void)

else

if(ret<0)

printk(kern_emerg "scdev_init!\n");

/*列印資訊,kern_emerg表示緊急資訊*/

return 0;

}static void scdev_exit(void)

module_init(scdev_init);

/*初始化函式*/

module_exit(scdev_exit);

/*解除安裝函式*/

相應的makefile檔案參考相關博文即可。

載入執行

– 使用命令「cat /proc/devices」檢視

– 動態載入模組之後再檢視裝置號

靜態申請字元類裝置號

字元裝置函式在檔案 include linux fs.h 中 核心提供了三個函式來註冊一組字元裝置編號,這三個函式分別是 register chrdev region alloc chrdev region register chrdev register chrdev region 是提前知道裝置...

linux 靜態申請字元類裝置號

包括主裝置號和次裝置號 字元裝置函式在檔案 include linux fs.h 中 核心提供了三個函式來註冊一組字元裝置編號,這三個函式分別是 register chrdev region alloc chrdev region register chrdev register chrdev re...

字元裝置驅動 靜態 動態申請裝置號

靜態裝置號申請 申請裝置號,from 要申請的裝置號,count 申請幾個裝置,name 裝置名字 int register chrdev region dev t from,unsigned count,const char name 登出裝置號,from 要登出的裝置號,count 登出幾個裝置...