虛擬字元驅動函式的實現

2021-06-18 23:37:46 字數 1326 閱讀 1642

根據網友的模版改寫的虛擬字元驅動函式的實現,可以實現應用程式資料寫入驅動程式所在核心空間,以及讀出功能。可作為驅動函式初學的參考

以下是驅動程式**

#include

#include

#include

#include

#include /* copy_to_user,copy_from_user */

#define my_major 240

static unsigned char buffer[1024];

int my_open (struct inode *inode,struct file *filp)

ssize_t my_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)

ssize_t my_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)

int my_release (struct inode *inode, struct file *filp)

struct file_operations my_fops =;

int __init my_init (void)

printk ("ok!\n");

return 0;

}void __exit my_exit (void)

module_license("gpl");

module_init(my_init);

module_exit(my_exit);

以下是makefile檔案 其中 /home/daiyinger/soft/linux-3.0.1/ 是我的核心**目錄

obj-m := driver_char.o

kdir := /home/daiyinger/soft/linux-3.0.1/

all:

make -c $(kdir) m=$(shell pwd) modules

install:

cp driver_char.ko /tftpboot/

clean:

make -c $(kdir) m=$(shell pwd) clean

把以上**拷貝到linux目錄下執行make

再把生成的driver_char.ko檔案拷到開發板目錄下

執行 insmod driver_char.ko

執行lsmod可以檢視模組是否載入成功

執行mknod /dev/my_char c 240 0 建立裝置檔案訪問節點

應用程式**如下

實現 dev null的字元裝置驅動

dev null是個黑洞裝置,它丟棄一切寫入其中的資料,空裝置通常被用於丟棄不需要的輸出流。任何寫入該裝置資料都會被丟棄掉。從這個裡面讀取資料返回空 也有人認為是讀該空裝置,直接讀到檔案尾,那就是返回 1 將一些不用內容經常傳送給這個裝置,丟棄不需要的資料。實現如下 include include ...

Linux裝置驅動 字元裝置驅動介面函式

核心提供了三個函式來註冊一組字元裝置編號,這三個函式分別是register chrdev region alloc chrdev region 和register chrdev 在linux2.6核心以前註冊字元裝置的函式介面是register chrdev,登出字元裝置介面函式是unregiste...

字元裝置實現控制led的驅動

乙個led驅動,做個記號 硬軟體環境 s3c2440 linux 2.6.36 busybox 1.18.4 arm linux gcc 4.4.3 下面是模組c include include include include include include module license gpl m...