生成裝置節點

2021-09-05 08:09:54 字數 2974 閱讀 6281

雜項裝置的主裝置號是10,在任何linux 系統中它都是固定的。

這樣雜項裝置的引入即解決了裝置號數量少的問題,又降低了使用難度,還能防止碎片化,

一舉多得。

雜項裝置的標頭檔案在「include/linux/miscdevice.h」

雜項裝置註冊函式;一般在probe 中呼叫,引數是miscdevice

extern int misc_deregister(struct miscdevice *misc);

雜項裝置解除安裝函式;一般是在hello_remove 中用於解除安裝驅動。

結構體miscdevice 中引數很多,下面幾個是常用的。

int .minor;裝置號,賦值為misc_dynamic_minor,這個巨集定義可以查到為255

const char *name;裝置名稱

const struct file_operations *fops;file_operations 結構。

file_operations 結構體在標頭檔案「include/linux/fs.h」中

struct module *owner;一般是this_module。

int (*open) (struct inode *, struct file *);對應上層的open 函式,開啟檔案。

int (*release) (struct inode *, struct file *);對應上層的close 函式,開啟檔案操作之後一

般需要關閉。

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);讀函式,上層應用從底層讀取

函式。ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);寫函式,上層應用向底

層傳輸資料。

long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);這個函式功能和寫

函式稍微有點重合,但是這個函式占用的記憶體非常小,主要針對io 口的控制。

實現例項原始碼:

#include #include /*驅動註冊的標頭檔案,包含驅動的結構體和註冊和解除安裝的函式*/

#include /*註冊雜項裝置標頭檔案*/

#include /*註冊裝置節點的檔案結構體*/

#include #define driver_name "hello_ctl"

#define device_name "hello_ctl123"

module_license("dual bsd/gpl");

module_author("skyfall");

//long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

//驅動實際操作的實現

static long hello_ioctl( struct file *file, unsigned int cmd, unsigned long arg)

//int (*release) (struct inode *, struct file *);

//關閉該驅動的實現

static int hello_release(struct inode *inode, struct file *file)

//int (*open) (struct inode *, struct file *);

//開啟該驅動的實現

static int hello_open(struct inode *inode, struct file *file)

//定義乙個字元裝置操作集合

static struct file_operations hello_ops = ;

//定義乙個雜項裝置結構體

static struct miscdevice hello_dev = ;

static int hello_probe(struct platform_device *pdv)

static int hello_remove(struct platform_device *pdv)

static void hello_shutdown(struct platform_device *pdv)

static int hello_suspend(struct platform_device *pdv,pm_message_t pmt)

static int hello_resume(struct platform_device *pdv)

struct platform_driver hello_driver =

};static int hello_init(void)

static void hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

相應的makefile檔案參考生成字元裝置的。

載入驅動進行測試:

載入之後, ls /dev,  可以看到新生成了裝置節點hello_ctl123,也就是裝置節點和驅動名以及裝置名沒有一關係,不過最好裝置節點的命名便於識別。

裝置檔案節點的生成

裝置檔案節點的生成 在linux系統下,裝置檔案是種特殊的檔案型別,其存在的主要意義是溝通使用者空間程式和核心空間驅動程式。換句話說,使用者空間的應用程式要想使用驅動程式提供的服務,需要經過裝置檔案來達成。當然,如果你的驅動程式只是為核心中的其他模組提供服務,則沒有必要生成對應的裝置檔案。按照通用的...

android如何手動生成裝置節點

init.rc裡所有可用的command都定義在system core init keyword.h裡,預設是不包含mknod的。事實上,android的init程序會通過kenel的uevent來自動建立裝置節點 見system core init devices.c裡的make device 函...

7 生成雜項裝置節點

7.1 介紹 雜項裝置是主裝置號是10的封裝好了的裝置。雜項裝置部分初始化檔案 強制編譯的簡單的 drivers char misc.c 7.2 雜項裝置註冊檔案 1.雜項裝置註冊標頭檔案 include linux miscdevice.h 結構體miscdevice minor裝置號 一般設為m...