日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux驱动程序

發布時間:2025/3/15 linux 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux驱动程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在使用自己編譯的內核進linux開發的時候,好像所示哪一個環節沒有修改好,使用nfs的過程中一直出現這種情況,設備能夠正常的進行掛載但是掛載成功之后進行文件拷貝的時候一直提示:

nfs: server 192.168.1.107 not responding still trying

如果檢查了你的服務器上的文件已經有權限的,并且一切的配置也是沒有問題的那么就可能使是因為nfs使用的是udp進行的通訊,相對于電腦來說你的設備的網卡實在是太慢了,所以使用的時候需要進行設置,使用一下命令進行:

mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 192.168.199.142:/home/wxp/nfs_root /mnt

proc文件與dev文件的區別
1、proc目錄是一個虛擬文件系統,可以為linux用戶空間和內核空間提供交互
它只存在于內存中,而不占實際的flash或硬盤空間
2、/proc/devices/里的設備是加載驅動程序時生成的
3、/dev/下的設備是通過創建設備節點生成的,用戶通過此設備節點來訪問內核里的驅動

開發2440的led驅動程序
驅動程序  first_drv.c

#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h>static struct class *firstdrv_class; static struct class_device *firstdrv_class_dev;volatile unsigned long *gpfcon = NULL; volatile unsigned long *gpfdat = NULL;//從用戶空間向內核復制數據 static int first_drv_open(struct inode *inode, struct file *file) {printk("first_drv_open\n");/* ,5, */*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));return 0; }static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {int val;printk("first_drv_write\n");//從用戶向內核復制數值copy_from_user(&val, buf, count); // copy_to_user();//copy_to_user從內核向用戶復制if (val == 1){// *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));}else{// *gpfdat |= (1<<4) | (1<<5) | (1<<6);}return 0; }//聲明一個結構體成員,用于初始化驅動函數 static struct file_operations first_drv_fops = {//. + 結構體成員是  C99以上的結構體使用的是結構體成員,定義一個這樣的結構體,并對結構體進行部分成員進行復制//結構體成員都是函數指針.owner = THIS_MODULE, /*   */.open = first_drv_open, .write = first_drv_write, };int major; static int first_drv_init(void) {//major  主設備號 major = register_chrdev(0, "first_drv", &first_drv_fops); //注冊驅動程序//這里的第一個參數0的意思是讓內核為設備分配主設備號//major是寫0分配之后的設備號firstdrv_class = class_create(THIS_MODULE, "firstdrv");firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);gpfdat = gpfcon + 1;return 0; }static void first_drv_exit(void) {unregister_chrdev(major, "first_drv"); // ж��class_device_unregister(firstdrv_class_dev);class_destroy(firstdrv_class);iounmap(gpfcon); }module_init(first_drv_init); module_exit(first_drv_exit);MODULE_LICENSE("GPL");

Makefile文件

KERN_DIR = /work/svn_linux/linux #  KERN_DIR是內核的目錄位置 #注意指向的內核一定是編譯過測內核,并且使用的是和你設備中是同一版本,最好是同一個內核 #指到這個位置是為了使用內核目錄下的makefile對該文件進行編譯 all:make -C $(KERN_DIR) M=`pwd` modules clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderobj-m += first_drv.o

在編譯之后會生成相應的.ko文件
然后編輯相應的的測試文件;
firstdrvtest.c

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>/* firstdrvtest on* firstdrvtest off*/ int main(int argc, char **argv) {int fd;int val = 1;///dev/xyz 這個名字隨便起,因為最終決定 /dev下面的設備節點和/proc/devices裝載驅動程序時生成的節點進行連接的是設備的編號,這個是驅動程序決定的,也就是register_chrdev 函數的第一個參數決定//若 register_chrdev 函數的第一個參數是 0 內核將根據情況為設備分配一個設備節點號fd = open("/dev/xyz", O_RDWR);if (fd < 0){printf("can't open!\n");}if (argc != 2){printf("Usage :\n");printf("%s <on|off>\n", argv[0]);return 0;}if (strcmp(argv[1], "on") == 0){val = 1;}else{val = 0;}write(fd, &val, 4);return 0; }

然后就是使用mount 進行設備的掛載

mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 192.168.199.142:/home/wxp/nfs_root /mnt

掛載之后使用

insmod xxx.ko

手動進行驅動程序的安裝
安裝完成之后使用   mknod 在/dev目錄下創建相應的設備節點,注意節點是在驅動程序助攻確定的,要是自己不確定可以再設備中的a/proc目錄中使用

cat /proc/devices

命令進行查看相應的設備的節點編號

比如是252
就可以使用命令:

mknod /dev/xyz c 252 0

/dev/xyz 是設備節點  在測試函數進行打開文件設備的時候確定
c說明是創建字符型設備節點
252是創建與/proc/devices中加載的驅動程序的設備節點號對應的數值,也就是主設備節點
0是次設備節點  若是只有一個,就直接為0



mdev會根據系統的信息創建設備節點
怎樣查看自己的設備節點
進入到 /sys/class/dev   目錄下面會有相應的驅動名稱的信息,相應的信息在與驅動文件名相關的,目錄中之中

總結

以上是生活随笔為你收集整理的linux驱动程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。