生活随笔
收集整理的這篇文章主要介紹了
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");*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
); 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
= {.owner
= THIS_MODULE
, .open
= first_drv_open
, .write
= first_drv_write
,
};int major
;
static int first_drv_init(void)
{major
= register_chrdev(0, "first_drv", &first_drv_fops
); firstdrv_class
= class_create(THIS_MODULE
, "firstdrv");firstdrv_class_dev
= class_device_create(firstdrv_class
, NULL, MKDEV(major
, 0), NULL, "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
all:
make -C
$(KERN_DIR) M
=`pwd` modules clean:
make -C
$(KERN_DIR) M
=`pwd` modules clean
rm -rf modules.orderobj-m +
= first_drv.o
在編譯之后會生成相應的.ko文件
然后編輯相應的的測試文件;
firstdrvtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc
, char **argv
)
{int fd
;int val
= 1;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驱动程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。