第乙個Linux字元裝置驅動程式和應用測試

2021-07-29 01:52:00 字數 1377 閱讀 8870

1 首先寫乙個字元裝置原始檔 ***.c

字元裝置驅動程式的框架結構

/*檔案開啟函式*/

int scull_open(struct inode *inode, struct file *filp);

/*檔案釋放函式*/int scull_release(struct inode *inode, struct file *filp);/*讀函式*/

static ssize_t scull_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos);

/*寫函式*/

static ssize_t scull_write(struct file *filp, const

char __user *buf, size_t size, loff_t *ppos);

/* seek檔案定位函式 */

static loff_t scull_llseek(struct file *filp, loff_t offset, int whence);

/*檔案操作結構體*/

static

const

struct file_operations scull_fops ={};

/*裝置驅動模組載入函式*/

static

int sculldev_init(void);

/*模組解除安裝函式*/

static

void sculldev_exit(void);

2   編寫標頭檔案***.h

3 編寫makefile

完成以上3個步驟後編譯make

生成***.ko

安裝到系統內

sudo insmod ***.ko 

檢視驅動裝置

cat  /proc/devices

前面的數字為主裝置號,如果與已有裝置號衝突,則需要修改主裝置號

建立裝置節點

sudo mknod ***x c aa bb

上邊***x  代表裝置驅動的名稱   c 代表字元裝置  aa 主裝置號  bb次裝置號

可以用 ls -al ***x 檢視驅動裝置的相關資訊

4 編寫應用程式main.c 測試驅動程式

編譯並執行

執行時需要加sudo

第乙個驅動之字元裝置驅動(二)mdev

mdev是busybox提供的乙個工具,用在嵌入式系統中,相當於簡化版的udev,作用是在系統啟動和熱插拔或動態載入驅動程式時,自動建立裝置節點。檔案系統中的 dev目錄下的裝置節點都是由mdev建立的。在載入驅動過程中,根據驅動程式,在 dev下自動建立裝置節點。前面的部落格實現了第乙個版本,但是...

第乙個驅動之字元裝置驅動(二)mdev

mdev是busybox提供的乙個工具,用在嵌入式系統中,相當於簡化版的udev,作用是在系統啟動和熱插拔或動態載入驅動程式時,自動建立裝置節點。檔案系統中的 dev目錄下的裝置節點都是由mdev建立的。在載入驅動過程中,根據驅動程式,在 dev下自動建立裝置節點。前面的部落格實現了第乙個版本,但是...

第乙個linux 驅動

以前看過很多次linux相關的資料,一直沒親自動手寫,今天通過半天努力,終於完成了乙個自己的linux小驅動 hello.c include include module license dual bsd gpl static int hello init void static void hell...